You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jackrabbit.apache.org by Alex Lukin <lu...@stu.cn.ua> on 2008/02/05 21:58:00 UTC
"trivial" OCM init code
Hi, all!
Just in case someone will use OCM, there's quick and dirty init code needed to run it using annotations.
I think this code be hidden somewhere in OCM init function in future versions of OCM.
As by now, OCM must be inited by user. I do it in ServletConextListener
--------------------------- init method -------------------------
/** namespace prefix constant */
public static final String OCM_NAMESPACE_PREFIX = "ocm";
/** namespace constant */
public static final String OCM_NAMESPACE = "http://jackrabbit.apache.org/ocm";
protected void initOCM (ServletContext sc, Session session, String regpath)
throws InvalidNodeTypeDefException, javax.jcr.RepositoryException, IOException {
//Hell, register OCM namespaces first!
try {
// log.info("Setup Jcr session setup ...");
String[] jcrNamespaces = session.getWorkspace().getNamespaceRegistry().getPrefixes();
boolean createNamespace = true;
for (int i = 0; i < jcrNamespaces.length; i++) {
if (jcrNamespaces[i].equals(OCM_NAMESPACE_PREFIX)) {
createNamespace = false;
// log.debug("Jackrabbit OCM namespace exists.");
}
}
if (createNamespace) {
session.getWorkspace().getNamespaceRegistry().registerNamespace(OCM_NAMESPACE_PREFIX, OCM_NAMESPACE);
//
}
if (session.getRootNode() != null) {
//
}
} catch (Exception e) {
Logger.getLogger(SemarilStarter.class.getName()).log(Level.SEVERE, "Repository exception in bootstrap code while registering OCM namespaces", e);
}
// Hell! Register OCM types
InputStream xml = sc.getResourceAsStream(regpath);
// HINT: throws InvalidNodeTypeDefException, IOException
NodeTypeDef[] types = NodeTypeReader.read(xml);
Workspace workspace = session.getWorkspace();
NodeTypeManager ntMgr = workspace.getNodeTypeManager();
NodeTypeRegistry ntReg = ((NodeTypeManagerImpl) ntMgr).getNodeTypeRegistry();
for (int j = 0; j < types.length; j++) {
NodeTypeDef def = types[j];
try {
ntReg.getNodeTypeDef(def.getName());
} catch (NoSuchNodeTypeException nsne) {
// HINT: if not already registered than register custom node type
ntReg.registerNodeType(def);
}
}
}
---------------------------- minimal nodetype file to load ----------------------------
<?xml version="1.0" encoding="UTF-8"?>
<nodeTypes xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:rep="internal"
xmlns:sv="http://www.jcp.org/jcr/sv/1.0"
xmlns:test="http://www.apache.org/jackrabbit/test"
xmlns:mix="http://www.jcp.org/jcr/mix/1.0"
xmlns:ocm="http://jackrabbit.apache.org/ocm">
<nodeType name="ocm:discriminator" isMixin="true">
<supertypes>
<supertype>nt:base</supertype>
</supertypes>
<propertyDefinition name="ocm:classname" requiredType="String" autoCreated="false" mandatory="true" onParentVersion="COPY" protected="false" multiple="false" />
</nodeType>
</nodeTypes>
----------------------------- example: how classes must be registerd before use with OCM ---------------
import java.util.ArrayList;
import java.util.List;
import org.apache.jackrabbit.ocm.mapper.Mapper;
import org.apache.jackrabbit.ocm.mapper.impl.annotation.AnnotationMapperImpl;
import ua.org.dg.semaril.auth.HomesNode;
import ua.org.dg.semaril.auth.UserHome;
import ua.org.dg.semaril.auth.UserNode;
import ua.org.dg.semaril.auth.UsersNode;
/**
* This class contains statical registration of all classes used by
* application through OCM
* @author al
*/
public class OCMRegistry {
static List<Class> classes = new ArrayList<Class>();
static Mapper mapper;
static{
//register annotated classes
classes.add(SystemNode.class);
classes.add(UsersNode.class);
classes.add(UserNode.class);
classes.add(UsersNode.class);
classes.add(UserHome.class);
classes.add(HomesNode.class);
mapper = new AnnotationMapperImpl(classes);
}
public static Mapper getMapper() {
return mapper;
}
}
-------------------------- get OCM and use it----------
Session ses = ....// get JCR session
SystemNode sn = new SystemNode();
sn.setPath("/MySystemNode);
ObjectContentManager ocm = new ObjectContentManagerImpl(ses, OCMRegistry.getMapper());
ocm.insert(sn);
ocm.save();
....
sn = (SystemNode) ocm.getObject("/MySystemNode");
System.out.println( sn.getSomeParam());
....
------------------------------------
Such "trivial" setup of OCM, folks! :).
--
SY, Alex Lukin
RIPE NIC HDL: LEXA1-RIPE
Re: "trivial" OCM init code
Posted by Jukka Zitting <ju...@gmail.com>.
Hi,
On Feb 6, 2008 11:35 AM, Alex Lukin <lu...@stu.cn.ua> wrote:
> Wednesday 06 February 2008 10:53:25 Christophe Lombart написав:
> > I will added in Confluence.
> By the way, is new website available somewhere?
You can preview it now at http://jackrabbit.apache.org/JCR/. I'm
currently migrating it to http://jackrabbit.apache.org/.
BR,
Jukka Zitting
Re: "trivial" OCM init code
Posted by Alex Lukin <lu...@stu.cn.ua>.
Hi!
Wednesday 06 February 2008 10:53:25 Christophe Lombart написав:
> Thanks,
>
> I will added in Confluence.
By the way, is new website available somewhere?
> Such "trivial" setup of OCM, folks! :).
>
>
> The problem is that the node type creation is specific to the
> implementation and we would like to set OCM more independant.
> Let's try to find a solution to drop the ocm:discriminator.
Well one of the beauties of OOP is hidden implementation. May be you can just hide this setup code somewhere behind the scene.
I don not think that ocm:discriminator such a bad solution. Anyway we need to know which class of which version used to create the node.
BTW, I think that class version info must be present in repository and checked.
--
SY, Alex Lukin
RIPE NIC HDL: LEXA1-RIPE
Re: "trivial" OCM init code
Posted by Christophe Lombart <ch...@gmail.com>.
Thanks,
I will added in Confluence.
Such "trivial" setup of OCM, folks! :).
The problem is that the node type creation is specific to the implementation
and we would like to set OCM more independant.
Let's try to find a solution to drop the ocm:discriminator.