KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > language > programming > CompiledProgrammingLanguage


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
31
32 /**
33  * A compiled programming language. This class extends <code>AbstractProgrammingLanguage</code> adding support for compilation
34  * and object program files
35  * @author <a HREF="mailto:ricardo@apache.org">Ricardo Rocha</a>
36  * @version CVS $Id: CompiledProgrammingLanguage.java 30932 2004-07-29 17:35:38Z vgritsenko $
37  */

38 public abstract class CompiledProgrammingLanguage extends AbstractProgrammingLanguage implements Contextualizable {
39
40     /** The compiler */
41     protected Class JavaDoc compilerClass;
42
43     /** The local classpath */
44     protected String JavaDoc classpath;
45
46     /** The source deletion option */
47     protected boolean deleteSources = false;
48
49     /**
50      * Set the configuration parameters. This method instantiates the sitemap-specified language compiler
51      * @param params The configuration parameters
52      * @exception ParameterException If the language compiler cannot be loaded
53      */

54     public void parameterize(Parameters params) throws ParameterException {
55         super.parameterize(params);
56
57         String JavaDoc compilerClass = params.getParameter("compiler");
58         try {
59             this.compilerClass = ClassUtils.loadClass(compilerClass);
60         } catch (ClassNotFoundException JavaDoc e) {
61             throw new ParameterException("Unable to load compiler: " + compilerClass, e);
62         }
63         this.deleteSources = params.getParameterAsBoolean("delete-sources", false);
64     }
65
66     /**
67      * Set the context
68      * @param context The context
69      */

70     public void contextualize(Context context) throws ContextException {
71         this.classpath = (String JavaDoc) context.get(Constants.CONTEXT_CLASSPATH);
72     }
73
74     /**
75      * Return the language's canonical object file extension.
76      * @return The object file extension
77      */

78     public abstract String JavaDoc getObjectExtension();
79
80     /**
81      * Unload a previously loaded program
82      * @param program A previously loaded object program
83      * @exception LanguageException If an error occurs during unloading
84      */

85     public abstract void doUnload(Object JavaDoc program) throws LanguageException;
86
87     /**
88      * Unload a previously loaded program given its original filesystem location
89      * @param program The previously loaded object program
90      * @param filename The base filename of the object program
91      * @param baseDirectory The directory contaning the object program file
92      * @exception LanguageException If an error occurs
93      */

94     protected final void doUnload(Object JavaDoc program, String JavaDoc filename, File JavaDoc baseDirectory) throws LanguageException {
95         int index = filename.lastIndexOf(File.separator);
96         String JavaDoc dir = filename.substring(0, index);
97         String JavaDoc file = filename.substring(index + 1);
98
99         File JavaDoc baseDir = new File JavaDoc(baseDirectory, dir);
100         File JavaDoc[] 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     /**
111      * Actually load an object program from a file.
112      * @param filename The object program base file name
113      * @param baseDirectory The directory containing the object program file
114      * @return The loaded object program
115      * @exception LanguageException If an error occurs during loading
116      */

117     protected abstract Class JavaDoc loadProgram(String JavaDoc filename, File JavaDoc baseDirectory) throws LanguageException;
118
119     /**
120      * Compile a source file yielding a loadable object file.
121      * @param filename The object program base file name
122      * @param baseDirectory The directory containing the object program file
123      * @param encoding The encoding expected in the source file or <code>null</code> if it is the platform's default encoding
124      * @exception LanguageException If an error occurs during compilation
125      */

126     protected abstract void compile(String JavaDoc filename, File JavaDoc baseDirectory, String JavaDoc encoding) throws LanguageException;
127
128     /**
129      * Preload an object program from a file.
130      * This method does not compiles the corresponding source file.
131      *
132      * @param filename The object program base file name
133      * @param baseDirectory The directory containing the object program file
134      * @param encoding The encoding expected in the source file or <code>null</code> if it is the platform's default encoding
135      * @return The loaded object program
136      * @exception LanguageException If an error occurs during compilation
137      */

138     public Program preload(String JavaDoc filename, File JavaDoc baseDirectory, String JavaDoc encoding) throws LanguageException {
139         // Don't need to test for existence of the object code as it might be bundled into the WAR.
140
try {
141             Class JavaDoc program = this.loadProgram(filename, baseDirectory);
142             // Create and discard test instance.
143
program.newInstance();
144             return new JavaProgram(program);
145         } catch (Throwable JavaDoc t) {
146             throw new LanguageException("Unable to preload program " + filename, t);
147         }
148     }
149
150     /**
151      * Load an object program from a file.
152      * This method compiles the corresponding source file if necessary.
153      *
154      * @param filename The object program base file name
155      * @param baseDirectory The directory containing the object program file
156      * @param encoding The encoding expected in the source file or <code>null</code> if it is the platform's default encoding
157      * @return The loaded object program
158      * @exception LanguageException If an error occurs during compilation
159      */

160     public Program load(String JavaDoc filename, File JavaDoc baseDirectory, String JavaDoc encoding) throws LanguageException {
161
162         // Does source file exist?
163
File JavaDoc sourceFile = new File JavaDoc(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 JavaDoc program = this.loadProgram(filename, baseDirectory);
178
179         // Try to instantiate once to ensure there are no exceptions thrown in the constructor
180
try {
181             // Create and discard test instance
182
program.newInstance();
183         } catch(IllegalAccessException JavaDoc iae) {
184             getLogger().debug("No public constructor for class " + program.getName());
185         } catch(Exception JavaDoc e) {
186             // Unload class and delete the object file, or it won't be recompiled
187
// (leave the source file to allow examination).
188
this.doUnload(program);
189             new File JavaDoc(baseDirectory, filename + "." + this.getObjectExtension()).delete();
190
191             String JavaDoc 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