View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.context;
35  
36  import java.util.HashMap;
37  import java.util.Map;
38  import javax.inject.Inject;
39  import javax.jcr.LoginException;
40  import javax.jcr.RepositoryException;
41  import javax.jcr.Session;
42  import javax.jcr.observation.EventListener;
43  import javax.jcr.observation.EventListenerIterator;
44  import javax.jcr.observation.ObservationManager;
45  
46  import org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  
49  import info.magnolia.repository.RepositoryManager;
50  
51  
52  /**
53   * Basic implementation of the <code>RepositoryAcquiringStrategy</code> providing storage of JCR sessions and hierarchy manager instances to extending classes.
54   *
55   * @version $Id$
56   */
57  public abstract class AbstractRepositoryStrategy implements JCRSessionStrategy {
58      private static final Logger log = LoggerFactory.getLogger(AbstractRepositoryStrategy.class);
59  
60      private final Map<String, Session> jcrSessions = new HashMap<String, Session>();
61  
62      protected final RepositoryManager repositoryManager;
63  
64      @Inject
65      protected AbstractRepositoryStrategy(RepositoryManager repositoryManager) {
66          this.repositoryManager = repositoryManager;
67      }
68  
69      @Override
70      public Session getSession(String workspaceName) throws LoginException, RepositoryException {
71          Session jcrSession = jcrSessions.get(workspaceName);
72  
73          if (jcrSession == null) {
74              log.debug("creating jcr session {} by thread {}", workspaceName, Thread.currentThread().getName());
75  
76              jcrSession = internalGetSession(workspaceName);
77              jcrSessions.put(workspaceName, jcrSession);
78          }
79  
80          return jcrSession;
81      }
82  
83      protected abstract Session internalGetSession(String workspaceName) throws RepositoryException;
84  
85      protected void release(boolean checkObservation) {
86          log.debug("releasing jcr sessions");
87          for (Session session : jcrSessions.values()) {
88              releaseSession(session, checkObservation);
89          }
90          jcrSessions.clear();
91      }
92  
93      protected void releaseSession(final Session session, boolean checkObservation) {
94          final String workspaceName = session.getWorkspace().getName();
95          if (session.isLive()) {
96              try {
97                  final ObservationManager observationManager = session.getWorkspace().getObservationManager();
98                  final EventListenerIterator listeners = observationManager.getRegisteredEventListeners();
99                  if (!checkObservation || !listeners.hasNext()) {
100                     session.logout();
101                     log.debug("logged out jcr session: {} by thread {}", session, Thread.currentThread().getName());
102 
103                 } else {
104                     log.warn("won't close session because of registered observation listener {}", workspaceName);
105                     if (log.isDebugEnabled()) {
106                         while (listeners.hasNext()) {
107                             EventListener listener = listeners.nextEventListener();
108                             log.debug("registered listener {}", listener);
109                         }
110                     }
111                 }
112             }
113             catch (RepositoryException e) {
114                 log.error("can't check if event listeners are registered", e);
115             }
116         } else {
117             log.warn("session has been already closed {}", workspaceName);
118         }
119     }
120 
121     /**
122      * Returns the number of sessions managed by this strategy.
123      */
124     protected int getLocalSessionCount() {
125         return jcrSessions.size();
126     }
127 }