01: package org.drools.brms.server.repository;
02:
03: /*
04: * Copyright 2005 JBoss Inc
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: import java.util.Collection;
20:
21: import javax.jcr.LoginException;
22: import javax.jcr.Repository;
23: import javax.jcr.RepositoryException;
24: import javax.jcr.Session;
25: import javax.jcr.SimpleCredentials;
26:
27: import org.drools.repository.JCRRepositoryConfigurator;
28: import org.drools.repository.JackrabbitRepositoryConfigurator;
29: import org.drools.repository.RulesRepositoryAdministrator;
30: import org.drools.repository.RulesRepositoryException;
31: import org.jboss.seam.ScopeType;
32: import org.jboss.seam.annotations.Create;
33: import org.jboss.seam.annotations.Name;
34: import org.jboss.seam.annotations.Scope;
35: import org.jboss.seam.annotations.Startup;
36:
37: /**
38: * This startup class manages the JCR repository, sets it up if necessary.
39: * @author Michael Neale
40: */
41: @Scope(ScopeType.APPLICATION)
42: @Startup
43: @Name("repositoryConfiguration")
44: public class BRMSRepositoryConfiguration {
45:
46: JCRRepositoryConfigurator configurator = new JackrabbitRepositoryConfigurator();
47: String repositoryHomeDirectory = null;
48:
49: Repository repository;
50:
51: @Create
52: public void create() {
53: repository = configurator
54: .getJCRRepository(repositoryHomeDirectory);
55: Session sessionForSetup = newSession("admin");
56: create(sessionForSetup);
57: }
58:
59: void create(Session sessionForSetup) {
60:
61: RulesRepositoryAdministrator admin = new RulesRepositoryAdministrator(
62: sessionForSetup);
63: if (!admin.isRepositoryInitialized()) {
64: configurator.setupRulesRepository(sessionForSetup);
65: }
66: sessionForSetup.logout();
67: }
68:
69: public void setHomeDirectory(String home) {
70: this .repositoryHomeDirectory = home;
71: }
72:
73: public void setRepositoryConfigurator(String clazz)
74: throws ClassNotFoundException, InstantiationException,
75: IllegalAccessException {
76: Class cls = Class.forName(clazz);
77: this .configurator = (JCRRepositoryConfigurator) cls
78: .newInstance();
79: }
80:
81: /**
82: * This will create a new Session, based on the current user.
83: * @return
84: */
85: public Session newSession(String userName) {
86:
87: try {
88: return repository.login(new SimpleCredentials(userName,
89: "password".toCharArray()));
90: } catch (LoginException e) {
91: throw new RulesRepositoryException(
92: "Unable to login to JCR backend.");
93: } catch (RepositoryException e) {
94: throw new RulesRepositoryException(e);
95: }
96: }
97:
98: }
|