Apache Jackrabbit 2 / JCR2 Nullpointer Exception on shutdown

Published: 04/14/10 07:30 PM


I just fiddled around with Apaches Jackrabbit 2 and found the following error and solution noteworthy.
Testing around with the docs on Jackrabbit, I configured and implemented a pretty simple Prototype that embeds a Jackrabbit Repository.
Java Code:

public class App
{
    public static void main( String[] args )
    {
        BasicConfigurator.configure();
        try {
            String xml = "repository/configuration.xml";
            String dir = "repository";
            RepositoryConfig config = RepositoryConfig.create(xml, dir);
            Repository repository = RepositoryImpl.create(config);
            try {
                Thread.sleep(10000);
            } catch (InterruptedException ex) {
                Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
            }
            ((RepositoryImpl) repository).shutdown();
        }catch (ConfigurationException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RepositoryException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

The Repository configuration:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Repository
          PUBLIC "-//The Apache Software Foundation//DTD Jackrabbit 2.0//EN"
          "http://jackrabbit.apache.org/dtd/repository-2.0.dtd">
<Repository>
	<FileSystem class="org.apache.jackrabbit.core.fs.local.LocalFileSystem" >
		<param name="path" value="${rep.home}/repository" />
	</FileSystem>
	<Security appName="jackrabbit-nodez-poc" >
		<LoginManager class="org.apache.jackrabbit.core.security.SimpleAccessManager" />
		<LoginModule class="org.apache.jackrabbit.core.security.SimpleLoginModule" />
	</Security>
	<Workspaces rootPath="${rep.home}/workspaces" defaultWorkspace="default" />
	<Workspace name="${wsp.name}">
		<FileSystem class="org.apache.jackrabbit.core.fs.local.LocalFileSystem" >
			<param name="path" value="${wsp.home}" />
		</FileSystem>
		<PersistenceManager class="org.apache.jackrabbit.core.persistence.bundle.BundleFsPersistenceManager"/>
	</Workspace>
	<Versioning rootPath="${rep.home}/version">
		<FileSystem class="org.apache.jackrabbit.core.fs.local.LocalFileSystem" >
			<param name="path" value="${rep.home}/version" />
		</FileSystem>
		<PersistenceManager class="org.apache.jackrabbit.core.persistence.bundle.BundleFsPersistenceManager"/>
	</Versioning>
</Repository>

It turned out that an NPE was thrown when I tried to shut down the Repository:

20223 [main] INFO org.apache.jackrabbit.core.RepositoryImpl  - Shutting down repository...
20224 [main] INFO org.apache.jackrabbit.core.RepositoryImpl  - shutting down workspace 'default'...
20224 [main] INFO org.apache.jackrabbit.core.observation.ObservationDispatcher  - Notification of EventListeners stopped.
Exception in thread "main" java.lang.NullPointerException: dispatcher
        at org.apache.jackrabbit.core.observation.ObservationManagerImpl.<init>(ObservationManagerImpl.java:97)
        at org.apache.jackrabbit.core.WorkspaceImpl.getObservationManager(WorkspaceImpl.java:748)
        at org.apache.jackrabbit.core.SessionImpl.removeRegisteredEventListeners(SessionImpl.java:1189)
        at org.apache.jackrabbit.core.SessionImpl.logout(SessionImpl.java:1217)
        at org.apache.jackrabbit.core.RepositoryImpl$WorkspaceInfo.doDispose(RepositoryImpl.java:2183)
        at org.apache.jackrabbit.core.RepositoryImpl$WorkspaceInfo.dispose(RepositoryImpl.java:2138)
        at org.apache.jackrabbit.core.RepositoryImpl.doShutdown(RepositoryImpl.java:1175)
        at org.apache.jackrabbit.core.RepositoryImpl.shutdown(RepositoryImpl.java:1127)
        at ...

Solution

Make sure to login.
Yes. It’s that simple, just add

1
Session session = repository.login();

before you shut it down.
Note: It seems to be a better practice to shutdown using a RepositoryImpl you retreived from this Session as well:

1
 ((RepositoryImpl)session.getRepository()).shutdown();

The resulting “Hello Repository” code would look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//use this to get a quick configuration for log4j output
BasicConfigurator.configure();
        try {
            String xml = "repository/configuration.xml";
            String dir = "repository";
            RepositoryConfig config = RepositoryConfig.create(xml, dir);
            Repository repository = RepositoryImpl.create(config);
            Session session = repository.login();
            try {
                Thread.sleep(10000);
            } catch (InterruptedException ex) {
                Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
            }
            ((RepositoryImpl)session.getRepository()).shutdown();
        }catch (ConfigurationException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RepositoryException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }

Ein Kommentar zu “Apache Jackrabbit 2 / JCR2 Nullpointer Exception on shutdown”

  1. Angel sagt:

    It’s curious. This error must by a generic error used by jacrabbit developers, because appears in a lot of cases. ;)
    In my opinion, Jackrabbit 2 has a serious problem with Exceptions messages.

Kommentieren Sie diesen Artikel

Embedded gecko-browser in java-application with DJNativeSwing

Object Type or object value changed in php-session