001: package org.drools.repository;
002:
003: import java.io.IOException;
004: import java.io.InputStreamReader;
005: import java.io.Reader;
006: import java.util.Iterator;
007: import java.util.List;
008:
009: import javax.jcr.Node;
010: import javax.jcr.Repository;
011: import javax.jcr.Session;
012: import javax.jcr.Workspace;
013:
014: import org.apache.jackrabbit.core.TransientRepository;
015: import org.apache.jackrabbit.core.nodetype.InvalidNodeTypeDefException;
016: import org.apache.jackrabbit.core.nodetype.NodeTypeDef;
017: import org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl;
018: import org.apache.jackrabbit.core.nodetype.NodeTypeRegistry;
019: import org.apache.jackrabbit.core.nodetype.compact.CompactNodeTypeDefReader;
020: import org.apache.log4j.Logger;
021:
022: /**
023: * This contains code to initialise the repository for jackrabbit.
024: * This is mostly a collection of utilities.
025: * Any jackrabbit specific code needs to go in here.
026: */
027: public class JackrabbitRepositoryConfigurator implements
028: JCRRepositoryConfigurator {
029:
030: private static final Logger log = Logger
031: .getLogger(JackrabbitRepositoryConfigurator.class);
032:
033: /* (non-Javadoc)
034: * @see org.drools.repository.RepositoryConfigurator#getJCRRepository()
035: */
036: public Repository getJCRRepository(String repoRootDir) {
037: try {
038: if (repoRootDir == null) {
039: return new TransientRepository();
040: } else {
041: return new TransientRepository(repoRootDir
042: + "/repository.xml", repoRootDir);
043: }
044: } catch (IOException e) {
045: throw new RulesRepositoryException(
046: "Unable to create a Repository instance.", e);
047: }
048: }
049:
050: /* (non-Javadoc)
051: * @see org.drools.repository.RepositoryConfigurator#setupRulesRepository(javax.jcr.Session)
052: */
053: public void setupRulesRepository(Session session)
054: throws RulesRepositoryException {
055: System.out
056: .println("Setting up the repository, registering node types etc.");
057: try {
058: Node root = session.getRootNode();
059: Workspace ws = session.getWorkspace();
060:
061: //no need to set it up again, skip it if it has.
062: boolean registered = RulesRepositoryAdministrator
063: .isNamespaceRegistered(session);
064:
065: if (!registered) {
066: ws.getNamespaceRegistry().registerNamespace("drools",
067: RulesRepository.DROOLS_URI);
068:
069: //Note, the order in which they are registered actually does matter !
070: this .registerNodeTypesFromCndFile(
071: "/node_type_definitions/tag_node_type.cnd", ws);
072: this .registerNodeTypesFromCndFile(
073: "/node_type_definitions/state_node_type.cnd",
074: ws);
075: this
076: .registerNodeTypesFromCndFile(
077: "/node_type_definitions/versionable_node_type.cnd",
078: ws);
079: this
080: .registerNodeTypesFromCndFile(
081: "/node_type_definitions/versionable_asset_folder_node_type.cnd",
082: ws);
083:
084: this
085: .registerNodeTypesFromCndFile(
086: "/node_type_definitions/rule_node_type.cnd",
087: ws);
088: this
089: .registerNodeTypesFromCndFile(
090: "/node_type_definitions/rulepackage_node_type.cnd",
091: ws);
092:
093: }
094:
095: // Setup the rule repository node
096: Node repositoryNode = RulesRepository.addNodeIfNew(root,
097: RulesRepository.RULES_REPOSITORY_NAME, "nt:folder");
098:
099: // Setup the RulePackageItem area
100: RulesRepository.addNodeIfNew(repositoryNode,
101: RulesRepository.RULE_PACKAGE_AREA, "nt:folder");
102:
103: // Setup the Snapshot area
104: RulesRepository.addNodeIfNew(repositoryNode,
105: RulesRepository.PACKAGE_SNAPSHOT_AREA, "nt:folder");
106:
107: //Setup the Cateogry area
108: RulesRepository.addNodeIfNew(repositoryNode,
109: RulesRepository.TAG_AREA, "nt:folder");
110:
111: //Setup the State area
112: RulesRepository.addNodeIfNew(repositoryNode,
113: RulesRepository.STATE_AREA, "nt:folder");
114:
115: //and we need the "Draft" state
116: RulesRepository.addNodeIfNew(repositoryNode
117: .getNode(RulesRepository.STATE_AREA),
118: StateItem.DRAFT_STATE_NAME,
119: StateItem.STATE_NODE_TYPE_NAME);
120:
121: session.save();
122: } catch (Exception e) {
123: log.error("Caught Exception", e);
124: System.err.println(e.getMessage());
125: throw new RulesRepositoryException(e);
126: }
127: }
128:
129: private void registerNodeTypesFromCndFile(String cndFileName,
130: Workspace ws) throws RulesRepositoryException,
131: InvalidNodeTypeDefException {
132: try {
133: //Read in the CND file
134: Reader in = new InputStreamReader(this .getClass()
135: .getResourceAsStream(cndFileName));
136:
137: // Create a CompactNodeTypeDefReader
138: CompactNodeTypeDefReader cndReader = new CompactNodeTypeDefReader(
139: in, cndFileName);
140:
141: // Get the List of NodeTypeDef objects
142: List ntdList = cndReader.getNodeTypeDefs();
143:
144: // Get the NodeTypeManager from the Workspace.
145: // Note that it must be cast from the generic JCR NodeTypeManager to the
146: // Jackrabbit-specific implementation.
147: NodeTypeManagerImpl ntmgr = (NodeTypeManagerImpl) ws
148: .getNodeTypeManager();
149:
150: // Acquire the NodeTypeRegistry
151: NodeTypeRegistry ntreg = ntmgr.getNodeTypeRegistry();
152:
153: // Loop through the prepared NodeTypeDefs
154: for (Iterator i = ntdList.iterator(); i.hasNext();) {
155: // Get the NodeTypeDef...
156: NodeTypeDef ntd = (NodeTypeDef) i.next();
157:
158: log.debug("Attempting to regsiter node type named: "
159: + ntd.getName());
160:
161: // ...and register it
162: ntreg.registerNodeType(ntd);
163: }
164: } catch (InvalidNodeTypeDefException e) {
165: log
166: .warn("InvalidNodeTypeDefinitionException caught when trying to add node from CND file: "
167: + cndFileName
168: + ". This will happen if the node type was already registered. "
169: + e);
170: throw e;
171: } catch (Exception e) {
172: log.error("Caught Exception", e);
173: throw new RulesRepositoryException(e);
174: }
175: }
176:
177: }
|