1 16 package org.apache.cocoon.components.language.programming.java; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.util.List ; 21 import java.util.StringTokenizer ; 22 23 import org.apache.avalon.framework.activity.Disposable; 24 import org.apache.avalon.framework.activity.Initializable; 25 import org.apache.avalon.framework.logger.LogEnabled; 26 import org.apache.avalon.framework.parameters.ParameterException; 27 import org.apache.avalon.framework.parameters.Parameters; 28 import org.apache.avalon.framework.service.ServiceException; 29 import org.apache.avalon.framework.service.ServiceManager; 30 import org.apache.avalon.framework.service.Serviceable; 31 import org.apache.avalon.framework.thread.ThreadSafe; 32 33 import org.apache.cocoon.components.classloader.ClassLoaderManager; 34 import org.apache.cocoon.components.language.LanguageException; 35 import org.apache.cocoon.components.language.markup.xsp.XSLTExtension; 36 import org.apache.cocoon.components.language.programming.CompiledProgrammingLanguage; 37 import org.apache.cocoon.components.language.programming.CompilerError; 38 import org.apache.cocoon.components.language.programming.LanguageCompiler; 39 import org.apache.cocoon.util.ClassUtils; 40 import org.apache.cocoon.util.JavaArchiveFilter; 41 import org.apache.commons.lang.SystemUtils; 42 43 49 public class JavaLanguage extends CompiledProgrammingLanguage 50 implements Initializable, ThreadSafe, Serviceable, Disposable { 51 52 53 private ClassLoaderManager classLoaderManager; 54 55 56 protected ServiceManager manager = null; 57 58 59 private String classpath; 60 61 62 private String classLoaderClass; 63 64 65 private int compilerComplianceLevel; 66 67 72 public String getSourceExtension() { 73 return "java"; 74 } 75 76 81 public String getObjectExtension() { 82 return "class"; 83 } 84 85 93 public void parameterize(Parameters params) throws ParameterException { 94 super.parameterize(params); 95 96 97 this.classLoaderClass = params.getParameter("class-loader", null); 98 if (this.classLoaderClass != null) { 99 try { 100 this.classLoaderManager = (ClassLoaderManager) 101 ClassUtils.newInstance(this.classLoaderClass); 102 } catch (Exception e) { 103 throw new ParameterException("Unable to load class loader: " 104 + this.classLoaderClass, e); 105 } 106 } else { 107 try { 108 getLogger().debug("Looking up " + ClassLoaderManager.ROLE); 109 this.classLoaderManager = (ClassLoaderManager) 110 manager.lookup(ClassLoaderManager.ROLE); 111 } catch (ServiceException e) { 112 throw new ParameterException("Lookup of ClassLoaderManager failed", e); 113 } 114 } 115 String sourceVer = params.getParameter("compiler-compliance-level", "auto"); 117 if (sourceVer.equalsIgnoreCase("auto")) { 118 this.compilerComplianceLevel = SystemUtils.JAVA_VERSION_INT; 119 } else { 120 try { 121 compilerComplianceLevel = new Float (Float.parseFloat(sourceVer) * 100).intValue(); 122 } catch (NumberFormatException e) { 123 throw new ParameterException("XSP: compiler-compliance-level parameter value not valid!", e); 124 } 125 } 126 } 127 128 133 public void service(ServiceManager manager) throws ServiceException { 134 this.manager = manager; 135 } 136 137 public void initialize() throws Exception { 138 139 String systemBootClasspath = System.getProperty("sun.boot.class.path"); 141 String systemClasspath = SystemUtils.JAVA_CLASS_PATH; 142 String systemExtDirs = SystemUtils.JAVA_EXT_DIRS; 143 String systemExtClasspath = null; 144 145 try { 146 systemExtClasspath = expandDirs(systemExtDirs); 147 } catch (Exception e) { 148 getLogger().warn("Could not expand Directory:" + systemExtDirs, e); 149 } 150 151 this.classpath = 152 ((super.classpath != null) ? File.pathSeparator + super.classpath : "") + 153 ((systemBootClasspath != null) ? File.pathSeparator + systemBootClasspath : "") + 154 ((systemClasspath != null) ? File.pathSeparator + systemClasspath : "") + 155 ((systemExtClasspath != null) ? File.pathSeparator + systemExtClasspath : ""); 156 } 157 158 166 protected Class loadProgram(String name, File baseDirectory) 167 throws LanguageException { 168 try { 169 this.classLoaderManager.addDirectory(baseDirectory); 170 return this.classLoaderManager.loadClass(name.replace(File.separatorChar, '.')); 171 } catch (Exception e) { 172 throw new LanguageException("Could not load class for program '" + name + "' due to a " + e.getClass().getName() + ": " + e.getMessage()); 173 } 174 } 175 176 185 protected void compile(String name, File baseDirectory, String encoding) 186 throws LanguageException { 187 188 try { 189 LanguageCompiler compiler = (LanguageCompiler)this.compilerClass.newInstance(); 190 if (compiler instanceof LogEnabled) { 192 ((LogEnabled)compiler).enableLogging(getLogger()); 193 } 194 if (compiler instanceof Serviceable) { 195 ((Serviceable)compiler).service(this.manager); 196 } 197 198 int pos = name.lastIndexOf(File.separatorChar); 199 String filename = name.substring(pos + 1); 200 201 final String basePath = baseDirectory.getCanonicalPath(); 202 String filepath = basePath + File.separator + name + "." + getSourceExtension(); 203 204 compiler.setFile(filepath); 205 compiler.setSource(basePath); 206 compiler.setDestination(basePath); 207 compiler.setClasspath(basePath + this.classpath); 208 compiler.setCompilerComplianceLevel(compilerComplianceLevel); 209 210 if (encoding != null) { 211 compiler.setEncoding(encoding); 212 } 213 214 if (getLogger().isDebugEnabled()) { 215 getLogger().debug("Compiling " + filepath); 216 } 217 if (!compiler.compile()) { 218 StringBuffer message = new StringBuffer ("Error compiling "); 219 message.append(filename); 220 message.append(":\n"); 221 222 List errors = compiler.getErrors(); 223 CompilerError[] compilerErrors = new CompilerError[errors.size()]; 224 errors.toArray(compilerErrors); 225 226 throw new LanguageException(message.toString(), filepath, compilerErrors); 227 } 228 229 } catch (InstantiationException e) { 230 getLogger().warn("Could not instantiate the compiler", e); 231 throw new LanguageException("Could not instantiate the compiler: " + e.getMessage()); 232 } catch (IllegalAccessException e) { 233 getLogger().warn("Could not access the compiler class", e); 234 throw new LanguageException("Could not access the compiler class: " + e.getMessage()); 235 } catch (IOException e) { 236 getLogger().warn("Error during compilation", e); 237 throw new LanguageException("Error during compilation: " + e.getMessage()); 238 } catch (ServiceException e) { 239 getLogger().warn("Could not initialize the compiler", e); 240 throw new LanguageException("Could not initialize the compiler: " + e.getMessage()); 241 } 242 } 243 244 252 public void doUnload(Object program) throws LanguageException { 253 this.classLoaderManager.reinstantiate(); 254 } 255 256 263 public String quoteString(String constant) { 264 return XSLTExtension.escapeJavaString(constant); 265 } 266 267 278 private String expandDirs(String dirPaths) { 279 StringTokenizer st = new StringTokenizer (dirPaths, File.pathSeparator); 280 StringBuffer buffer = new StringBuffer (); 281 while (st.hasMoreTokens()) { 282 String d = st.nextToken(); 283 File dir = new File (d); 284 if (!dir.isDirectory()) { 285 if (getLogger().isWarnEnabled()) { 287 getLogger().warn("Attempted to retrieve directory listing of non-directory " + dir.toString()); 288 } 289 } else { 290 File [] files = dir.listFiles(new JavaArchiveFilter()); 291 for (int i = 0; i < files.length; i++) { 292 buffer.append(files[i]).append(File.pathSeparator); 293 } 294 } 295 } 296 return buffer.toString(); 297 } 298 299 302 public void dispose() { 303 if (this.classLoaderClass == null && this.classLoaderManager != null) { 304 manager.release(this.classLoaderManager); 305 this.classLoaderManager = null; 306 } 307 } 308 } 309 | Popular Tags |