KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > genbase > generator > AbsGenerator


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2003-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: AbsGenerator.java,v 1.1 2004/10/11 13:16:14 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_lib.genbase.generator;
27
28 import java.io.File JavaDoc;
29 import java.io.FileFilter JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 import org.objectweb.jonas_lib.I18n;
34 import org.objectweb.jonas_lib.genbase.GenBaseException;
35 import org.objectweb.jonas_lib.genbase.archive.Archive;
36 import org.objectweb.jonas_lib.genbase.utils.TempRepository;
37
38 import org.objectweb.jonas.common.Log;
39
40 import org.objectweb.util.monolog.api.BasicLevel;
41 import org.objectweb.util.monolog.api.Logger;
42
43 /**
44  * Generators provide a structure to be extended for specific generation
45  * mechanisms.
46  * @author Guillaume Sauthier
47  */

48 public abstract class AbsGenerator {
49
50     /**
51      * i18n
52      */

53     private static I18n i18n = I18n.getInstance(AbsGenerator.class);
54
55     /**
56      * logger
57      */

58     private static Logger logger = Log.getLogger(Log.JONAS_GENBASE_PREFIX);
59
60     /**
61      * Configuration to be used
62      */

63     private Config config;
64
65     /**
66      * compiled classes directory
67      */

68     private File JavaDoc classes;
69
70     /**
71      * generated files directory
72      */

73     private File JavaDoc sources;
74
75     /**
76      * Creates a new Generator with the given Config.
77      * @param config internal configuration object.
78      * @throws GenBaseException When sources and target temporary directory
79      * cannot be created
80      */

81     public AbsGenerator(Config config) throws GenBaseException {
82         this.config = config;
83
84         // creates temporary directories
85
TempRepository tr = TempRepository.getInstance();
86
87         try {
88             sources = tr.createDir();
89             classes = tr.createDir();
90         } catch (IOException JavaDoc ioe) {
91             String JavaDoc err = i18n.getMessage("AbsGenerator.constr.ioe");
92             logger.log(BasicLevel.ERROR, err);
93             throw new GenBaseException(err, ioe);
94         }
95     }
96
97     /**
98      * Generate files.
99      * @throws GenBaseException When generation fails.
100      */

101     public abstract void generate() throws GenBaseException;
102
103     /**
104      * Compile generated java files into classes directory.
105      * @throws GenBaseException When compilation fails
106      */

107     public abstract void compile() throws GenBaseException;
108
109     /**
110      * Recursively add java files into a Vector.
111      * @param src base directory
112      * @param list Vector to be filled
113      */

114     protected void addJavaSources(File JavaDoc src, Vector JavaDoc list) {
115         // add each java sources contained in the directory
116
File JavaDoc[] files = src.listFiles(new FileFilter JavaDoc() {
117
118             public boolean accept(File JavaDoc file) {
119                 return file.isFile() && 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 JavaDoc() {
129
130             public boolean accept(File JavaDoc 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) throws GenBaseException;
146
147     /**
148      * @return the config.
149      */

150     public Config getConfig() {
151         return config;
152     }
153
154     /**
155      * @return the logger.
156      */

157     public static Logger getLogger() {
158         return logger;
159     }
160
161     /**
162      * @return the classes.
163      */

164     public File JavaDoc getClasses() {
165         return classes;
166     }
167
168     /**
169      * @return the sources.
170      */

171     public File JavaDoc getSources() {
172         return sources;
173     }
174 }
Popular Tags