1 16 package org.apache.cocoon.components.language.programming; 17 18 import org.apache.avalon.framework.context.Context; 19 import org.apache.avalon.framework.context.ContextException; 20 import org.apache.avalon.framework.context.Contextualizable; 21 import org.apache.avalon.framework.parameters.Parameters; 22 import org.apache.avalon.framework.parameters.ParameterException; 23 24 import org.apache.cocoon.Constants; 25 import org.apache.cocoon.components.language.LanguageException; 26 import org.apache.cocoon.components.language.programming.java.JavaProgram; 27 import org.apache.cocoon.util.ClassUtils; 28 import org.apache.cocoon.util.IOUtils; 29 30 import java.io.File ; 31 32 38 public abstract class CompiledProgrammingLanguage extends AbstractProgrammingLanguage implements Contextualizable { 39 40 41 protected Class compilerClass; 42 43 44 protected String classpath; 45 46 47 protected boolean deleteSources = false; 48 49 54 public void parameterize(Parameters params) throws ParameterException { 55 super.parameterize(params); 56 57 String compilerClass = params.getParameter("compiler"); 58 try { 59 this.compilerClass = ClassUtils.loadClass(compilerClass); 60 } catch (ClassNotFoundException e) { 61 throw new ParameterException("Unable to load compiler: " + compilerClass, e); 62 } 63 this.deleteSources = params.getParameterAsBoolean("delete-sources", false); 64 } 65 66 70 public void contextualize(Context context) throws ContextException { 71 this.classpath = (String ) context.get(Constants.CONTEXT_CLASSPATH); 72 } 73 74 78 public abstract String getObjectExtension(); 79 80 85 public abstract void doUnload(Object program) throws LanguageException; 86 87 94 protected final void doUnload(Object program, String filename, File baseDirectory) throws LanguageException { 95 int index = filename.lastIndexOf(File.separator); 96 String dir = filename.substring(0, index); 97 String file = filename.substring(index + 1); 98 99 File baseDir = new File (baseDirectory, dir); 100 File [] files = baseDir.listFiles(); 101 102 for (int i = 0;(files != null) && (i < files.length); i++) { 103 if (files[i].getName().startsWith(file)) { 104 files[i].delete(); 105 } 106 } 107 this.doUnload(program); 108 } 109 110 117 protected abstract Class loadProgram(String filename, File baseDirectory) throws LanguageException; 118 119 126 protected abstract void compile(String filename, File baseDirectory, String encoding) throws LanguageException; 127 128 138 public Program preload(String filename, File baseDirectory, String encoding) throws LanguageException { 139 try { 141 Class program = this.loadProgram(filename, baseDirectory); 142 program.newInstance(); 144 return new JavaProgram(program); 145 } catch (Throwable t) { 146 throw new LanguageException("Unable to preload program " + filename, t); 147 } 148 } 149 150 160 public Program load(String filename, File baseDirectory, String encoding) throws LanguageException { 161 162 File sourceFile = new File (baseDirectory, filename + "." + this.getSourceExtension()); 164 if (!sourceFile.exists()) { 165 throw new LanguageException("Can't load program - File doesn't exist: " + IOUtils.getFullFilename(sourceFile)); 166 } 167 if (!sourceFile.isFile()) { 168 throw new LanguageException("Can't load program - File is not a normal file: " + IOUtils.getFullFilename(sourceFile)); 169 } 170 if (!sourceFile.canRead()) { 171 throw new LanguageException("Can't load program - File cannot be read: " + IOUtils.getFullFilename(sourceFile)); 172 } 173 this.compile(filename, baseDirectory, encoding); 174 if (this.deleteSources) { 175 sourceFile.delete(); 176 } 177 Class program = this.loadProgram(filename, baseDirectory); 178 179 try { 181 program.newInstance(); 183 } catch(IllegalAccessException iae) { 184 getLogger().debug("No public constructor for class " + program.getName()); 185 } catch(Exception e) { 186 this.doUnload(program); 189 new File (baseDirectory, filename + "." + this.getObjectExtension()).delete(); 190 191 String message = "Error while instantiating " + filename; 192 getLogger().debug(message, e); 193 throw new LanguageException(message, e); 194 } 195 196 if (program == null) { 197 throw new LanguageException("Can't load program : " + baseDirectory.toString() + File.separator + filename); 198 } 199 200 return new JavaProgram(program); 201 } 202 } 203 | Popular Tags |