View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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 info.magnolia.cms.core.HierarchyManager;
37  import info.magnolia.cms.security.AccessManager;
38  import info.magnolia.cms.security.Permission;
39  import info.magnolia.cms.security.PermissionImpl;
40  import info.magnolia.cms.security.SystemUserManager;
41  import info.magnolia.cms.util.UrlPattern;
42  import info.magnolia.cms.util.WorkspaceAccessUtil;
43  
44  import java.util.ArrayList;
45  import java.util.HashMap;
46  import java.util.List;
47  import java.util.Map;
48  
49  import javax.jcr.RepositoryException;
50  import javax.jcr.UnsupportedRepositoryOperationException;
51  import javax.jcr.observation.Event;
52  import javax.jcr.observation.EventIterator;
53  import javax.jcr.observation.EventListener;
54  
55  import org.slf4j.Logger;
56  import org.slf4j.LoggerFactory;
57  
58  /**
59   * Uses a single full access AccessManager. JCR sessions are only released if no event listener were registered.
60   */
61  public class SystemRepositoryStrategy extends AbstractRepositoryStrategy {
62  
63      private static final Logger log = LoggerFactory.getLogger(SystemRepositoryStrategy.class);
64  
65      private AccessManager accessManager;
66    
67      private Map<String, EventListener> observedHMs = new HashMap<String, EventListener>();
68   
69  
70      public SystemRepositoryStrategy(SystemContext context) {
71      }
72  
73      public AccessManager getAccessManager(String repositoryId, String workspaceId) {
74          if (accessManager == null) {
75              accessManager = WorkspaceAccessUtil.getInstance().createAccessManager(getSystemPermissions(), repositoryId, workspaceId);
76          }
77  
78          return accessManager;
79      }
80  
81      protected List<Permission> getSystemPermissions() {
82          List<Permission> acl = new ArrayList<Permission>();
83          UrlPattern p = UrlPattern.MATCH_ALL;
84          Permission permission = new PermissionImpl();
85          permission.setPattern(p);
86          permission.setPermissions(Permission.ALL);
87          acl.add(permission);
88          return acl;
89      }
90  
91      protected String getUserId() {
92          return SystemUserManager.SYSTEM_USER;
93      }
94  
95      public void release() {
96          if (!observedHMs.isEmpty()) {
97              for (Map.Entry<String, EventListener> entry : observedHMs.entrySet()) {
98                  final String[] key = entry.getKey().split("_");
99                  final HierarchyManager hm = super.getHierarchyManager(key[0], key[1]);
100                 try {
101                     hm.getWorkspace().getObservationManager().removeEventListener(entry.getValue());
102                 } catch (UnsupportedRepositoryOperationException e) {
103                     log.error("Failed to remove listener from short living session. Session doesn't support listener removal.");
104                 } catch (RepositoryException e) {
105                     log.error("Failed to remove listener from short living session. Will not be able to release this session.");
106                 }
107             }
108             // release reference to listener so it can be GCed
109             observedHMs.clear();
110         }
111         super.release(true);
112     }
113 
114     @Override
115     public HierarchyManager getHierarchyManager(String repositoryId, String workspaceId) {
116         final HierarchyManager hm = super.getHierarchyManager(repositoryId, workspaceId);
117         final String key = repositoryId + "_" + workspaceId;
118         if (!observedHMs.keySet().contains(key)) {
119             final EventListener listener = new EventListener() {
120 
121                 public void onEvent(EventIterator iterator) {
122                     // reload everything
123                     try {
124                         hm.refresh(true);
125                     } catch (RepositoryException e) {
126                         log.error("Failed to refresh short living session after update. Session will not be able to see content changes if repository uses update-on-read strategy.");
127                     }
128                 }
129             };
130             try {
131                 hm.getWorkspace().getObservationManager().addEventListener(listener, Event.NODE_ADDED | Event.NODE_REMOVED | Event.PROPERTY_ADDED | Event.PROPERTY_CHANGED | Event.PROPERTY_REMOVED, "/", true, null, null, false);
132                 observedHMs.put(key, listener);
133             } catch (UnsupportedRepositoryOperationException e) {
134                 log.warn("Repository doesn't support observation. Observers will not be notified of changes in repository.");
135             } catch (RepositoryException e) {
136                 log.error("Failed to register observer for repository updates.");
137             }
138             //ObservationUtil.registerChangeListener(hm.getName(), "/", listener );
139         }
140         return hm;
141     }
142 }