0% found this document useful (0 votes)
30 views3 pages

Building Modular Cloud Applications - Listings

The document provides code listings for building modular cloud applications in Java. It shows interfaces and classes for an agenda application that allows adding and listing conferences, implemented with both simple in-memory and MongoDB storage backends.

Uploaded by

sefdeni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views3 pages

Building Modular Cloud Applications - Listings

The document provides code listings for building modular cloud applications in Java. It shows interfaces and classes for an agenda application that allows adding and listing conferences, implemented with both simple in-memory and MongoDB storage backends.

Uploaded by

sefdeni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 3

Code listings for "Building Modular Cloud Applications in Java," Java Magazine,

January/February 2015
[Listing 1]
package agenda.api;
public interface Agenda {
void addConference(String name, String location);
List<Conference> listConferences();
}

[Listing 2]
package agenda.api;
public class Conference {
private String name;
private String location;
public Conference() {}
public Conference(String name, String location) {
this.name = name;
this.location = location;
}
// Getters/setters omitted for brevity
}

[Listing 3]
package agenda.simple;
public class SimpleAgendaService implements Agenda {
private List<Conference> conferences = new CopyOnWriteArrayList<>();
@Override
public void addConference(Conference conference) {
conferences.add(conference);
}
@Override
public List<Conference> listConferences() {
return conferences;
}
}

[Listing 4]
package agenda.simple;
public class Activator extends DependencyActivatorBase {
@Override
public void destroy(BundleContext ctx, DependencyManager dm)

throws Exception {
}
@Override
public void init(BundleContext ctx, DependencyManager dm)
throws Exception {
dm.add(createComponent()
.setInterface(Agenda.class.getName(), null)
.setImplementation(SimpleAgendaService.class));
}
}

[Listing 5]
package agenda.rest;
@Path("agenda")
public class AgendaResource {
private volatile Agenda agenda;
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Conference> listConferences() {
return agenda.listConferences();
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
public void addConference(Conference conference) {
agenda.addConference(conference.getName(),
conference.getLocation());
}
}

[Listing 6]
package agenda.rest;
public class Activator extends DependencyActivatorBase {
@Override
public void destroy(BundleContext ctx, DependencyManager dm)
throws Exception {
}
@Override
public void init(BundleContext ctx, DependencyManager dm)
throws Exception {
dm.add(createComponent()
.setInterface(Object.class.getName(), null)
.setImplementation(AgendaResource.class)
.add(createServiceDependency()
.setService(Agenda.class)));
}
}

[Listing 7]

package agenda.mongo;
public class MongoAgendaService implements Agenda {
private volatile MongoDBService mongoDBService;
private volatile Jongo jongo;
private volatile MongoCollection agenda;
public void start() {
jongo = new Jongo(mongoDBService.getDB());
agenda = jongo.getCollection("agenda");
}
@Override
public void addConference(String name, String location) {
agenda.save(new Conference(name, location));
}
@Override
public List<Conference> listConferences() {
List<Conference> result = new ArrayList<>();
agenda.find().as(Conference.class).forEach(result::add);
return result;
}
}

[Listing 8]
package agenda.mongo;
public class Activator extends DependencyActivatorBase {
@Override
public void destroy(BundleContext ctx, DependencyManager dm)
throws Exception {
}
@Override
public void init(BundleContext ctx, DependencyManager dm)
throws Exception {
dm.add(createComponent()
.setInterface(Agenda.class.getName(), null)
.setImplementation(MongoAgendaService.class)
.add(createServiceDependency()
.setService(MongoDBService.class)));
}
}

Copyright2015OracleCorporation

You might also like