01: package org.drools.repository;
02:
03: import javax.jcr.Node;
04: import javax.jcr.PathNotFoundException;
05: import javax.jcr.RepositoryException;
06: import javax.jcr.Session;
07: import javax.jcr.Workspace;
08:
09: import org.apache.log4j.Logger;
10:
11: /**
12: * This class is for administering the rules repo.
13: * Any "sensitive" actions can happen in here.
14: *
15: * @author Michael Neale
16: *
17: */
18: public class RulesRepositoryAdministrator {
19:
20: private static final Logger log = Logger
21: .getLogger(RulesRepositoryAdministrator.class);
22:
23: private final Session session;
24:
25: /**
26: * Pass in a session that is capable of doing admin-ey type stuff.
27: */
28: public RulesRepositoryAdministrator(Session session) {
29: this .session = session;
30: }
31:
32: static boolean isNamespaceRegistered(Session session)
33: throws RepositoryException {
34: Workspace ws = session.getWorkspace();
35: //no need to set it up again, skip it if it has.
36: String uris[] = ws.getNamespaceRegistry().getURIs();
37: for (int i = 0; i < uris.length; i++) {
38: if (RulesRepository.DROOLS_URI.equals(uris[i])) {
39: return true;
40: }
41: }
42: return false;
43: }
44:
45: /**
46: * This will tell you if the repository currently connected to is initialized.
47: * This includes the basic data/folders, as well as the name space registered.
48: * The name space registration is JCR implementation dependent (jackrabbit is the default).
49: */
50: public boolean isRepositoryInitialized() {
51: try {
52: return isNamespaceRegistered(session)
53: && session.getRootNode().hasNode(
54: RulesRepository.RULES_REPOSITORY_NAME);
55: } catch (RepositoryException e) {
56: throw new RulesRepositoryException(e);
57: }
58: }
59:
60: /**
61: * Clears out the entire tree below the rules repository node of the JCR repository.
62: * IMPORTANT: after calling this, RepositoryConfigurator.setupRulesRepository() should
63: * be called to set up the minimal data for a "blank" setup. If importing other data, however, this is probably not needed.
64: */
65: public void clearRulesRepository() {
66: log.debug("Clearing repository database. UserId="
67: + session.getUserID());
68: try {
69:
70: if (session.getRootNode().hasNode(
71: RulesRepository.RULES_REPOSITORY_NAME)) {
72: System.out.println("Clearing rules repository");
73: Node node = session.getRootNode().getNode(
74: RulesRepository.RULES_REPOSITORY_NAME);
75: node.remove();
76: session.save();
77: } else {
78: System.out
79: .println("Repo not setup, ergo not clearing it !");
80: }
81: } catch (PathNotFoundException e) {
82: log.error(e);
83: } catch (RepositoryException e) {
84: log.error(e);
85: }
86: }
87:
88: }
|