You can access any JCR repository from a Java environment. This section explains how to set up your Java development environment and how to write a Java or a web application that connects to a remote repository.
Writing an Application Connecting to a Remote JCR
The examples connect to a CRX repository that is available for RMI on port 1099 (the default RMI port). Make sure that you have enabled RMI access to the repository.
The Java Content Repository (JCR) framework lets you access content at the following levels:
- Repository: The entire CRX repository. A repository contains one or more workspaces.
- Workspace: One data container in the repository. A workspace stores content as a hierarchy of nodes and properties.
- Session: A session is a connection to a workspace by a user. Depending on the user access rights, a session can see part of or all of a workspace, and may have different access rights (read or write) for different parts of the workspace. Also, a session offers temporary storage, so you may modify items in a session, and then later save the modifications to the workspace.
- Node: A node is a hierarchical entity in the workspace. Nodes can have different types, which you can define freely. Depending on the node type, a node may have required or optional sub-nodes and properties, or may freely allow any nodes and properties below it.
- Property: A property is a content entity. Properties can store text data, numbers, dates and binary data. A property cannot have nodes and other properties below it.
For more information, read the JSR-283 specification.
There are different ways to access a JCR repository in a Java environment: via RMI, JNDI, WebDAV or via the JCA connector. Please refer to the section Accessing the CRX Repository to get more information on each method.
The following code connects to a JCR repository via RMI (see below for how to use the code in a Java application or in a Web application):
To create a Hello World project in Eclipse, proceed as follows:
1. In the File menu, point to New, and then click Project. The New Project Window opens.
2. Click Java Project, and then click Next.
3. In the Project name field, type the name of the project, for example Hello World. Click Next.
4. Click the Libraries tab, and then click Add External JARs. From the folder lib of your connector installation folder, add the libraries jcr-2.0.jar and crx-rmi-2.0.jar from the folder server/runtime/0/_crx/WEB-INF/lib of your CRX installation folder.
5. Click Finish. You have now created a new Java application project.
To create a Hello World class, proceed as follows:
1. Right-click the project you have created, point to New, and then click Class. The New Java Class window opens.
2. In the Name field, type the name of the class, for example HelloWorld.
3. Click Finish. Eclipse creates the new class for you and opens it in the edit window.
4. Paste the following code into the class:
Note: To run the class, click the Run button (green, right arrow). To configure how Eclipse runs the class, click the Run menu, and then click Run.
The Web Application framework specifies the folder structure you have to use. For a Hello World Web application, you need the following folders and files:
- In the folder webapps of your server, create a new folder named helloworld. This is your Web application folder.
- In your Web application folder, create a folder named WEB-INF.
- In the WEB-INF folder, create the folders src, lib, and classes. These folders store the source files, libraries and the compiled Java classes of the Web application, respectively.
- In the WEB-INF folder, create an empty text file named web.xml. This file contains the Web application configuration.
The webapps folder now looks as follows:
|jcr-2.0.jar||The JCR API. This API lets you access data sources that adhere to the Java Content Repository standard, such as CRX.|
|crx-rmi-2.0.jar||The library that lets you connect to CRX using an RMI connection.|
The file webapps/helloworld/WEB-INF/web.xml configures the Web application. For the Hello World example used in this section, configure it as follows:
Note that if you write your own Web application, the <servlet-class> element must point to the class you use.
After you have made these changes, and before you access the Web application for the first time, you must restart CRX.
To create an Eclipse project to develop your Web application, proceed as follows:
- Start Eclipse and close all open projects.
- In the File menu, point to New, and then click Project. The New Project window opens.
- Click Java Project, and then click Next.
- In the Project Name field, type the name of your Web project for example, Hello World Web Application.
- In the Contents group, click Create project from existing source. Click Browse, and then select the folder of your Web application, for example the folder webapps/helloworld. Click Next. Eclipse configures the folders and libraries automatically, and opens the Java Settings window.
- In the field “Default output folder”, type Hello World Web Application/WEB-INF/classes. Eclipse compiles the Java classes into this folder.
- Click the Source tab. Right-click the folder src, and then click Use as Source Folder.
- Click the Libraries tab, and then click the Add External JARs button. You need to add the Servlet library. On a Tomcat server, the library is at common/lib/servlet.jar.
- Click Finish to create the project.
Note: Some changes require that you restart the Virtual Machine. In general, you can change code in a class or method, but you cannot add methods, or change the arguments that a method accepts.
In Eclipse, configure the debug connection as follows:
- In the Run menu, click Debug. The Debug window opens.
- In the left list, click the Remote Java Application entry, and then click New to add a new debug connection.
- Next to the Project field, click Browse, and select the Hello World Web Application class.
- If the Virtual Machine runs on your own computer, leave the Host field to localhost. If you run the Virtual Machine on another computer, type the URL of the computer.
- In the Port field, type the port at which the Virtual Machine is open for debugging. This in the address value of the VM option you have configured above (for example, 7403).
- Click Debug to start the debug connection.
Note: To see if a debug connection is active, click the Debug tab in the bottom right pane of Eclipse. If the connection is active, the tab lists all threads that currently run on the Virtual Machine.
In the Web application framework, classes are stored in unique packages, starting with the reverse URL of your company. For example, if you work at a company with the Web site www.myCompany.com, and create a Hello World project, you may store the project in the package com.myCompany.helloWorld. Proceed as follows to create the HelloWorld class for your Web application:
- Right-click the Hello World Web Application project, point to New and then click Class. The New Java Class window opens.
- In the Package field, type com.myCompany.helloWorld
- In the Name field, type HelloWorld
- Click OK. Eclipse creates a new class and opens it in the editor.
If you encounter problems, restart CRX to make sure that the Web application is properly registered and configured on the server.