JNDI

Magnolia JNDI is a simple module that allows exposing Magnolia's JCR repository to the outside of Magnolia web application. Magnolia JNDI module is available in Magnolia Enterprise Edition 3.0 and higher.

Installation

  • Copy magnolia-jndi-VERSION.jar and jcr-1.0.jar to YOUR_J2EE_CONTAINER/SHARED_LIBS
  • To share the repository, modify WEB-INF/config/default/repositories.xml to use magnolia-jndi.
<!-- Magnolia default repository -->
        <Repository name="magnolia" provider="...." loadOnStartup="true">
            .......
-->         <param name="contextFactoryClass" value="info.magnolia.jndi.InMemoryContextFactory" />
-->         <param name="providerURL" value="http://www.magnolia.info/jcr" />
-->         <param name="bindName" value="GlobalRepository" />
            <workspace name="website" />
            <workspace name="config" />
            <workspace name="users" />
            <workspace name="userroles" />
            <workspace name="usergroups" />
            <workspace name="mgnlSystem" />
            <workspace name="mgnlVersion" />
        </Repository>
  • Make sure that you do not duplicate any libraries used in the shared object, all related jars must "only" be present in shared libraries.

Code example

Simple code example on how to access Magnolia repository from outside of Magnolia using magnolia-jndi module (as long as access happens still within same JVM).

package magnolia.utilities;

import java.io.IOException; import java.io.PrintWriter; import java.util.Hashtable; import javax.jcr.Session; import javax.jcr.SimpleCredentials; import javax.jcr.Repository; import javax.jcr.Session; import javax.jcr.SimpleCredentials; import javax.jcr.Workspace; import javax.naming.Context; import javax.naming.InitialContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

public class MgnlJndiConnector extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print("Testing with Magnolia 3.5.4 ............\\n"); String contextFactoryClass = "info.magnolia.jndi.InMemoryContextFactory"; String providerURL = "http://www.magnolia.info/jcr"; final String bindName = "magnoliaAuthor"; final Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactoryClass); env.put(Context.PROVIDER_URL, providerURL); try { InitialContext ctx = new InitialContext(env); // first try to find the existing object if any Repository repository = (Repository) ctx.lookup(bindName); SimpleCredentials credentials = new SimpleCredentials( "superuser", "superuser".toCharArray()); Session jcrSession = repository.login(credentials); Workspace workspace = jcrSession.getWorkspace(); out.print("Got a workspace:" + workspace.getName() + "\\n"); } catch (Exception e) { throw new RuntimeException(e); } } }