Clover icon

magnolia-module-groovy 2.4.7

  1. Project Clover database Thu Dec 1 2016 10:48:40 CET
  2. Package info.magnolia.module.groovy.console

File MgnlGroovyConsoleContext.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart1.png
48% of files have more coverage

Code metrics

4
23
9
4
144
88
13
0.57
2.56
2.25
1.44

Classes

Class Line # Actions
MgnlGroovyConsoleContext 64 18 0% 8 24
0.076923087.7%
MgnlGroovyConsoleContext.InternalHierarchyManagerWrapper 66 2 0% 2 4
0.00%
MgnlGroovyConsoleContext.InternalJCRSessionWrapper 78 2 0% 2 4
0.00%
MgnlGroovyConsoleContext.SerializableRepositoryStrategy 92 1 0% 1 2
0.00%
 

Contributing tests

This file is covered by 8 tests. .

Source view

1    /**
2    * This file Copyright (c) 2010-2016 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  0 toggle private InternalHierarchyManagerWrapper(HierarchyManager wrappedHM) {
69  0 super(wrappedHM);
70    }
71   
 
72  0 toggle @Override
73    protected Content wrap(Content content) {
74  0 return new MgnlGroovyNode(content);
75    }
76    }
77   
 
78    private static final class InternalJCRSessionWrapper extends
79    NodeWrappingDelegateSessionWrapper {
 
80  0 toggle private InternalJCRSessionWrapper(Session session) {
81  0 super(session);
82    }
83   
 
84  0 toggle @Override
85    protected Node wrapNode(Node node) {
86  0 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  0 toggle public SerializableRepositoryStrategy(UserContext context) {
96  0 super(Components.getComponent(RepositoryManager.class), context);
97    }
98    }
99   
100    private static final long serialVersionUID = 1L;
101   
 
102  8 toggle public MgnlGroovyConsoleContext(Context ctx) {
103  8 super(ctx);
104    }
105   
 
106  0 toggle @Override
107    public JCRSessionStrategy getRepositoryStrategy() {
108   
109  0 JCRSessionStrategy strategy = super.getRepositoryStrategy();
110  0 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  0 if (SystemContext.class.isAssignableFrom(ctx.getClass())) {
114  0 strategy = new SystemRepositoryStrategy(Components.getComponent(RepositoryManager.class));
115    } else {
116  0 strategy = new SerializableRepositoryStrategy((UserContext) ctx);
117    }
118  0 setRepositoryStrategy(strategy);
119    }
120  0 return strategy;
121    }
122   
 
123  0 toggle @Override
124    public HierarchyManager getHierarchyManager(String workspaceName) {
125  0 Session session;
126  0 try {
127  0 session = getRepositoryStrategy().getSession(workspaceName);
128    } catch (RepositoryException e) {
129  0 throw new RuntimeRepositoryException(e);
130    }
131  0 return new InternalHierarchyManagerWrapper(HierarchyManagerUtil.asHierarchyManager(session));
132    }
133   
 
134  0 toggle @Override
135    public Session getJCRSession(String workspaceName) throws LoginException, RepositoryException {
136  0 Session session;
137  0 try {
138  0 session = getRepositoryStrategy().getSession(workspaceName);
139    } catch (RepositoryException e) {
140  0 throw new RuntimeRepositoryException(e);
141    }
142  0 return new InternalJCRSessionWrapper(session);
143    }
144    }