Example usage for com.mongodb CommandResult ok

List of usage examples for com.mongodb CommandResult ok

Introduction

In this page you can find the example usage for com.mongodb CommandResult ok.

Prototype

public boolean ok() 

Source Link

Document

Gets the "ok" field, which is whether this command executed correctly or not.

Usage

From source file:org.swissbib.docproc.flink.plugins.GNDContentEnrichment.java

License:Open Source License

private void initializeMongoConnection(HashMap<String, String> configuration) throws Exception {

    try {//from w w  w.  j a va  2  s.co m

        String[] mongoClient = configuration.get("MONGO.CLIENT").split("###");
        String[] mongoAuthentication = null;

        if (configuration.containsKey("MONGO.AUTHENTICATION")) {
            mongoAuthentication = configuration.get("MONGO.AUTHENTICATION").split("###");
        }

        ServerAddress server = new ServerAddress(mongoClient[0], Integer.valueOf(mongoClient[1]));
        String[] mongoDB = configuration.get("MONGO.DB").split("###");

        DB db = null;
        if (mongoAuthentication != null) {
            MongoCredential credential = MongoCredential.createMongoCRCredential(mongoAuthentication[1],
                    mongoAuthentication[0], mongoAuthentication[2].toCharArray());
            mClient = new MongoClient(server, Arrays.asList(credential));
            db = mClient.getDB(mongoAuthentication[0]);
        } else {
            mClient = new MongoClient(server);
            db = mClient.getDB(mongoDB[0]);
        }

        //simple test if authentication was successfull
        CommandResult cR = db.getStats();

        if (cR != null && !cR.ok()) {
            throw new Exception(
                    "authentication against database wasn't possible - no GND Processing will take place when type is called from XSLT templates");
        }

        nativeSource = mClient.getDB(mongoDB[0]);
        searchCollection = nativeSource.getCollection(mongoDB[1]);
        searchField = mongoDB[2];
        responseField = mongoDB[3];
        if (mongoDB.length > 4) {
            responseFieldMACS = mongoDB[4];
        }

    } catch (UnknownHostException uHE) {
        gndProcessingError.error("MongoError", "Mongo Connection couldn't be established");
        gndProcessingError.error("MongoError", uHE);
        uHE.printStackTrace();
        throw uHE;

    } catch (Exception ex) {
        gndProcessingError.error("MongoError", "General Exception while trying to connect to Mongo");
        gndProcessingError.error("MongoError", ex);
        ex.printStackTrace();
        throw ex;

    }
}

From source file:org.swissbib.srw.SRWUpdateServiceLifecyle.java

License:Open Source License

@Override
public void startUp(ConfigurationContext configurationContext, AxisService axisService) {

    System.out.println("in startup");

    final String UPD_DIR = "updateDir";
    final String DEL_DIR = "deleteDir";
    final String DEL_PATTERN = "deletePattern";
    final String TRANSFORM_TEMPLATE = "transformTemplate";
    final String FILE_PREFIX = "filePrefix";
    final String FILE_SUFFIX = "fileSuffix";
    final String RECORD_NS = "recordWithNamespaces";
    final String NORMALIZE_CHARS = "normalizeChars";
    final String RECORD_IN_RESPONSE = "includeRecordInResponse";

    final String LOG_MESSAGES = "logMessages";
    final String MONGO_CLIENT = "MONGO.CLIENT";
    final String MONGO_AUTHENTICATION = "MONGO.AUTHENTICATION";
    final String MONGO_DB = "MONGO.DB";
    final String ACTIVE_MONGO_CLIENT = "activeMongoClient";
    final String ACTIVE_MONGO_COLLECTION = "activeMongoCollection";
    final String CHECK_LEADER_FOR_DELETE = "checkLeaderForDelete";

    try {//from  ww w  .  j av  a  2  s.c  o  m
        Parameter updDir = axisService.getParameter(UPD_DIR);
        axisService.addParameter(updDir);

        Parameter delDir = axisService.getParameter(DEL_DIR);
        axisService.addParameter(delDir);

        Parameter filePrefix = axisService.getParameter(FILE_PREFIX);
        axisService.addParameter(filePrefix);

        Parameter fileSuffix = axisService.getParameter(FILE_SUFFIX);
        axisService.addParameter(fileSuffix);

        Parameter recordNS = axisService.getParameter(RECORD_NS);
        axisService.addParameter(recordNS);

        Parameter checkLeaderForDelete = axisService.getParameter(CHECK_LEADER_FOR_DELETE);
        boolean checkLeader = Boolean.valueOf(checkLeaderForDelete.getValue().toString());
        axisService.addParameter(CHECK_LEADER_FOR_DELETE, checkLeader);

        Parameter normalizeChars = axisService.getParameter(NORMALIZE_CHARS);
        axisService.addParameter(normalizeChars);

        Parameter recordInResponse = axisService.getParameter(RECORD_IN_RESPONSE);
        axisService.addParameter(recordInResponse);

        String transformTemplate = axisService.getParameter(TRANSFORM_TEMPLATE).getValue().toString();

        InputStream stream = getClass().getClassLoader().getResourceAsStream(transformTemplate);

        StreamSource source = new StreamSource(stream);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        //Transformer transform = transformerFactory.newTransformer(source);
        Templates recordTransformer = transformerFactory.newTemplates(source);

        axisService.addParameter(new Parameter(TRANSFORM_TEMPLATE, recordTransformer));

        //logging of messages?
        Parameter logging = axisService.getParameter(LOG_MESSAGES);

        boolean logActive = Boolean.valueOf(logging.getValue().toString());
        if (logActive) {

            try {

                //it is expected:
                // <parameter name="MONGO.CLIENT">[host]###[port]</parameter>
                String[] mongoClient = ((String) axisService.getParameter(MONGO_CLIENT).getValue())
                        .split("###");

                //Todo: right now I expect the Mongo storage is running in secure mode
                //if not the procedure to connect is a little bit different
                //take a look at GND and DSV11 Plugin in content2SearchDoc repository. Configuration for messageCatcher should be adapted

                //it is expected that mongoAuthentication contains the values for:
                //<parameter name="MONGO.AUTHENTICATION">[auth-db]###[user]###[password]</parameter>
                String[] mongoAuthentication = ((String) axisService.getParameter(MONGO_AUTHENTICATION)
                        .getValue()).split("###");
                //it is expected:
                //<parameter name="MONGO.DB">[logging DB]###[collection]</parameter>
                String[] mongoDB = ((String) axisService.getParameter(MONGO_DB).getValue()).split("###");

                System.out.println("mongoHost: " + mongoClient[0]);
                System.out.println("mongoPort: " + mongoClient[1]);

                ServerAddress server = new ServerAddress(mongoClient[0], Integer.valueOf(mongoClient[1]));

                MongoCredential credential = MongoCredential.createMongoCRCredential(mongoAuthentication[1],
                        mongoAuthentication[0], mongoAuthentication[2].toCharArray());
                MongoClient mClient = new MongoClient(server, Arrays.asList(credential));
                DB db = mClient.getDB(mongoAuthentication[0]);

                //simple test if authentication was successfull
                CommandResult cR = db.getStats();

                if (cR != null && cR.ok()) {

                    System.out.println("authenticated");
                    //mongoDB contains the application DB and collection within this DB
                    DB messageDB = mClient.getDB(mongoDB[0]);
                    DBCollection messageCollection = messageDB.getCollection(mongoDB[1]);
                    axisService.addParameter(new Parameter(ACTIVE_MONGO_COLLECTION, messageCollection));
                    axisService.addParameter(new Parameter(LOG_MESSAGES, "true"));

                } else {

                    System.out.println("not authenticated");
                    throw new Exception(
                            "authentication against Mongo database wasn't possible - message logging isn't possible");
                }

            } catch (UnknownHostException uhExc) {
                axisService.addParameter(new Parameter(LOG_MESSAGES, "false"));
                uhExc.printStackTrace();

            } catch (Exception exc) {

                axisService.addParameter(new Parameter(LOG_MESSAGES, "false"));
                exc.printStackTrace();
            }

        } else {
            axisService.addParameter(new Parameter(LOG_MESSAGES, "false"));
        }

    } catch (AxisFault aF) {
        System.out.println("Fehler bei Speichern von UPD_DIR und DEL_DIR");
        aF.printStackTrace();

    } catch (TransformerConfigurationException configExcept) {
        configExcept.printStackTrace();
    }

}