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.jackrabbit.oak.run.DataStoreCheckCommand.java

License:Apache License

@Override
public void execute(String... args) throws Exception {
    OptionParser parser = new OptionParser();
    parser.allowsUnrecognizedOptions();/*from   w  w w. jav  a2s  . c o m*/

    String helpStr = "datastorecheck [--id] [--ref] [--consistency] [--store <path>|<mongo_uri>] "
            + "[--s3ds <s3ds_config>|--fds <fds_config>] [--dump <path>]";

    Closer closer = Closer.create();
    try {
        // Options for operations requested
        OptionSpecBuilder idOp = parser.accepts("id", "Get ids");
        OptionSpecBuilder refOp = parser.accepts("ref", "Get references");
        OptionSpecBuilder consistencyOp = parser.accepts("consistency", "Check consistency");

        // Node Store - needed for --ref, --consistency
        ArgumentAcceptingOptionSpec<String> store = parser.accepts("store", "Node Store")
                .requiredIf(refOp, consistencyOp).withRequiredArg().ofType(String.class);
        // Optional argument to specify the dump path
        ArgumentAcceptingOptionSpec<String> dump = parser.accepts("dump", "Dump Path").withRequiredArg()
                .ofType(String.class);
        OptionSpec segmentTar = parser.accepts("segment-tar", "Use oak-segment-tar instead of oak-segment");

        OptionSpec<?> help = parser.acceptsAll(asList("h", "?", "help"), "show help").forHelp();

        // Required rules (any one of --id, --ref, --consistency)
        idOp.requiredUnless(refOp, consistencyOp);
        refOp.requiredUnless(idOp, consistencyOp);
        consistencyOp.requiredUnless(idOp, refOp);

        OptionSet options = null;
        try {
            options = parser.parse(args);
        } catch (Exception e) {
            System.err.println(e);
            parser.printHelpOn(System.err);
            return;
        }

        if (options.has(help)) {
            parser.printHelpOn(System.out);
            return;
        }

        String dumpPath = JAVA_IO_TMPDIR.value();
        if (options.has(dump)) {
            dumpPath = options.valueOf(dump);
        }

        GarbageCollectableBlobStore blobStore = null;
        BlobReferenceRetriever marker = null;
        if (options.has(store)) {
            String source = options.valueOf(store);
            if (source.startsWith(MongoURI.MONGODB_PREFIX)) {
                MongoClientURI uri = new MongoClientURI(source);
                MongoClient client = new MongoClient(uri);
                DocumentNodeStore nodeStore = new DocumentMK.Builder()
                        .setMongoDB(client.getDB(uri.getDatabase())).getNodeStore();
                closer.register(Utils.asCloseable(nodeStore));
                blobStore = (GarbageCollectableBlobStore) nodeStore.getBlobStore();
                marker = new DocumentBlobReferenceRetriever(nodeStore);
            } else if (options.has(segmentTar)) {
                marker = SegmentTarUtils.newBlobReferenceRetriever(source, closer);
            } else {
                FileStore fileStore = openFileStore(source);
                closer.register(Utils.asCloseable(fileStore));
                marker = new SegmentBlobReferenceRetriever(fileStore.getTracker());
            }
        }

        // Initialize S3/FileDataStore if configured
        GarbageCollectableBlobStore dataStore = Utils.bootstrapDataStore(args, closer);
        if (dataStore != null) {
            blobStore = dataStore;
        }

        // blob store still not initialized means configuration not supported
        if (blobStore == null) {
            System.err.println("Operation not defined for SegmentNodeStore without external datastore");
            parser.printHelpOn(System.err);
            return;
        }

        FileRegister register = new FileRegister(options);
        closer.register(register);

        if (options.has(idOp) || options.has(consistencyOp)) {
            retrieveBlobIds(blobStore, register.createFile(idOp, dumpPath));
        }

        if (options.has(refOp) || options.has(consistencyOp)) {
            retrieveBlobReferences(blobStore, marker, register.createFile(refOp, dumpPath));
        }

        if (options.has(consistencyOp)) {
            checkConsistency(register.get(idOp), register.get(refOp),
                    register.createFile(consistencyOp, dumpPath));
        }
    } catch (Throwable t) {
        t.printStackTrace();
    } finally {
        closer.close();
    }
}

From source file:org.apache.jackrabbit.oak.run.ResetClusterIdCommand.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("usage: resetclusterid {<path>|<mongo-uri>}");
        System.exit(1);/* ww w  .  ja  v  a2  s. co  m*/
    }

    String source = options.nonOptionArguments().get(0).toString();

    Closer closer = Closer.create();
    try {
        NodeStore store;
        if (args[0].startsWith(MongoURI.MONGODB_PREFIX)) {
            MongoClientURI uri = new MongoClientURI(source);
            MongoClient client = new MongoClient(uri);
            final DocumentNodeStore dns = new DocumentMK.Builder().setMongoDB(client.getDB(uri.getDatabase()))
                    .getNodeStore();
            closer.register(Utils.asCloseable(dns));
            store = dns;
        } else if (options.has(segmentTar)) {
            store = SegmentTarUtils.bootstrapNodeStore(source, closer);
        } else {
            FileStore fs = openFileStore(source);
            closer.register(Utils.asCloseable(fs));
            store = SegmentNodeStore.builder(fs).build();
        }

        deleteClusterId(store);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }

}

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

License:Apache License

public static NodeStore bootstrapNodeStore(String[] args, Closer closer, String h) throws IOException {
    //TODO add support for other NodeStore flags
    OptionParser parser = new OptionParser();
    OptionSpec<Integer> clusterId = parser.accepts("clusterId", "MongoMK clusterId").withRequiredArg()
            .ofType(Integer.class).defaultsTo(0);
    OptionSpec segmentTar = parser.accepts("segment-tar", "Use oak-segment-tar instead of oak-segment");
    OptionSpec<?> help = parser.acceptsAll(asList("h", "?", "help"), "show help").forHelp();
    OptionSpec<String> nonOption = parser.nonOptions(h);

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

    if (options.has(help)) {
        parser.printHelpOn(System.out);
        System.exit(0);/*from w w  w.  j a v  a  2  s.c o  m*/
    }

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

    String src = nonOptions.get(0);
    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);
        }
        MongoConnection mongo = new MongoConnection(uri.getURI());
        closer.register(asCloseable(mongo));
        DocumentNodeStore store = new DocumentMK.Builder().setMongoDB(mongo.getDB()).setLeaseCheck(false)
                .setClusterId(clusterId.value(options)).getNodeStore();
        closer.register(asCloseable(store));
        return store;
    }

    if (options.has(segmentTar)) {
        return SegmentTarUtils.bootstrapNodeStore(src, closer);
    }

    return SegmentUtils.bootstrapNodeStore(src, closer);
}

From source file:org.apache.jackrabbit.oak.upgrade.cli.container.MongoNodeStoreContainer.java

License:Apache License

private static boolean testMongoAvailability() {
    Mongo mongo = null;/*from   w  w w.j a v a 2s.  c om*/
    try {
        MongoClientURI uri = new MongoClientURI(MONGO_URI + "?connectTimeoutMS=3000");
        mongo = new MongoClient(uri);
        mongo.getDatabaseNames();
        return true;
    } catch (Exception e) {
        return false;
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }
}

From source file:org.apache.jackrabbit.oak.upgrade.cli.container.MongoNodeStoreContainer.java

License:Apache License

@Override
public void clean() throws IOException {
    MongoClientURI uri = new MongoClientURI(mongoUri);
    MongoClient client = new MongoClient(uri);
    client.dropDatabase(uri.getDatabase());
    blob.clean();//from ww w  .  j  a  v  a  2  s.  c  o  m
}

From source file:org.apache.jackrabbit.oak.upgrade.cli.node.MongoFactory.java

License:Apache License

public MongoFactory(String repoDesc, int cacheSize) {
    this.uri = new MongoClientURI(repoDesc);
    this.cacheSize = cacheSize;
}

From source file:org.apache.karaf.decanter.appender.mongodb.MongoDbAppender.java

License:Apache License

@Activate
public void activate(ComponentContext componentContext) {
    Dictionary<String, Object> config = componentContext.getProperties();

    String uri = getValue(config, "uri", "mongodb://localhost");
    String database = getValue(config, "database", "decanter");
    String collection = getValue(config, "collection", "decanter");

    mongoClient = new MongoClient(new MongoClientURI(uri));
    mongoDatabase = mongoClient.getDatabase(database);
    mongoCollection = mongoDatabase.getCollection(collection);
}

From source file:org.apache.nifi.mongodb.AbstractMongoDBControllerService.java

License:Apache License

protected final void createClient(ConfigurationContext context) throws IOException {
    if (mongoClient != null) {
        closeClient();/*www.  j  a  v  a2  s .  com*/
    }

    getLogger().info("Creating MongoClient");

    // Set up the client for secure (SSL/TLS communications) if configured to do so
    final SSLContextService sslService = context.getProperty(SSL_CONTEXT_SERVICE)
            .asControllerService(SSLContextService.class);
    final String rawClientAuth = context.getProperty(CLIENT_AUTH).getValue();
    final SSLContext sslContext;

    if (sslService != null) {
        final SSLContextService.ClientAuth clientAuth;
        if (StringUtils.isBlank(rawClientAuth)) {
            clientAuth = SSLContextService.ClientAuth.REQUIRED;
        } else {
            try {
                clientAuth = SSLContextService.ClientAuth.valueOf(rawClientAuth);
            } catch (final IllegalArgumentException iae) {
                throw new ProviderCreationException(
                        String.format("Unrecognized client auth '%s'. Possible values are [%s]", rawClientAuth,
                                StringUtils.join(SslContextFactory.ClientAuth.values(), ", ")));
            }
        }
        sslContext = sslService.createSSLContext(clientAuth);
    } else {
        sslContext = null;
    }

    try {
        if (sslContext == null) {
            mongoClient = new MongoClient(new MongoClientURI(getURI(context)));
        } else {
            mongoClient = new MongoClient(new MongoClientURI(getURI(context), getClientOptions(sslContext)));
        }
    } catch (Exception e) {
        getLogger().error("Failed to schedule {} due to {}", new Object[] { this.getClass().getName(), e }, e);
        throw e;
    }
}

From source file:org.apache.nifi.processors.mongodb.AbstractMongoProcessor.java

License:Apache License

@OnScheduled
public final void createClient(ProcessContext context) throws IOException {
    if (mongoClient != null) {
        closeClient();//  www  . ja  v a2  s. c o m
    }

    getLogger().info("Creating MongoClient");

    // Set up the client for secure (SSL/TLS communications) if configured to do so
    final SSLContextService sslService = context.getProperty(SSL_CONTEXT_SERVICE)
            .asControllerService(SSLContextService.class);
    final String rawClientAuth = context.getProperty(CLIENT_AUTH).getValue();
    final SSLContext sslContext;

    if (sslService != null) {
        final SSLContextService.ClientAuth clientAuth;
        if (StringUtils.isBlank(rawClientAuth)) {
            clientAuth = SSLContextService.ClientAuth.REQUIRED;
        } else {
            try {
                clientAuth = SSLContextService.ClientAuth.valueOf(rawClientAuth);
            } catch (final IllegalArgumentException iae) {
                throw new ProviderCreationException(
                        String.format("Unrecognized client auth '%s'. Possible values are [%s]", rawClientAuth,
                                StringUtils.join(SslContextFactory.ClientAuth.values(), ", ")));
            }
        }
        sslContext = sslService.createSSLContext(clientAuth);
    } else {
        sslContext = null;
    }

    try {
        if (sslContext == null) {
            mongoClient = new MongoClient(new MongoClientURI(getURI(context)));
        } else {
            mongoClient = new MongoClient(new MongoClientURI(getURI(context), getClientOptions(sslContext)));
        }
    } catch (Exception e) {
        getLogger().error("Failed to schedule {} due to {}", new Object[] { this.getClass().getName(), e }, e);
        throw e;
    }
}

From source file:org.apache.nifi.processors.mongodb.MongoWriteTestBase.java

License:Apache License

public void setup(Class processor) {
    DATABASE_NAME = processor.getSimpleName().toLowerCase();
    runner = TestRunners.newTestRunner(processor);
    runner.setVariable("uri", MONGO_URI);
    runner.setVariable("db", DATABASE_NAME);
    runner.setVariable("collection", COLLECTION_NAME);
    runner.setProperty(AbstractMongoProcessor.URI, "${uri}");
    runner.setProperty(AbstractMongoProcessor.DATABASE_NAME, "${db}");
    runner.setProperty(AbstractMongoProcessor.COLLECTION_NAME, "${collection}");

    mongoClient = new MongoClient(new MongoClientURI(MONGO_URI));

    collection = mongoClient.getDatabase(DATABASE_NAME).getCollection(COLLECTION_NAME);
}