1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
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 |
|
@link |
62 |
|
|
63 |
|
|
64 |
|
@link |
65 |
|
|
|
|
| 77.1% |
Uncovered Elements: 8 (35) |
Complexity: 12 |
Complexity Density: 0.55 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
75 |
31 |
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@link |
83 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
84 |
0 |
@Deprecated... |
85 |
|
MgnlGroovyResourceLoader(GroovyResourceLoader delegate, Context context, String repoName) throws UnsupportedOperationException { |
86 |
0 |
throw new UnsupportedOperationException(); |
87 |
|
} |
88 |
|
|
89 |
|
|
90 |
|
@deprecated@link |
91 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
92 |
0 |
@Deprecated... |
93 |
|
MgnlGroovyResourceLoader(GroovyResourceLoader delegate, HierarchyManagerProvider hmp, String repoName) throws UnsupportedOperationException { |
94 |
0 |
throw new UnsupportedOperationException(); |
95 |
|
} |
96 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
97 |
148 |
@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 |
|
|
108 |
|
|
109 |
|
|
|
|
| 78.9% |
Uncovered Elements: 4 (19) |
Complexity: 7 |
Complexity Density: 0.54 |
|
110 |
148 |
private URL loadGroovySourceFromRepository(String name) throws MalformedURLException {... |
111 |
148 |
if (name.startsWith("[") || name.contains("$")) { |
112 |
|
|
113 |
|
|
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 |
|
|
137 |
|
|
|
|
| 60% |
Uncovered Elements: 4 (10) |
Complexity: 4 |
Complexity Density: 0.8 |
|
138 |
|
protected static class MagnoliaStreamHandler extends URLStreamHandler { |
139 |
|
private final Session session; |
140 |
|
|
141 |
|
|
142 |
|
@deprecated@link |
143 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
144 |
0 |
@Deprecated... |
145 |
|
public MagnoliaStreamHandler(HierarchyManager hm) { |
146 |
0 |
this(hm.getWorkspace().getSession()); |
147 |
|
} |
148 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
149 |
16 |
public MagnoliaStreamHandler(Session session) {... |
150 |
16 |
this.session = session; |
151 |
|
} |
152 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
153 |
20 |
@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 |
|
|
164 |
|
|
|
|
| 66.7% |
Uncovered Elements: 6 (18) |
Complexity: 8 |
Complexity Density: 0.62 |
|
165 |
|
protected static class MagnoliaURLConnection extends URLConnection { |
166 |
|
private final Node node; |
167 |
|
|
168 |
|
|
169 |
|
@deprecated@link |
170 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
171 |
0 |
@Deprecated... |
172 |
|
public MagnoliaURLConnection(URL u, HierarchyManager hm) throws IOException { |
173 |
0 |
this(u, hm.getWorkspace().getSession()); |
174 |
|
} |
175 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
176 |
20 |
public MagnoliaURLConnection(URL u, Session session) throws IOException {... |
177 |
20 |
super(u); |
178 |
|
|
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 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
188 |
0 |
@Override... |
189 |
|
public void connect() throws IOException { |
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
} |
196 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
197 |
20 |
@Override... |
198 |
|
public InputStream getInputStream() throws IOException { |
199 |
|
|
200 |
|
|
201 |
20 |
try { |
202 |
20 |
return node.getProperty("text").getBinary().getStream(); |
203 |
|
} catch (RepositoryException e) { |
204 |
0 |
throw new IOException(e); |
205 |
|
} |
206 |
|
} |
207 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
208 |
10 |
@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 |
|
} |