ContentRepository handle

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

ContentRepository handle

techie2k
Without using sling/felix, is there a way to get ContentRepository object  for Oak(using MongoDB as document store) in spring based web application?

Currently using ceateRepository, everytime I cannot call following code on server(Tomcat) restart:

Jcr jcr = new Jcr(new Oak(documentNodeStore)).with(getSecurityProvider());
Repository repository = jcr.createRepository();


Is there any factory class available, so that I pass mongodb connection params and get repository object?

Reply | Threaded
Open this post in threaded view
|

Re: ContentRepository handle

Torgeir Veimo-3
If you want a pure java config, then you can create a
RepositoryConfiguration class and use a bean method like this one;

    @Bean
    public Repository getRepository(String oakMongoRepositoryUri) throws
ContentException {

        try {
            LOG.info("setting up oak with mongodb at uri: {}",
oakMongoRepositoryUri);
            MongoClientURI uri = new MongoClientURI(oakMongoRepositoryUri);
            mongoClient = new MongoClient(uri);
            DB db = mongoClient.getDB(oakMongoRepositoryDB);
            documentNodeStore = new
DocumentMK.Builder().setMongoDB(db).getNodeStore();
            NodeStore nodeStore = documentNodeStore;

            LuceneIndexProvider luceneIndexProvider = new
LuceneIndexProvider();
            oakRepository = new Jcr(new Oak(nodeStore))
                    .with(new LuceneLocalInitialContent())
                    .with((QueryIndexProvider) luceneIndexProvider)
                    .with((Observer) luceneIndexProvider)
                    .with(new LuceneIndexEditorProvider())
                    .withAsyncIndexing("async", 5)
                    .withFastQueryResultSize(true)
                    .withAtomicCounter()
                    .with(AggregateIndexProvider.wrap(new
LuceneIndexProvider()))
                    .createRepository();
            return oakRepository;

        } catch (Exception e) {
            LOG.error("unable to configure OAK repository; ", e);
            throw new ContentException("unable to configure OAK repository;
", e);
        }
    }

Where LuceneLocalInitialContent is a separate class that configures default
index.


On 6 April 2017 at 19:04, techie2k <[hidden email]> wrote:

> Without using sling/felix, is there a way to get ContentRepository object
> for Oak(using MongoDB as document store) in spring based web application?
>
> Currently using ceateRepository, everytime I *cannot *call following code
> on
> server(Tomcat) restart:
>
> *Jcr jcr = new Jcr(new Oak(documentNodeStore)).with(
> getSecurityProvider());
> Repository repository = jcr.createRepository();*
>
> Is there any factory class available, so that I pass mongodb connection
> params and get repository object?
>
>
>
>
>
> --
> View this message in context: http://jackrabbit.510166.n4.
> nabble.com/ContentRepository-handle-tp4666696.html
> Sent from the Jackrabbit - Users mailing list archive at Nabble.com.
>



--
-Tor
Reply | Threaded
Open this post in threaded view
|

Re: ContentRepository handle

techie2k
Thanks for the update.

I got answer partially. Every time I cannot call, createRepository method to get Repository.

First time, create repository is fine, If I restart MongoDB server or Tomcat Server, again calling createRepository is  incorrect, I suppose. Already repository is all set, why should I call to createRepository to access repository?
Reply | Threaded
Open this post in threaded view
|

Re: ContentRepository handle

Torgeir Veimo-3
What we do is to have a service class RepositoryService, which has
an @Autowired Repository oakRepository which will be created with that bean
method in the RepositoryConfiguration class. The RepositoryService class
then has a method which you use when you want to access the repository;

public Session getSession() throws RepositoryException {
  return oakRepository.login(new SimpleCredentials(ADMIN_USER,
ADMIN_USER.toCharArray()));
}

Then make sure you log out the session after use like

 public void someMethod() throws ContentException {
        Session session = null;
        try {
            session = repositoryService.getSession();

            Node node = session.getNodeByIdentifier(id);
            ...

        } catch (RepositoryException e) {
            LOG.error("unable to xxx; ", e);
            throw new IOException("unable to xxx; ", e);
        } finally {
            if (session != null) {
                session.logout();
            }
        }
    }

Also, your RepositoryConfiguration class should implement DisposableBean
and have a method like

@Override
    public void destroy() {
        LOG.info("closing repository");
        if (oakRepository != null) {
            ((RepositoryImpl)oakRepository).shutdown();
            oakRepository = null;
        }
        if (documentNodeStore != null) {
            documentNodeStore.dispose();
        }
        if (mongoClient != null) {
            mongoClient.close();
        }
    }

so that it closes down the mongodb connection.


On 6 April 2017 at 20:57, techie2k <[hidden email]> wrote:

> Thanks for the update.
>
> I got answer partially. Every time I cannot call, createRepository method
> to
> get Repository.
>
> First time, create repository is fine, If I restart MongoDB server or
> Tomcat
> Server, again calling createRepository is  incorrect, I suppose. Already
> repository is all set, why should I call to createRepository to access
> repository?
>
>
>
> --
> View this message in context: http://jackrabbit.510166.n4.
> nabble.com/ContentRepository-handle-tp4666696p4666702.html
> Sent from the Jackrabbit - Users mailing list archive at Nabble.com.
>



--
-Tor
Reply | Threaded
Open this post in threaded view
|

Re: ContentRepository handle

chetan mehrotra
Also have a look at standalone example [1] which demonstrates using
Oak in a Spring Boot application

Chetan Mehrotra
[1] https://github.com/apache/jackrabbit-oak/tree/trunk/oak-examples/standalone