Numerous kinds of content repositories are provided these days, some of them are Open source like Apache Jackrabbit while others are still closed source and. You could look also at Silverpeas code. It’s using Jackrabbit with some simple code to create nodes, browse them etc. For example take a look. Readers performing this tutorial have just finished an overview of Apache Jackrabbit and the JCR specifications and are looking for examples.
|Published (Last):||2 November 2006|
|PDF File Size:||3.2 Mb|
|ePub File Size:||20.78 Mb|
|Price:||Free* [*Free Regsitration Required]|
Welcome to your first hops into the world of Jackrabbit! Once you have finished hopping through this document, you should be all set to continue on your own with the official JCR specification and the documentation on this site. The easiest way to get started with Jackrabbit is to download the runnable Standalone Server jar.
In addition to running it, you can also put it in your classpath to quickly access all the jackrabbkt and interfaces you need below. Alternatively, if you use the Apache Maven jackrbabit system which we recommendyou can set up your first hops project with the following dependencies. You probably have an error in your classpath settings if you get a ClassNotFoundException message when trying to compile or run the examples below.
The full example application that does this is shown below, with line-by-line explanations following shortly after. You can also download the source file as FirstHop. If you have your classpath set up, you can compile the application with javac FirstHop.
In addition to producing the above status line the application copies a default repository configuration file to repository. You can use the system properties org. The promise of the Tutorlal API is that if you only use these interfaces in your content application, it should remain mostly independent of the underlying content repository implementation.
The Repository interface represents a given content repository instance and the Tuotrial interface represents a single login session for accessing the repository. A session is jcakrabbit to access any content within a repository. Note that a Session instance is not guaranteed to be thread-safe so you should start multiple sessions if you need to access repository content simultaneously from different threads.
This is especially important for things like web applications. The best practice for deploying Jackrabbit is to jackrabbti JNDI or some other configuration mechanism in a container environment to keep the application code free of direct Jackrabbit dependencies, but since we are creating a simple standalone application we can take a shortcut by using the JcrUtils class from Jackrabbit commons.
The FirstHop example is a simple standalone application that fits nicely in the main method and lets the JVM take care tktorial the possible exceptions. More substantial content applications could also be written as web application or EJB components with different setup and error handling patterns. The actual implementation depends on the jar files available on the classpath and in this example is a TransientRepository. The implementation contains a utility feature that will take care of the initial configuration and repository construction when the first session is started.
Thus there is no need for manual configuration for now unless you want direct control over the repository setup. The TransientRepository implementation will automatically initialize the content repository when the first session is started and shut it down when the last session is closed. Thus there is no need for explicit repository shutdown as long as all sessions are properly closed. Note that a Jackrabbit repository ttuorial contains a lock file that tutoorial it from being accessed simultaneously by multiple processes.
You will tutoril repository startup exceptions caused by the lock file if you fail to properly close all sessions or otherwise shut down the repository before leaving the process that accesses a repository. Normally you can just manually remove the lock file in such cases but such cases always present a chance of repository corruption especially if you use a non-transactional persistence manager. Jackrabibt we use the TransientRepository class as the Repository implementation, this step a;ache also cause the repository to be initialized.
It is a good practice to properly release all acquired resources, and the JCR sessions are no exception. The try-finally idiom is a good way to ensure that a resource really gets released, as the release method gets called even if the intervening code throws an exception or otherwise jumps outside the scope for example using a return, break, or continue statement.
The username or identifier of the user associated with a session is available using the Session. Jackrabbit returns “anonymous” by default.
Apache Jackrabbit – First Hops
Each content repository implementation publishes a number of string descriptors that jackrabbih the various implementation properties, like the implementation level and the supported optional JCR features. See the Repository interface for a list of the standard repository descriptors. The main function of a content repository is allow applications to store and retrieve content.
The content in a JCR content repository consists of structured or unstructured data modeled as a hierarchy of nodes with properties that contain the actual data. The following example application first stores some content to the initially empty content repository, then retrieves the stored content and outputs it, and finally removes the stored content. Like in the first hop, this example source is also available as SecondHop. You can also compile and run this class just like you did in the first hop example.
Running this example should produce the following output:. These are two new classes we need for this example. The SimpleCredentials class is a simple implementation of the Credentials interface used for passing explicit user credentials to the Repository.
The Node interface is used to manage the content nodes in jacirabbit repository. There is a related interface called Property for managing apsche properties, but in this example we use the Property interface only indirectly. As discussed in the First Hop example, a login with GuestCredentials returns an anonymous read-only session in the Jackrabbit default configuration. To be able to store and remove content we need to create a session with write access, and to do that we need to pass credentials with a username and password to the.
The default Jackrabbit login mechanism accepts only username and password as valid credentials for known users.
Creating a Content Repository Using Jackrabbit Oak and MongoDB
A Jackrabbit repository with a default configuration will create an admin user when it is first initialized. Thus we need to construct tutroial use a SimpleCredentials instance aoache the username and initial default password of the admin user, in this case “admin” and “admin”.
The SimpleCredentials constructor follows the JAAS convention of representing the username as a normal String, but the password as a character array, so we need to use the String. Each JCR session is associated with a workspace that contains a single node tree.
A simple way to access the root jadkrabbit is to call the Session. Having a reference to the root node allows us to easily store and retrieve content in the current workspace. New content nodes can be added using the Node. The method takes the name or relative path of the node to be added and creates the named node in the transient storage associated with the current session.
Until the transient storage is persisted, the added node is only visible within the current session and not within any other session that is concurrently accessing the content repository. This code snippet creates two new nodes, called “hello” and “world”with “hello” being a child of the root node and “world” a child of the “hello” node. To add some content to the structure created using the “hello” and “world” nodes, we use the Node.
The value of the property is the string “Hello, World! Like the added nodes, also the property is first created in the transient storage associated with the current session. If the named property already exists, then this method will change the value of that property.
This way other sessions could also access the example content we just created.
If you like, you could even split the example application into three pieces for respectively storing, retrieving, and removing the example content. Such a split would not work unless we persisted the changes we make.
The changes are written to the persistent repository storage and they become visible to all sessions accessing the same workspace. Without this call all changes will be lost forever when the session is closed. The path syntax follows common file system conventions: The end result is that the method returns a node instance that represents the same content node as the world instance created a few lines earlier.
Each content node and property is uniquely identified by its absolute path within the workspace. The absolute path starts with a forward slash and contains all the names of the ancestor nodes in order before the name of the current node or jackrbabit. The path of a node or property can be retrieved using the Item.
The Item interface is a superinterface of Tytorial and Property, and contains all the functionality shared by nodes and properties. Properties can be accessed using the Node.
A JCR property can contain either a single or multiple values of a given type. There are property types for storing strings, numbers, dates, binary streams, node references, etc. We just want the apacye string tuhorial, so we use the Property. The result of this statement is the line “Hello, World! Nodes and properties can jackragbit removed using the Item. The method removes the entire content subtree, so we only need to remove the topmost “hello” node to get rid of all the content we added before.
Removals are first stored in the session-local transient storage, just like added and changed content. Like before, the transient changes need to be explicitly saved for the content to be removed from the persistent storage. The third example application shown below will import the XML file called test.
Once the XML content is imported, the application recursively dumps the contents of the entire workspace using the simple dump method. This hop has a lot of similarities with the Second Tutorual example: By now you should be familiar with logging into a repository Repository. This deserializes an XML document and adds the resulting item subgraph as a child of the node at the provided path. Incoming nodes are added in the same way that new node is added with Node.
That is, they are either assigned newly created identifiers upon addition or upon save. In either case, identifier collisions will not occur.
If an incoming node has the same identifier as a node already existing in the workspace then the already existing node and its subgraph is removed from wherever tutrial may be in the workspace before the incoming javkrabbit is added. Both the removal and the new addition will be dispatched on save. If an incoming node has the same identifier as a node already existing in the workspace, then the already-existing node is replaced by the incoming node in the same position as the existing node.
In the most extreme case this behavior may result in no node at all being added as child tutoeial parentAbsPath. This will occur if the topmost element of the incoming XML has the same identifier as an existing node elsewhere in the workspace. The change will be dispatched on save. If an incoming node has the same identifier as a node already existing in the workspace then an ItemExistsException is thrown.
This can be used as an example of how to do a recursive traversal of all the repository, and check the properties of each node. Notice how we have to pay special attention to the multi-value properties, because that impacts the jackkrabbit we use them:. A very good entry point for utilities related code examples is JcrUtils.