001: /**
002: * JOnAS : Java(TM) OpenSource Application Server
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017: * USA
018: *
019: * Initial Developer : Guillaume Sauthier
020: * --------------------------------------------------------------------------
021: * $Id: JVelocity.java 5972 2004-12-16 09:46:37Z benoitf $
022: * --------------------------------------------------------------------------
023: */package org.objectweb.jonas_ws.wsgen.generator.axis;
024:
025: import java.io.File;
026: import java.io.FileWriter;
027: import java.io.IOException;
028:
029: import org.apache.velocity.Template;
030: import org.apache.velocity.VelocityContext;
031: import org.apache.velocity.app.VelocityEngine;
032: import org.apache.velocity.runtime.RuntimeConstants;
033:
034: import org.objectweb.jonas_lib.I18n;
035:
036: import org.objectweb.jonas_ws.wsgen.WsGenException;
037:
038: import org.objectweb.jonas.common.Log;
039:
040: import org.objectweb.util.monolog.api.BasicLevel;
041: import org.objectweb.util.monolog.api.Logger;
042:
043: /**
044: * Wrapper around Velocity tool.
045: *
046: * @author Guillaume Sauthier
047: */
048: public class JVelocity {
049:
050: /** i18n */
051: private static I18n i18n = I18n.getInstance(JVelocity.class);
052:
053: /**
054: * logger
055: */
056: private static Logger logger = Log
057: .getLogger(Log.JONAS_WSGEN_PREFIX);
058:
059: /** Velocity Engine */
060: private VelocityEngine vEngine;
061:
062: /** Velocity Template */
063: private Template template;
064:
065: /**
066: * Creates a new JVelocity instance using the given template.
067: *
068: * @param tmplName the template filename
069: *
070: * @exception WsGenException when error occurs during Velocity
071: * initialization.
072: */
073: public JVelocity(String tmplName) throws WsGenException {
074: // Prepare Velocity Engine
075: String jonasRoot = System.getProperty("install.root");
076:
077: if (jonasRoot == null) {
078: String err = i18n.getMessage("JVelocity.constr.notset");
079: throw new WsGenException(err);
080: }
081:
082: String path2Tmpl = new String(jonasRoot + File.separatorChar
083: + "templates" + File.separatorChar + "wsgen"
084: + File.separatorChar + "generator" + File.separatorChar
085: + "axis");
086:
087: // instanciate the engine
088: vEngine = new VelocityEngine();
089: vEngine.setProperty(RuntimeConstants.VM_LIBRARY, "");
090: vEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
091: vEngine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
092: path2Tmpl);
093:
094: try {
095: vEngine.init();
096: } catch (Exception e) {
097: String err = i18n
098: .getMessage("JVelocity.constr.initFailure");
099: throw new WsGenException(err, e);
100: }
101:
102: // Create the Template
103: try {
104: template = vEngine.getTemplate(tmplName);
105: } catch (Exception e) {
106: String err = i18n.getMessage("JVelocity.constr.tmplError",
107: tmplName);
108: throw new WsGenException(err, e);
109: }
110: }
111:
112: /**
113: * Generate the given file with the given VelocityContext.
114: *
115: * @param fs the output file
116: * @param context VelocityContext
117: *
118: * @throws WsGenException when velocity generation fails
119: */
120: public void generate(File fs, VelocityContext context)
121: throws WsGenException {
122: FileWriter fwriter = null;
123:
124: try {
125: // Create before the parent directory
126: File fdir = fs.getParentFile();
127:
128: if (fdir != null) {
129: if (!fdir.exists()) {
130: if (!fdir.mkdirs()) {
131: String err = i18n.getMessage(
132: "JVelocity.generate.directories", fdir
133: .getPath());
134: throw new WsGenException(err);
135: }
136: }
137: }
138:
139: fwriter = new FileWriter(fs);
140: } catch (IOException e) {
141: String err = i18n.getMessage("JVelocity.generate.file", fs);
142: throw new WsGenException(err, e);
143: }
144:
145: try {
146: template.merge(context, fwriter);
147: } catch (Exception e) {
148: String err = i18n.getMessage("JVelocity.generate.cannot",
149: fs);
150: throw new WsGenException(err, e);
151: }
152:
153: try {
154: fwriter.flush();
155: fwriter.close();
156: } catch (IOException e) {
157: // do nothing, just a warn
158: String err = i18n
159: .getMessage("JVelocity.generate.close", fs);
160: logger.log(BasicLevel.WARN, err);
161: }
162: }
163: }
|