コード例 #1
0
 private void mapClasses() throws ClassNotFoundException {
   // Register all models.Class
   Set<String> classes = new HashSet<String>();
   classes.addAll(Classpath.getTypesAnnotatedWith(application, "models", Entity.class));
   classes.addAll(Classpath.getTypesAnnotatedWith(application, "models", Embedded.class));
   for (String clazz : classes) {
     MorphiaLogger.debug("mapping class: %1$s", clazz);
     morphia.map(Class.forName(clazz, true, application.classloader()));
   }
   // @see http://code.google.com/p/morphia/wiki/Datastore#Ensure_Indexes_and_Caps
   ds.ensureCaps(); // creates capped collections from @Entity
   ds.ensureIndexes(); // creates indexes from @Index annotations in your entities
 }
コード例 #2
0
  @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);
    }
  }