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.Session;
25:
26: import org.apache.commons.logging.Log;
27: import org.apache.commons.logging.LogFactory;
28: import org.jbpm.JbpmContext;
29: import org.jbpm.JbpmException;
30: import org.jbpm.jcr.JcrService;
31: import org.jbpm.svc.Services;
32: import org.jbpm.tx.TxService;
33:
34: public class JcrServiceImpl implements JcrService {
35:
36: private static final long serialVersionUID = 1L;
37:
38: protected Session session = null;
39: Services services = null;
40:
41: public JcrServiceImpl(Session session) {
42: this .session = session;
43: this .services = JbpmContext.getCurrentJbpmContext()
44: .getServices();
45: }
46:
47: public Session getSession() {
48: return session;
49: }
50:
51: public void close() {
52: log.debug("closing jcr session");
53:
54: TxService txService = (services != null ? services
55: .getTxService() : null);
56: if (txService != null) {
57: if (txService.isRollbackOnly()) {
58: log
59: .debug("refreshing jcr session because tx service is marked with rollback-only");
60: try {
61: session.refresh(false);
62: } catch (Exception e) {
63: // NOTE that Error's are not caught because that might halt the JVM and mask the original Error.
64: throw new JbpmException(
65: "couldn't refresh(rollback) JCR session", e);
66: }
67: } else {
68: log
69: .debug("committing non-JTA JCR session by invoking the session.save()");
70: save();
71: }
72: } else {
73: save();
74: }
75:
76: try {
77: session.logout();
78: } catch (Exception e) {
79: // NOTE that Error's are not caught because that might halt the JVM and mask the original Error.
80: throw new JbpmException("couldn't save JCR session", e);
81: }
82: }
83:
84: private void save() {
85: log.debug("saving jcr session");
86: try {
87: session.save();
88: } catch (Exception e) {
89: // NOTE that Error's are not caught because that might halt the JVM and mask the original Error.
90: throw new JbpmException(
91: "couldn't save jackrabbit jcr session", e);
92: }
93: }
94:
95: private static Log log = LogFactory.getLog(JcrServiceImpl.class);
96: }
|