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