Skip to content

Commit

Permalink
Updated morphia service to use the latest packaging for the morphia APIs
Browse files Browse the repository at this point in the history
Added count operations to several services.
Added data service info and runtime information services to the acme air web app.
Fixed a bug for the info page to be able to login/logout.
Added rest/info/* rest path to allow the info page and data service counts to be accessed without needing to log in.
  • Loading branch information
dougrt committed Mar 28, 2015
1 parent 4685925 commit a8bca17
Show file tree
Hide file tree
Showing 22 changed files with 544 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@XmlRootElement
public class Customer implements Serializable{

public enum MemberShipStatus { NONE, SILVER, GOLD, PLATINUM, EXEC_PLATINUM, GRAPHITE };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@XmlRootElement
public class CustomerAddress implements Serializable{

private static final long serialVersionUID = 1L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import java.math.BigDecimal;

import com.github.jmkgreen.morphia.converters.SimpleValueConverter;
import com.github.jmkgreen.morphia.converters.TypeConverter;
import com.github.jmkgreen.morphia.mapping.MappedField;
import com.github.jmkgreen.morphia.mapping.MappingException;
import org.mongodb.morphia.converters.SimpleValueConverter;
import org.mongodb.morphia.converters.TypeConverter;
import org.mongodb.morphia.mapping.MappedField;
import org.mongodb.morphia.mapping.MappingException;

public class BigDecimalConverter extends TypeConverter implements SimpleValueConverter{

Expand All @@ -21,7 +21,6 @@ public Object encode(Object value, MappedField optionalExtraInfo) {
@Override
public Object decode(Class targetClass, Object fromDBObject, MappedField optionalExtraInfo) throws MappingException {
if (fromDBObject == null) return null;

return new BigDecimal(fromDBObject.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import java.math.BigInteger;

import com.github.jmkgreen.morphia.converters.SimpleValueConverter;
import com.github.jmkgreen.morphia.converters.TypeConverter;
import com.github.jmkgreen.morphia.mapping.MappedField;
import com.github.jmkgreen.morphia.mapping.MappingException;
import org.mongodb.morphia.converters.SimpleValueConverter;
import org.mongodb.morphia.converters.TypeConverter;
import org.mongodb.morphia.mapping.MappedField;
import org.mongodb.morphia.mapping.MappingException;

public class BigIntegerConverter extends TypeConverter implements SimpleValueConverter{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import com.acmeair.entities.Booking;
import com.acmeair.entities.Flight;
import com.acmeair.entities.FlightSegment;
import com.github.jmkgreen.morphia.Datastore;
import com.github.jmkgreen.morphia.Morphia;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientURI;
Expand Down Expand Up @@ -69,7 +69,7 @@ public static Datastore getDatastore(Datastore ds)

MongoClientURI mongoURI = new MongoClientURI(mongourl, builder);
MongoClient mongo = new MongoClient(mongoURI);
Morphia morphia = new Morphia();
Morphia morphia = new Morphia();
result = morphia.createDatastore( mongo ,mongoURI.getDatabase());
System.out.println("create mongo datastore with options:"+result.getMongo().getMongoOptions());
}catch (Exception e)
Expand All @@ -78,8 +78,8 @@ public static Datastore getDatastore(Datastore ds)
}
}
// The converter is added for handing JDK 7 issue
result.getMapper().getConverters().addConverter(new BigDecimalConverter());
result.getMapper().getConverters().addConverter(new BigIntegerConverter());
// result.getMapper().getConverters().addConverter(new BigDecimalConverter());
// result.getMapper().getConverters().addConverter(new BigIntegerConverter());

// Enable index
result.ensureIndex(Booking.class, "pkey.customerId");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public interface MorphiaConstants extends AcmeAirConstants {
public static final String KEY = "morphia";
public static final String KEY_DESCRIPTION = "mongoDB with morphia implementation";

public static final String HOSTNAME = "mongo/hostname";
public static final String PORT = "mongo/port";
public static final String DATABASE = "mongo/database";
public static final String HOSTNAME = "mongohostname";
public static final String PORT = "mongoport";
public static final String DATABASE = "mongodatabase";


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.mongodb.morphia.Datastore;

import com.acmeair.entities.Booking;
import com.acmeair.entities.BookingPK;
import com.acmeair.entities.Customer;
Expand All @@ -19,8 +21,8 @@
import com.acmeair.service.DataService;
import com.acmeair.service.FlightService;
import com.acmeair.service.ServiceLocator;
import com.github.jmkgreen.morphia.*;
import com.github.jmkgreen.morphia.query.Query;
import org.mongodb.morphia.*;
import org.mongodb.morphia.query.Query;
import com.mongodb.DB;


Expand All @@ -41,7 +43,7 @@ public class BookingServiceImpl implements BookingService, MorphiaConstants {

@PostConstruct
public void initialization() {
datastore = MongoConnectionManager.getConnectionManager().getDatastore();
datastore = MongoConnectionManager.getConnectionManager().getDatastore();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.acmeair.entities.Customer;
import com.acmeair.entities.Customer.MemberShipStatus;
Expand All @@ -18,9 +16,9 @@
import com.acmeair.morphia.services.util.MongoConnectionManager;
import com.acmeair.service.DataService;
import com.acmeair.service.CustomerService;
import com.github.jmkgreen.morphia.Datastore;
import com.github.jmkgreen.morphia.Morphia;
import com.github.jmkgreen.morphia.query.Query;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.mongodb.morphia.query.Query;
import com.mongodb.DB;

@DataService(name=MorphiaConstants.KEY,description=MorphiaConstants.KEY_DESCRIPTION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import com.acmeair.morphia.services.util.MongoConnectionManager;
import com.acmeair.service.DataService;
import com.acmeair.service.FlightService;
import com.github.jmkgreen.morphia.Datastore;
import com.github.jmkgreen.morphia.query.Query;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;

@DataService(name=MorphiaConstants.KEY,description=MorphiaConstants.KEY_DESCRIPTION)
public class FlightServiceImpl implements FlightService, MorphiaConstants {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.acmeair.morphia.services.util;

import java.net.UnknownHostException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.acmeair.morphia.BigDecimalConverter;
import com.acmeair.morphia.MorphiaConstants;
import com.github.jmkgreen.morphia.Datastore;
import com.github.jmkgreen.morphia.Morphia;

import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;

import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
Expand All @@ -19,7 +23,6 @@ public class MongoConnectionManager implements MorphiaConstants{

private final static Logger logger = Logger.getLogger(MongoConnectionManager.class.getName());
protected DB db;
protected MongoClient mongo;
protected Datastore datastore;

public static MongoConnectionManager getConnectionManager() {
Expand Down Expand Up @@ -49,9 +52,9 @@ private MongoConnectionManager (){
String database;
logger.fine("Creating the MongoDB Client connection. Looking up host and port information " );
try {
host = (String) new InitialContext().lookup(HOSTNAME);
port = (String) new InitialContext().lookup(PORT);
database = (String) new InitialContext().lookup(DATABASE);
host = (String) new InitialContext().lookup("java:comp/env/" + HOSTNAME);
port = (String) new InitialContext().lookup("java:comp/env/" + PORT);
database = (String) new InitialContext().lookup("java:comp/env/" + DATABASE);
ServerAddress server = new ServerAddress(host, Integer.parseInt(port));
MongoClient mongo = new MongoClient(server);
db = mongo.getDB(database);
Expand All @@ -66,7 +69,13 @@ private MongoConnectionManager (){
logger.severe("Unable to retreive reference to database, please check the server logs.");
} else {
Morphia morphia = new Morphia();
datastore = morphia.createDatastore(db.getMongo(), db.getName());
try {
morphia.getMapper().getConverters().addConverter(new BigDecimalConverter());
datastore = morphia.createDatastore(new MongoClient(db.getMongo().getConnectPoint()), db.getName());
} catch (UnknownHostException e) {
logger.severe("Caught Exception : " + e.getMessage() );
}

}
}

Expand All @@ -78,7 +87,11 @@ public Datastore getDatastore(){
return datastore;
}

public void close(){
mongo.close();
public String getDriverVersion(){
return datastore.getMongo().getVersion();
}

public String getMongoVersion(){
return datastore.getDB().command("buildInfo").getString("version");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;

Expand All @@ -40,13 +41,19 @@
import com.ibm.websphere.objectgrid.ObjectGridException;
import com.ibm.websphere.objectgrid.ObjectMap;
import com.ibm.websphere.objectgrid.Session;
import com.ibm.websphere.objectgrid.UndefinedMapException;
import com.ibm.websphere.objectgrid.plugins.TransactionCallbackException;
import com.ibm.websphere.objectgrid.plugins.index.MapIndex;
import com.ibm.websphere.objectgrid.plugins.index.MapIndexPlugin;
import com.ibm.websphere.objectgrid.query.ObjectQuery;

@DataService(name=WXSConstants.KEY,description=WXSConstants.KEY_DESCRIPTION)
public class BookingServiceImpl implements BookingService, WXSConstants {

private final static Logger logger = Logger.getLogger(BookingService.class.getName());

private static final String BOOKING_MAP_NAME="Booking";
private static String BOOKING_MAP_NAME="Booking";
private static String BASE_BOOKING_MAP_NAME="Booking";

private ObjectGrid og;

Expand All @@ -61,6 +68,7 @@ public class BookingServiceImpl implements BookingService, WXSConstants {
private void initialization() {
try {
og = WXSSessionManager.getSessionManager().getObjectGrid();
BOOKING_MAP_NAME = BASE_BOOKING_MAP_NAME + WXSSessionManager.getSessionManager().getMapSuffix();
} catch (ObjectGridException e) {
logger.severe("Unable to retreive the ObjectGrid reference " + e.getMessage());
}
Expand Down Expand Up @@ -190,6 +198,33 @@ public List<Booking> getBookingsByUser(String user) {

@Override
public Long count () {
try {
Session session = og.getSession();
ObjectMap objectMap = session.getMap(BOOKING_MAP_NAME);
MapIndex mapIndex = (MapIndex)objectMap.getIndex("com.ibm.ws.objectgrid.builtin.map.KeyIndex");
Iterator<?> keyIterator = mapIndex.findAll();
Long result = 0L;
while(keyIterator.hasNext()) {
keyIterator.next();
result++;
}
/*
int partitions = og.getMap(BOOKING_MAP_NAME).getPartitionManager().getNumOfPartitions();
Long result = 0L;
ObjectQuery query = og.getSession().createObjectQuery("SELECT COUNT ( o ) FROM " + BOOKING_MAP_NAME + " o ");
for(int i = 0; i<partitions;i++){
query.setPartition(i);
result += (Long) query.getSingleResult();
}
*/
return result;
} catch (UndefinedMapException e) {
e.printStackTrace();
} catch (TransactionCallbackException e) {
e.printStackTrace();
} catch (ObjectGridException e) {
e.printStackTrace();
}
return -1L;
}
}
Loading

0 comments on commit a8bca17

Please sign in to comment.