Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2010 eXo Platform SAS. This is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this software; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 
 
 package org.exoplatform.services.jcr.ext.script.groovy;
 
 
 import  org.picocontainer.Startable;
 
 import java.net.URL;
 import java.util.List;
 import java.util.Set;

JcrGroovyCompiler can load source code of groovy script from JCR and parse it via GroovyClassLoader.

Author(s):
Andrey Parfonov
Version:
$Id$
 
 public class JcrGroovyCompiler implements Startable
 {
   
Logger.
 
    private static final Log LOG = ExoLogger.getLogger(JcrGroovyCompiler.class);
 
 
 
    public JcrGroovyCompiler(JcrGroovyClassLoaderProvider classLoaderProvider)
    {
       this. = classLoaderProvider;
    }
 
    public JcrGroovyCompiler()
    {
       this(new JcrGroovyClassLoaderProvider());
    }

   
Compile Groovy source that located in sourceReferences. Compiled sources can be dependent to each other and dependent to Groovy sources that are accessible for this compiler.

Parameters:
sourceReferences references to Groovy sources to be compiled
Returns:
result of compilation
Throws:
IOException if any i/o errors occurs
 
    public Class<?>[] compile(UnifiedNodeReference... sourceReferencesthrows IOException
    {
       return compile(nullsourceReferences);
    }

   
Compile Groovy source that located in sourceReferences. Compiled sources can be dependent to each other and dependent to Groovy sources that are accessible for this compiler and with additional Groovy sources src. NOTE To be able load Groovy source files from specified folders the following rules must be observed:
  • Groovy source files must be located in folder with respect to package structure
  • Name of Groovy source files must be the same as name of class located in file
  • Groovy source file must have extension '.groovy'

Example: If source stream that we want compile contains the following code:
           package c.b.a
           
           import a.b.c.A
           
           class B extends A {
           // Do something.
           }
 
Assume we store dependencies in JCR then URL of folder with Groovy sources may be like this: jcr://repository/workspace#/groovy-library. Then absolute path to JCR node that contains Groovy source must be as following: /groovy-library/a/b/c/A.groovy

Parameters:
src additional Groovy source location that should be added in class-path when compile sourceReferences
sourceReferences references to Groovy sources to be compiled
Returns:
result of compilation
Throws:
IOException if any i/o errors occurs
   public Class<?>[] compile(SourceFolder[] srcUnifiedNodeReference... sourceReferencesthrows IOException
   {
      SourceFile[] files = new SourceFile[sourceReferences.length];
      for (int i = 0; i < sourceReferences.lengthi++)
         files[i] = new SourceFile(sourceReferences[i].getURL());
   }

   
Compile Groovy source that located in files. Compiled sources can be dependent to each other and dependent to Groovy sources that are accessible for this compiler and with additional Groovy sources src. NOTE To be able load Groovy source files from specified folders the following rules must be observed:
  • Groovy source files must be located in folder with respect to package structure
  • Name of Groovy source files must be the same as name of class located in file
  • Groovy source file must have extension '.groovy'

Parameters:
src additional Groovy source location that should be added in class-path when compile files
files Groovy sources to be compiled
Returns:
result of compilation
Throws:
IOException if any i/o errors occurs
   public Class<?>[] compile(SourceFolder[] srcSourceFile[] filesthrows IOException
   {
   }

   
Compile Groovy source that located in files. Compiled sources can be dependent to each other and dependent to Groovy sources that are accessible for this compiler.

Parameters:
files Groovy sources to be compiled
Returns:
result of compilation
Throws:
IOException if any i/o errors occurs
   public Class<?>[] compile(SourceFile[] filesthrows IOException
   {
   }
   @SuppressWarnings("rawtypes")
   private Class<?>[] doCompile(final JcrGroovyClassLoader clfinal SourceFile[] filesthrows IOException
   {
      Class[] classes = SecurityHelper.doPrivilegedAction(new PrivilegedAction<Class[]>() {
         public Class[] run()
         {
            return cl.parseClasses(files);
         }
      });
      return classes;
   }

   
Get URLs of classes (stored in JCR only) required to compile sources files. Result array includes URLs of files plus URLs of other required sources if they can be found in class-path.

Parameters:
sources additional Groovy source location that should be added in class-path when analyze files
files set of sources for analyzing
Returns:
URLs
Throws:
IOException if any i/o errors occurs
   public URL[] getDependencies(final SourceFolder[] sourcesfinal SourceFile[] filesthrows IOException
   {
      try
      {
         return SecurityHelper.doPrivilegedExceptionAction(new PrivilegedExceptionAction<URL[]>() {
            public URL[] run() throws IOException
            {
               return ((JcrGroovyClassLoader).getGroovyClassLoader()).findDependencies(sources,
                  files);
            }
         });
      }
      catch (PrivilegedActionException e)
      {
         Throwable cause = e.getCause();
         throw (IOException)cause;
      }
   }

   

Returns:
get underling groovy class loader
   {
   }

   
Set groovy class loader.

Parameters:
gcl groovy class loader
Throws:
NullPointerException if gcl == null
   public void setGroovyClassLoader(GroovyClassLoader gcl)
   {
      .warn("Method setGroovyClassLoader is deprecated.");
   }

   
Create GroovyCodeSource from given stream and name. Code base 'file:/groovy/script' (default code base used for all Groovy classes) will be used.

Parameters:
in groovy source code stream
name code source name
Returns:
GroovyCodeSource
   // Override this method if need other behavior.
   protected GroovyCodeSource createCodeSource(final InputStream infinal String name)
   {
      GroovyCodeSource gcs = SecurityHelper.doPrivilegedAction(new PrivilegedAction<GroovyCodeSource>() {
         public GroovyCodeSource run()
         {
            return new GroovyCodeSource(inname"/groovy/script");
         }
      });
      gcs.setCachable(false);
      return gcs;
   }

   

See also:
org.picocontainer.Startable.start()
   public void start()
   {
      if ( != null && .size() > 0)
      {
         try
         {
            Set<URLrepos = new HashSet<URL>();
            for (GroovyScriptAddRepoPlugin pl : )
               repos.addAll(pl.getRepositories());
               new JcrGroovyResourceLoader(repos.toArray(new URL[repos.size()])));
         }
         catch (MalformedURLException e)
         {
            .error("Unable add groovy script repository. "e);
         }
      }
   }

   

See also:
org.picocontainer.Startable.stop()
   public void stop()
   {
   }
New to GrepCode? Check out our FAQ X