001    package com.khubla.pragmatach.plugin.mongodb.db;
002    
003    import javax.persistence.Entity;
004    
005    import com.khubla.pragmatach.framework.api.PragmatachException;
006    import com.khubla.pragmatach.framework.application.Application;
007    import com.mongodb.DB;
008    import com.mongodb.DBCollection;
009    import com.mongodb.MongoClient;
010    
011    /**
012     * @author tom
013     */
014    public class DBCollectionFactory {
015       /**
016        * singleton
017        */
018       private static DBCollectionFactory instance = null;
019    
020       private static DB getDB() throws PragmatachException {
021          try {
022             /*
023              * params
024              */
025             final String hostname = Application.getConfiguration().getParameter("mongodb.Hostname");
026             final String database = Application.getConfiguration().getParameter("mongodb.Database");
027             final String username = Application.getConfiguration().getParameter("mongodb.Username");
028             final String password = Application.getConfiguration().getParameter("mongodb.Password");
029             /*
030              * client & db
031              */
032             final MongoClient mongoClient = new MongoClient(hostname);
033             final DB db = mongoClient.getDB(database);
034             if (null != username) {
035                final boolean auth = db.authenticate(username, password.toCharArray());
036                if (false == auth) {
037                   throw new Exception("Unable to authenticate to db '" + database + "' with username '" + username + "'");
038                }
039             }
040             return db;
041          } catch (final Exception e) {
042             throw new PragmatachException("Exception in getDB", e);
043          }
044       }
045    
046       public static DBCollectionFactory getInstance() {
047          if (null == instance) {
048             instance = new DBCollectionFactory();
049          }
050          return instance;
051       }
052    
053       /**
054        * ctor
055        */
056       private DBCollectionFactory() {
057       }
058    
059       /**
060        * drop the DB, for testing purposes
061        */
062       public void dropDB() throws PragmatachException {
063          try {
064             final String database = Application.getConfiguration().getParameter("mongodb.Database");
065             final String hostname = Application.getConfiguration().getParameter("mongodb.Hostname");
066             final MongoClient mongoClient = new MongoClient(hostname);
067             final DB db = mongoClient.getDB(database);
068             db.dropDatabase();
069          } catch (final Exception e) {
070             throw new PragmatachException("Exception in dropDB", e);
071          }
072       }
073    
074       /**
075        * get the DBCollection
076        */
077       public <T> DBCollection getDBCollection(Class<T> clazz) {
078          try {
079             /*
080              * DB
081              */
082             final DB db = getDB();
083             /*
084              * collection name
085              */
086             final Entity entity = getEntity(clazz);
087             if (null == entity) {
088                throw new Exception("Type '" + clazz.getCanonicalName() + "' is not annotated as an @Entity");
089             }
090             /*
091              * collection
092              */
093             String collectionName = clazz.getSimpleName();
094             if (((null != entity.name()) && (entity.name().length() > 0))) {
095                collectionName = entity.name();
096             }
097             /*
098              * get collection
099              */
100             final DBCollection ret = db.getCollection(collectionName);
101             /*
102              * done
103              */
104             return ret;
105          } catch (final Exception e) {
106             throw new ExceptionInInitializerError(e);
107          }
108       }
109    
110       /**
111        * get the entity
112        */
113       private <T> Entity getEntity(Class<T> clazz) {
114          return clazz.getAnnotation(Entity.class);
115       }
116    }