1 2 24 package org.enhydra.tool.archive; 25 26 import org.enhydra.tool.archive.wizard.ArchiveType; 28 import org.enhydra.tool.archive.wizard.E3Type; 29 import org.enhydra.tool.archive.wizard.EarType; 30 import org.enhydra.tool.archive.wizard.EjbType; 31 import org.enhydra.tool.archive.wizard.WarType; 32 import org.enhydra.tool.common.PathHandle; 33 34 import java.io.BufferedOutputStream ; 36 import java.io.IOException ; 37 import java.io.File ; 38 import java.io.FileNotFoundException ; 39 import java.io.FileOutputStream ; 40 import java.util.Properties ; 41 import java.util.jar.JarFile ; 42 import java.util.jar.Manifest ; 43 44 public class PropertyWriter implements PropertyKeys, ManifestKeys { 46 47 private final String [] types = { 49 E3Type.EXT, EarType.EXT, EjbType.EXT, WarType.EXT 50 }; 51 private File file = null; 52 private Properties props = new Properties (); 53 private int count = 0; 54 55 public PropertyWriter() {} 56 57 public void write(JarPlan[] plans, File out) { 58 FileOutputStream fileStream = null; 59 BufferedOutputStream bufStream = null; 60 61 file = out; 62 write(plans); 63 try { 64 fileStream = new FileOutputStream (file); 65 bufStream = new BufferedOutputStream (fileStream); 66 props.store(bufStream, "Kelp Archive Properties"); 67 fileStream.flush(); 68 bufStream.flush(); 69 bufStream.close(); 70 fileStream.close(); 71 } catch (FileNotFoundException e) { 72 e.printStackTrace(System.err); 73 } catch (IOException e) { 74 e.printStackTrace(System.err); 75 } 76 } 77 78 public Properties write(JarPlan[] plans) { 79 for (int i = 0; i < plans.length; i++) { 80 write(plans[i]); 81 } 82 return props; 83 } 84 85 public Properties write(String [] jars) { 86 87 for (int i = 0; i < jars.length; i++) { 90 write(jars[i]); 91 } 92 return props; 93 } 94 95 public Properties write(JarPlan plan) { 96 String value = null; 97 98 value = plan.getArchivePath(); 99 props.setProperty(PATH + count, pathValue(value)); 100 if (plan instanceof WarPlan) { 101 writeWarProps((WarPlan) plan); 102 } else if (plan instanceof EarPlan) { 103 writeEarProps((EarPlan) plan); 104 } else if (plan instanceof EjbPlan) { 105 writeEjbProps((EjbPlan) plan); 106 } else { 107 writeJarProps(plan); 108 } 109 count++; 110 return props; 111 } 112 113 private void write(String archive) { 114 ArchiveType type = null; 115 PathHandle ph = null; 116 JarPlan plan = null; 117 118 ph = PathHandle.createPathHandle(archive); 119 type = getType(ph.getPath()); 120 if (type != null) { 121 type.readArchive(ph.getPath()); 122 try { 123 plan = type.getPlan(); 124 } catch (ArchiveException e) { 125 e.printStackTrace(System.err); 126 } 127 write(plan); 128 } 129 } 130 131 private void writeWarProps(WarPlan plan) { 132 String [] files = new String [0]; 133 String root = null; 134 String dd = null; 135 136 writeJarProps(plan); 137 138 files = plan.getContentFiles(); 140 root = plan.getContentRoot(); 141 props.setProperty(CONTENT_ROOT_PROP + count, pathValue(root)); 142 if (files != null) { 143 for (int i = 0; i < files.length; i++) { 144 props.setProperty(CONTENT + count + "." + i, 145 pathValue(files[i])); 146 } 147 } 148 149 files = plan.getLibFiles(); 151 for (int i = 0; i < files.length; i++) { 152 props.setProperty(LIBRARY_PROP + count + "." + i, 153 pathValue(files[i])); 154 } 155 } 156 157 private void writeEarProps(EarPlan plan) { 158 String [] files = new String [0]; 159 Descriptor[] dd = new Descriptor[0]; 160 WebApplication[] webApps = new WebApplication[0]; 161 Boolean b = null; 162 163 props.setProperty(APP_NAME_PROP + count, plan.getAppName()); 165 166 props.setProperty(APP_DESC_PROP + count, plan.getDescription()); 168 169 b = new Boolean (plan.isAlt()); 171 props.setProperty(ALT_DD_PROP + count, b.toString()); 172 173 b = new Boolean (plan.isCopyClient()); 175 props.setProperty(COPY_CLIENT_PROP + count, b.toString()); 176 177 files = plan.getEnterpriseModules(); 179 for (int i = 0; i < files.length; i++) { 180 props.setProperty(ENTERPRISE_MODULE_PROP + count + "." + i, 181 pathValue(files[i])); 182 } 183 184 webApps = plan.getWebApplications(); 186 for (int i = 0; i < webApps.length; i++) { 187 props.setProperty(WEBAPP_MODULE_PROP + count + "." + i, 188 pathValue(webApps[i].getArchive())); 189 props.setProperty(WEBAPP_CONTEXT_PROP + count + "." + i, 190 webApps[i].getContextRoot()); 191 } 192 193 dd = plan.getDescriptors(); 195 for (int i = 0; i < dd.length; i++) { 196 props.setProperty(META_DD_PROP + count + "." + i, 197 pathValue(dd[i].getPath())); 198 } 199 200 files = plan.getLibFiles(); 202 for (int i = 0; i < files.length; i++) { 203 props.setProperty(LIBRARY_PROP + count + "." + i, 204 pathValue(files[i])); 205 } 206 207 } 209 210 private void writeEjbProps(EjbPlan plan) { 211 Boolean b = null; 212 213 writeJarProps(plan); 214 215 b = new Boolean (plan.isCreateClient()); 217 props.setProperty(CREATE_CLIENT_PROP + count, b.toString()); 218 } 219 220 private void writeJarProps(JarPlan plan) { 221 String [] files = new String [0]; 222 Descriptor[] dd = new Descriptor[0]; 223 String root = null; 224 225 root = plan.getClassRoot(); 227 files = plan.getClassFiles(); 228 props.setProperty(CLASS_ROOT_PROP + count, pathValue(root)); 229 if (files != null) { 230 for (int i = 0; i < files.length; i++) { 231 props.setProperty(CLASS + count + "." + i, 232 pathValue(files[i])); 233 } 234 } 235 236 dd = plan.getDescriptors(); 238 for (int i = 0; i < dd.length; i++) { 239 props.setProperty(dd[i].getType() + "." + count, 240 pathValue(dd[i].getPath())); 241 } 242 } 243 244 private ArchiveType getType(String archive) { 245 PathHandle ph = null; 246 JarFile jar = null; 247 Manifest manifest = null; 248 ArchiveType type = null; 249 String value = null; 250 251 ph = PathHandle.createPathHandle(archive); 252 if (ph.isFile() && ph.getFile().canRead() && ph.hasExtension(types)) { 253 try { 254 jar = new JarFile (ph.getFile()); 255 manifest = jar.getManifest(); 256 value = manifest.getMainAttributes().getValue(KELP_VERSION); 257 if (value == null) { 258 System.err.println("Kelp properties not found in manifest of: " 259 + ph.getPath()); 260 } else if (ph.hasExtension(WarType.EXT)) { 261 type = new WarType(); 262 } else if (ph.hasExtension(EarType.EXT)) { 263 type = new EarType(); 264 } else { 265 value = 266 manifest.getMainAttributes().getValue(CREATE_CLIENT); 267 if (value == null) { 268 type = new E3Type(); 269 } else { 270 type = new EjbType(); 271 } 272 } 273 } catch (IOException e) { 274 e.printStackTrace(System.err); 275 } 276 } 277 return type; 278 } 279 280 private String pathValue(String in) { 281 String out = null; 282 283 if (file == null) { 284 out = in; 285 } else { 286 PathHandle propPh = null; 287 PathHandle inPh = null; 288 289 propPh = PathHandle.createPathHandle(file); 290 inPh = PathHandle.createPathHandle(in); 291 out = propPh.getRelativePath(inPh); 292 } 293 return out; 294 } 295 296 } 297 | Popular Tags |