01: package org.drools.brms.server.util;
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.io.File;
20:
21: import javax.jcr.Repository;
22: import javax.jcr.Session;
23: import javax.jcr.SimpleCredentials;
24:
25: import org.drools.repository.JCRRepositoryConfigurator;
26: import org.drools.repository.JackrabbitRepositoryConfigurator;
27: import org.drools.repository.RepositorySessionUtil;
28: import org.drools.repository.RulesRepositoryAdministrator;
29:
30: /**
31: * This is only to be used for testing, eg in hosted mode, or unit tests.
32: *
33: * @author Michael Neale
34: */
35: public class TestEnvironmentSessionHelper {
36:
37: public static Repository repository;
38:
39: public static Session getSession() throws Exception {
40: return getSession(true);
41: }
42:
43: public static synchronized Session getSession(boolean erase)
44: throws Exception {
45: if (repository == null) {
46:
47: if (erase) {
48: File repoDir = new File("repository");
49: System.out.println("DELETE test repo dir: "
50: + repoDir.getAbsolutePath());
51: RepositorySessionUtil.deleteDir(repoDir);
52: System.out.println("TEST repo dir deleted.");
53: }
54:
55: JCRRepositoryConfigurator config = new JackrabbitRepositoryConfigurator();
56: repository = config.getJCRRepository(null);
57: ;
58:
59: Session testSession = repository
60: .login(new SimpleCredentials("alan_parsons",
61: "password".toCharArray()));
62:
63: RulesRepositoryAdministrator admin = new RulesRepositoryAdministrator(
64: testSession);
65: if (erase && admin.isRepositoryInitialized()) {
66:
67: admin.clearRulesRepository();
68: }
69: config.setupRulesRepository(testSession);
70: return testSession;
71: } else {
72: return repository.login(new SimpleCredentials(
73: "alan_parsons", "password".toCharArray()));
74: }
75:
76: }
77:
78: }
|