View Javadoc

1   /*
2    * Copyright Emmanouil Batsis
3    * 
4    * Licensed under the GNU General Public 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.gnu.org/licenses/gpl.txt
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   */
17  package com.geekologue.md4j.tools.ant;
18  
19  import java.io.File;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Map;
23  import java.util.Set;
24  import java.util.HashSet;
25  import org.apache.tools.ant.BuildException;
26  import org.apache.tools.ant.DirectoryScanner;
27  import org.apache.tools.ant.Project;
28  import org.apache.tools.ant.taskdefs.MatchingTask;
29  import org.apache.tools.ant.types.XMLCatalog;
30  import com.geekologue.md4j.generators.Ejb21Generator;
31  import com.geekologue.md4j.generators.HibernateDaoGenerator;
32  import com.geekologue.md4j.generators.StrutsAction1Generator;
33  import com.geekologue.md4j.util.ClassUtils;
34  
35  /***
36   * @TODO add documentation Common context parameters:
37   *       <ul>
38   *       <li>pagePathPrefix: The path prefix to use for JSP pages etc (for
39   *       example many people put their JSPs in WEB-INF for security)</li>
40   *       <li>ejbdocletViewMethod: The EJB view method to output as XDoclet
41   *       tags, one of 'remote', 'local' or 'both', with the last being the
42   *       default</li>
43   *       <li>remoteEjbViewMethod: Whether to use remote EJB interfaces for
44   *       accessing EJBs from the web tier, default is false</li>
45   *       <li></li>
46   *       </ul>
47   */
48  public class HibernateMappingProcessorTask extends MatchingTask {
49      public static final String MD4J_BASE_URI        = "http://abiss.gr/md4j/jar/";
50  
51      private String             generators           = new StringBuffer(
52                                                              HibernateDaoGenerator.class
53                                                                      .getName())
54                                                              .append(" ")
55                                                              .append(
56                                                                      Ejb21Generator.class
57                                                                              .getName())
58                                                              .append(" ")
59                                                              .append(
60                                                                      StrutsAction1Generator.class
61                                                                              .getName())
62                                                              .toString();
63  
64      private String             mapper               = "simple";
65  
66      private Map                context              = new HashMap();
67  
68      /*** The domain objects package */
69      private String             packageName          = null;
70  
71      /*** Whether to fail the build if an error occurs in this task */
72      private boolean            failonerror          = true;
73  
74      /*** destination directory */
75      private File               destDir              = null;
76  
77      /*** where to find the source XML file, default is the project's basedir */
78      private File               baseDir              = null;
79  
80      /*** Force output of target files even if they already exist */
81      private boolean            force                = false;
82  
83      /*** For resolving entities such as dtds */
84      private XMLCatalog         xmlCatalog           = new XMLCatalog();
85  
86      /***
87       * The transformers used, given by the web, business and dao configuration
88       * objects
89       */
90      private Set                processors;
91  
92      /*** Whether to proccess all files in the included directories */
93      private boolean            performDirectoryScan = true;
94  
95      public HibernateMappingProcessorTask() {
96          this.context.put("task", this);
97          this.context.put("pagePathPrefix", "");
98          this.context.put("ejbdocletViewMethod", "both");
99          this.context.put("remoteEjbViewMethod", "false");
100         this.context.put("pagePathPrefix", "");
101     }
102 
103     public void addConfiguredContextParam(ContextParam param) {
104         this.context.put(param.getName(), param.getValue());
105         this.log("Added context param: " + param.getName() + ": "
106                 + param.getValue());
107     }
108 
109     /***
110      * Initialize internal instance of XMLCatalog
111      */
112     public void init() throws BuildException {
113         super.init();
114         this.xmlCatalog.setProject(getProject());
115     }
116 
117     /***
118      */
119     private void initialize() {
120         this.context.put("package", this.getPackageName());
121         if (this.destDir == null) {
122             throw new BuildException("No destDir specified", this.getLocation());
123         }
124         if (this.packageName == null) {
125             throw new BuildException("No packageName was specified", this
126                     .getLocation());
127         }
128         String[] tierSettings = this.generators.split(" ");
129         // initialize HibernateMappingProcessors based on classnames
130         this.processors = new HashSet();
131         for (int i = 0; i < tierSettings.length; i++) {
132             HibernateMappingProcessorLiason proc;
133             try {
134                 proc = (HibernateMappingProcessorLiason) ClassUtils
135                         .newInstance(tierSettings[i]);
136             } catch (Exception e) {
137                 throw new BuildException("Could not load Class: "
138                         + tierSettings[i], e, this.getLocation());
139             }
140             proc.init(this.context);
141             this.processors.add(proc);
142         }
143     }
144 
145     /***
146      * Executes the task.
147      * 
148      * @exception BuildException
149      *                if a problem occurs
150      */
151     public void execute() throws BuildException {
152         this.initialize();
153         File savedBaseDir = this.baseDir;
154         DirectoryScanner scanner;
155         String[] list;
156         String[] dirs;
157         try {
158             log("Generating classes into " + this.destDir, Project.MSG_INFO);
159             scanner = getDirectoryScanner(this.baseDir);
160             log("Transforming into " + this.destDir, Project.MSG_INFO);
161             // process matching files
162             list = scanner.getIncludedFiles();
163             for (int i = 0; i < list.length; ++i) {
164                 process(this.baseDir, list[i]);
165             }
166             if (this.performDirectoryScan) {
167                 // process matching files in sub dirs
168                 dirs = scanner.getIncludedDirectories();
169                 for (int j = 0; j < dirs.length; ++j) {
170                     list = new File(this.baseDir, dirs[j]).list();
171                     for (int i = 0; i < list.length; ++i) {
172                         process(this.baseDir, dirs[j] + File.separator
173                                 + list[i]);
174                     }
175                 }
176             }
177         } finally {
178             this.baseDir = savedBaseDir;
179         }
180     }
181 
182     private void process(File contextDir, String sFile) {
183         for (Iterator iter = this.processors.iterator(); iter.hasNext();) {
184             HibernateMappingProcessorLiason proc = (HibernateMappingProcessorLiason) iter
185                     .next();
186             proc.process(contextDir, sFile);
187         }
188     }
189 
190     public void logOrThrowError(String msg) throws BuildException {
191         if (this.failonerror) {
192             throw new BuildException(msg, getLocation());
193         }
194         log(msg, Project.MSG_ERR);
195     }
196 
197     public void logOrThrowError(String msg, Exception e) throws BuildException {
198         if (this.failonerror) {
199             throw new BuildException(msg, e, getLocation());
200         }
201         log(msg, Project.MSG_ERR);
202     }
203 
204     /***
205      * @param dir
206      *            The baseDir to set.
207      */
208     public void setBaseDir(File dir) {
209         this.baseDir = dir;
210     }
211 
212     /***
213      * @param dir
214      *            The destDir to set.
215      */
216     public void setDestDir(File dir) {
217         this.destDir = dir;
218     }
219 
220     /***
221      * @param b
222      *            To force or not
223      */
224     public void setForce(boolean b) {
225         this.force = b;
226     }
227 
228     /***
229      * Add the catalog to our internal catalog
230      * 
231      * @param xmlCatalog
232      *            the XMLCatalog instance to use to look up DTDs
233      */
234     public void addConfiguredXMLCatalog(XMLCatalog catalog) {
235         this.xmlCatalog.addConfiguredXMLCatalog(catalog);
236     }
237 
238     /***
239      * @param b
240      *            To fail on error or not
241      */
242     public void setFailonerror(boolean b) {
243         this.failonerror = b;
244     }
245 
246     /***
247      * @return Returns the xmlCatalog.
248      */
249     public XMLCatalog getXmlCatalog() {
250         return xmlCatalog;
251     }
252 
253     /***
254      * @return Returns the mapper.
255      */
256     public String getMapper() {
257         return mapper;
258     }
259 
260     /***
261      * @param mapper
262      *            The mapper to set.
263      */
264     public void setMapper(String mapper) {
265         this.mapper = mapper;
266     }
267 
268     /***
269      * @return Returns the force.
270      */
271     public boolean isForce() {
272         return force;
273     }
274 
275     public String getPackageName() {
276         return this.packageName;
277     }
278 
279     public void setPackageName(String packageName) {
280         this.packageName = packageName;
281     }
282 
283     public String getGenerators() {
284         return this.generators;
285     }
286 
287     public void setGenerators(String dataTierGenerator) {
288         this.generators = dataTierGenerator;
289     }
290 
291     public File getDestDir() {
292         return this.destDir;
293     }
294 }