Example usage for com.mongodb MongoClientURI MongoClientURI

List of usage examples for com.mongodb MongoClientURI MongoClientURI

Introduction

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

Prototype

public MongoClientURI(final String uri) 

Source Link

Document

Creates a MongoURI from the given string.

Usage

From source file:org.apache.eagle.alert.engine.publisher.dedup.MongoDedupEventsStore.java

License:Apache License

public MongoDedupEventsStore(Config config, String publishName) {
    this.config = config;
    this.publishName = publishName;
    this.connection = this.config.getString("connection");
    try {//from   w w  w  .j a v  a2  s .  c o  m
        this.client = new MongoClient(new MongoClientURI(this.connection));
        init();
    } catch (Throwable t) {
        LOG.error(String.format("initialize mongodb %s client failed", this.connection), t);
    }
}

From source file:org.apache.eagle.alert.metadata.impl.MongoMetadataDaoImpl.java

License:Apache License

@Inject
public MongoMetadataDaoImpl(Config config) {
    this.connection = config.getString("connection");
    this.cappedMaxSize = config.hasPath("cappedMaxSize") ? config.getInt("cappedMaxSize")
            : DEFAULT_CAPPED_MAX_SIZE;//from w  w  w.j  a  va 2 s  .c  o  m
    this.cappedMaxDocuments = config.hasPath("cappedMaxDocuments") ? config.getInt("cappedMaxDocuments")
            : DEFAULT_CAPPED_MAX_DOCUMENTS;
    this.client = new MongoClient(new MongoClientURI(this.connection));
    init();
}

From source file:org.apache.jackrabbit.oak.console.Console.java

License:Apache License

public static void main(String[] args) throws Exception {
    OptionParser parser = new OptionParser();
    OptionSpec<Integer> clusterId = parser.accepts("clusterId", "MongoMK clusterId").withRequiredArg()
            .ofType(Integer.class).defaultsTo(0);
    OptionSpec quiet = parser.accepts("quiet", "be less chatty");
    OptionSpec shell = parser.accepts("shell", "run the shell after executing files");
    OptionSpec readWrite = parser.accepts("read-write", "connect to repository in read-write mode");
    OptionSpec<String> fdsPathSpec = parser.accepts("fds-path", "Path to FDS store").withOptionalArg()
            .defaultsTo("");
    OptionSpec segmentTar = parser.accepts("segment-tar", "Use oak-segment-tar instead of oak-segment");
    OptionSpec help = parser.acceptsAll(asList("h", "?", "help"), "show help").forHelp();

    // RDB specific options
    OptionSpec<String> rdbjdbcuser = parser.accepts("rdbjdbcuser", "RDB JDBC user").withOptionalArg()
            .defaultsTo("");
    OptionSpec<String> rdbjdbcpasswd = parser.accepts("rdbjdbcpasswd", "RDB JDBC password").withOptionalArg()
            .defaultsTo("");

    OptionSpec<String> nonOption = parser.nonOptions("console {<path-to-repository> | <mongodb-uri>}");

    OptionSet options = parser.parse(args);
    List<String> nonOptions = nonOption.values(options);

    if (options.has(help)) {
        parser.printHelpOn(System.out);
        System.exit(0);/*  www  .j a va 2 s  .  co m*/
    }

    if (nonOptions.isEmpty()) {
        parser.printHelpOn(System.err);
        System.exit(1);
    }

    BlobStore blobStore = null;
    String fdsPath = fdsPathSpec.value(options);
    if (!"".equals(fdsPath)) {
        File fdsDir = new File(fdsPath);
        if (fdsDir.exists()) {
            FileDataStore fds = new FileDataStore();
            fds.setPath(fdsDir.getAbsolutePath());
            fds.init(null);

            blobStore = new DataStoreBlobStore(fds);
        }
    }

    boolean readOnly = !options.has(readWrite);

    NodeStoreFixture fixture;
    if (nonOptions.get(0).startsWith(MongoURI.MONGODB_PREFIX)) {
        MongoClientURI uri = new MongoClientURI(nonOptions.get(0));
        if (uri.getDatabase() == null) {
            System.err.println("Database missing in MongoDB URI: " + uri.getURI());
            System.exit(1);
        }
        MongoConnection mongo = new MongoConnection(uri.getURI());

        DocumentMK.Builder builder = new DocumentMK.Builder().setBlobStore(blobStore).setMongoDB(mongo.getDB())
                .setClusterId(clusterId.value(options));
        if (readOnly) {
            builder.setReadOnlyMode();
        }
        DocumentNodeStore store = builder.getNodeStore();
        fixture = new MongoFixture(store);
    } else if (nonOptions.get(0).startsWith("jdbc")) {
        DataSource ds = RDBDataSourceFactory.forJdbcUrl(nonOptions.get(0), rdbjdbcuser.value(options),
                rdbjdbcpasswd.value(options));
        DocumentMK.Builder builder = new DocumentMK.Builder().setBlobStore(blobStore).setRDBConnection(ds)
                .setClusterId(clusterId.value(options));
        if (readOnly) {
            builder.setReadOnlyMode();
        }
        DocumentNodeStore store = builder.getNodeStore();
        fixture = new MongoFixture(store);
    } else if (options.has(segmentTar)) {
        fixture = SegmentTarFixture.create(new File(nonOptions.get(0)), readOnly, blobStore);
    } else {
        FileStore.Builder fsBuilder = FileStore.builder(new File(nonOptions.get(0))).withMaxFileSize(256);
        if (blobStore != null) {
            fsBuilder.withBlobStore(blobStore);
        }
        FileStore store;
        if (readOnly) {
            store = fsBuilder.buildReadOnly();
        } else {
            store = fsBuilder.build();
        }
        fixture = new SegmentFixture(store);
    }

    List<String> scriptArgs = nonOptions.size() > 1 ? nonOptions.subList(1, nonOptions.size())
            : Collections.<String>emptyList();
    IO io = new IO();

    if (options.has(quiet)) {
        io.setVerbosity(IO.Verbosity.QUIET);
    }

    if (readOnly) {
        io.out.println("Repository connected in read-only mode. Use '--read-write' for write operations");
    }

    GroovyConsole console = new GroovyConsole(ConsoleSession.create(fixture.getStore()), new IO(), fixture);

    int code = 0;
    if (!scriptArgs.isEmpty()) {
        code = console.execute(scriptArgs);
    }

    if (scriptArgs.isEmpty() || options.has(shell)) {
        code = console.run();
    }

    System.exit(code);
}

From source file:org.apache.jackrabbit.oak.fixture.DocumentMongoFixture.java

License:Apache License

protected DB getDb(String suffix) throws UnknownHostException {
    String dbName = new MongoClientURI(uri).getDatabase();
    MongoConnection connection = new MongoConnection(uri);
    return connection.getDB(dbName + "-" + suffix);
}

From source file:org.apache.jackrabbit.oak.plugins.document.DocumentNodeStoreService.java

License:Apache License

private void registerNodeStore() throws IOException {
    String uri = PropertiesUtil.toString(prop(PROP_URI, FWK_PROP_URI), DEFAULT_URI);
    String db = PropertiesUtil.toString(prop(PROP_DB, FWK_PROP_DB), DEFAULT_DB);

    int cacheSize = toInteger(prop(PROP_CACHE), DEFAULT_CACHE);
    int nodeCachePercentage = toInteger(prop(PROP_NODE_CACHE_PERCENTAGE), DEFAULT_NODE_CACHE_PERCENTAGE);
    int prevDocCachePercentage = toInteger(prop(PROP_PREV_DOC_CACHE_PERCENTAGE), DEFAULT_NODE_CACHE_PERCENTAGE);
    int childrenCachePercentage = toInteger(prop(PROP_CHILDREN_CACHE_PERCENTAGE),
            DEFAULT_CHILDREN_CACHE_PERCENTAGE);
    int docChildrenCachePercentage = toInteger(prop(PROP_DOC_CHILDREN_CACHE_PERCENTAGE),
            DEFAULT_DOC_CHILDREN_CACHE_PERCENTAGE);
    int diffCachePercentage = toInteger(prop(PROP_DIFF_CACHE_PERCENTAGE), DEFAULT_DIFF_CACHE_PERCENTAGE);
    int blobCacheSize = toInteger(prop(PROP_BLOB_CACHE_SIZE), DEFAULT_BLOB_CACHE_SIZE);
    String persistentCache = PropertiesUtil.toString(prop(PROP_PERSISTENT_CACHE), DEFAULT_PERSISTENT_CACHE);
    int cacheSegmentCount = toInteger(prop(PROP_CACHE_SEGMENT_COUNT), DEFAULT_CACHE_SEGMENT_COUNT);
    int cacheStackMoveDistance = toInteger(prop(PROP_CACHE_STACK_MOVE_DISTANCE),
            DEFAULT_CACHE_STACK_MOVE_DISTANCE);
    DocumentMK.Builder mkBuilder = new DocumentMK.Builder().setStatisticsProvider(statisticsProvider)
            .memoryCacheSize(cacheSize * MB)
            .memoryCacheDistribution(nodeCachePercentage, prevDocCachePercentage, childrenCachePercentage,
                    docChildrenCachePercentage, diffCachePercentage)
            .setCacheSegmentCount(cacheSegmentCount).setCacheStackMoveDistance(cacheStackMoveDistance)
            .setLeaseCheck(true /* OAK-2739: enabled by default */)
            .setLeaseFailureHandler(new LeaseFailureHandler() {

                @Override/*from  w  w  w.  ja  v  a  2  s.  c  o m*/
                public void handleLeaseFailure() {
                    try {
                        // plan A: try stopping oak-core
                        log.error("handleLeaseFailure: stopping oak-core...");
                        Bundle bundle = context.getBundleContext().getBundle();
                        bundle.stop();
                        log.error("handleLeaseFailure: stopped oak-core.");
                        // plan A worked, perfect!
                    } catch (BundleException e) {
                        log.error("handleLeaseFailure: exception while stopping oak-core: " + e, e);
                        // plan B: stop only DocumentNodeStoreService (to stop the background threads)
                        log.error("handleLeaseFailure: stopping DocumentNodeStoreService...");
                        context.disableComponent(DocumentNodeStoreService.class.getName());
                        log.error("handleLeaseFailure: stopped DocumentNodeStoreService");
                        // plan B succeeded.
                    }
                }
            });

    if (persistentCache != null && persistentCache.length() > 0) {
        mkBuilder.setPersistentCache(persistentCache);
    }

    boolean wrappingCustomBlobStore = customBlobStore && blobStore instanceof BlobStoreWrapper;

    //Set blobstore before setting the DB
    if (customBlobStore && !wrappingCustomBlobStore) {
        checkNotNull(blobStore,
                "Use of custom BlobStore enabled via  [%s] but blobStore reference not " + "initialized",
                CUSTOM_BLOB_STORE);
        mkBuilder.setBlobStore(blobStore);
    }

    if (documentStoreType == DocumentStoreType.RDB) {
        checkNotNull(dataSource, "DataStore type set [%s] but DataSource reference not initialized",
                PROP_DS_TYPE);
        if (!customBlobStore) {
            checkNotNull(blobDataSource, "DataStore type set [%s] but BlobDataSource reference not initialized",
                    PROP_DS_TYPE);
            mkBuilder.setRDBConnection(dataSource, blobDataSource);
            log.info("Connected to datasources {} {}", dataSource, blobDataSource);
        } else {
            if (blobDataSource != null && blobDataSource != dataSource) {
                log.info("Ignoring blobDataSource {} as custom blob store takes precedence.", blobDataSource);
            }
            mkBuilder.setRDBConnection(dataSource);
            log.info("Connected to datasource {}", dataSource);
        }
    } else {
        MongoClientURI mongoURI = new MongoClientURI(uri);

        if (log.isInfoEnabled()) {
            // Take care around not logging the uri directly as it
            // might contain passwords
            log.info(
                    "Starting DocumentNodeStore with host={}, db={}, cache size (MB)={}, persistentCache={}, "
                            + "blobCacheSize (MB)={}, maxReplicationLagInSecs={}",
                    mongoURI.getHosts(), db, cacheSize, persistentCache, blobCacheSize,
                    maxReplicationLagInSecs);
            log.info("Mongo Connection details {}", MongoConnection.toString(mongoURI.getOptions()));
        }

        mkBuilder.setMaxReplicationLag(maxReplicationLagInSecs, TimeUnit.SECONDS);
        mkBuilder.setMongoDB(uri, db, blobCacheSize);

        log.info("Connected to database '{}'", db);
    }

    if (!customBlobStore) {
        defaultBlobStore = mkBuilder.getBlobStore();
        log.info("Registering the BlobStore with ServiceRegistry");
        blobStoreReg = context.getBundleContext().registerService(BlobStore.class.getName(), defaultBlobStore,
                null);
    }

    //Set wrapping blob store after setting the DB
    if (wrappingCustomBlobStore) {
        ((BlobStoreWrapper) blobStore).setBlobStore(mkBuilder.getBlobStore());
        mkBuilder.setBlobStore(blobStore);
    }

    mkBuilder.setExecutor(executor);
    mk = mkBuilder.open();

    // ensure a clusterId is initialized 
    // and expose it as 'oak.clusterid' repository descriptor
    GenericDescriptors clusterIdDesc = new GenericDescriptors();
    clusterIdDesc.put(ClusterRepositoryInfo.OAK_CLUSTERID_REPOSITORY_DESCRIPTOR_KEY,
            new SimpleValueFactory().createValue(ClusterRepositoryInfo.getOrCreateId(mk.getNodeStore())), true,
            false);
    whiteboard.register(Descriptors.class, clusterIdDesc, Collections.emptyMap());

    // If a shared data store register the repo id in the data store
    if (SharedDataStoreUtils.isShared(blobStore)) {
        try {
            String repoId = ClusterRepositoryInfo.getOrCreateId(mk.getNodeStore());
            ((SharedDataStore) blobStore).addMetadataRecord(new ByteArrayInputStream(new byte[0]),
                    SharedDataStoreUtils.SharedStoreRecordType.REPOSITORY.getNameFromId(repoId));
        } catch (Exception e) {
            throw new IOException("Could not register a unique repositoryId", e);
        }
    }

    registerJMXBeans(mk.getNodeStore(), mkBuilder);
    registerLastRevRecoveryJob(mk.getNodeStore());
    registerJournalGC(mk.getNodeStore());

    NodeStore store;
    documentNodeStore = mk.getNodeStore();
    store = documentNodeStore;
    observerTracker = new ObserverTracker(documentNodeStore);

    observerTracker.start(context.getBundleContext());

    DocumentStore ds = mk.getDocumentStore();

    // OAK-2682: time difference detection applied at startup with a default
    // max time diff of 2000 millis (2sec)
    final long maxDiff = Long.parseLong(System.getProperty("oak.documentMK.maxServerTimeDiffMillis", "2000"));
    try {
        if (maxDiff >= 0) {
            final long timeDiff = ds.determineServerTimeDifferenceMillis();
            log.info("registerNodeStore: server time difference: {}ms (max allowed: {}ms)", timeDiff, maxDiff);
            if (Math.abs(timeDiff) > maxDiff) {
                throw new AssertionError("Server clock seems off (" + timeDiff
                        + "ms) by more than configured amount (" + maxDiff + "ms)");
            }
        }
    } catch (RuntimeException e) { // no checked exception
        // in case of a RuntimeException, just log but continue
        log.warn("registerNodeStore: got RuntimeException while trying to determine time difference to server: "
                + e, e);
    }

    Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put(Constants.SERVICE_PID, DocumentNodeStore.class.getName());
    props.put(DESCRIPTION, getMetadata(ds));
    // OAK-2844: in order to allow DocumentDiscoveryLiteService to directly
    // require a service DocumentNodeStore (instead of having to do an 'instanceof')
    // the registration is now done for both NodeStore and DocumentNodeStore here.
    nodeStoreReg = context.getBundleContext().registerService(new String[] { NodeStore.class.getName(),
            DocumentNodeStore.class.getName(), Clusterable.class.getName() }, store, props);
}

From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore.java

License:Apache License

@Override
public void setReadWriteMode(String readWriteMode) {
    if (readWriteMode == null || readWriteMode.equals(lastReadWriteMode)) {
        return;/*from w w w.j a  va 2 s .com*/
    }
    lastReadWriteMode = readWriteMode;
    try {
        String rwModeUri = readWriteMode;
        if (!readWriteMode.startsWith("mongodb://")) {
            rwModeUri = String.format("mongodb://localhost/?%s", readWriteMode);
        }
        MongoClientURI uri = new MongoClientURI(rwModeUri);
        ReadPreference readPref = uri.getOptions().getReadPreference();

        if (!readPref.equals(nodes.getReadPreference())) {
            nodes.setReadPreference(readPref);
            LOG.info("Using ReadPreference {} ", readPref);
        }

        WriteConcern writeConcern = uri.getOptions().getWriteConcern();
        if (!writeConcern.equals(nodes.getWriteConcern())) {
            nodes.setWriteConcern(writeConcern);
            LOG.info("Using WriteConcern " + writeConcern);
        }
    } catch (Exception e) {
        LOG.error("Error setting readWriteMode " + readWriteMode, e);
    }
}

From source file:org.apache.jackrabbit.oak.plugins.document.mongo.replica.NodeCollectionProvider.java

License:Apache License

@SuppressWarnings("deprecation")
public DBCollection get(String hostname) throws UnknownHostException {
    if (collections.containsKey(hostname)) {
        return collections.get(hostname);
    }/*from  www  . ja va  2s  .  com*/

    MongoClient client;
    if (originalMongoUri == null) {
        MongoClientURI uri = new MongoClientURI("mongodb://" + hostname);
        client = new MongoClient(uri);
    } else {
        client = prepareClientForHostname(hostname);
    }

    DB db = client.getDB(dbName);
    db.getMongo().slaveOk();
    DBCollection collection = db.getCollection(Collection.NODES.toString());
    collections.put(hostname, collection);
    return collection;
}

From source file:org.apache.jackrabbit.oak.plugins.document.mongo.replica.NodeCollectionProvider.java

License:Apache License

private MongoClient prepareClientForHostname(String hostname) throws UnknownHostException {
    ServerAddress address;//from  ww  w . jav a  2s .  c  o m
    if (hostname.contains(":")) {
        String[] hostSplit = hostname.split(":");
        if (hostSplit.length != 2) {
            throw new IllegalArgumentException("Not a valid hostname: " + hostname);
        }
        address = new ServerAddress(hostSplit[0], Integer.parseInt(hostSplit[1]));
    } else {
        address = new ServerAddress(hostname);
    }

    MongoClientURI originalUri = new MongoClientURI(originalMongoUri);
    List<MongoCredential> credentialList = new ArrayList<MongoCredential>(1);
    if (originalUri.getCredentials() != null) {
        credentialList.add(originalUri.getCredentials());
    }
    return new MongoClient(address, credentialList, originalUri.getOptions());
}

From source file:org.apache.jackrabbit.oak.plugins.tika.TextExtractorMain.java

License:Apache License

private static NodeStore bootStrapNodeStore(String src, boolean segmentTar, BlobStore blobStore, Closer closer)
        throws IOException {
    if (src.startsWith(MongoURI.MONGODB_PREFIX)) {
        MongoClientURI uri = new MongoClientURI(src);
        if (uri.getDatabase() == null) {
            System.err.println("Database missing in MongoDB URI: " + uri.getURI());
            System.exit(1);/*from  ww w  .ja  v  a  2 s .co m*/
        }
        MongoConnection mongo = new MongoConnection(uri.getURI());
        closer.register(asCloseable(mongo));
        DocumentNodeStore store = new DocumentMK.Builder().setBlobStore(blobStore).setMongoDB(mongo.getDB())
                .getNodeStore();
        closer.register(asCloseable(store));
        return store;
    }

    if (segmentTar) {
        return SegmentTarUtils.bootstrap(src, blobStore, closer);
    }

    return SegmentUtils.bootstrap(src, blobStore, closer);
}

From source file:org.apache.jackrabbit.oak.run.CheckpointsCommand.java

License:Apache License

@Override
public void execute(String... args) throws Exception {
    OptionParser parser = new OptionParser();
    OptionSpec segmentTar = parser.accepts("segment-tar", "Use oak-segment-tar instead of oak-segment");
    OptionSet options = parser.parse(args);

    if (options.nonOptionArguments().isEmpty()) {
        System.out.println(/*from   w  w  w  .  j  av a2s  . c om*/
                "usage: checkpoints {<path>|<mongo-uri>} [list|rm-all|rm-unreferenced|rm <checkpoint>] [--segment-tar]");
        System.exit(1);
    }

    boolean success = false;
    Checkpoints cps;
    Closer closer = Closer.create();
    try {
        String op = "list";
        if (options.nonOptionArguments().size() >= 2) {
            op = options.nonOptionArguments().get(1).toString();
            if (!"list".equals(op) && !"rm-all".equals(op) && !"rm-unreferenced".equals(op)
                    && !"rm".equals(op)) {
                failWith("Unknown command.");
            }
        }

        String connection = options.nonOptionArguments().get(0).toString();
        if (connection.startsWith(MongoURI.MONGODB_PREFIX)) {
            MongoClientURI uri = new MongoClientURI(connection);
            MongoClient client = new MongoClient(uri);
            final DocumentNodeStore store = new DocumentMK.Builder().setMongoDB(client.getDB(uri.getDatabase()))
                    .getNodeStore();
            closer.register(Utils.asCloseable(store));
            cps = Checkpoints.onDocumentMK(store);
        } else if (options.has(segmentTar)) {
            cps = Checkpoints.onSegmentTar(new File(connection), closer);
        } else {
            cps = Checkpoints.onSegment(new File(connection), closer);
        }

        System.out.println("Checkpoints " + connection);
        if ("list".equals(op)) {
            int cnt = 0;
            for (Checkpoints.CP cp : cps.list()) {
                System.out.printf("- %s created %s expires %s%n", cp.id, new Timestamp(cp.created),
                        new Timestamp(cp.expires));
                cnt++;
            }
            System.out.println("Found " + cnt + " checkpoints");
        } else if ("rm-all".equals(op)) {
            long time = System.currentTimeMillis();
            long cnt = cps.removeAll();
            time = System.currentTimeMillis() - time;
            if (cnt != -1) {
                System.out.println("Removed " + cnt + " checkpoints in " + time + "ms.");
            } else {
                failWith("Failed to remove all checkpoints.");
            }
        } else if ("rm-unreferenced".equals(op)) {
            long time = System.currentTimeMillis();
            long cnt = cps.removeUnreferenced();
            time = System.currentTimeMillis() - time;
            if (cnt != -1) {
                System.out.println("Removed " + cnt + " checkpoints in " + time + "ms.");
            } else {
                failWith("Failed to remove unreferenced checkpoints.");
            }
        } else if ("rm".equals(op)) {
            if (options.nonOptionArguments().size() < 3) {
                failWith("Missing checkpoint id");
            } else {
                String cp = options.nonOptionArguments().get(2).toString();
                long time = System.currentTimeMillis();
                int cnt = cps.remove(cp);
                time = System.currentTimeMillis() - time;
                if (cnt != 0) {
                    if (cnt == 1) {
                        System.out.println("Removed checkpoint " + cp + " in " + time + "ms.");
                    } else {
                        failWith("Failed to remove checkpoint " + cp);
                    }
                } else {
                    failWith("Checkpoint '" + cp + "' not found.");
                }
            }
        }
        success = true;
    } catch (Throwable t) {
        System.err.println(t.getMessage());
    } finally {
        closer.close();
    }
    if (!success) {
        System.exit(1);
    }
}