View Javadoc
1   /**
2    * This file Copyright (c) 2010-2017 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.module.groovy.console;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.HierarchyManager;
38  import info.magnolia.cms.util.HierarchyManagerUtil;
39  import info.magnolia.cms.util.HierarchyManagerWrapper;
40  import info.magnolia.context.Context;
41  import info.magnolia.context.ContextDecorator;
42  import info.magnolia.context.DefaultRepositoryStrategy;
43  import info.magnolia.context.JCRSessionStrategy;
44  import info.magnolia.context.SystemContext;
45  import info.magnolia.context.SystemRepositoryStrategy;
46  import info.magnolia.context.UserContext;
47  import info.magnolia.jcr.RuntimeRepositoryException;
48  import info.magnolia.jcr.wrapper.NodeWrappingDelegateSessionWrapper;
49  import info.magnolia.module.groovy.support.nodes.MgnlGroovyJCRNode;
50  import info.magnolia.module.groovy.support.nodes.MgnlGroovyNode;
51  import info.magnolia.objectfactory.Components;
52  import info.magnolia.repository.RepositoryManager;
53  
54  import java.io.Serializable;
55  
56  import javax.jcr.LoginException;
57  import javax.jcr.Node;
58  import javax.jcr.RepositoryException;
59  import javax.jcr.Session;
60  
61  /**
62   * Custom context which returns a special version of {@link HierarchyManager} wrapping content as {@link MgnlGroovyNode}.
63   */
64  public class MgnlGroovyConsoleContext extends ContextDecorator {
65  
66      private static final class InternalHierarchyManagerWrapper extends
67              HierarchyManagerWrapper {
68          private InternalHierarchyManagerWrapper(HierarchyManager wrappedHM) {
69              super(wrappedHM);
70          }
71  
72          @Override
73          protected Content wrap(Content content) {
74              return new MgnlGroovyNode(content);
75          }
76      }
77  
78      private static final class InternalJCRSessionWrapper extends
79              NodeWrappingDelegateSessionWrapper {
80          private InternalJCRSessionWrapper(Session session) {
81              super(session);
82          }
83  
84          @Override
85          protected Node wrapNode(Node node) {
86              return new MgnlGroovyJCRNode(node);
87          }
88  
89      }
90  
91      // need this to avoid warning when setting strategy as session attribute in context
92      private static final class SerializableRepositoryStrategy extends DefaultRepositoryStrategy implements Serializable {
93          private static final long serialVersionUID = 1L;
94  
95          public SerializableRepositoryStrategy(UserContext context) {
96              super(Components.getComponent(RepositoryManager.class), context);
97          }
98      }
99  
100     private static final long serialVersionUID = 1L;
101 
102     public MgnlGroovyConsoleContext(Context ctx) {
103         super(ctx);
104     }
105 
106     @Override
107     public JCRSessionStrategy getRepositoryStrategy() {
108 
109         JCRSessionStrategy strategy = super.getRepositoryStrategy();
110         if (strategy == null) {
111             // this is only needed by the the MgnlGroovyRescueServlet (that is in exceptional cases, when you're in dire straits)
112             // as operations there are executed in SystemContext
113             if (SystemContext.class.isAssignableFrom(ctx.getClass())) {
114                 strategy = new SystemRepositoryStrategy(Components.getComponent(RepositoryManager.class));
115             } else {
116                 strategy = new SerializableRepositoryStrategy((UserContext) ctx);
117             }
118             setRepositoryStrategy(strategy);
119         }
120         return strategy;
121     }
122 
123     @Override
124     public HierarchyManager getHierarchyManager(String workspaceName) {
125         Session session;
126         try {
127             session = getRepositoryStrategy().getSession(workspaceName);
128         } catch (RepositoryException e) {
129             throw new RuntimeRepositoryException(e);
130         }
131         return new InternalHierarchyManagerWrapper(HierarchyManagerUtil.asHierarchyManager(session));
132     }
133 
134     @Override
135     public Session getJCRSession(String workspaceName) throws LoginException, RepositoryException {
136         Session session;
137         try {
138             session = getRepositoryStrategy().getSession(workspaceName);
139         } catch (RepositoryException e) {
140             throw new RuntimeRepositoryException(e);
141         }
142         return new InternalJCRSessionWrapper(session);
143     }
144 }