View Javadoc

1   /**
2    * This file Copyright (c) 2010-2012 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              //Node c = new MgnlGroovyJCRNode(node);
91              try {
92                  return new MgnlGroovyJCRNode(node);
93              } catch (RepositoryException e) {
94                  return null;
95              }
96          }
97  
98      }
99  
100     //need this to avoid warning when setting strategy as session attribute in context
101     private static final class SerializableRepositoryStrategy extends DefaultRepositoryStrategy implements Serializable {
102         private static final long serialVersionUID = 1L;
103 
104         public SerializableRepositoryStrategy(UserContext context) {
105             super(Components.getComponent(RepositoryManager.class), context);
106         }
107     }
108 
109     private static final long serialVersionUID = 1L;
110 
111     private static final String STRATEGY_ATTRIBUTE = MgnlGroovyConsole.class.getName() + ".strategy";
112 
113     public MgnlGroovyConsoleContext(Context ctx) {
114         super(ctx);
115     }
116 
117     @Override
118     public JCRSessionStrategy getRepositoryStrategy() {
119 
120         JCRSessionStrategy strategy = (JCRSessionStrategy) getAttribute(STRATEGY_ATTRIBUTE, Context.SESSION_SCOPE);
121         if (strategy == null) {
122             //this is only needed by the the MgnlGroovyRescueServlet (that is in exceptional cases, when you're in dire straits)
123             //as operations there are executed in SystemContext
124             if(SystemContext.class.isAssignableFrom(ctx.getClass())){
125                 strategy = new SystemRepositoryStrategy(Components.getComponent(RepositoryManager.class));
126             } else {
127                 strategy = new SerializableRepositoryStrategy((UserContext) ctx);
128             }
129             setAttribute(STRATEGY_ATTRIBUTE, strategy, Context.SESSION_SCOPE);
130         }
131         return strategy;
132     }
133 
134     @Override
135     public HierarchyManager getHierarchyManager(String workspaceName) {
136         Session session;
137         try {
138             session = getRepositoryStrategy().getSession(workspaceName);
139         } catch (RepositoryException e) {
140             throw new RuntimeRepositoryException(e);
141         }
142         return new InternalHierarchyManagerWrapper(HierarchyManagerUtil.asHierarchyManager(session));
143     }
144 
145     @Override
146     public Session getJCRSession(String workspaceName) throws LoginException, RepositoryException {
147         Session session;
148         try {
149             session = getRepositoryStrategy().getSession(workspaceName);
150         } catch (RepositoryException e) {
151             throw new RuntimeRepositoryException(e);
152         }
153         return new InternalJCRSessionWrapper(session);
154     }
155 }