public static Datastore ds(String dbName) { if (StringUtils.isBlank(dbName)) { return ds(); } Datastore ds = dataStores.get(dbName); if (null == ds) { Datastore ds0 = morphia.createDatastore(mongo, dbName); ds = dataStores.putIfAbsent(dbName, ds0); if (null == ds) { ds = ds0; } } return ds; }
@Override public void onStart() { if (!isEnabled) { return; } // Register SLF4JLogrImplFactory as Logger // @see http://nesbot.com/2011/11/28/play-2-morphia-logging-error MorphiaLoggerFactory.reset(); MorphiaLoggerFactory.registerLogger(SLF4JLogrImplFactory.class); try { Configuration morphiaConf = Configuration.root().getConfig(ConfigKey.PREFIX); if (morphiaConf == null) { throw Configuration.root() .reportError(ConfigKey.PREFIX, "Missing Morphia configuration", null); } MorphiaLogger.debug(morphiaConf); String dbName = morphiaConf.getString(ConfigKey.DB_NAME.getKey()); if (StringUtils.isBlank(dbName)) { throw morphiaConf.reportError( ConfigKey.DB_NAME.getKey(), "Missing Morphia configuration", null); } // Connect to MongoDB String seeds = morphiaConf.getString(ConfigKey.DB_SEEDS.getKey()); if (StringUtils.isNotBlank(seeds)) { mongo = connect(seeds); } else { mongo = connect( morphiaConf.getString(ConfigKey.DB_HOST.getKey()), morphiaConf.getString(ConfigKey.DB_PORT.getKey())); } morphia = new Morphia(); // To prevent problem during hot-reload if (application.isDev()) { morphia.getMapper().getOptions().objectFactory = new PlayCreator(); } // Configure validator new ValidationExtension(morphia); // Check if credentials parameters are present String username = morphiaConf.getString(ConfigKey.DB_USERNAME.getKey()); String password = morphiaConf.getString(ConfigKey.DB_PASSWORD.getKey()); if (StringUtils.isNotBlank(username) ^ StringUtils.isNotBlank(password)) { throw morphiaConf.reportError( ConfigKey.DB_NAME.getKey(), "Missing username or password", null); } // Create datastore if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) { ds = morphia.createDatastore(mongo, dbName, username, password.toCharArray()); } else { ds = morphia.createDatastore(mongo, dbName); } MorphiaLogger.debug("Datastore [%s] created", dbName); // Create GridFS String uploadCollection = morphiaConf.getString(ConfigKey.COLLECTION_UPLOADS.getKey()); if (StringUtils.isBlank(dbName)) { uploadCollection = "uploads"; MorphiaLogger.warn( "Missing Morphia configuration key [%s]. Use default value instead [%s]", ConfigKey.COLLECTION_UPLOADS, "uploads"); } gridfs = new GridFS(ds.getDB(), uploadCollection); MorphiaLogger.debug("GridFS created", ""); MorphiaLogger.debug("Add Interceptor...", ""); morphia .getMapper() .addInterceptor( new AbstractEntityInterceptor() { @Override public void postLoad(final Object ent, final DBObject dbObj, final Mapper mapr) { if (ent instanceof Model) { Model m = (Model) ent; m._post_Load(); } } }); MorphiaLogger.debug("Classes mapping...", ""); mapClasses(); MorphiaLogger.debug("End of initializing Morphia", ""); } catch (MongoException e) { MorphiaLogger.error(e, "Problem connecting MongoDB"); throw new RuntimeException(e); } catch (ClassNotFoundException e) { MorphiaLogger.error(e, "Problem mapping class"); throw new RuntimeException(e); } }