01: /*
02: * JBoss, Home of Professional Open Source
03: * Copyright 2005, JBoss Inc., and individual contributors as indicated
04: * by the @authors tag. See the copyright.txt in the distribution for a
05: * full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jbpm.jcr.impl;
23:
24: import javax.jcr.Credentials;
25: import javax.jcr.Repository;
26: import javax.jcr.Session;
27: import javax.jcr.SimpleCredentials;
28:
29: import org.apache.commons.logging.Log;
30: import org.apache.commons.logging.LogFactory;
31: import org.jbpm.JbpmException;
32: import org.jbpm.svc.Service;
33: import org.jbpm.svc.ServiceFactory;
34:
35: public abstract class AbstractJcrServiceFactory implements
36: ServiceFactory {
37:
38: private static final long serialVersionUID = 1L;
39:
40: String username = null;
41: String password = null;
42: String workspace = null;
43:
44: public Service openService() {
45: Session session = null;
46: try {
47: Repository jcrRepository = getRepository();
48: log.debug("opening jcr session "
49: + (username != null ? username : "") + " "
50: + (workspace != null ? workspace : ""));
51: if ((username == null) && (workspace == null)) {
52: session = jcrRepository.login();
53: } else if (username == null) {
54: session = jcrRepository.login(workspace);
55: } else if (workspace == null) {
56: session = jcrRepository.login(getCredentials());
57: } else {
58: session = jcrRepository.login(getCredentials(),
59: workspace);
60: }
61: } catch (Exception e) {
62: // NOTE that Error's are not caught because that might halt the JVM and mask the original Error.
63: throw new JbpmException(
64: "couldn't open session to JCR repository, userId("
65: + username + "), workspace(" + workspace
66: + ")", e);
67: }
68: return new JcrServiceImpl(session);
69: }
70:
71: Credentials getCredentials() {
72: char[] pwdChars = (password != null ? password.toCharArray()
73: : null);
74: Credentials credentials = new SimpleCredentials(username,
75: pwdChars);
76: return credentials;
77: }
78:
79: protected abstract Repository getRepository();
80:
81: public void close() {
82: }
83:
84: private static Log log = LogFactory
85: .getLog(AbstractJcrServiceFactory.class);
86: }
|