Search in sources :

Example 6 with MongoException

use of com.mongodb.MongoException in project spring-cloud-connectors by spring-cloud.

the class MongoDbFactoryCreator method create.

@Override
public MongoDbFactory create(MongoServiceInfo serviceInfo, ServiceConnectorConfig config) {
    try {
        MongoClientOptions.Builder mongoOptionsToUse = getMongoOptions((MongoDbFactoryConfig) config);
        SimpleMongoDbFactory mongoDbFactory = createMongoDbFactory(serviceInfo, mongoOptionsToUse);
        return configure(mongoDbFactory, (MongoDbFactoryConfig) config);
    } catch (UnknownHostException e) {
        throw new ServiceConnectorCreationException(e);
    } catch (MongoException e) {
        throw new ServiceConnectorCreationException(e);
    }
}
Also used : MongoException(com.mongodb.MongoException) SimpleMongoDbFactory(org.springframework.data.mongodb.core.SimpleMongoDbFactory) UnknownHostException(java.net.UnknownHostException) MongoClientOptions(com.mongodb.MongoClientOptions) ServiceConnectorCreationException(org.springframework.cloud.service.ServiceConnectorCreationException) Builder(com.mongodb.MongoClientOptions.Builder)

Example 7 with MongoException

use of com.mongodb.MongoException in project meclipse by flaper87.

the class Collection method makeActions.

private void makeActions() {
    insert = new Action(getCaption("collection.insertDoc")) {

        @Override
        public void run() {
            FileDialog dialog = new FileDialog(view.getSite().getShell(), SWT.OPEN);
            dialog.setFilterExtensions(new String[] { "*.json" });
            String result = dialog.open();
            if (result != null) {
                try {
                    String jsonText = IOUtils.readFile(new File(result));
                    JSONObject jsonObj = new JSONObject(jsonText);
                    col.insert(JSONUtils.toDBObject(jsonObj));
                } catch (Exception ex) {
                    UIUtils.openErrorDialog(view.getSite().getShell(), ex.toString());
                }
            }
        }
    };
    rename = new Action(getCaption("collection.renameColl")) {

        @Override
        public void run() {
            InputDialog dialog = new InputDialog(view.getSite().getShell(), getCaption("collection.renameColl"), getCaption("collection.msg.newCollName"), col.getName(), new RequiredInputValidator(getCaption("collection.msg.inputCollName")));
            if (dialog.open() == InputDialog.OK) {
                try {
                    col.rename(dialog.getValue());
                } catch (MongoException ex) {
                    UIUtils.openErrorDialog(view.getSite().getShell(), ex.toString());
                }
                view.getViewer().refresh(getParent());
            }
        }
    };
    delete = new Action(getCaption("collection.deleteColl")) {

        @Override
        public void run() {
            if (MessageDialog.openConfirm(view.getSite().getShell(), getCaption("confirm"), String.format(getCaption("collection.msg.reallyDeleteColl"), col.getName()))) {
                col.drop();
                view.getViewer().refresh(getParent());
            }
        }
    };
}
Also used : RequiredInputValidator(org.mongodb.meclipse.util.RequiredInputValidator) IAction(org.eclipse.jface.action.IAction) Action(org.eclipse.jface.action.Action) InputDialog(org.eclipse.jface.dialogs.InputDialog) MongoException(com.mongodb.MongoException) JSONObject(org.json.JSONObject) FileDialog(org.eclipse.swt.widgets.FileDialog) File(java.io.File) MongoException(com.mongodb.MongoException)

Example 8 with MongoException

use of com.mongodb.MongoException in project meclipse by flaper87.

the class Connection method getMongo.

// public TreeObject [] getChildren() {
// loadDatabases();
// return super.getChildren();
// }
/**
	 * In the style of lazy-loading, this is where we actually initiate the
	 * connection to a MongoDB instance - and this is where a user would 1st
	 * request to see data obtained via the connection.
	 * 
	 * @return
	 */
public Mongo getMongo() {
    MongoInstance mongoInstance = MeclipsePlugin.getDefault().getMongoInstance(this.getName());
    Exception ex;
    if (mongoInstance.getMongo() == null) {
        Mongo mongo;
        try {
            mongo = new Mongo(mongoInstance.getHost(), mongoInstance.getPort());
            mongo.getDatabaseNames();
            // add the active Mongo instance
            mongoInstance.setMongo(mongo);
            // to the plug-in's state
            isDown = false;
            return mongo;
        /* catch some possible exceptions */
        } catch (MongoException e) {
            ex = e;
        } catch (UnknownHostException e) {
            ex = e;
        }
        if (!isDown) {
            this.showMessage(String.format(getCaption("connection.connectionError"), this.getName(), mongoInstance.getHost(), ex));
            isDown = true;
        }
        return null;
    } else {
        return mongoInstance.getMongo();
    }
}
Also used : MongoException(com.mongodb.MongoException) UnknownHostException(java.net.UnknownHostException) Mongo(com.mongodb.Mongo) MongoInstance(org.mongodb.meclipse.preferences.MongoInstance) MongoException(com.mongodb.MongoException) UnknownHostException(java.net.UnknownHostException)

Example 9 with MongoException

use of com.mongodb.MongoException in project GNS by MobilityFirst.

the class MongoRecords method contains.

@Override
public boolean contains(String collectionName, String guid) throws FailedDBOperationException {
    db.requestStart();
    try {
        String primaryKey = mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey().getName();
        db.requestEnsureConnection();
        DBCollection collection = db.getCollection(collectionName);
        BasicDBObject query = new BasicDBObject(primaryKey, guid);
        DBCursor cursor = collection.find(query);
        return cursor.hasNext();
    } catch (MongoException e) {
        DatabaseConfig.getLogger().log(Level.FINE, "{0} contains failed: {1}", new Object[] { dbName, e.getMessage() });
        throw new FailedDBOperationException(collectionName, guid, "Original mongo exception:" + e.getMessage());
    } finally {
        db.requestDone();
    }
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) MongoException(com.mongodb.MongoException) JSONObject(org.json.JSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Example 10 with MongoException

use of com.mongodb.MongoException in project GNS by MobilityFirst.

the class MongoRecords method lookupEntireRecord.

private JSONObject lookupEntireRecord(String collectionName, String guid, boolean explain) throws RecordNotFoundException, FailedDBOperationException {
    long startTime = System.currentTimeMillis();
    db.requestStart();
    try {
        String primaryKey = mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey().getName();
        db.requestEnsureConnection();
        DBCollection collection = db.getCollection(collectionName);
        BasicDBObject query = new BasicDBObject(primaryKey, guid);
        DBCursor cursor = collection.find(query);
        if (explain) {
            System.out.println(cursor.explain().toString());
        }
        if (cursor.hasNext()) {
            DBObject obj = cursor.next();
            // arun: optimized for the common case of Map
            @SuppressWarnings("unchecked") JSONObject json = obj instanceof Map ? DiskMapRecords.recursiveCopyMap((Map<String, ?>) obj) : new JSONObject(obj.toString());
            // instrumentation
            DelayProfiler.updateDelay("lookupEntireRecord", startTime);
            // older style
            int lookupTime = (int) (System.currentTimeMillis() - startTime);
            if (lookupTime > 20) {
                DatabaseConfig.getLogger().log(Level.FINE, "{0} mongoLookup Long delay {1}", new Object[] { dbName, lookupTime });
            }
            return json;
        } else {
            throw new RecordNotFoundException(guid);
        }
    } catch (JSONException e) {
        DatabaseConfig.getLogger().log(Level.WARNING, "{0} Unable to parse JSON: {1}", new Object[] { dbName, e.getMessage() });
        return null;
    } catch (MongoException e) {
        DatabaseConfig.getLogger().log(Level.FINE, "{0} lookupEntireRecord failed: {1}", new Object[] { dbName, e.getMessage() });
        throw new FailedDBOperationException(collectionName, guid, "Original mongo exception:" + e.getMessage());
    } finally {
        db.requestDone();
    }
}
Also used : MongoException(com.mongodb.MongoException) JSONException(org.json.JSONException) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) HashMap(java.util.HashMap) Map(java.util.Map) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap)

Aggregations

MongoException (com.mongodb.MongoException)42 BasicDBObject (com.mongodb.BasicDBObject)22 DBObject (com.mongodb.DBObject)21 DBCollection (com.mongodb.DBCollection)16 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)12 JSONObject (org.json.JSONObject)12 DBCursor (com.mongodb.DBCursor)8 RecordNotFoundException (edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException)4 IOException (java.io.IOException)4 UnknownHostException (java.net.UnknownHostException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 JSONException (org.json.JSONException)4 Stopwatch (com.google.common.base.Stopwatch)3 BasicDBList (com.mongodb.BasicDBList)3 DuplicateKeyException (com.mongodb.DuplicateKeyException)3 MongoClient (com.mongodb.MongoClient)3 BulkWriteOperation (com.mongodb.BulkWriteOperation)2 CommandResult (com.mongodb.CommandResult)2 MongoClientURI (com.mongodb.MongoClientURI)2