Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add datastoreFactory to define converter and index
- Loading branch information
Showing
7 changed files
with
164 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
acmeair-services-morphia/src/main/java/com/acmeair/morphia/BigDecimalConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.acmeair.morphia; | ||
|
||
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; | ||
|
||
public class BigDecimalConverter extends TypeConverter implements SimpleValueConverter{ | ||
|
||
public BigDecimalConverter() { | ||
super(BigDecimal.class); | ||
} | ||
|
||
@Override | ||
public Object encode(Object value, MappedField optionalExtraInfo) { | ||
return value.toString(); | ||
} | ||
|
||
@Override | ||
public Object decode(Class targetClass, Object fromDBObject, MappedField optionalExtraInfo) throws MappingException { | ||
if (fromDBObject == null) return null; | ||
|
||
return new BigDecimal(fromDBObject.toString()); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
acmeair-services-morphia/src/main/java/com/acmeair/morphia/BigIntegerConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.acmeair.morphia; | ||
|
||
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; | ||
|
||
public class BigIntegerConverter extends TypeConverter implements SimpleValueConverter{ | ||
|
||
public BigIntegerConverter() { | ||
super(BigInteger.class); | ||
} | ||
|
||
@Override | ||
public Object encode(Object value, MappedField optionalExtraInfo) { | ||
return value.toString(); | ||
} | ||
|
||
@Override | ||
public Object decode(Class targetClass, Object fromDBObject, MappedField optionalExtraInfo) throws MappingException { | ||
if (fromDBObject == null) return null; | ||
|
||
return new BigInteger(fromDBObject.toString()); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
acmeair-services-morphia/src/main/java/com/acmeair/morphia/DatastoreFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.acmeair.morphia; | ||
|
||
import java.util.Properties; | ||
|
||
import org.json.simple.JSONArray; | ||
import org.json.simple.JSONObject; | ||
import org.json.simple.JSONValue; | ||
|
||
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 com.mongodb.MongoClient; | ||
import com.mongodb.MongoClientOptions; | ||
import com.mongodb.MongoClientURI; | ||
import com.mongodb.WriteConcern; | ||
|
||
public class DatastoreFactory { | ||
|
||
private static String mongourl = null; | ||
|
||
static { | ||
String vcapJSONString = System.getenv("VCAP_SERVICES"); | ||
if (vcapJSONString != null) { | ||
System.out.println("Reading VCAP_SERVICES"); | ||
Object jsonObject = JSONValue.parse(vcapJSONString); | ||
JSONObject json = (JSONObject)jsonObject; | ||
System.out.println("jsonObject = " + json.toJSONString()); | ||
for (Object key: json.keySet()) | ||
{ | ||
if (((String)key).contains("mongo")) | ||
{ | ||
System.out.println("Found mongo service:" +key); | ||
JSONArray mongoServiceArray = (JSONArray)json.get(key); | ||
JSONObject mongoService = (JSONObject) mongoServiceArray.get(0); | ||
JSONObject credentials = (JSONObject)mongoService.get("credentials"); | ||
mongourl = (String)credentials.get("url"); | ||
if (mongourl==null) | ||
mongourl= (String)credentials.get("uri"); | ||
System.out.println("service url = " + mongourl); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
public static Datastore getDatastore(Datastore ds) | ||
{ | ||
Datastore result =ds; | ||
|
||
if (mongourl!=null) | ||
{ | ||
try{ | ||
Properties prop = new Properties(); | ||
prop.load(DatastoreFactory.class.getResource("/acmeair-mongo.properties").openStream()); | ||
boolean fsync = new Boolean(prop.getProperty("mongo.fsync")); | ||
int w = new Integer(prop.getProperty("mongo.w")); | ||
int connectionsPerHost = new Integer(prop.getProperty("mongo.connectionsPerHost")); | ||
int threadsAllowedToBlockForConnectionMultiplier = new Integer(prop.getProperty("mongo.threadsAllowedToBlockForConnectionMultiplier")); | ||
|
||
// To match the local options | ||
MongoClientOptions.Builder builder = new MongoClientOptions.Builder() | ||
.writeConcern(new WriteConcern(w, 0, fsync)) | ||
.connectionsPerHost(connectionsPerHost) | ||
.threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier); | ||
|
||
MongoClientURI mongoURI = new MongoClientURI(mongourl, builder); | ||
MongoClient mongo = new MongoClient(mongoURI); | ||
Morphia morphia = new Morphia(); | ||
result = morphia.createDatastore( mongo ,mongoURI.getDatabase()); | ||
System.out.println("create mongo datastore with options:"+result.getMongo().getMongoOptions()); | ||
}catch (Exception e) | ||
{ | ||
e.printStackTrace(); | ||
} | ||
} | ||
// The converter is added for handing JDK 7 issue | ||
result.getMapper().getConverters().addConverter(new BigDecimalConverter()); | ||
result.getMapper().getConverters().addConverter(new BigIntegerConverter()); | ||
|
||
// Enable index | ||
result.ensureIndex(Booking.class, "pkey.customerId"); | ||
result.ensureIndex(Flight.class, "pkey.flightSegmentId,scheduledDepartureTime"); | ||
result.ensureIndex(FlightSegment.class, "originPort,destPort"); | ||
|
||
return result; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
acmeair-services-morphia/src/main/resources/acmeair-mongo.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
mongo.host=localhost | ||
mongo.port=27017 | ||
mongo.database=acmeair | ||
mongo.fsync=false | ||
mongo.w=0 | ||
mongo.connectionsPerHost=20 | ||
mongo.threadsAllowedToBlockForConnectionMultiplier=10 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters