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 2011/02/15 17:27:37 UTC

[Jackrabbit Wiki] Update of "ExamplesPage" by amallogia

Dear Wiki user,

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

The "ExamplesPage" page has been changed by amallogia.
http://wiki.apache.org/jackrabbit/ExamplesPage?action=diff&rev1=37&rev2=38

--------------------------------------------------

  == Apache Jackrabbit Examples ==
+ This page will be used to show solutions to common problems related to Apache Jackrabbit and the JCR API. These examples shouldn't be considered Best Practices - general error checking and exception handling have been omitted to keep the example code simple.
- 
- This page will be used to show solutions to common problems related to Apache Jackrabbit
- and the JCR API. These examples shouldn't be considered Best Practices - general error checking
- and exception handling have been omitted to keep the example code simple.
  
  Please feel free to add your own examples.
  
  <<TableOfContents>>
  
  == Importing a File ==
- 
  Also see: [[http://svn.apache.org/repos/asf/jackrabbit/sandbox/examples/src/java/org/apache/jackrabbit/examples/FSImport.java|FSImport.java]] for more complete filesystem examples.
  
  {{{
@@ -20, +16 @@

      {
          //create the file node - see section 6.7.22.6 of the spec
          Node fileNode = folderNode.addNode (file.getName (), "nt:file");
-         
+ 
          //create the mandatory child node - jcr:content
          Node resNode = fileNode.addNode ("jcr:content", "nt:resource");
          resNode.setProperty ("jcr:mimeType", mimeType);
@@ -33, +29 @@

          return fileNode;
      }
  }}}
- 
- 
  == Renaming a Node ==
- 
  {{{
-     void rename(Node node, String newName) throws RepositoryException 
+     void rename(Node node, String newName) throws RepositoryException
      {
          node.getSession().move(node.getPath(), node.getParent().getPath() + "/" + newName);
          // Don't forget - not necessarily here at this place:
          // node.getSession().save();
      }
  }}}
- 
- 
- 
  == Register a Node Type ==
- 
  There are a few solutions in the works. For example, see [[http://svn.osafoundation.org/server/commons/trunk/jackrabbit/|OSAF offline tool]],  [[http://issues.apache.org/jira/browse/GRFT-23|Graffito Jira Issue]].
  
  {{{
@@ -58, +47 @@

          //NodeTypeRegistry object
          Workspace wsp = session.getWorkspace();
          NodeTypeManager ntMgr = wsp.getNodeTypeManager();
-         
+ 
          //non-JSR 170 - jackrabbit specific
-         NodeTypeRegistry ntReg = 
+         NodeTypeRegistry ntReg =
                  ((NodeTypeManagerImpl) ntMgr).getNodeTypeRegistry();
          ntReg.registerNodeType(nodeTypeDef);
      }
  }}}
+ You can use JCR API to create and register custom node type (Groovy syntax).
  
+ {{{
+ /* Retrieve node type manager from the session */
+ NodeTypeManager nodeTypeManager = session.workspace.nodeTypeManager
+ /* Create node type */
+ NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate()
+ nodeType.name = "my_custom_node_type"
+ /* Create a new property */
+ PropertyDefinitionTemplate customProperty = nodeTypeManager.createPropertyDefinitionTemplate()
+ customProperty.name = "my_custom_property"
+ customProperty.requiredType = PropertyType.LONG
+ /* Add property to node type */
+ nodeType.propertyDefinitionTemplates << customProperty
+ 
+ /* Register node type */
+ nodeTypeManager.registerNodeType(nodeType, false)
+ }}}
  == 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.
- 
- 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)
@@ -88, +92 @@

          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).
- 
- 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)
@@ -98, +100 @@

          Session session;
  
          session = repository.login(cred);
-  
+ 
          // create 'custom' namespace if necessary
          Workspace workspace = session.getWorkspace();
          NamespaceRegistry reg = workspace.getNamespaceRegistry();
@@ -110, +112 @@

          return session;
      }
  }}}
- 
  == Versioning Basics ==
- 
  {{{
      public void versioningBasics (Node parentNode, Session session) throws RepositoryException
      {
@@ -136, +136 @@

              Version version = (Version) it.next();
              System.out.println(version.getCreated().getTime());
            }
-           
+ 
            //restoring old version
            child.checkout();
-           child.restore(firstVersion, true);          
+           child.restore(firstVersion, true);
      }
  }}}
- 
- 
  == Creating a Workspace ==
- 
  {{{
  ((org.apache.jackrabbit.core.WorkspaceImpl)workspace).createWorkspace(name);
  }}}
- 
- 
  == Deleting a Workspace ==
- 
  {{{
  You have to manually remove the workspace.xml - there's no programmatic way yet.
  }}}
- 
- 
  == Shutting Down the Repository ==
- 
  {{{
  javax.jcr.Session session = ...;
  ((org.apache.jackrabbit.core.RepositoryImpl) session.getRepository()).shutdown();
  }}}
- 
  == Jackrabbit Cache Configuration ==
  This info has moved to the CacheManager page.
  
  == Is the Repository Running? ==
- 
  {{{
  /**
   * Check if a repository is currently running. This only works when
@@ -189, +178 @@

      return lock.exists();
  }
  }}}
- 
  == Spring Configuration ==
- 
  You can create a Repository reference in Spring in multiple ways, but here's one that uses the RepositoryImpl class:
  
  {{{
      <bean id="repository" class="org.apache.jackrabbit.core.RepositoryImpl">
          <constructor-arg index="0" ref="config" />
      </bean>
-     <bean id="config" class="org.apache.jackrabbit.core.config.RepositoryConfig" factory-method="create">        
+     <bean id="config" class="org.apache.jackrabbit.core.config.RepositoryConfig" factory-method="create">
          <constructor-arg index="0" value="./repository.xml"/>
          <constructor-arg index="1" value="." />
      </bean>
  }}}
- 
  This will create a repository in the current directory, using ./repository.xml as the configuration file. This isn't as complete as se-jcr will be (hopefully) but this does work with Spring 3.0 and JackRabbit 2.0.
  
  == See also ==
+  * EncodingAndEscaping
  
-  * [[EncodingAndEscaping]]
-