Project Report: fawkez

Packagesummary org.jcoderz.commons.taskdefs

org.jcoderz.commons.taskdefs.MakeReadonlyBeans

LineHitsNoteSource
1  /*
2   * $Id: MakeReadonlyBeans.java 1011 2008-06-16 17:57:36Z amandel $
3   *
4   * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5   *
6   * Redistribution and use in source and binary forms, with or without
7   * modification, are permitted provided that the following conditions are
8   * met:
9   *
10   *    * Redistributions of source code must retain the above copyright
11   *      notice, this list of conditions and the following disclaimer.
12   *    * Redistributions in binary form must reproduce the above
13   *      copyright notice, this list of conditions and the following
14   *      disclaimer in the documentation and/or other materials
15   *      provided with the distribution.
16   *    * Neither the name of the jCoderZ.org Project nor the names of
17   *      its contributors may be used to endorse or promote products
18   *      derived from this software without specific prior written
19   *      permission.
20   *
21   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   */
33  package org.jcoderz.commons.taskdefs;
34  
35  import java.io.File;
36  import java.io.FileInputStream;
37  import java.io.FileNotFoundException;
38  import java.io.InputStream;
39  import java.util.ArrayList;
40  import java.util.Iterator;
41  import java.util.List;
42  
43  import javax.xml.transform.Transformer;
44  import javax.xml.transform.TransformerFactory;
45  import javax.xml.transform.sax.SAXSource;
46  import javax.xml.transform.stream.StreamResult;
47  import javax.xml.transform.stream.StreamSource;
48  
49  import org.apache.tools.ant.BuildException;
50  import org.apache.tools.ant.Project;
51  import org.apache.tools.ant.Task;
52  import org.xml.sax.InputSource;
53  import org.xml.sax.SAXException;
54  import org.xml.sax.XMLReader;
55  import org.xml.sax.helpers.XMLReaderFactory;
56  
57  
58  
59  /**
60   * This task is used to modify a deployment descriptor, making a
61   * copy of the given entity beans with their name changed from
62   * FooEntity to FooReaderEntity. It also modifies the accompanying
63   * weblogic specific deployment descriptors.
64   * @author Albrecht Messner
65   */
660 public class MakeReadonlyBeans
67        extends Task
68  {
69     private static final String STYLE_GENERIC = "make-readonly-bean-generic.xsl";
70     private static final String STYLE_WEBLOGIC = "make-readonly-bean-wl.xsl";
71     private static final String STYLE_WEBLOGIC_CMP
72           = "make-readonly-bean-wlcmp.xsl";
73     private static final String STYLE_GENERATE_UTIL
74           = "generate-readonly-entity-util.xsl";
75  
76     /** The output directory. */
77     private File mDestDir;
78  
79     /** The generic deployment descriptor (ejb-jar.xml) to transform. */
800    private File mGenericDeploymentDescriptor = null;
81  
82     /**
83      * The weblogic ejb deployment descriptor (weblogic-ejb-jar.xml) to
84      * transform.
85      */
860    private File mWlsDeploymentDescriptor = null;
87  
88     /**
89      * The weblogic RDBMS deployment descriptor (weblogic-cmp-rdbms-jar.xml)
90      * to transform.
91      */
920    private File mWlsCmpDeploymentDescriptor = null;
93  
94     /** List of all beans that need to be made read-only. */
950    private final List mReadonlyBeans = new ArrayList();
96  
97     /** force output of target files even if they already exist. */
980    private boolean mForce = false;
99  
100     /** terminate ant build on error. */
1010    private boolean mFailOnError = false;
102  
103     /** The directory to which generated source files should be written. */
104     private File mSourceDestDir;
105  
106     /**
107      * Set the destination directory into which the XSL result
108      * files should be copied to. This parameter is required.
109      * @param dir the name of the destination directory.
110      **/
111     public void setDestdir (File dir)
112     {
1130        mDestDir = dir;
1140    }
115  
116 (1)   public void setSourceDestdir (File dir)
117     {
1180       mSourceDestDir = dir;
1190    }
120  
121     /**
122      * Sets the force output of target files flag to the given value.
123      *
124      * @param b Whether we should force the generation of output files.
125      */
126     public void setForce (boolean b)
127     {
1280       mForce = b;
1290    }
130  
131     /**
132      * Set whether we should fail on an error.
133      *
134      * @param b Whether we should fail on an error.
135      */
136     public void setFailonerror (boolean b)
137     {
1380       mFailOnError = b;
1390    }
140  
141 (2)   public void setDeploymentDescriptor (File f)
142     {
1430       mGenericDeploymentDescriptor = f;
1440    }
145  
146 (3)   public void setWeblogicDeploymentDescriptor (File f)
147     {
1480       mWlsDeploymentDescriptor = f;
1490    }
150  
151 (4)   public void setWeblogicCmpDeploymentDescriptor (File f)
152     {
1530       mWlsCmpDeploymentDescriptor = f;
1540    }
155  
156 (5)   public ReadOnlyBean createReadOnlyBean ()
157     {
1580       final ReadOnlyBean bn = new ReadOnlyBean();
1590       mReadonlyBeans.add(bn);
1600       return bn;
161     }
162  
163     /** {@inheritDoc} */
164     public void execute ()
165           throws BuildException
166     {
167        try
168        {
1690          checkFilesAndDirectories();
170  
1710          if (mForce || checkIfBuildRequired())
172           {
1730             final File genericDdTarget
174                    = new File(mDestDir, mGenericDeploymentDescriptor.getName());
1750             transform(mGenericDeploymentDescriptor,
176                    genericDdTarget,
177                    STYLE_GENERIC);
1780             transform(mWlsDeploymentDescriptor,
179                    new File(mDestDir, mWlsDeploymentDescriptor.getName()),
180                    STYLE_WEBLOGIC);
1810             transform(mWlsCmpDeploymentDescriptor,
182                    new File(mDestDir, mWlsCmpDeploymentDescriptor.getName()),
183                    STYLE_WEBLOGIC_CMP);
1840             transform(genericDdTarget,
185                    new File(mDestDir, "generate-utils-stdout.txt"),
186                    STYLE_GENERATE_UTIL);
1870          }
188           else
189           {
1900             log("All files are up-to-date.", Project.MSG_INFO);
191           }
192        }
1930       catch (BuildException e)
194        {
1950          if (mFailOnError)
196           {
1970             throw e;
198           }
1990          log(e.getMessage(), Project.MSG_ERR);
2000       }
2010    }
202  
203     private String createListOfReadonlyBeans ()
204     {
2050       final StringBuffer sbuf = new StringBuffer();
2060       for (final Iterator it = mReadonlyBeans.iterator(); it.hasNext(); )
207        {
2080          if (sbuf.length() == 0)
209           {
2100             sbuf.append('|');
211           }
2120          final ReadOnlyBean bean = (ReadOnlyBean) it.next();
2130          sbuf.append(bean.getName());
2140          sbuf.append('|');
2150       }
2160       if (sbuf.length() == 0)
217        {
2180          sbuf.append("||");
219        }
2200       return sbuf.toString();
221     }
222  
223     private boolean checkIfBuildRequired ()
224     {
2250       final File genericOutFile
226              = new File(mDestDir, mGenericDeploymentDescriptor.getName());
2270       final File weblogicOutFile
228              = new File(mDestDir, mWlsDeploymentDescriptor.getName());
2290       final File weblogicCmpOutFile
230              = new File(mDestDir, mWlsCmpDeploymentDescriptor.getName());
2310       boolean buildRequired = false;
2320       if (! genericOutFile.exists()
233              || genericOutFile.lastModified()
234                 < mGenericDeploymentDescriptor.lastModified())
235        {
2360          buildRequired = true;
237        }
2380       if (! weblogicOutFile.exists()
239              || weblogicOutFile.lastModified()
240                 < mWlsDeploymentDescriptor.lastModified())
241        {
2420          buildRequired = true;
243        }
2440       if (! weblogicCmpOutFile.exists()
245              || weblogicCmpOutFile.lastModified()
246                 < mWlsCmpDeploymentDescriptor.lastModified())
247        {
2480          buildRequired = true;
249        }
2500       return buildRequired;
251     }
252  
253     private void checkFilesAndDirectories ()
254     {
2550       checkFile("genericDeploymentDescriptor", mGenericDeploymentDescriptor);
2560       checkFile("weblogicDeploymentDescriptor", mWlsDeploymentDescriptor);
2570       checkFile("weblogicCmpDeploymentDescriptor", mWlsCmpDeploymentDescriptor);
2580       checkDirectory("destdir", mDestDir);
2590       checkDirectory("sourceDestdir", mSourceDestDir);
2600    }
261  
262     private void checkFile (String paramName, File file)
263     {
2640       if (file == null)
265        {
2660          throw new BuildException(
267                 "Mandatory attribute '" + paramName + "' is missing.");
268        }
2690       if (! file.exists())
270        {
2710          throw new BuildException("File '" + file + "' does not exist");
272        }
2730    }
274  
275     private void checkDirectory (String paramName, File directory)
276     {
2770       if (directory == null)
278        {
2790          throw new BuildException(
280                 "Mandatory attribute '" + paramName + "' missing.");
281        }
2820       if (! directory.exists())
283        {
2840          throw new BuildException(
285                 "Output directory '" + directory + "' does not exist.");
286        }
2870       if (! directory.isDirectory())
288        {
2890          throw new BuildException("'" + directory + "' is not a directory");
290        }
2910    }
292  
293     /**
294      * Execute the XSL transformation.
295      * @throws BuildException if an error during transformation occurs.
296      */
297     private void transform (File inFile, File outFile, String xslFile)
298           throws BuildException
299     {
300        try
301        {
302           // Xalan2 transformator is required,
303           // that why we explicit use this factory
3040          final TransformerFactory factory
305                 = new org.apache.xalan.processor.TransformerFactoryImpl();
3060          factory.setURIResolver(new JarArchiveUriResolver(this));
307  
3080          final InputStream xslStream
309              = LogMessageGenerator.class.getResourceAsStream(xslFile);
310  
3110          final Transformer transformer
312                 = factory.newTransformer(new StreamSource(xslStream));
313  
3140          transformer.setParameter("outdir", mDestDir.getAbsolutePath());
3150          transformer.setParameter("srcdir", mSourceDestDir.getAbsolutePath());
3160          transformer.setParameter("bean-names", createListOfReadonlyBeans());
317  
3180          final StreamResult out = new StreamResult(outFile);
319  
3200          transformer.transform(getSaxSource(inFile), out);
321        }
3220       catch (Exception e)
323        {
3240          throw new BuildException("Error during transformation: " + e, e);
3250       }
3260    }
327  
328     private SAXSource getSaxSource (File xmlFile)
329           throws SAXException, FileNotFoundException
330     {
3310       final XMLReader xr = XMLReaderFactory.createXMLReader(
332              "org.apache.xerces.parsers.SAXParser");
3330       xr.setEntityResolver(new DummyEntityResolver(this));
3340       final FileInputStream fileInStream = new FileInputStream(xmlFile);
3350       final InputSource inSource = new InputSource(fileInStream);
3360       return new SAXSource(xr, inSource);
337     }
338  
3390(6)(7)   public final class ReadOnlyBean
340     {
341        private String mName;
342  
343        /**
344         * @return Returns the beanName.
345         */
346        public String getName ()
347        {
3480          return mName;
349        }
350  
351        /**
352         * @param beanName The beanName to set.
353         */
354        public void setName (String beanName)
355        {
3560          mName = beanName;
3570       }
358     }
359  }

Findings in this File

i (8) MakeReadonlyBeans.mDestDir not initialized in constructor
i (9) MakeReadonlyBeans.mSourceDestDir not initialized in constructor
c (1) 116 : 4 Missing a Javadoc comment.
c (2) 141 : 4 Missing a Javadoc comment.
c (3) 146 : 4 Missing a Javadoc comment.
c (4) 151 : 4 Missing a Javadoc comment.
c (5) 156 : 4 Missing a Javadoc comment.
c (6) 339 : 0 Missing a Javadoc comment.
w (7) 339 : 0 Should org.jcoderz.commons.taskdefs.MakeReadonlyBeans$ReadOnlyBean be a _static_ inner class?