You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by Apache Wiki <wi...@apache.org> on 2009/05/11 18:04:06 UTC

[Jackrabbit Wiki] Update of "ExamplesPage" by BearGiles

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Jackrabbit Wiki" for change notification.

The following page has been changed by BearGiles:
http://wiki.apache.org/jackrabbit/ExamplesPage

The comment on the change is:
Added example of creating customer node type/namespace using CND.

------------------------------------------------------------------------------
      }
  }}}
  
+ === Register a Node Type [CND] ===
+ 
+ Register one or more node types using CND.  CND is described in
+ http://jackrabbit.apache.org/node-type-notation.html.
+ 
+ {{{
+     public void createCustomNodeTypes(Session session)
+         throws RepositoryException, IOException {
+ 
+         // Get the JackrabbitNodeTypeManager from the Workspace.
+         // Note that it must be cast from the generic JCR NodeTypeManager to
+         // the  Jackrabbit-specific implementation.
+         // (see: http://jackrabbit.apache.org/node-types.html)
+         JackrabbitNodeTypeManager manager =
+            (JackrabbitNodeTypeManager) session.getWorkspace().getNodeTypeManager();
+ 
+         // Register the custom node types defined in the CND file
+         InputStream is = Thread.currentThread().getContextClassLoader()
+                              .getResourceAsStream("com/example/jcr/custom.cnd");
+         manager.registerNodeTypes(is, JackrabbitNodeTypeManager.TEXT_X_JCR_CND);
+     }
+ }}}
+ 
+ You can automatically install your node types/namespace during initialization.
+ (This is why the method above pulls the file as classloader resource).
+ 
+ {{{
+     public Session setup(Credentials cred)
+         throws RepositoryException, IOException {
+         Session session;
+ 
+         session = repository.login(cred);
+  
+         // create 'custom' namespace if necessary
+         Workspace workspace = session.getWorkspace();
+         NamespaceRegistry reg = workspace.getNamespaceRegistry();
+ 
+         if (!Arrays.asList(reg.getPrefixes()).contains("custom")) {
+             createCustomNodeTypes(session);
+         }
+ 
+         return session;
+     }
+ }}}
  
  === Versioning Basics ===