View Javadoc
1   /**
2    * This file Copyright (c) 2010-2017 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 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.objectfactory.Components;
42  
43  import java.io.IOException;
44  import java.net.URL;
45  import java.net.URLConnection;
46  import java.security.CodeSource;
47  import java.util.List;
48  
49  import javax.inject.Provider;
50  import javax.jcr.RepositoryException;
51  import javax.jcr.Session;
52  
53  import org.apache.commons.lang3.StringUtils;
54  import org.codehaus.groovy.ast.ASTNode;
55  import org.codehaus.groovy.ast.ClassNode;
56  import org.codehaus.groovy.ast.builder.AstBuilder;
57  import org.codehaus.groovy.control.CompilationFailedException;
58  import org.codehaus.groovy.control.CompilationUnit;
59  import org.codehaus.groovy.control.CompilationUnit.SourceUnitOperation;
60  import org.codehaus.groovy.control.CompilePhase;
61  import org.codehaus.groovy.control.CompilerConfiguration;
62  import org.codehaus.groovy.control.Phases;
63  import org.codehaus.groovy.control.SourceUnit;
64  import org.codehaus.groovy.control.io.URLReaderSource;
65  import org.slf4j.Logger;
66  import org.slf4j.LoggerFactory;
67  
68  import groovy.lang.GroovyClassLoader;
69  
70  /**
71   * Magnolia class loader which extends {@link GroovyClassLoader} and internally uses {@link MgnlGroovyResourceLoader}.
72   *
73   * @see MgnlGroovyClassLoader#isSourceNewer(URL, Class)
74   * @see MgnlGroovyClassLoader.AddDefaultImportOperation
75   * @see MgnlGroovyClassLoader.PackageAndClassNameConsistencyOperation
76   */
77  public final class MgnlGroovyClassLoader extends GroovyClassLoader {
78      private static final Logger log = LoggerFactory.getLogger(MgnlGroovyClassLoader.class);
79  
80      private final Provider<Context> provider;
81  
82      private boolean compileTimeChecks;
83      private String path;
84  
85      public MgnlGroovyClassLoader() {
86          this.provider = new Provider<Context>() {
87              @Override
88              public Context get() {
89                  return Components.getComponent(SystemContext.class);
90              }
91          };
92          this.setShouldRecompile(true);
93          this.setResourceLoader(new MgnlGroovyResourceLoader(super.getResourceLoader(), provider, SCRIPTS_REPOSITORY_NAME));
94      }
95  
96      /**
97       * @deprecated since 2.4.4 please use {@link #MgnlGroovyClassLoader()} instead.
98       */
99      @Deprecated
100     public MgnlGroovyClassLoader(Context context) {
101         this();
102     }
103 
104     @Override
105     protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource codeSource) {
106         CompilationUnit cu = super.createCompilationUnit(config, codeSource);
107         if (compileTimeChecks) {
108             log.debug("enforcing compile-time checks...");
109             /*
110              * "Generally speaking, there is more type information available later in the phases. If your transformation is concerned with
111              * reading the AST, then a later phase where information is more plentiful might be a good choice. If your transformation is
112              * concerned with writing AST, then an earlier phase where the tree is more sparse might be more convenient."
113              * see http://groovy.codehaus.org/Compiler+Phase+Guide
114              */
115             try {
116                 cu.addPhaseOperation(new PackageAndClassNameConsistencyOperation(provider.get().getJCRSession(SCRIPTS_REPOSITORY_NAME), path), Phases.CONVERSION);
117             } catch (RepositoryException e) {
118                 throw new RuntimeRepositoryException(e);
119             }
120         }
121 
122         cu.addPhaseOperation(new AddDefaultImportOperation(), Phases.CONVERSION);
123         return cu;
124     }
125 
126     /**
127      * <em>Source newer</em> in our case means that the groovy source
128      * representing a certain class in the <em>scripts</em> repository is more recent than that of
129      * the corresponding class currently loaded in this classloader (i.e. it has been saved more recently).
130      * Please note that if source protocol is <em>file</em>, then <strong>recompilation is forced</strong>
131      * otherwise the caller gets the old class in the current classloader which might be the compiled script
132      * from the scripts repository if the latter (the script, that is) was just disabled.
133      */
134     @Override
135     protected boolean isSourceNewer(URL source, Class cls) throws IOException {
136         long lastMod;
137         if ("file".equals(source.getProtocol())) {
138             return true;
139         } else {
140             URLConnection conn = source.openConnection();
141             lastMod = conn.getLastModified();
142             conn.getInputStream().close();
143         }
144         boolean isNewer = lastMod > getTimeStamp(cls);
145         if (isNewer) {
146             log.debug("{} source has changed", cls.getName());
147         }
148         return isNewer;
149     }
150 
151     /**
152      * Checks that the given source compiles correctly and, in case of a script which has to act as a
153      * class, that some consistency constraints imposed by our classloading mechanism are enforced. Throws a
154      * {@link CompilationFailedException} in case of compilation failure.
155      * For the applied constraints, see {@link MgnlGroovyClassLoader.PackageAndClassNameConsistencyOperation}.
156      *
157      * @param source String the Groovy source
158      * @param enforceCompileChecks boolean if <code>true</code> enforce additional compile time checks
159      * @param path String the path to the script in the repository. The substring after the last '/' is assumed to be
160      * the script name itself. Will be ignored if <em>enforceCompileChecks</em> is <code>false</code>. Cannot be
161      * <code>null</code> if <em>enforceCompileChecks</em> is <code>true</code>.
162      */
163     public final void verify(final String source, final boolean enforceCompileChecks, final String path) throws CompilationFailedException {
164         this.compileTimeChecks = enforceCompileChecks;
165         this.path = path;
166         if (compileTimeChecks && path == null) {
167             throw new IllegalArgumentException("When compilation checks are enforced, mgnlPath cannot be null");
168         }
169         parseClass(source);
170 
171     }
172 
173     private static final class AddDefaultImportOperation extends SourceUnitOperation {
174 
175         @Override
176         public void call(SourceUnit source) throws CompilationFailedException {
177             // yes, to import all the classes of a certain package you have to omit the * at the end
178             // see http://jira.codehaus.org/browse/GROOVY-3041
179             log.debug("adding default imports...");
180             source.getAST().addStarImport("info.magnolia.context.");
181             source.getAST().addStarImport("info.magnolia.cms.core.");
182             source.getAST().addStarImport("info.magnolia.cms.util.");
183             source.getAST().addStarImport("info.magnolia.jcr.util.");
184             source.getAST().addStarImport("info.magnolia.module.groovy.support.");
185             source.getAST().addStarImport("info.magnolia.module.groovy.xml.");
186         }
187     }
188 
189     /**
190      * Checks package and class name consistency on source code during Groovy's
191      * compilation phase. The checks performed are
192      * <ol>
193      * <li>Ensure that the class declares a package, unless it is being saved at root level of the <code>scripts</code>
194      * repository</li>
195      * <li>Ensure that the package matches the path to the source in Magnolia's scripts repository</li>
196      * <li>Ensure that the source contains at least one class named as the script itself.</li>
197      * <li>Ensure that the source doesn't mix and match class declarations and scripting, as it can lead to duplicate class definition error</li>
198      * </ol>
199      */
200     private static final class PackageAndClassNameConsistencyOperation extends SourceUnitOperation {
201         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";
202         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";
203         private static final String COMPILATION_ERROR_CLASSNAME_AND_PACKAGE = "%s compilation failed: %s should declare at least one class named %s %s";
204         private static final String COMPILATION_ERROR_CLASSNAME_UNDER_ROOT_WITH_PACKAGE = "%s compilation failed: your class is under root but declares a package %s";
205         private static final String COMPILATION_ERROR_NO_PACKAGE = "%s compilation failed: you must specify a package for your class.";
206         private static final String COMPILATION_ERROR_CLASS_AND_SCRIPT_MIXED = "%s compilation failed: you cannot mix classes declarations and scripts here. " +
207                 "Please, either remove the script part or set this source as a script in the UI";
208 
209         private final Session session;
210         private final String mgnlPath;
211 
212         public PackageAndClassNameConsistencyOperation(Session session, String mgnlPath) {
213             this.session = session;
214             this.mgnlPath = mgnlPath;
215         }
216 
217         @Override
218         public void call(SourceUnit source) throws CompilationFailedException {
219             final String parentPath = StringUtils.defaultIfEmpty(StringUtils.substringBeforeLast(mgnlPath, "/"), "/");
220             final String scriptName = StringUtils.substringAfterLast(mgnlPath, "/");
221             final boolean isParentRoot = "/".equals(parentPath);
222             final String packageName = source.getAST().hasPackageName() ? source.getAST().getPackageName() : "";
223             final String packageNameAsPath = "/" + packageName.replace('.', '/');
224 
225             log.debug("parent path is {}, script name is {}, isParentRoot? {}", new Object[]{parentPath, scriptName, isParentRoot});
226 
227             if (!source.getAST().getStatementBlock().isEmpty()) {
228                 final String msg = String.format(COMPILATION_ERROR_CLASS_AND_SCRIPT_MIXED, scriptName);
229                 throw new CompilationFailedException(source.getPhase(), source, new Throwable(msg));
230             }
231 
232             if (isParentRoot) {
233                 if (StringUtils.isNotBlank(packageName)) {
234                     final String msg = String.format(COMPILATION_ERROR_CLASSNAME_UNDER_ROOT_WITH_PACKAGE, scriptName, packageName);
235                     throw new CompilationFailedException(source.getPhase(), source, new Throwable(msg));
236                 }
237             } else {
238                 if (StringUtils.isBlank(packageName)) {
239                     String msg = String.format(COMPILATION_ERROR_NO_PACKAGE, scriptName);
240                     throw new CompilationFailedException(source.getPhase(), source, new Throwable(msg));
241                 }
242 
243                 if (!packageNameAsPath.equals(parentPath + "/")) {
244                     final String msg = String.format(COMPILATION_ERROR_PARENT_NODE_UNMATCHED, scriptName, packageName, parentPath, SCRIPTS_REPOSITORY_NAME);
245                     throw new CompilationFailedException(source.getPhase(), source, new Throwable(msg));
246                 }
247 
248                 try {
249                     if (!session.nodeExists(packageNameAsPath)) {
250                         final String msg = String.format(COMPILATION_ERROR_EXISTING_PATH_UNMATCHED, scriptName, packageName, packageNameAsPath, SCRIPTS_REPOSITORY_NAME);
251                         throw new CompilationFailedException(source.getPhase(), source, new Throwable(msg));
252                     }
253                 } catch (RepositoryException e) {
254                     throw new RuntimeRepositoryException(e);
255                 }
256             }
257             // we don't need to check that class A used in class B (included via URLReaderSource) declare class A in current script
258             if (source.getSource() instanceof URLReaderSource) {
259                 return;
260             }
261 
262             // now checking that at least one class declared in the source matches the source file name
263             boolean match = false;
264             final String fullyQualifiedClassName = StringUtils.isNotBlank(packageName) ? packageName + scriptName : scriptName;
265             for (ClassNode cn : source.getAST().getClasses()) {
266                 if (fullyQualifiedClassName.equals(cn.getName())) {
267                     log.debug("found a matching class name {}, we can proceed", fullyQualifiedClassName);
268                     match = true;
269                     break;
270                 }
271             }
272             if (!match) {
273                 final String msg = String.format(COMPILATION_ERROR_CLASSNAME_AND_PACKAGE, scriptName, fullyQualifiedClassName, scriptName, (StringUtils.isNotBlank(packageName) ? "and a package named " + packageName : ""));
274                 throw new CompilationFailedException(source.getPhase(), source, new Throwable(msg));
275             }
276         }
277     }
278 
279     /**
280      * Utility method to resolve the script or class name from source.
281      */
282     public static final String resolveScriptNameFromSource(final String source) throws RepositoryException {
283         final List<ASTNode> nodes = new AstBuilder().buildFromString(CompilePhase.CONVERSION, source);
284         for (ASTNode astNode : nodes) {
285             if (astNode instanceof ClassNode) {
286                 ClassNode cn = (ClassNode) astNode;
287                 String name = cn.getNameWithoutPackage();
288                 if (!name.startsWith("script")) {
289                     return name;
290                 }
291             }
292         }
293         return "untitledGroovyScript";
294     }
295 }