What is a Java content repository (Java content repository) (3)

Source: Internet
Author: User
Develop our exampleProgram
Jackrabbit has been configured. Now let's create our sample program. This example program will call the JCR-170 API. Obviously, we need to do two things: adding, deleting, modifying, and querying data in the background (persistence layer), and developing the corresponding UI (web layer ). First, Let's define a DaO interface. This interface blogentrydao. Java is as follows:

Public Interface Blogentrydao {
Public Void Insertblogentry (blogentrydto)
Throws Blogapplicationexception;
Public Void Updateblogentry (blogentrydto)
Throws Blogapplicationexception;
Public Arraylist getbloglist ()
Throws Blogapplicationexception;
Public Blogentrydto getblogentry (string blogtitle)
Throws Blogapplicationexception;
Public Void Removeblogentry (string blogtitle)
Throws Blogapplicationexception;
Public Arraylist searchbloglist (string username)
Throws Blogapplicationexception;
Public Void Attachfiletoblogentry (string blogtitle, inputstream uploadinputstream)
Throws Blogapplicationexception;
Public Inputstream getattachedfile (string blogtitle)
Throws Blogapplicationexception;
}

As you can see, this interface provides methods for adding, deleting, modifying, and querying, and also provides two methods for processing attachments. Next, we need a DTO object to transfer data between two layers.

Public Class Blogentrydto {

PrivateString username;
PrivateString title;
PrivateString blogcontent;
PrivateCalendar creationtime;

//Getter and setter methods for each of these properties
}

Here we will only discuss the persistence layer.

Connect to jackrabbit
Now, the first thing is to develop a component to get a connection to the Jackrabbit content repository. For simplicity, we will get the connection when the program starts, and then release the connection when the program stops. Here we use struts, so we need to develop a plugin class. As follows:

Public ClassJackrabbitpluginImplements Plugin {
Public Static Session session;
Public Void Destroy (){
Session. logout ();
}
Public Void Init (actionservlet, moduleconfig)
Throws Servletexception {
Try {
System. setproperty ("Org. Apache. jackrabbit. repository. Home" ,
"C:/temp/blogging" );
Repository= New Transientrepository ();
Session=Repository. login (NewSimplecredentials ("Username" ,
"Password" . Tochararray ()));
}Catch (Loginexception e ){
Throw New Servletexception (E );
}Catch (Ioexception e ){
Throw New Servletexception (E );
}Catch (Repositoryexception e ){
Throw New Servletexception (E );
}
}
Public Static Session getsession (){
Return Session;
}
}

The init () method will be called when the program starts, and destroy () will be called when the program stops. We obtained the connection to the Jackrabbit content warehouse in the init () method. LookCodeThe first thing we do is to set the system attribute org. Apache. jackrabbit. repository. Home.ArticleThis attribute is used to point to the main directory of our content warehouse. Here we set it to C:/temp/blogging. Next, we created an instance of transientrepository. This is a class provided by jackrabbit, which provides a proxy to the content repository. It automatically starts the content warehouse when the first session is opened, and closes the content warehouse when the last session is closed.
Once we obtain a content repository object, we can call its login () method to open a connection. The login () method requires a credential object as a parameter. If the credential object is null, Jackrabbit will think that other mechanisms have done this verification (such as JAAS ). The login () method can also pass in a workspace name as a parameter. If this parameter is not input, Jackrabbit will return a session object pointing to the default workspace. Note that the workspace and session are one-to-one, that is, a session corresponds to only one workspace. (Note: If the credential object is not input, the returned session is read-only to the workspace)

Add content
The connection has been established. Let's implement the blogentrydao interface. The first method we want to implement is to insert the data insertblogentry ()

Public Void Insertblogentry (blogentrydto)
Throws Blogapplicationexception {
Session session= Jackrabbitplugin. getsession ();
Node rootnode= Session. getrootnode ();
Node blogentry=Rootnode. addnode ("Blogentry" );
Blogentry. setproperty ("Title" , Blogentrydto. gettitle ());
Blogentry. setproperty ("Blogcontent" , Blogentrydto. getblogcontent ());
Blogentry. setproperty ("Creationtime" , Blogentrydto. getcreationtime ());
Blogentry. setproperty ("Username" , Blogentrydto. GetUserName ());
Session. Save ();
}

First, obtain the session object, that is, the connection to a specific workspace in the content warehouse. Then, we call the getrootnode () method on this session object to obtain the root node of this workspace. The path of this root node is ("/"). once we obtain this root node, we can add a new subnode under this root node through the addnode () method. The name of the new node is blogentry. data is stored in the node's property through the setproperty () method. As we have previously stated, the real data is stored in the property element, and the property element is the leaf.
Pay attention to the session. Save () line of code. This method must be called. Before this method is called, any node or property changes are saved in a temporary region of this session, none of the other sessions connected to the same workspace as this session can see these changes. After this method is called and successfully executed, the changes to the node and property will be persisted to the workspace associated with this session, at the same time, all sessions associated with this workspace can see these changes. Correspondingly, session. Refresh (false) will discard all these changes. Item. Save () is similar to item. Refresh (false), but the impact scope is limited to a single item (note, including its subnodes)

Http://www.blogjava.net/ronghao Rong Hao original, thank you very much!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.