1 package net.sf.invicta.ant.taskdef; 2 3 import org.apache.tools.ant.BuildException; 4 import org.apache.tools.ant.Task; 5 6 import java.io.File ; 7 import java.io.FileInputStream ; 8 import java.io.FileNotFoundException ; 9 import java.io.FileOutputStream ; 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.io.InputStreamReader ; 13 import java.io.PrintWriter ; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.jar.JarFile ; 18 import java.util.zip.ZipEntry ; 19 20 25 public class ApplicationFileGenerator extends Task { 26 27 28 private final static String EAR_APPLICATION_XML_PATH = 29 "META-INF/application.xml"; 30 31 private final static String APPLICATION_CLOSING_TAG = "</application>"; 32 private final String header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 33 "<!DOCTYPE application PUBLIC \"-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN\"" + 34 35 " \"http://java.sun.com/dtd/application_1_3.dtd\">"; 36 37 private final String TYPE_EJB = "ejb"; 38 private final String TYPE_JAVA = "java"; 39 private final String TYPE_WEB = "web"; 40 41 private String appxml; 42 private String projectName; 43 private List modules; 44 private String earFile; 45 46 49 public ApplicationFileGenerator() 50 { 51 super(); 52 this.appxml = null; 53 this.modules = new ArrayList (); 54 this.earFile = null; 55 } 56 57 public void execute() throws BuildException { 58 if (this.appxml == null) 60 throw new BuildException("Appxml file was not specified."); 61 if (this.projectName == null) 62 throw new BuildException("Project name was not specified."); 63 64 StringBuffer existingApp = null; 65 if (this.earFile != null) { 66 log("Using application.xml from existing ear file '" + this.earFile + "'"); 67 existingApp = fetchExistingContent(this.earFile); 68 } 69 70 byte[] buffer = new byte[1024]; 72 InputStream inputStream; 73 StringBuffer previousFileContent = new StringBuffer (); 74 try { 75 inputStream = new FileInputStream (this.appxml); 76 int res = 0; 77 while ((res = inputStream.read(buffer)) != -1) 78 previousFileContent.append(new String (buffer, 0, res)); 79 inputStream.close(); 80 } catch (FileNotFoundException e) { 81 } catch (IOException e) { 82 } 83 84 85 86 StringBuffer output = new StringBuffer (); 88 89 for (Iterator iter = this.modules.iterator(); iter.hasNext();) { 91 Module module = (Module) iter.next(); 92 93 94 if (module.getType() == null) 96 throw new BuildException("Module type was not specified."); 97 if (module.getFile() == null) 98 throw new BuildException("Module file was not specified."); 99 100 if ((this.earFile != null) && 102 (existingApp.toString().indexOf(">" + module.getFile() + "<") != -1)) { 103 log("Warning: Module '" + module.getFile() + "' is already defined in application.xml"); 104 continue; 105 } 106 107 output.append("\t<module>\n"); 108 109 File file = new File (module.getFile()); 110 111 if(module.getType().equalsIgnoreCase(TYPE_JAVA)) { 113 output.append("\t\t<java>" + file.getName() + "</java>\n"); 114 115 } else if(module.getType().equalsIgnoreCase(TYPE_EJB)) { 117 output.append("\t\t<ejb>" + file.getName() + "</ejb>\n"); 118 119 } else if(module.getType().equalsIgnoreCase(TYPE_WEB)) { 121 if (module.getName() == null) 122 throw new BuildException("Module name was not specified."); 123 124 output.append("\t\t<web>\n"); 125 output.append("\t\t\t<web-uri>" + file.getName() + "</web-uri>\n"); 126 output.append("\t\t\t<context-root>" + module.getName() + "</context-root>\n"); 127 output.append("\t\t</web>\n"); 128 129 } else { 130 throw new BuildException("Unknown type: " + module.getType()); 131 } 132 133 output.append("\t</module>\n"); 134 135 } 136 137 if (this.earFile == null) { 138 139 output.insert(0, 140 header + "\n\n<application>\n" + 141 " <display-name>" + this.projectName + "</display-name>\n" + 142 " <description>'" + this.projectName + "' applications descriptor file</description>\n"); 143 144 145 146 output.append("</application>\n"); 148 } else { 149 int tagPos = existingApp.toString().indexOf(APPLICATION_CLOSING_TAG); 150 if (tagPos == 1) 151 throw new BuildException("Invalid EAR application.xml (missing closing tag)"); 152 153 existingApp.insert(tagPos, output.toString()); 154 155 output = existingApp; 156 } 157 158 if (!previousFileContent.toString().equals(output.toString())) { 161 162 PrintWriter out = null; 164 try { 165 out = new PrintWriter (new FileOutputStream (this.appxml)); 166 } catch (FileNotFoundException e) { 167 throw new BuildException(e.toString()); 168 } 169 out.print(output.toString()); 170 171 out.close(); 172 log("Application file was created successfully (" + this.appxml + ")"); 173 } else { 174 log("Application file should not be modified (" + this.appxml + ")"); 175 } 176 177 178 } 179 184 private StringBuffer fetchExistingContent(String earFile) { 185 186 char[] buffer = new char[1024]; 187 try { 188 189 JarFile jarFile = new JarFile (earFile); 190 ZipEntry appEntry = jarFile.getEntry(EAR_APPLICATION_XML_PATH); 191 if (appEntry == null) 192 throw new BuildException("No application.xml found in '" + earFile + "'"); 193 194 InputStreamReader appStream = new InputStreamReader (jarFile.getInputStream(appEntry)); 195 196 StringBuffer appContent = new StringBuffer (); 197 int res = 0; 198 while ((res = appStream.read(buffer)) != -1) 199 appContent.append(new String (buffer, 0, res)); 200 201 return appContent; 202 } catch (IOException e) { 203 throw new BuildException("Error in reading EAR file '" + earFile + "': " + e.getMessage()); 204 } 205 } 206 207 public void setAppxml(String appxml) 208 { 209 this.appxml = appxml; 210 } 211 212 public void addConfiguredModule(Module module) 213 { 214 this.modules.add(module); 215 } 216 217 218 222 public void setProjectName(String projectName) { 223 this.projectName = projectName; 224 } 225 226 230 public String getEarFile() { 231 return earFile; 232 } 233 234 238 public void setEarFile(String earFile) { 239 this.earFile = earFile; 240 } 241 242 } 243 244 245 | Popular Tags |