001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.tools.ant;
034:
035: import org.apache.tools.ant.BuildException;
036: import org.apache.tools.ant.Task;
037:
038: import java.io.*;
039: import java.util.ArrayList;
040: import java.util.List;
041:
042: /**
043: * ANT task to build application.xml descriptors on the fly with descriptor entries
044: * for drop-in applications
045: *
046: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
047: */
048: public class ApplicationDescriptorBuilderTask extends Task {
049:
050: private String dropDir = null;
051: private String srcFile = null;
052: private String destFile = null;
053:
054: public void setDropDir(String dropDir) {
055: this .dropDir = dropDir;
056: }
057:
058: public void setSrcFile(String srcFile) {
059: this .srcFile = srcFile;
060: }
061:
062: public void setDestFile(String destFile) {
063: this .destFile = destFile;
064: }
065:
066: public String getTaskName() {
067: return "applicationDescriptorBuilder";
068: }
069:
070: public void execute() throws BuildException {
071: if (srcFile == null || !(new File(srcFile).exists()))
072: throw new BuildException("argument srcFile (" + srcFile
073: + ") does not exist!");
074: storeFile(loadFile(new File(srcFile)).replace(
075: "<!--DROP_IN_PLACEHOLDER-->", processDrops()),
076: new File(destFile));
077: }
078:
079: /**
080: * Load the contents of a file, returning it as a String.
081: * This method should only be used when really necessary since no real error handling is performed!!!
082: *
083: * @param file the File to load
084: * @return file contents
085: */
086: public static String loadFile(File file) {
087: try {
088: return loadFromInputStream(new FileInputStream(file),
089: (int) file.length());
090: } catch (FileNotFoundException e) {
091: throw new BuildException(e);
092: }
093: }
094:
095: /**
096: * Load a String from an InputStream (until end of stream)
097: *
098: * @param in InputStream
099: * @param length length of the string if < -1 (NOT number of bytes to read!)
100: * @return String
101: */
102: public static String loadFromInputStream(InputStream in, int length) {
103: StringBuilder sb = new StringBuilder(length > 0 ? length : 5000);
104: try {
105: int read;
106: byte[] buffer = new byte[1024];
107: while ((read = in.read(buffer)) != -1) {
108: sb.append(new String(buffer, 0, read));
109: }
110: } catch (IOException e) {
111: throw new BuildException(e);
112: } finally {
113: if (in != null) {
114: try {
115: in.close();
116: } catch (IOException e) {
117: // ignore
118: }
119: }
120: }
121: return sb.toString();
122: }
123:
124: /**
125: * Rather primitive "write String to file" helper, returns <code>false</code> if failed
126: *
127: * @param contents the String to store
128: * @param file the file, if existing it will be overwritten
129: * @return if successful
130: */
131: public static boolean storeFile(String contents, File file) {
132: FileOutputStream out = null;
133: try {
134: out = new FileOutputStream(file);
135: out.write(contents.getBytes("UTF-8"));
136: out.flush();
137: out.close();
138: return true;
139: } catch (IOException e) {
140: return false;
141: } finally {
142: if (out != null) {
143: try {
144: out.close();
145: } catch (IOException e) {
146: //ignore
147: }
148: }
149: }
150: }
151:
152: private String processDrops() {
153: StringBuilder sb = new StringBuilder(500).append("\n");
154: StringBuilder dropNames = new StringBuilder(100);
155: File drops = new File(dropDir);
156: if (!drops.exists() || !drops.isDirectory())
157: throw new BuildException("argument dropDir (" + dropDir
158: + ") is not a valid directory!");
159: List<File> jar = new ArrayList<File>(5);
160: List<File> ejb = new ArrayList<File>(5);
161: List<File> war = new ArrayList<File>(5);
162: for (File f : drops.listFiles()) {
163: if (f.getAbsolutePath().toLowerCase().endsWith("-ejb.jar"))
164: ejb.add(f);
165: else if (f.getAbsolutePath().toLowerCase().endsWith(".jar"))
166: jar.add(f);
167: else if (f.getAbsolutePath().toLowerCase().endsWith(".war"))
168: war.add(f);
169: else if (!f.getName().startsWith(".")
170: && !f.getName().equals("drops.archives")) //ignore .svn, .cvs etc files
171: System.out.println("Warning: Unrecognized drop file: "
172: + f.getName());
173: }
174: // Currently, JARs will be copied to the ear/lib directory and do not have to be registered
175: /*
176: for (File f : jar) {
177: sb.append(" <module>\n" + " <java>").
178: append(f.getName()).
179: append("</java>\n" + " </module>\n\n");
180: }
181: */
182: for (File f : ejb) {
183: sb.append(" <module>\n" + " <ejb>").append(
184: f.getName()).append(
185: "</ejb>\n" + " </module>\n\n");
186: }
187: for (File f : war) {
188: if (dropNames.length() > 0)
189: dropNames.append(',');
190: dropNames.append(f.getName().substring(0,
191: f.getName().lastIndexOf('.')));
192: sb.append(
193: " <module>\n" + " <web>\n"
194: + " <web-uri>").append(
195: f.getName()).append(
196: "</web-uri>\n" + " <context-root>/")
197: .append(
198: f.getName().substring(0,
199: f.getName().lastIndexOf('.')))
200: .append(
201: "</context-root>\n" + " </web>\n"
202: + " </module>\n");
203: }
204: storeFile(dropNames.toString(), new File(drops
205: .getAbsolutePath()
206: + File.separatorChar + "drops.archives"));
207: return sb.toString();
208: }
209: }
|