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 static info.magnolia.module.groovy.GroovyModule.SCRIPTS_REPOSITORY_NAME; |
37 |
|
|
38 |
|
import info.magnolia.context.Context; |
39 |
|
import info.magnolia.context.SystemContext; |
40 |
|
import info.magnolia.jcr.RuntimeRepositoryException; |
41 |
|
import info.magnolia.module.groovy.support.HierarchyManagerProvider; |
42 |
|
import info.magnolia.objectfactory.Components; |
43 |
|
|
44 |
|
import java.io.IOException; |
45 |
|
import java.net.URL; |
46 |
|
import java.net.URLConnection; |
47 |
|
import java.security.CodeSource; |
48 |
|
import java.util.List; |
49 |
|
|
50 |
|
import javax.inject.Provider; |
51 |
|
import javax.jcr.RepositoryException; |
52 |
|
import javax.jcr.Session; |
53 |
|
|
54 |
|
import org.apache.commons.lang.StringUtils; |
55 |
|
import org.codehaus.groovy.ast.ASTNode; |
56 |
|
import org.codehaus.groovy.ast.ClassNode; |
57 |
|
import org.codehaus.groovy.ast.builder.AstBuilder; |
58 |
|
import org.codehaus.groovy.control.CompilationFailedException; |
59 |
|
import org.codehaus.groovy.control.CompilationUnit; |
60 |
|
import org.codehaus.groovy.control.CompilationUnit.SourceUnitOperation; |
61 |
|
import org.codehaus.groovy.control.CompilePhase; |
62 |
|
import org.codehaus.groovy.control.CompilerConfiguration; |
63 |
|
import org.codehaus.groovy.control.Phases; |
64 |
|
import org.codehaus.groovy.control.SourceUnit; |
65 |
|
import org.codehaus.groovy.control.io.URLReaderSource; |
66 |
|
import org.slf4j.Logger; |
67 |
|
import org.slf4j.LoggerFactory; |
68 |
|
|
69 |
|
import groovy.lang.GroovyClassLoader; |
70 |
|
|
71 |
|
|
72 |
|
@link@link |
73 |
|
|
74 |
|
@see |
75 |
|
@see |
76 |
|
@see |
77 |
|
|
|
|
| 86% |
Uncovered Elements: 8 (57) |
Complexity: 16 |
Complexity Density: 0.43 |
|
78 |
|
public final class MgnlGroovyClassLoader extends GroovyClassLoader { |
79 |
|
private static final Logger log = LoggerFactory.getLogger(MgnlGroovyClassLoader.class); |
80 |
|
|
81 |
|
private final Provider<Context> provider; |
82 |
|
|
83 |
|
private boolean compileTimeChecks; |
84 |
|
private String path; |
85 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
86 |
31 |
public MgnlGroovyClassLoader() {... |
87 |
31 |
this.provider = new Provider<Context>() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
88 |
162 |
@Override... |
89 |
|
public Context get() { |
90 |
162 |
return Components.getComponent(SystemContext.class); |
91 |
|
} |
92 |
|
}; |
93 |
31 |
this.setShouldRecompile(true); |
94 |
31 |
this.setResourceLoader(new MgnlGroovyResourceLoader(super.getResourceLoader(), provider, SCRIPTS_REPOSITORY_NAME)); |
95 |
|
} |
96 |
|
|
97 |
|
|
98 |
|
@deprecated@link |
99 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
100 |
0 |
@Deprecated... |
101 |
|
public MgnlGroovyClassLoader(Context context) { |
102 |
0 |
this(); |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
|
@deprecated@link |
107 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
108 |
0 |
@Deprecated... |
109 |
|
public MgnlGroovyClassLoader(HierarchyManagerProvider hmp) { |
110 |
0 |
this(); |
111 |
|
} |
112 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
113 |
42 |
@Override... |
114 |
|
protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource codeSource) { |
115 |
42 |
CompilationUnit cu = super.createCompilationUnit(config, codeSource); |
116 |
42 |
if (compileTimeChecks) { |
117 |
14 |
log.debug("enforcing compile-time checks..."); |
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
14 |
try { |
125 |
14 |
cu.addPhaseOperation(new PackageAndClassNameConsistencyOperation(provider.get().getJCRSession(SCRIPTS_REPOSITORY_NAME), path), Phases.CONVERSION); |
126 |
|
} catch (RepositoryException e) { |
127 |
0 |
throw new RuntimeRepositoryException(e); |
128 |
|
} |
129 |
|
} |
130 |
|
|
131 |
42 |
cu.addPhaseOperation(new AddDefaultImportOperation(), Phases.CONVERSION); |
132 |
42 |
return cu; |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 3 |
Complexity Density: 0.3 |
|
143 |
14 |
@Override... |
144 |
|
protected boolean isSourceNewer(URL source, Class cls) throws IOException { |
145 |
14 |
long lastMod; |
146 |
14 |
if ("file".equals(source.getProtocol())) { |
147 |
4 |
return true; |
148 |
|
} else { |
149 |
10 |
URLConnection conn = source.openConnection(); |
150 |
10 |
lastMod = conn.getLastModified(); |
151 |
10 |
conn.getInputStream().close(); |
152 |
|
} |
153 |
10 |
boolean isNewer = lastMod > getTimeStamp(cls); |
154 |
10 |
if (isNewer) { |
155 |
4 |
log.debug("{} source has changed", cls.getName()); |
156 |
|
} |
157 |
10 |
return isNewer; |
158 |
|
} |
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
@link |
164 |
|
@link |
165 |
|
|
166 |
|
@param |
167 |
|
@param |
168 |
|
@param |
169 |
|
|
170 |
|
|
171 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
172 |
17 |
public final void verify(final String source, final boolean enforceCompileChecks, final String path) throws CompilationFailedException {... |
173 |
17 |
this.compileTimeChecks = enforceCompileChecks; |
174 |
17 |
this.path = path; |
175 |
17 |
if (compileTimeChecks && path == null) { |
176 |
0 |
throw new IllegalArgumentException("When compilation checks are enforced, mgnlPath cannot be null"); |
177 |
|
} |
178 |
17 |
parseClass(source); |
179 |
|
|
180 |
|
} |
181 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.14 |
|
182 |
|
private static final class AddDefaultImportOperation extends SourceUnitOperation { |
183 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
184 |
36 |
@Override... |
185 |
|
public void call(SourceUnit source) throws CompilationFailedException { |
186 |
|
|
187 |
|
|
188 |
36 |
log.debug("adding default imports..."); |
189 |
36 |
source.getAST().addStarImport("info.magnolia.context."); |
190 |
36 |
source.getAST().addStarImport("info.magnolia.cms.core."); |
191 |
36 |
source.getAST().addStarImport("info.magnolia.cms.util."); |
192 |
36 |
source.getAST().addStarImport("info.magnolia.jcr.util."); |
193 |
36 |
source.getAST().addStarImport("info.magnolia.module.groovy.support."); |
194 |
36 |
source.getAST().addStarImport("info.magnolia.module.groovy.xml."); |
195 |
|
} |
196 |
|
} |
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
|
|
|
| 93.8% |
Uncovered Elements: 4 (64) |
Complexity: 14 |
Complexity Density: 0.35 |
|
208 |
|
private static final class PackageAndClassNameConsistencyOperation extends SourceUnitOperation { |
209 |
|
private static final String COMPILATION_ERROR_PARENT_NODE_UNMATCHED = "%s compilation failed: class package '%s' does not match parent node '%s' path in the %s repository"; |
210 |
|
private static final String COMPILATION_ERROR_EXISTING_PATH_UNMATCHED = "%s compilation failed: class package '%s' does not match an existing '%s' path in the %s repository"; |
211 |
|
private static final String COMPILATION_ERROR_CLASSNAME_AND_PACKAGE = "%s compilation failed: %s should declare at least one class named %s %s"; |
212 |
|
private static final String COMPILATION_ERROR_CLASSNAME_UNDER_ROOT_WITH_PACKAGE = "%s compilation failed: your class is under root but declares a package %s"; |
213 |
|
private static final String COMPILATION_ERROR_NO_PACKAGE = "%s compilation failed: you must specify a package for your class."; |
214 |
|
|
215 |
|
private final Session session; |
216 |
|
private final String mgnlPath; |
217 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
218 |
14 |
public PackageAndClassNameConsistencyOperation(Session session, String mgnlPath) {... |
219 |
14 |
this.session = session; |
220 |
14 |
this.mgnlPath = mgnlPath; |
221 |
|
} |
222 |
|
|
|
|
| 93.3% |
Uncovered Elements: 4 (60) |
Complexity: 13 |
Complexity Density: 0.34 |
|
223 |
14 |
@Override... |
224 |
|
public void call(SourceUnit source) throws CompilationFailedException { |
225 |
14 |
final String parentPath = StringUtils.defaultIfEmpty(StringUtils.substringBeforeLast(mgnlPath, "/"), "/"); |
226 |
14 |
final String scriptName = StringUtils.substringAfterLast(mgnlPath, "/"); |
227 |
14 |
final boolean isParentRoot = "/".equals(parentPath); |
228 |
14 |
final String packageName = source.getAST().hasPackageName() ? source.getAST().getPackageName() : ""; |
229 |
14 |
final String packageNameAsPath = "/" + packageName.replace('.', '/'); |
230 |
|
|
231 |
14 |
log.debug("parent path is {}, script name is {}, isParentRoot? {}", new Object[]{parentPath, scriptName, isParentRoot}); |
232 |
|
|
233 |
14 |
if (!isParentRoot) { |
234 |
11 |
if (StringUtils.isBlank(packageName)) { |
235 |
1 |
String msg = String.format(COMPILATION_ERROR_NO_PACKAGE, scriptName); |
236 |
1 |
log.warn(msg); |
237 |
1 |
throw new CompilationFailedException(source.getPhase(), source, new Throwable(msg)); |
238 |
|
} |
239 |
|
|
240 |
10 |
if (!packageNameAsPath.equals(parentPath + "/")) { |
241 |
1 |
final String msg = String.format(COMPILATION_ERROR_PARENT_NODE_UNMATCHED, scriptName, packageName, parentPath, SCRIPTS_REPOSITORY_NAME); |
242 |
1 |
log.warn(msg); |
243 |
1 |
throw new CompilationFailedException(source.getPhase(), source, new Throwable(msg)); |
244 |
|
} |
245 |
|
|
246 |
9 |
try { |
247 |
9 |
if (!session.nodeExists(packageNameAsPath)) { |
248 |
2 |
String msg = String.format(COMPILATION_ERROR_EXISTING_PATH_UNMATCHED, scriptName, packageName, packageNameAsPath, SCRIPTS_REPOSITORY_NAME); |
249 |
2 |
log.warn(msg); |
250 |
2 |
throw new CompilationFailedException(source.getPhase(), source, new Throwable(msg)); |
251 |
|
} |
252 |
|
} catch (RepositoryException e) { |
253 |
0 |
throw new RuntimeRepositoryException(e); |
254 |
|
} |
255 |
|
} else { |
256 |
3 |
if (StringUtils.isNotBlank(packageName)) { |
257 |
1 |
String msg = String.format(COMPILATION_ERROR_CLASSNAME_UNDER_ROOT_WITH_PACKAGE, scriptName, packageName); |
258 |
1 |
log.warn(msg); |
259 |
1 |
throw new CompilationFailedException(source.getPhase(), source, new Throwable(msg)); |
260 |
|
} |
261 |
|
} |
262 |
|
|
263 |
9 |
if (source.getSource() instanceof URLReaderSource) { |
264 |
0 |
return; |
265 |
|
} |
266 |
|
|
267 |
|
|
268 |
9 |
boolean match = false; |
269 |
9 |
final String fullyQualifiedClassName = StringUtils.isNotBlank(packageName) ? packageName + scriptName : scriptName; |
270 |
9 |
for (ClassNode cn : source.getAST().getClasses()) { |
271 |
10 |
if (fullyQualifiedClassName.equals(cn.getName())) { |
272 |
8 |
log.debug("found a matching class name {}, we can proceed", fullyQualifiedClassName); |
273 |
8 |
match = true; |
274 |
8 |
break; |
275 |
|
} |
276 |
|
} |
277 |
9 |
if (!match) { |
278 |
1 |
String msg = String.format(COMPILATION_ERROR_CLASSNAME_AND_PACKAGE, scriptName, fullyQualifiedClassName, scriptName, (StringUtils.isNotBlank(packageName) ? "and a package named " + packageName : "")); |
279 |
1 |
log.warn(msg); |
280 |
1 |
throw new CompilationFailedException(source.getPhase(), source, new Throwable(msg)); |
281 |
|
} |
282 |
|
} |
283 |
|
} |
284 |
|
|
285 |
|
|
286 |
|
|
287 |
|
|
|
|
| 91.7% |
Uncovered Elements: 1 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
288 |
8 |
public static final String resolveScriptNameFromSource(final String source) throws RepositoryException {... |
289 |
8 |
final List<ASTNode> nodes = new AstBuilder().buildFromString(CompilePhase.CONVERSION, source); |
290 |
8 |
for (ASTNode astNode : nodes) { |
291 |
15 |
if (astNode instanceof ClassNode) { |
292 |
7 |
ClassNode cn = (ClassNode) astNode; |
293 |
7 |
String name = cn.getNameWithoutPackage(); |
294 |
7 |
if (!name.startsWith("script")) { |
295 |
7 |
return name; |
296 |
|
} |
297 |
|
} |
298 |
|
} |
299 |
1 |
return "untitledGroovyScript"; |
300 |
|
} |
301 |
|
} |