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.support.classes

File MgnlGroovyResourceLoader.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
20% of files have more coverage

Code metrics

10
40
13
3
219
122
24
0.6
3.08
4.33
1.85

Classes

Class Line # Actions
MgnlGroovyResourceLoader 66 22 0% 12 8
0.771428677.1%
MgnlGroovyResourceLoader.MagnoliaStreamHandler 138 5 0% 4 4
0.660%
MgnlGroovyResourceLoader.MagnoliaURLConnection 165 13 0% 8 6
0.666666766.7%
 

Contributing tests

This file is covered by 26 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.support.classes;
35   
36    import info.magnolia.cms.core.HierarchyManager;
37    import info.magnolia.context.Context;
38    import info.magnolia.jcr.util.NodeTypes;
39    import info.magnolia.jcr.util.PropertyUtil;
40    import info.magnolia.module.groovy.support.HierarchyManagerProvider;
41   
42    import java.io.IOException;
43    import java.io.InputStream;
44    import java.net.MalformedURLException;
45    import java.net.URL;
46    import java.net.URLConnection;
47    import java.net.URLStreamHandler;
48    import java.util.Calendar;
49   
50    import javax.inject.Provider;
51    import javax.jcr.Node;
52    import javax.jcr.RepositoryException;
53    import javax.jcr.Session;
54   
55    import org.slf4j.Logger;
56    import org.slf4j.LoggerFactory;
57   
58    import groovy.lang.GroovyResourceLoader;
59   
60    /**
61    * A {@link GroovyResourceLoader} implementation which is able to load Groovy source from a given repository.
62    * <strong>Warning:</strong> this is based on URLs objects. It uses a custom "protocol", but this is only
63    * supported in as far as the URL object instance is passed around. Using the URL's string representation
64    * outside the context of this class will invariably fail, unless {@link info.magnolia.module.groovy.support.classes.MgnlGroovyResourceLoader.MagnoliaStreamHandler} is passed too.
65    */
 
66    class MgnlGroovyResourceLoader implements GroovyResourceLoader {
67   
68    private static final Logger log = LoggerFactory.getLogger(MgnlGroovyResourceLoader.class);
69    private static final String PROTOCOL = "magnolia-repository://";
70   
71    private final GroovyResourceLoader delegate;
72    private final Provider<Context> provider;
73    private final String workspaceName;
74   
 
75  31 toggle MgnlGroovyResourceLoader(GroovyResourceLoader delegate, Provider<Context> provider, String workspaceName) {
76  31 this.delegate = delegate;
77  31 this.provider = provider;
78  31 this.workspaceName = workspaceName;
79    }
80   
81    /**
82    * @deprecated since 2.4.4 please use {@link #MgnlGroovyResourceLoader(GroovyResourceLoader, Provider, String)} instead.
83    */
 
84  0 toggle @Deprecated
85    MgnlGroovyResourceLoader(GroovyResourceLoader delegate, Context context, String repoName) throws UnsupportedOperationException {
86  0 throw new UnsupportedOperationException();
87    }
88   
89    /**
90    * @deprecated since 2.4.3 please use {@link #MgnlGroovyResourceLoader(GroovyResourceLoader, Provider, String)} instead.
91    */
 
92  0 toggle @Deprecated
93    MgnlGroovyResourceLoader(GroovyResourceLoader delegate, HierarchyManagerProvider hmp, String repoName) throws UnsupportedOperationException {
94  0 throw new UnsupportedOperationException();
95    }
96   
 
97  148 toggle @Override
98    public URL loadGroovySource(String name) throws MalformedURLException {
99  148 final URL url = loadGroovySourceFromRepository(name);
100  148 if (url == null) {
101  132 return delegate.loadGroovySource(name);
102    }
103  16 return url;
104    }
105   
106    /**
107    * Returns a URL <em>only if</em> the path to the source exits <strong>AND</strong> is <strong>NOT</strong> deleted <strong>AND</strong> the enabled flag is <code>true</code>.
108    * In all other cases it <strong>must</strong> return <code>null</code>
109    */
 
110  148 toggle private URL loadGroovySourceFromRepository(String name) throws MalformedURLException {
111  148 if (name.startsWith("[") || name.contains("$")) {
112    // we don't even try loading array classes or nested classes.
113    // see java.lang.Class#forName()
114  0 log.debug("Skipping {}, array or nested class.", name);
115  0 return null;
116    }
117   
118  148 final String path = "/" + name.replace('.', '/');
119  148 try {
120  148 final Session session = provider.get().getJCRSession(workspaceName);
121  148 if (!session.nodeExists(path)) {
122  128 return null;
123    }
124  20 final Node node = session.getNode(path);
125  20 if (NodeTypes.Deleted.getDeleted(node) == null && PropertyUtil.getBoolean(node, "enabled", false)) {
126  16 return new URL(null, PROTOCOL + session.getWorkspace().getName() + path, new MagnoliaStreamHandler(session));
127    } else {
128  4 return null;
129    }
130    } catch (RepositoryException e) {
131  0 return null;
132    }
133    }
134   
135    /**
136    * Magnolia Stream Handler.
137    */
 
138    protected static class MagnoliaStreamHandler extends URLStreamHandler {
139    private final Session session;
140   
141    /**
142    * @deprecated since 2.4.3 please use {@link #MagnoliaStreamHandler(Session)} instead.
143    */
 
144  0 toggle @Deprecated
145    public MagnoliaStreamHandler(HierarchyManager hm) {
146  0 this(hm.getWorkspace().getSession());
147    }
148   
 
149  16 toggle public MagnoliaStreamHandler(Session session) {
150  16 this.session = session;
151    }
152   
 
153  20 toggle @Override
154    protected URLConnection openConnection(URL u) throws IOException {
155  20 if (!"magnolia-repository".equals(u.getProtocol())) {
156  0 throw new IllegalStateException("Unsupported protocol: " + u.getProtocol() + " (only supports \"magnolia-repository\")");
157    }
158  20 return new MagnoliaURLConnection(u, session);
159    }
160    }
161   
162    /**
163    * Magnolia URL Connection.
164    */
 
165    protected static class MagnoliaURLConnection extends URLConnection {
166    private final Node node;
167   
168    /**
169    * @deprecated since 2.4.3 please use {@link #MagnoliaURLConnection(URL, Session)} instead.
170    */
 
171  0 toggle @Deprecated
172    public MagnoliaURLConnection(URL u, HierarchyManager hm) throws IOException {
173  0 this(u, hm.getWorkspace().getSession());
174    }
175   
 
176  20 toggle public MagnoliaURLConnection(URL u, Session session) throws IOException {
177  20 super(u);
178    // final String repository = u.getHost();
179  20 final String path = u.getPath();
180   
181  20 try {
182  20 this.node = session.getNode(path);
183    } catch (RepositoryException e) {
184  0 throw new IOException("Can't get " + path + " from the " + session.getWorkspace().getName() + " workspace: " + e.getClass().getSimpleName() + ": " + e.getMessage());
185    }
186    }
187   
 
188  0 toggle @Override
189    public void connect() throws IOException {
190    // Nothing to do here.
191    // I'm assuming this could be needed for connections that can timeout, and/or when the URL object is
192    // used long after it's been instantiated. In our specific case, we know for a fact that it's used
193    // immediately, and not kept around, which is why we fetch the node from the repository straight from
194    // the constructor of this class.
195    }
196   
 
197  20 toggle @Override
198    public InputStream getInputStream() throws IOException {
199    // TODO - double check "enabled" flag ?
200    // TODO - doesn't work with MockNodeData: return prop.getStream();
201  20 try {
202  20 return node.getProperty("text").getBinary().getStream();
203    } catch (RepositoryException e) {
204  0 throw new IOException(e);
205    }
206    }
207   
 
208  10 toggle @Override
209    public long getLastModified() {
210  10 try {
211  10 Calendar date = NodeTypes.LastModified.getLastModified(node);
212  10 return date.getTimeInMillis();
213    } catch (RepositoryException e) {
214  0 return 0;
215    }
216   
217    }
218    }
219    }