| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 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 | | | |
| 61 | | | |
| 62 | | | |
| 63 | | | |
| 64 | | | @author |
| 65 | | | |
| 66 | 0 | | 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 | | | |
| 77 | | | private File mDestDir; |
| 78 | | | |
| 79 | | | |
| 80 | 0 | | private File mGenericDeploymentDescriptor = null; |
| 81 | | | |
| 82 | | | |
| 83 | | | |
| 84 | | | |
| 85 | | | |
| 86 | 0 | | private File mWlsDeploymentDescriptor = null; |
| 87 | | | |
| 88 | | | |
| 89 | | | |
| 90 | | | |
| 91 | | | |
| 92 | 0 | | private File mWlsCmpDeploymentDescriptor = null; |
| 93 | | | |
| 94 | | | |
| 95 | 0 | | private final List mReadonlyBeans = new ArrayList(); |
| 96 | | | |
| 97 | | | |
| 98 | 0 | | private boolean mForce = false; |
| 99 | | | |
| 100 | | | |
| 101 | 0 | | private boolean mFailOnError = false; |
| 102 | | | |
| 103 | | | |
| 104 | | | private File mSourceDestDir; |
| 105 | | | |
| 106 | | | |
| 107 | | | |
| 108 | | | |
| 109 | | | @param |
| 110 | | | |
| 111 | | | public void setDestdir (File dir) |
| 112 | | | { |
| 113 | 0 | | mDestDir = dir; |
| 114 | 0 | | } |
| 115 | | | |
| 116 | | (1) | public void setSourceDestdir (File dir) |
| 117 | | | { |
| 118 | 0 | | mSourceDestDir = dir; |
| 119 | 0 | | } |
| 120 | | | |
| 121 | | | |
| 122 | | | |
| 123 | | | |
| 124 | | | @param |
| 125 | | | |
| 126 | | | public void setForce (boolean b) |
| 127 | | | { |
| 128 | 0 | | mForce = b; |
| 129 | 0 | | } |
| 130 | | | |
| 131 | | | |
| 132 | | | |
| 133 | | | |
| 134 | | | @param |
| 135 | | | |
| 136 | | | public void setFailonerror (boolean b) |
| 137 | | | { |
| 138 | 0 | | mFailOnError = b; |
| 139 | 0 | | } |
| 140 | | | |
| 141 | | (2) | public void setDeploymentDescriptor (File f) |
| 142 | | | { |
| 143 | 0 | | mGenericDeploymentDescriptor = f; |
| 144 | 0 | | } |
| 145 | | | |
| 146 | | (3) | public void setWeblogicDeploymentDescriptor (File f) |
| 147 | | | { |
| 148 | 0 | | mWlsDeploymentDescriptor = f; |
| 149 | 0 | | } |
| 150 | | | |
| 151 | | (4) | public void setWeblogicCmpDeploymentDescriptor (File f) |
| 152 | | | { |
| 153 | 0 | | mWlsCmpDeploymentDescriptor = f; |
| 154 | 0 | | } |
| 155 | | | |
| 156 | | (5) | public ReadOnlyBean createReadOnlyBean () |
| 157 | | | { |
| 158 | 0 | | final ReadOnlyBean bn = new ReadOnlyBean(); |
| 159 | 0 | | mReadonlyBeans.add(bn); |
| 160 | 0 | | return bn; |
| 161 | | | } |
| 162 | | | |
| 163 | | | {@inheritDoc} |
| 164 | | | public void execute () |
| 165 | | | throws BuildException |
| 166 | | | { |
| 167 | | | try |
| 168 | | | { |
| 169 | 0 | | checkFilesAndDirectories(); |
| 170 | | | |
| 171 | 0 | | if (mForce || checkIfBuildRequired()) |
| 172 | | | { |
| 173 | 0 | | final File genericDdTarget |
| 174 | | | = new File(mDestDir, mGenericDeploymentDescriptor.getName()); |
| 175 | 0 | | transform(mGenericDeploymentDescriptor, |
| 176 | | | genericDdTarget, |
| 177 | | | STYLE_GENERIC); |
| 178 | 0 | | transform(mWlsDeploymentDescriptor, |
| 179 | | | new File(mDestDir, mWlsDeploymentDescriptor.getName()), |
| 180 | | | STYLE_WEBLOGIC); |
| 181 | 0 | | transform(mWlsCmpDeploymentDescriptor, |
| 182 | | | new File(mDestDir, mWlsCmpDeploymentDescriptor.getName()), |
| 183 | | | STYLE_WEBLOGIC_CMP); |
| 184 | 0 | | transform(genericDdTarget, |
| 185 | | | new File(mDestDir, "generate-utils-stdout.txt"), |
| 186 | | | STYLE_GENERATE_UTIL); |
| 187 | 0 | | } |
| 188 | | | else |
| 189 | | | { |
| 190 | 0 | | log("All files are up-to-date.", Project.MSG_INFO); |
| 191 | | | } |
| 192 | | | } |
| 193 | 0 | | catch (BuildException e) |
| 194 | | | { |
| 195 | 0 | | if (mFailOnError) |
| 196 | | | { |
| 197 | 0 | | throw e; |
| 198 | | | } |
| 199 | 0 | | log(e.getMessage(), Project.MSG_ERR); |
| 200 | 0 | | } |
| 201 | 0 | | } |
| 202 | | | |
| 203 | | | private String createListOfReadonlyBeans () |
| 204 | | | { |
| 205 | 0 | | final StringBuffer sbuf = new StringBuffer(); |
| 206 | 0 | | for (final Iterator it = mReadonlyBeans.iterator(); it.hasNext(); ) |
| 207 | | | { |
| 208 | 0 | | if (sbuf.length() == 0) |
| 209 | | | { |
| 210 | 0 | | sbuf.append('|'); |
| 211 | | | } |
| 212 | 0 | | final ReadOnlyBean bean = (ReadOnlyBean) it.next(); |
| 213 | 0 | | sbuf.append(bean.getName()); |
| 214 | 0 | | sbuf.append('|'); |
| 215 | 0 | | } |
| 216 | 0 | | if (sbuf.length() == 0) |
| 217 | | | { |
| 218 | 0 | | sbuf.append("||"); |
| 219 | | | } |
| 220 | 0 | | return sbuf.toString(); |
| 221 | | | } |
| 222 | | | |
| 223 | | | private boolean checkIfBuildRequired () |
| 224 | | | { |
| 225 | 0 | | final File genericOutFile |
| 226 | | | = new File(mDestDir, mGenericDeploymentDescriptor.getName()); |
| 227 | 0 | | final File weblogicOutFile |
| 228 | | | = new File(mDestDir, mWlsDeploymentDescriptor.getName()); |
| 229 | 0 | | final File weblogicCmpOutFile |
| 230 | | | = new File(mDestDir, mWlsCmpDeploymentDescriptor.getName()); |
| 231 | 0 | | boolean buildRequired = false; |
| 232 | 0 | | if (! genericOutFile.exists() |
| 233 | | | || genericOutFile.lastModified() |
| 234 | | | < mGenericDeploymentDescriptor.lastModified()) |
| 235 | | | { |
| 236 | 0 | | buildRequired = true; |
| 237 | | | } |
| 238 | 0 | | if (! weblogicOutFile.exists() |
| 239 | | | || weblogicOutFile.lastModified() |
| 240 | | | < mWlsDeploymentDescriptor.lastModified()) |
| 241 | | | { |
| 242 | 0 | | buildRequired = true; |
| 243 | | | } |
| 244 | 0 | | if (! weblogicCmpOutFile.exists() |
| 245 | | | || weblogicCmpOutFile.lastModified() |
| 246 | | | < mWlsCmpDeploymentDescriptor.lastModified()) |
| 247 | | | { |
| 248 | 0 | | buildRequired = true; |
| 249 | | | } |
| 250 | 0 | | return buildRequired; |
| 251 | | | } |
| 252 | | | |
| 253 | | | private void checkFilesAndDirectories () |
| 254 | | | { |
| 255 | 0 | | checkFile("genericDeploymentDescriptor", mGenericDeploymentDescriptor); |
| 256 | 0 | | checkFile("weblogicDeploymentDescriptor", mWlsDeploymentDescriptor); |
| 257 | 0 | | checkFile("weblogicCmpDeploymentDescriptor", mWlsCmpDeploymentDescriptor); |
| 258 | 0 | | checkDirectory("destdir", mDestDir); |
| 259 | 0 | | checkDirectory("sourceDestdir", mSourceDestDir); |
| 260 | 0 | | } |
| 261 | | | |
| 262 | | | private void checkFile (String paramName, File file) |
| 263 | | | { |
| 264 | 0 | | if (file == null) |
| 265 | | | { |
| 266 | 0 | | throw new BuildException( |
| 267 | | | "Mandatory attribute '" + paramName + "' is missing."); |
| 268 | | | } |
| 269 | 0 | | if (! file.exists()) |
| 270 | | | { |
| 271 | 0 | | throw new BuildException("File '" + file + "' does not exist"); |
| 272 | | | } |
| 273 | 0 | | } |
| 274 | | | |
| 275 | | | private void checkDirectory (String paramName, File directory) |
| 276 | | | { |
| 277 | 0 | | if (directory == null) |
| 278 | | | { |
| 279 | 0 | | throw new BuildException( |
| 280 | | | "Mandatory attribute '" + paramName + "' missing."); |
| 281 | | | } |
| 282 | 0 | | if (! directory.exists()) |
| 283 | | | { |
| 284 | 0 | | throw new BuildException( |
| 285 | | | "Output directory '" + directory + "' does not exist."); |
| 286 | | | } |
| 287 | 0 | | if (! directory.isDirectory()) |
| 288 | | | { |
| 289 | 0 | | throw new BuildException("'" + directory + "' is not a directory"); |
| 290 | | | } |
| 291 | 0 | | } |
| 292 | | | |
| 293 | | | |
| 294 | | | |
| 295 | | | @throws |
| 296 | | | |
| 297 | | | private void transform (File inFile, File outFile, String xslFile) |
| 298 | | | throws BuildException |
| 299 | | | { |
| 300 | | | try |
| 301 | | | { |
| 302 | | | |
| 303 | | | |
| 304 | 0 | | final TransformerFactory factory |
| 305 | | | = new org.apache.xalan.processor.TransformerFactoryImpl(); |
| 306 | 0 | | factory.setURIResolver(new JarArchiveUriResolver(this)); |
| 307 | | | |
| 308 | 0 | | final InputStream xslStream |
| 309 | | | = LogMessageGenerator.class.getResourceAsStream(xslFile); |
| 310 | | | |
| 311 | 0 | | final Transformer transformer |
| 312 | | | = factory.newTransformer(new StreamSource(xslStream)); |
| 313 | | | |
| 314 | 0 | | transformer.setParameter("outdir", mDestDir.getAbsolutePath()); |
| 315 | 0 | | transformer.setParameter("srcdir", mSourceDestDir.getAbsolutePath()); |
| 316 | 0 | | transformer.setParameter("bean-names", createListOfReadonlyBeans()); |
| 317 | | | |
| 318 | 0 | | final StreamResult out = new StreamResult(outFile); |
| 319 | | | |
| 320 | 0 | | transformer.transform(getSaxSource(inFile), out); |
| 321 | | | } |
| 322 | 0 | | catch (Exception e) |
| 323 | | | { |
| 324 | 0 | | throw new BuildException("Error during transformation: " + e, e); |
| 325 | 0 | | } |
| 326 | 0 | | } |
| 327 | | | |
| 328 | | | private SAXSource getSaxSource (File xmlFile) |
| 329 | | | throws SAXException, FileNotFoundException |
| 330 | | | { |
| 331 | 0 | | final XMLReader xr = XMLReaderFactory.createXMLReader( |
| 332 | | | "org.apache.xerces.parsers.SAXParser"); |
| 333 | 0 | | xr.setEntityResolver(new DummyEntityResolver(this)); |
| 334 | 0 | | final FileInputStream fileInStream = new FileInputStream(xmlFile); |
| 335 | 0 | | final InputSource inSource = new InputSource(fileInStream); |
| 336 | 0 | | return new SAXSource(xr, inSource); |
| 337 | | | } |
| 338 | | | |
| 339 | 0 | (6)(7) | public final class ReadOnlyBean |
| 340 | | | { |
| 341 | | | private String mName; |
| 342 | | | |
| 343 | | | |
| 344 | | | @return |
| 345 | | | |
| 346 | | | public String getName () |
| 347 | | | { |
| 348 | 0 | | return mName; |
| 349 | | | } |
| 350 | | | |
| 351 | | | |
| 352 | | | @param |
| 353 | | | |
| 354 | | | public void setName (String beanName) |
| 355 | | | { |
| 356 | 0 | | mName = beanName; |
| 357 | 0 | | } |
| 358 | | | } |
| 359 | | | } |