Jackbrabbit OCM and Spring

June 16 20087 Commented

Categorized Under: Java, Software Engineer

Content Repository API becomes more and more popular nowadays, however it is rather difficult in use because its takes time of learning and use. OCM (Object Content Mapping) module is the great tool to helps developers save a lot of time to develop the content-driven application and Spring is the great DI platform to hide most of complexity of initializing, creating and executing repository. We are developing the engroup ECM module base on Jackbrabbit OCM and Spring Module. During development, we look for help in many forums, websites but unfortunately we do not seek the full solution, we hope that this article provide the full example of using Jackbrabbit and Spring in the real application. Part of engroup ECM code base is included in attached file, it is developed base on jackrabbit 1.5 (snapshot version - you can get it at apache maven repository http://people.apache.org/maven-snapshot-repository/), spring modules 0.9 and the patch spring-ocm got at http://jira.springframework.org/browse/MOD-446

First, create the POJOs for JCR repository:

  @Node(jcrMixinTypes = "mix:versionable")
  public class Content {
    @Field(uuid=true)
    protected String id;
    @Field(path=true)
    protected String path;
    @Field
    protected String name;
    ...
  }

Note: if you want to create the POJO inherit the JCR fields of its parent class, you must use the extend property field of annotation Node like the following example:

  @Node(jcrMixinTypes = "mix:versionable", extend = AbstractFile.class)
  public class File extends Content {
    @Field
    protected byte[] content;
    ...
  }

The next step is creating the spring beans to init the repository, register nodes types and POJOs with repository. Here is the part of configuration file (you can see the full file in the attachment):

  • Initialize the repository:
        <bean id="repository" class="org.springmodules.jcr.jackrabbit.RepositoryFactoryBean">
          <property name="configuration" value="classpath:jackrabbit-repo.xml" />
          <property name="homeDir" value="file:/tmp/repository" />
        </bean>
        <bean id="jcrSessionFactory" class="org.springmodules.jcr.jackrabbit.ocm.JackrabbitSessionFactory">
          <property name="repository" ref="repository" />
          <property name="credentials">
            <bean class="javax.jcr.SimpleCredentials">
              <constructor-arg index="0" value="superuser" />
              <!-- create the credentials using a bean factory -->
                <constructor-arg index="1">
                  <bean factory-bean="password" factory-method="toCharArray" />
                </constructor-arg>
            </bean>
          </property>
          <property name="nodeTypes2Import" value="nodetypes/custom_nodetypes.xml" />
        </bean>
  • Make the mapping between POJOs and annotation mapper

        <bean id="jcrMappingDescriptor" class="org.apache.jackrabbit.ocm.mapper.impl.annotation.AnnotationMapperImpl">
          <constructor-arg index="0">
            <!--Put all your POJOs in this list-->
            <list>
              <value>com.engroup.module.ecm.domain.AbstractFile</value>
              <value>com.engroup.module.ecm.domain.Content</value>
              <value>com.engroup.module.ecm.domain.File</value>
              <value>com.engroup.module.ecm.domain.Folder</value>
            </list>
          </constructor-arg>
        </bean>
  • Declare the Content Service Bean with transaction management support
    
     <bean id="internalContentService"
        class="com.engroup.module.ecm.service.impl.ContentServiceImpl">
        <property name="jcrTemplate" ref="jcrMappingTemplate" />
     </bean>
     <bean id="contentService" parent="baseTransactionProxy">
        <property name="proxyInterfaces">
          <value>com.engroup.module.ecm.service.ContentService</value>
        </property>
        <property name="target">
          <ref bean="internalContentService" />
        </property>
        <property name="transactionAttributes">
          <props>
            <prop key="*">PROPAGATION_REQUIRED</prop>
          </props>
        </property>
      </bean>

Well, all configuration tasks are done. Now, you can access the content of repository by using POJOs. Thanks for spring modules that helps you reduce lot of code for initilizing and manage repository. The tasks later just be simply like you work with Hibernate entity, all works are done by Java code (Of course, you need to know a little advance knowledge of repository to customize data types etc for your needs). After I create the service, now it is time to write some little unit test to make sure all configurations are set properly :)

  @RunWith(UnitilsJUnit4TestClassRunner.class)
  public class ContentServiceTest {
    @SpringBean
    private ContentService<Content> contentService;

    @Test
    public void testSave() {
      File file = createFile();
      contentService.save(file);
      file = (File)contentService.findByPath("/nextss");
      Assert.assertThat(file.getPath(), is("/nextss"));
      Assert.assertThat(file.getFileType(), is(FileType.UNDEFINED));
      contentService.remove(file);
    }

    private File createFile() {
      File file = new File();
      file.setId("1");
      file.setPath("/nextss");
      file.setName("ABC");
      file.setFileType(FileType.UNDEFINED);
      file.setTitle("Test Exam");
      file.setLastModified(Calendar.getInstance().toString());
      file.setContent("Hello world".getBytes());
      file.setComment("AAA");
      return file;
    }
    ...
  }

I am happy when the unit test run well :) . Hope it is the part complements with Jackrabbit OCM and Spring modules - OCM. Welcomes any comments from you.

Resources:

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • BarraPunto
  • Bitacoras.com
  • blinkbits
  • BlinkList
  • blogmarks
  • BlogMemes
  • BlogMemes Cn
  • BlogMemes Fr
  • BlogMemes Jp
  • BlogMemes Sp
  • Blogosphere News
  • Blogsvine
  • blogtercimlap
  • Book.mark.hu
  • Bumpzee
  • co.mments
  • connotea
  • De.lirio.us
  • Design Float
  • DotNetKicks
  • DZone
  • eKudos
  • email
  • Fark
  • Faves
  • feedmelinks
  • Fleck
  • Furl
  • GeenRedactie
  • Global Grind
  • Gwar
  • Haohao
  • HealthRanker
  • Hemidemi
  • Identi.ca
  • IndianPad
  • Internetmedia
  • kick.ie
  • Kirtsy
  • laaik.it
  • Leonaut
  • LinkaGoGo
  • LinkArena
  • LinkedIn
  • Linkter
  • Live
  • Ma.gnolia
  • Meneame
  • MisterWong
  • MisterWong.DE
  • muti
  • MyShare
  • MySpace
  • N4G
  • Netvibes
  • Netvouz
  • NewsVine
  • NuJIJ
  • Ping.fm
  • PlugIM
  • Pownce
  • ppnow
  • Print
  • Propeller
  • Ratimarks
  • Rec6
  • Reddit
  • SalesMarks
  • Scoopeo
  • scuttle
  • Segnalo
  • Shadows
  • Simpy
  • Slashdot
  • Smarking
  • Socialogs
  • SphereIt
  • Spurl
  • StumbleUpon
  • Symbaloo
  • Taggly
  • TailRank
  • Technorati
  • ThisNext
  • Tipd
  • Tumblr
  • TwitThis
  • Upnews
  • Webnews.de
  • Webride
  • Wikio
  • Wikio FR
  • Wikio IT
  • Wists
  • Wykop
  • Xerpi
  • Yahoo! Buzz
  • YahooMyWeb
  • Yigg

Related posts:

  1. Integration of Jackrabbit OCM and Spring (updated version) In the previous post Jackrabbit OCM and Spring, I have...
  2. Testing Spring beans against database with DbUnit This is another post of testing series we wrote so...
  3. Using Spring Integration in Osgi platform How to use Spring Integration in Osgi platform. This post...

7 Responses to “Jackbrabbit OCM and Spring”

  1. Leandro Santos says:

    Would you mind posting the source code again? the link is broken…

  2. Guest says:

    Can u upload the source once again.
    The actual upload seems to be broken.

    Thx

  3. Elcorin says:

    Hi there,
    Not sure that this is true:), but thanks for a post.

    Thanks
    Elcorin

  4. Nadine says:

    Hi,
    Amazing! Not clear for me, how offen you updating your blog.esofthead.com.

    Thank you
    Nadine

  5. admin says:

    Nadine, I do not update the blog very often. Be busy with assigned projects :( .

    To all: The source code of this site is invalid and current, we refactor the our OCM module to use the standard JCR-170 node type such as nt:file, nt:folder already and this implementation is rather complex with our current one so we should not use it for any purpose. Please wait to us the next post about Spring OCM.

  6. admin says:

    You could see the updated post of Jackrabbit OCM and Spring at http://blog.esofthead.com/?p=174

  7. [...] Time: first line of codes are written when we write the blog post http://blog.esofthead.com/jackbrabbit-ocm-and-spring. By time, the source code have many changes when we have more knowledge of using Jackrabbit, [...]

Leave a Reply