List of usage examples for com.mongodb Mongo close
public void close()
From source file:ezbake.services.centralPurge.thrift.EzCentralPurgeServiceHandler.java
License:Apache License
@Override public List<Long> getAllAgeOffEvents(EzSecurityToken token) throws TException { List<Long> result = null; DBCollection ageOffColl = null;/*from w ww. j a v a 2 s.c o m*/ Mongo mongoClient = null; AuditEvent evt = event(AuditEventType.FileObjectAccess.getName(), token).arg("event", "get all age off events"); try { validateCentralPurgeSecurityToken(token); // Get access to the ageoff collection within Mongo MongoConfigurationHelper mongoConfigurationHelper = new MongoConfigurationHelper(configuration); MongoHelper mongoHelper = new MongoHelper(configuration); mongoClient = mongoHelper.getMongo(); DB mongoDB = mongoClient.getDB(mongoConfigurationHelper.getMongoDBDatabaseName()); ageOffColl = mongoDB.getCollection(AGEOFF_COLLECTION); // Just get all ids from MongoDB result = new ArrayList<Long>(); DBCursor cursor = ageOffColl.find(); while (cursor.hasNext()) { result.add((Long) cursor.next().get(EzCentralPurgeServiceHelpers.AgeOffEventId)); } return result; } catch (EzSecurityTokenException e) { logError(e, evt, "CentralPurgeService failed when trying to validate token:[" + e.getClass().getName() + ":" + e.getMessage() + "]"); throw e; } catch (UnknownHostException e) { logError(e, evt, "CentralPurgeService unable to reach MongoDB in getAllAgeOffEvents:[" + e.getClass().getName() + ":" + e.getMessage() + "]"); throw new CentralPurgeServiceException( "CentralPurgeService unable to reach MongoDB in getAllAgeOffEvents:[" + e.getClass().getName() + ":" + e.getMessage() + "]"); } catch (Exception e) { logError(e, evt, "CentralPurgeService encountered an exception in getAllAgeOffEvents:[" + e.getClass().getName() + ":" + e.getMessage() + "]"); throw new CentralPurgeServiceException( "CentralPurgeService encountered an exception in getAllAgeOffEvents:[" + e.getClass().getName() + ":" + e.getMessage() + "]"); } finally { auditLogger.logEvent(evt); logEventToPlainLogs(logger, evt); if (mongoClient != null) mongoClient.close(); } }
From source file:ezbake.services.centralPurge.thrift.EzCentralPurgeServiceHandler.java
License:Apache License
@Override public CentralPurgeQueryResults getPagedSortedFilteredPurgeStates(EzSecurityToken token, List<CentralPurgeStatus> statuses, int pageNum, int numPerPage) throws EzSecurityTokenException, CentralPurgeServiceException { DBCollection purgeColl = null;/* w w w. j av a 2 s . com*/ Mongo mongoClient = null; AuditEvent evt = event(AuditEventType.FileObjectAccess.getName(), token) .arg("event", "getPagedSortedFilteredPurgeStates").arg("statuses", statuses).arg("pageNum", pageNum) .arg("numPerPage", numPerPage); try { validateCentralPurgeSecurityToken(token); // Get access to the purge collection within Mongo MongoConfigurationHelper mongoConfigurationHelper = new MongoConfigurationHelper(configuration); MongoHelper mongoHelper = new MongoHelper(configuration); mongoClient = mongoHelper.getMongo(); DB mongoDB = mongoClient.getDB(mongoConfigurationHelper.getMongoDBDatabaseName()); purgeColl = mongoDB.getCollection(PURGE_COLLECTION); List<CentralPurgeState> result = new ArrayList<>(); List<Integer> statusesValues = new LinkedList<>(); for (CentralPurgeStatus status : statuses) { statusesValues.add(status.getValue()); } // Gets all centralPurgeStates that are in the statuses and pages BasicDBObject query = new BasicDBObject( EzCentralPurgeServiceHelpers.CentralPurgeStateString + "." + EzCentralPurgeServiceHelpers.CentralPurgeStatusString, new BasicDBObject("$in", statusesValues)); DBCursor cursor = purgeColl.find(query).sort(new BasicDBObject(PurgeId, -1)) .skip(pageNum > 0 ? ((pageNum - 1) * numPerPage) : 0).limit(numPerPage); // Decodes each centralPurgeState from how it's stored in MongoDB and adds it to the return variable for (DBObject dbObject : cursor) { CentralPurgeState centralPurgeState = decodeCentralPurgeState( (DBObject) dbObject.get(CentralPurgeStateString)); result.add(centralPurgeState); } CentralPurgeQueryResults centralPurgeQueryResults = new CentralPurgeQueryResults(); centralPurgeQueryResults.setPurgeStates(result); centralPurgeQueryResults.setCount(purgeColl.count(query)); return centralPurgeQueryResults; } catch (EzSecurityTokenException e) { logError(e, evt, "CentralPurgeService failed when trying to validate token:[" + e.getClass().getName() + ":" + e.getMessage() + "]"); throw e; } catch (UnknownHostException e) { logError(e, evt, "CentralPurgeService unable to reach MongoDB in getPagedSortedFilteredPurgeStates:[" + e.getClass().getName() + ":" + e.getMessage() + "]"); throw new CentralPurgeServiceException( "CentralPurgeService unable to reach MongoDB in getPagedSortedFilteredPurgeStates:[" + e.getClass().getName() + ":" + e.getMessage() + "]"); } catch (Exception e) { logError(e, evt, "CentralPurgeService encountered an exception in getPagedSortedFilteredPurgeStates:[" + e.getClass().getName() + ":" + e.getMessage() + "]"); throw new CentralPurgeServiceException( "CentralPurgeService encountered an exception in getPagedSortedFilteredPurgeStates:[" + e.getClass().getName() + ":" + e.getMessage() + "]"); } finally { auditLogger.logEvent(evt); logEventToPlainLogs(logger, evt); if (mongoClient != null) mongoClient.close(); } }
From source file:ezbake.services.centralPurge.thrift.EzCentralPurgeServiceHandler.java
License:Apache License
@Override public CentralAgeOffEventQueryResults getPagedSortedFilteredAgeOffEventStates(EzSecurityToken token, List<CentralPurgeStatus> statuses, int pageNum, int numPerPage) throws EzSecurityTokenException, CentralPurgeServiceException { DBCollection ageOffColl = null;/*from w ww . jav a2s . com*/ Mongo mongoClient = null; AuditEvent evt = event(AuditEventType.FileObjectAccess.getName(), token) .arg("event", "getPagedSortedFilteredAgeOffEventStates").arg("statuses", statuses) .arg("pageNum", pageNum).arg("numPerPage", numPerPage); try { validateCentralPurgeSecurityToken(token); // Get access to the ageoff collection within Mongo MongoConfigurationHelper mongoConfigurationHelper = new MongoConfigurationHelper(configuration); MongoHelper mongoHelper = new MongoHelper(configuration); mongoClient = mongoHelper.getMongo(); DB mongoDB = mongoClient.getDB(mongoConfigurationHelper.getMongoDBDatabaseName()); ageOffColl = mongoDB.getCollection(AGEOFF_COLLECTION); List<CentralAgeOffEventState> result = new ArrayList<>(); List<Integer> statusesValues = new LinkedList<>(); for (CentralPurgeStatus status : statuses) { statusesValues.add(status.getValue()); } // Gets all centralPurgeStates that are in the statuses and pages BasicDBObject query = new BasicDBObject( EzCentralPurgeServiceHelpers.CentralAgeOffStateString + "." + EzCentralPurgeServiceHelpers.CentralPurgeStatusString, new BasicDBObject("$in", statusesValues)); DBCursor cursor = ageOffColl.find(query).sort(new BasicDBObject(AgeOffEventId, -1)) .skip(pageNum > 0 ? ((pageNum - 1) * numPerPage) : 0).limit(numPerPage); for (DBObject dbObject : cursor) { CentralAgeOffEventState centralAgeOffEventState = decodeCentralAgeOffEventState( (DBObject) dbObject.get(CentralAgeOffStateString)); result.add(centralAgeOffEventState); } CentralAgeOffEventQueryResults centralAgeOffEventQueryResults = new CentralAgeOffEventQueryResults(); centralAgeOffEventQueryResults.setAgeOffEventStates(result); centralAgeOffEventQueryResults.setCount(ageOffColl.count(query)); return centralAgeOffEventQueryResults; } catch (EzSecurityTokenException e) { logError(e, evt, "CentralPurgeService failed when trying to validate token:[" + e.getClass().getName() + ":" + e.getMessage() + "]"); throw e; } catch (UnknownHostException e) { logError(e, evt, "CentralPurgeService unable to reach MongoDB in getPagedSortedFilteredAgeOffEventStates:[" + e.getClass().getName() + ":" + e.getMessage() + "]"); throw new CentralPurgeServiceException( "CentralPurgeService unable to reach MongoDB in getPagedSortedFilteredAgeOffEventStates:[" + e.getClass().getName() + ":" + e.getMessage() + "]"); } catch (Exception e) { logError(e, evt, "CentralPurgeService encountered an exception in getPagedSortedFilteredAgeOffEventStates:[" + e.getClass().getName() + ":" + e.getMessage() + "]"); throw new CentralPurgeServiceException( "CentralPurgeService encountered an exception in getPagedSortedFilteredAgeOffEventStates:[" + e.getClass().getName() + ":" + e.getMessage() + "]"); } finally { auditLogger.logEvent(evt); logEventToPlainLogs(logger, evt); if (mongoClient != null) mongoClient.close(); } }
From source file:ezbake.services.centralPurge.thrift.EzCentralPurgeServiceHandler.java
License:Apache License
private CentralAgeOffEventState getCentralAgeOffEventState(Long ageOffId) throws UnknownHostException { Mongo mongoClient = null; DBCollection ageOffColl = null;//w w w. j ava 2 s. c o m try { MongoConfigurationHelper mongoConfigurationHelper = new MongoConfigurationHelper(configuration); MongoHelper mongoHelper = new MongoHelper(configuration); mongoClient = mongoHelper.getMongo(); DB mongoDB = mongoClient.getDB(mongoConfigurationHelper.getMongoDBDatabaseName()); ageOffColl = mongoDB.getCollection(AGEOFF_COLLECTION); //Get the centralAgeOffEventState and return it BasicDBObject query = new BasicDBObject(EzCentralPurgeServiceHelpers.AgeOffEventId, ageOffId); DBCursor cursor = ageOffColl.find(query); DBObject dbObject = null; if (cursor.hasNext()) { dbObject = cursor.next(); } else { return null; } return decodeCentralAgeOffEventState((DBObject) dbObject.get(CentralAgeOffStateString)); } finally { if (mongoClient != null) mongoClient.close(); } }
From source file:ezbake.services.centralPurge.thrift.EzCentralPurgeServiceHandler.java
License:Apache License
private void updateCentralAgeOffEventState(CentralAgeOffEventState centralAgeOffEventState, Long ageOffId) throws UnknownHostException { DBCollection ageOffColl = null;//from w w w . j a va 2 s . c o m Mongo mongoClient = null; try { MongoConfigurationHelper mongoConfigurationHelper = new MongoConfigurationHelper(configuration); MongoHelper mongoHelper = new MongoHelper(configuration); mongoClient = mongoHelper.getMongo(); DB mongoDB = mongoClient.getDB(mongoConfigurationHelper.getMongoDBDatabaseName()); ageOffColl = mongoDB.getCollection(AGEOFF_COLLECTION); // Update the state if it exists, insert if not BasicDBObject query = new BasicDBObject(EzCentralPurgeServiceHelpers.AgeOffEventId, ageOffId); BasicDBObject ageOffEvent = new BasicDBObject().append(AgeOffEventId, ageOffId) .append(CentralAgeOffStateString, encodeCentralAgeOffEventState(centralAgeOffEventState)); boolean upsert = true; boolean multiUpdate = false; ageOffColl.update(query, ageOffEvent, upsert, multiUpdate); } catch (UnknownHostException e) { // TODO: log that couldn't connect to MongoDB throw e; } finally { if (mongoClient != null) mongoClient.close(); } }
From source file:ezbake.services.centralPurge.thrift.EzCentralPurgeServiceHandler.java
License:Apache License
private List<Long> getAllPurgeIdsInMongo() throws UnknownHostException { List<Long> result = null; DBCollection purgeColl = null;//from w ww . j a v a 2s.c om Mongo mongoClient = null; try { MongoConfigurationHelper mongoConfigurationHelper = new MongoConfigurationHelper(configuration); MongoHelper mongoHelper = new MongoHelper(configuration); mongoClient = mongoHelper.getMongo(); DB mongoDB = mongoClient.getDB(mongoConfigurationHelper.getMongoDBDatabaseName()); purgeColl = mongoDB.getCollection(PURGE_COLLECTION); // Just get all ids from MongoDB result = new ArrayList<Long>(); DBCursor cursor = purgeColl.find(); while (cursor.hasNext()) { result.add((Long) cursor.next().get(EzCentralPurgeServiceHelpers.PurgeId)); } return result; } finally { if (mongoClient != null) { mongoClient.close(); } } }
From source file:ezbake.services.centralPurge.thrift.EzCentralPurgeServiceHandler.java
License:Apache License
private CentralPurgeState getCentralPurgeState(Long purgeId) throws UnknownHostException { DBCollection purgeColl = null;// www. ja v a 2 s . co m Mongo mongoClient = null; try { MongoConfigurationHelper mongoConfigurationHelper = new MongoConfigurationHelper(configuration); MongoHelper mongoHelper = new MongoHelper(configuration); mongoClient = mongoHelper.getMongo(); DB mongoDB = mongoClient.getDB(mongoConfigurationHelper.getMongoDBDatabaseName()); purgeColl = mongoDB.getCollection(PURGE_COLLECTION); //Get the centralPurgeState and return it BasicDBObject query = new BasicDBObject(PurgeId, purgeId); DBCursor cursor = purgeColl.find(query); DBObject dbObject = null; if (cursor.hasNext()) { dbObject = cursor.next(); } else { return null; } return decodeCentralPurgeState((DBObject) dbObject.get(CentralPurgeStateString)); } finally { if (mongoClient != null) { mongoClient.close(); } } }
From source file:ezbake.services.centralPurge.thrift.EzCentralPurgeServiceHandler.java
License:Apache License
private void updateCentralPurgeState(CentralPurgeState centralPurgeState, Long purgeId) throws UnknownHostException { DBCollection purgeColl = null;/*w w w .jav a2 s. c om*/ Mongo mongoClient = null; try { MongoConfigurationHelper mongoConfigurationHelper = new MongoConfigurationHelper(configuration); MongoHelper mongoHelper = new MongoHelper(configuration); mongoClient = mongoHelper.getMongo(); DB mongoDB = mongoClient.getDB(mongoConfigurationHelper.getMongoDBDatabaseName()); purgeColl = mongoDB.getCollection(PURGE_COLLECTION); // Update the state if it exists, insert if not BasicDBObject query = new BasicDBObject(PurgeId, purgeId); BasicDBObject purgeStatus = new BasicDBObject() .append(PurgeId, centralPurgeState.getPurgeInfo().getId()) .append(CentralPurgeStateString, encodeCentralPurgeState(centralPurgeState)); boolean upsert = true; boolean multiUpdate = false; purgeColl.update(query, purgeStatus, upsert, multiUpdate); } finally { if (mongoClient != null) mongoClient.close(); } }
From source file:fr.cnes.sitools.datasource.mongodb.ActivationDataSourceResource.java
License:Open Source License
/** * Testing connections//from w w w . ja v a 2 s. com * * @param ds * the DataSource to test * @param trace * a {@link List} of String to store the trace of the test. Can be null * @return Representation results of the connection test. */ public boolean testDataSourceConnection(MongoDBDataSource ds, List<String> trace) { Boolean result = Boolean.TRUE; Mongo mongo = null; try { do { trace(trace, "Test mongo data source connection ..."); try { mongo = new Mongo(ds.getUrl(), ds.getPortNumber()); } catch (Exception e) { result = false; getMongoDBDataSourceAdministration().getLogger().info(e.getMessage()); trace(trace, "Load driver class failed. Cause: " + e.getMessage()); break; } try { // check if the database exists // List<String> databases = mongo.getDatabaseNames(); // if (!databases.contains(ds.getDatabaseName())) { // result = false; // getMongoDBDataSourceAdministration().getLogger().info("Database does not exist"); // trace.add("Database " + ds.getDatabaseName() + " does not exist"); // break; // } DB db = mongo.getDB(ds.getDatabaseName()); // if no user and password given lets authenticate on the database if (ds.isAuthentication() && !db.isAuthenticated() && !db.authenticate(ds.getUserLogin(), ds.getUserPassword().toCharArray())) { result = false; getMongoDBDataSourceAdministration().getLogger().info("Authentication failed"); trace(trace, "Authentication failed"); break; } // try to get the stats of the database to check whether or not the database is accessible CommandResult cmd = db.getStats(); if (!cmd.ok()) { result = false; getMongoDBDataSourceAdministration().getLogger() .info("Error connecting to the database " + cmd); trace(trace, "Error connecting to the database " + cmd); break; } trace(trace, "Get connection to the database : OK"); } catch (Exception e) { result = false; getMongoDBDataSourceAdministration().getLogger().info(e.getMessage()); trace(trace, "Get connection to the database failed. Cause: " + e.getMessage()); break; } } while (false); } finally { if (mongo != null) { mongo.close(); } } return result; }
From source file:home.vitaly.simulator.mongoCursorReader.java
License:Open Source License
public static void main(String[] args) throws Exception { Mongo conn = null; mongoCursorReader manager = new mongoCursorReader(); try {/*ww w .java2 s . c o m*/ // Get the MongoDB connection. Mongo is the connection object. conn = manager.getConnection(); // Get the database from the Connection (Schema in RDBMS World). DB db = conn.getDB("DBTR"); // Get the collection (Table in RDBMS World). Don't worry, even if it is not there, // it can be a lazy fetch and will be created when the first object is inserted DBCollection collection = manager.getCollection("tr", db); // Check whether there is any document (RDBMS world -> Row), present // if (collection.count() == 0) { // if(collection.count() < 10) { // // Create the document in the TEST collection (RDBMS worlqd -> Table) // DBObject object = new BasicDBObject(); // object.put("name", "? "); // object.put("message", " ? !"); // collection.insert(object); // } manager.printCollection(collection); } finally { if (conn != null) { conn.close(); } } }