View Javadoc

1   /**
2    * This file Copyright (c) 2010-2013 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  /**
63   * Custom context which returns a special version of {@link HierarchyManager} wrapping content as {@link MgnlGroovyNode}.
64   * @author pbaerfuss
65   * @version $Id$
66   *
67   */
68  public class MgnlGroovyConsoleContext extends ContextDecorator {
69  
70      private static final class InternalHierarchyManagerWrapper extends
71          HierarchyManagerWrapper {
72          private InternalHierarchyManagerWrapper(HierarchyManager wrappedHM) {
73              super(wrappedHM);
74          }
75  
76          @Override
77          protected Content wrap(Content content) {
78              return new MgnlGroovyNode(content);
79          }
80      }
81      
82      private static final class InternalJCRSessionWrapper extends
83      NodeWrappingDelegateSessionWrapper {
84          private InternalJCRSessionWrapper(Session session) {
85              super(session);
86          }
87      
88          @Override
89          protected Node wrapNode(Node node) {
90              try {
91                  return new MgnlGroovyJCRNode(node);
92              } catch (RepositoryException e) {
93                  return null;
94              }
95          }
96  
97      }
98  
99      //need this to avoid warning when setting strategy as session attribute in context
100     private static final class SerializableRepositoryStrategy extends DefaultRepositoryStrategy implements Serializable {
101         private static final long serialVersionUID = 1L;
102 
103         public SerializableRepositoryStrategy(UserContext context) {
104             super(Components.getComponent(RepositoryManager.class), context);
105         }
106     }
107 
108     private static final long serialVersionUID = 1L;
109 
110     private static final String STRATEGY_ATTRIBUTE = MgnlGroovyConsole.class.getName() + ".strategy";
111 
112     public MgnlGroovyConsoleContext(Context ctx) {
113         super(ctx);
114     }
115 
116     @Override
117     public JCRSessionStrategy getRepositoryStrategy() {
118 
119         JCRSessionStrategy strategy = (JCRSessionStrategy) getAttribute(STRATEGY_ATTRIBUTE, Context.SESSION_SCOPE);
120         if (strategy == null) {
121             //this is only needed by the the MgnlGroovyRescueServlet (that is in exceptional cases, when you're in dire straits)
122             //as operations there are executed in SystemContext
123             if(SystemContext.class.isAssignableFrom(ctx.getClass())){
124                 strategy = new SystemRepositoryStrategy(Components.getComponent(RepositoryManager.class));
125             } else {
126                 strategy = new SerializableRepositoryStrategy((UserContext) ctx);
127             }
128             setAttribute(STRATEGY_ATTRIBUTE, strategy, Context.SESSION_SCOPE);
129         }
130         return strategy;
131     }
132 
133     @Override
134     public HierarchyManager getHierarchyManager(String workspaceName) {
135         Session session;
136         try {
137             session = getRepositoryStrategy().getSession(workspaceName);
138         } catch (RepositoryException e) {
139             throw new RuntimeRepositoryException(e);
140         }
141         return new InternalHierarchyManagerWrapper(HierarchyManagerUtil.asHierarchyManager(session));
142     }
143 
144     @Override
145     public Session getJCRSession(String workspaceName) throws LoginException, RepositoryException {
146         Session session;
147         try {
148             session = getRepositoryStrategy().getSession(workspaceName);
149         } catch (RepositoryException e) {
150             throw new RuntimeRepositoryException(e);
151         }
152         return new InternalJCRSessionWrapper(session);
153     }
154 }