View Javadoc

1   /**
2    * This file Copyright (c) 2010-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.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.exception.RuntimeRepositoryException;
48  import info.magnolia.module.groovy.support.nodes.MgnlGroovyNode;
49  import info.magnolia.objectfactory.Components;
50  import info.magnolia.repository.RepositoryManager;
51  
52  import java.io.Serializable;
53  
54  import javax.jcr.RepositoryException;
55  import javax.jcr.Session;
56  
57  
58  /**
59   * Custom context which returns a special version of {@link HierarchyManager} wrapping content as {@link MgnlGroovyNode}.
60   * @author pbaerfuss
61   * @version $Id$
62   *
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      //need this to avoid warning when setting strategy as session attribute in context
78      private static final class SerializableRepositoryStrategy extends DefaultRepositoryStrategy implements Serializable {
79          private static final long serialVersionUID = 1L;
80  
81          public SerializableRepositoryStrategy(UserContext context) {
82              super(Components.getComponent(RepositoryManager.class), context);
83          }
84      }
85  
86      private static final long serialVersionUID = 1L;
87  
88      private static final String STRATEGY_ATTRIBUTE = MgnlGroovyConsole.class.getName() + ".strategy";
89  
90      public MgnlGroovyConsoleContext(Context ctx) {
91          super(ctx);
92      }
93  
94      @Override
95      public JCRSessionStrategy getRepositoryStrategy() {
96  
97          JCRSessionStrategy strategy = (JCRSessionStrategy) getAttribute(STRATEGY_ATTRIBUTE, Context.SESSION_SCOPE);
98          if (strategy == null) {
99              //this is only needed by the the MgnlGroovyRescueServlet (that is in exceptional cases, when you're in dire straits)
100             //as operations there are executed in SystemContext
101             if(SystemContext.class.isAssignableFrom(ctx.getClass())){
102                 strategy = new SystemRepositoryStrategy(Components.getComponent(RepositoryManager.class));
103             } else {
104                 strategy = new SerializableRepositoryStrategy((UserContext) ctx);
105             }
106             setAttribute(STRATEGY_ATTRIBUTE, strategy, Context.SESSION_SCOPE);
107         }
108         return strategy;
109     }
110 
111     @Override
112     public HierarchyManager getHierarchyManager(String workspaceName) {
113         Session session;
114         try {
115             session = getRepositoryStrategy().getSession(workspaceName);
116         } catch (RepositoryException e) {
117             throw new RuntimeRepositoryException(e);
118         }
119         return new InternalHierarchyManagerWrapper(HierarchyManagerUtil.asHierarchyManager(session));
120     }
121 }