001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.language.programming;
018:
019: import org.apache.avalon.framework.logger.AbstractLogEnabled;
020: import org.apache.avalon.framework.logger.LogEnabled;
021: import org.apache.avalon.framework.parameters.Parameterizable;
022: import org.apache.avalon.framework.parameters.Parameters;
023: import org.apache.avalon.framework.parameters.ParameterException;
024:
025: import org.apache.cocoon.components.language.LanguageException;
026: import org.apache.cocoon.components.language.generator.CompiledComponent;
027: import org.apache.cocoon.util.ClassUtils;
028:
029: import java.io.File;
030:
031: /**
032: * Base implementation of <code>ProgrammingLanguage</code>. This class sets the
033: * <code>CodeFormatter</code> instance and deletes source program files after
034: * unloading.
035: *
036: * @author <a href="mailto:ricardo@apache.org">Ricardo Rocha</a>
037: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
038: * @version CVS $Id: AbstractProgrammingLanguage.java 433543 2006-08-22 06:22:54Z crossley $
039: */
040: public abstract class AbstractProgrammingLanguage extends
041: AbstractLogEnabled implements ProgrammingLanguage,
042: Parameterizable {
043:
044: /** The source code formatter */
045: protected Class codeFormatter;
046:
047: protected String languageName;
048:
049: /**
050: * Set the configuration parameters. This method instantiates the
051: * sitemap-specified source code formatter
052: *
053: * @param params The configuration parameters
054: * @exception ParameterException If the language compiler cannot be loaded
055: */
056: public void parameterize(Parameters params)
057: throws ParameterException {
058: String className = params.getParameter("code-formatter", null);
059:
060: try {
061: if (className != null) {
062: this .codeFormatter = ClassUtils.loadClass(className);
063: }
064: } catch (Exception e) {
065: getLogger().error(
066: "Error with \"code-formatter\" parameter", e);
067: throw new ParameterException(
068: "Unable to load code formatter: " + className, e);
069: }
070: }
071:
072: /**
073: * Return this language's source code formatter. A new formatter instance is
074: * created on each invocation.
075: *
076: * @return The language source code formatter
077: */
078: public CodeFormatter getCodeFormatter() {
079: if (this .codeFormatter != null) {
080: try {
081: CodeFormatter formatter = (CodeFormatter) this .codeFormatter
082: .newInstance();
083: if (formatter instanceof LogEnabled) {
084: ((LogEnabled) formatter).enableLogging(this
085: .getLogger());
086: }
087: return formatter;
088: } catch (Exception e) {
089: getLogger().error("Error instantiating CodeFormatter",
090: e);
091: }
092: }
093:
094: return null;
095: }
096:
097: /**
098: * Unload a previously loaded program
099: *
100: * @param program A previously loaded object program
101: * @exception LanguageException If an error occurs during unloading
102: */
103: protected abstract void doUnload(Object program, String filename,
104: File baseDirectory) throws LanguageException;
105:
106: public final void unload(Object program, String filename,
107: File baseDirectory) throws LanguageException {
108:
109: File file = new File(baseDirectory, filename + "."
110: + getSourceExtension());
111: file.delete();
112: this .doUnload(program, filename, baseDirectory);
113: }
114:
115: public final void setLanguageName(String name) {
116: this .languageName = name;
117: }
118:
119: public final String getLanguageName() {
120: return this .languageName;
121: }
122:
123: /**
124: * Create a new instance for the given class
125: *
126: * @param program The Java class
127: * @return A new class instance
128: * @exception LanguageException If an instantiation error occurs
129: */
130: public CompiledComponent instantiate(Program program)
131: throws LanguageException {
132: try {
133: return program.newInstance();
134: } catch (Exception e) {
135: getLogger().warn("Could not instantiate program instance",
136: e);
137: throw new LanguageException(
138: "Could not instantiate program instance due to a "
139: + e.getClass().getName() + ": "
140: + e.getMessage());
141: }
142: }
143: }
|