001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2003-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: AbsGenerator.java 5590 2004-10-11 13:16:15Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_lib.genbase.generator;
025:
026: import java.io.File;
027: import java.io.FileFilter;
028: import java.io.IOException;
029: import java.util.Vector;
030:
031: import org.objectweb.jonas_lib.I18n;
032: import org.objectweb.jonas_lib.genbase.GenBaseException;
033: import org.objectweb.jonas_lib.genbase.archive.Archive;
034: import org.objectweb.jonas_lib.genbase.utils.TempRepository;
035:
036: import org.objectweb.jonas.common.Log;
037:
038: import org.objectweb.util.monolog.api.BasicLevel;
039: import org.objectweb.util.monolog.api.Logger;
040:
041: /**
042: * Generators provide a structure to be extended for specific generation
043: * mechanisms.
044: * @author Guillaume Sauthier
045: */
046: public abstract class AbsGenerator {
047:
048: /**
049: * i18n
050: */
051: private static I18n i18n = I18n.getInstance(AbsGenerator.class);
052:
053: /**
054: * logger
055: */
056: private static Logger logger = Log
057: .getLogger(Log.JONAS_GENBASE_PREFIX);
058:
059: /**
060: * Configuration to be used
061: */
062: private Config config;
063:
064: /**
065: * compiled classes directory
066: */
067: private File classes;
068:
069: /**
070: * generated files directory
071: */
072: private File sources;
073:
074: /**
075: * Creates a new Generator with the given Config.
076: * @param config internal configuration object.
077: * @throws GenBaseException When sources and target temporary directory
078: * cannot be created
079: */
080: public AbsGenerator(Config config) throws GenBaseException {
081: this .config = config;
082:
083: // creates temporary directories
084: TempRepository tr = TempRepository.getInstance();
085:
086: try {
087: sources = tr.createDir();
088: classes = tr.createDir();
089: } catch (IOException ioe) {
090: String err = i18n.getMessage("AbsGenerator.constr.ioe");
091: logger.log(BasicLevel.ERROR, err);
092: throw new GenBaseException(err, ioe);
093: }
094: }
095:
096: /**
097: * Generate files.
098: * @throws GenBaseException When generation fails.
099: */
100: public abstract void generate() throws GenBaseException;
101:
102: /**
103: * Compile generated java files into classes directory.
104: * @throws GenBaseException When compilation fails
105: */
106: public abstract void compile() throws GenBaseException;
107:
108: /**
109: * Recursively add java files into a Vector.
110: * @param src base directory
111: * @param list Vector to be filled
112: */
113: protected void addJavaSources(File src, Vector list) {
114: // add each java sources contained in the directory
115: File[] files = src.listFiles(new FileFilter() {
116:
117: public boolean accept(File file) {
118: return file.isFile()
119: && file.getName().endsWith(".java");
120: }
121: });
122:
123: for (int i = 0; i < files.length; i++) {
124: list.add(files[i]);
125: }
126:
127: // reapply on subdirectories
128: files = src.listFiles(new FileFilter() {
129:
130: public boolean accept(File file) {
131: return file.isDirectory();
132: }
133: });
134:
135: for (int i = 0; i < files.length; i++) {
136: addJavaSources(files[i], list);
137: }
138: }
139:
140: /**
141: * Add generated files into an Archive
142: * @param archive the archive destination of generated files.
143: * @throws GenBaseException When files cannot be added in the given Archive.
144: */
145: public abstract void addFiles(Archive archive)
146: throws GenBaseException;
147:
148: /**
149: * @return the config.
150: */
151: public Config getConfig() {
152: return config;
153: }
154:
155: /**
156: * @return the logger.
157: */
158: public static Logger getLogger() {
159: return logger;
160: }
161:
162: /**
163: * @return the classes.
164: */
165: public File getClasses() {
166: return classes;
167: }
168:
169: /**
170: * @return the sources.
171: */
172: public File getSources() {
173: return sources;
174: }
175: }
|