1 2 24 package org.enhydra.tool.archive.wizard; 25 26 import org.enhydra.tool.ToolBoxInfo; 28 import org.enhydra.tool.archive.Descriptor; 29 import org.enhydra.tool.archive.EarPlan; 30 import org.enhydra.tool.archive.EjbPlan; 31 import org.enhydra.tool.archive.JarPlan; 32 import org.enhydra.tool.archive.ServicePlan; 33 import org.enhydra.tool.archive.WarPlan; 34 import org.enhydra.tool.archive.ArchiveException; 35 import org.enhydra.tool.archive.ArchiveTool; 36 import org.enhydra.tool.archive.ManifestKeys; 37 import org.enhydra.tool.common.PathHandle; 38 import org.enhydra.tool.common.SwingUtil; 39 import org.enhydra.tool.common.ToolException; 40 41 import javax.swing.*; 43 import javax.swing.border.*; 44 import java.awt.*; 45 import java.awt.event.ActionEvent ; 46 import java.awt.event.ActionListener ; 47 import java.io.BufferedInputStream ; 48 import java.io.File ; 49 import java.io.FileInputStream ; 50 import java.io.FileNotFoundException ; 51 import java.io.IOException ; 52 import java.util.Properties ; 53 import java.util.ArrayList ; 54 import java.util.ResourceBundle ; 55 import java.util.jar.JarInputStream ; 56 import java.util.jar.JarEntry ; 57 import java.util.jar.Manifest ; 58 59 abstract public class ArchiveType implements ManifestKeys { 61 private boolean mru = true; 62 private JarPlan plan = null; 63 private ArchivePanel[] panels = new ArchivePanel[0]; 64 65 static public ArchiveType createType(JarPlan plan) { 66 ArchiveType type = null; 67 68 if (plan instanceof WarPlan) { 69 type = new WarType(); 70 type.setPlan(plan); 71 } else if (plan instanceof EjbPlan) { 72 type = new EjbType(); 73 type.setPlan(plan); 74 } else if (plan instanceof EarPlan) { 75 type = new EarType(); 76 type.setPlan(plan); 77 } else if (plan instanceof ServicePlan) { 78 type = new ServiceType(); 79 type.setPlan(plan); 80 } else { 81 82 type = new E3Type(); 84 type.setPlan(plan); 85 } 86 return type; 87 } 88 89 static protected ArchiveType[] getAllTypes() { 91 ArchiveType[] types = new ArchiveType[0]; 92 93 if (ToolBoxInfo.isEnhydra3()) { 94 types = new ArchiveType[2]; 95 types[0] = new WarType(); 96 types[1] = new E3Type(); 97 } else { 98 types = new ArchiveType[5]; 99 types[0] = new WarType(); 100 types[1] = new EjbType(); 101 types[2] = new EarType(); 102 types[3] = new ServiceType(); 103 types[4] = new E3Type(); 104 } 105 return types; 106 } 107 108 public void setMostRecentlyUsed(boolean b) { 110 mru = b; 111 } 112 public boolean isMostRecentlyUsed() { 113 return mru; 114 } 115 116 public String toString() { 117 return getSelectionName(); 118 } 119 120 public void readArchive(String archive) { 121 JarInputStream jar = null; 122 BufferedInputStream buf = null; 123 Manifest manifest = null; 124 JarEntry entry = null; 125 ArrayList list = new ArrayList (); 126 String [] entries = new String [0]; 127 PathHandle path = PathHandle.createPathHandle(archive); 128 129 if (path.isFile()) { 130 try { 131 getPlan().setArchivePath(path.getPath()); 132 buf = 133 new BufferedInputStream (new FileInputStream (path.getFile())); 134 jar = new JarInputStream (buf); 135 manifest = jar.getManifest(); 136 entry = jar.getNextJarEntry(); 137 while (entry != null) { 138 list.add(entry.getName()); 139 entry = jar.getNextJarEntry(); 140 } 141 list.trimToSize(); 142 entries = new String [list.size()]; 143 entries = (String []) list.toArray(entries); 144 list.clear(); 145 readArchive(manifest, entries); 146 jar.close(); 147 buf.close(); 148 } catch (FileNotFoundException e) { 149 e.printStackTrace(System.err); 150 } catch (IOException e) { 151 e.printStackTrace(System.err); 152 } catch (ArchiveException e) { 153 e.printStackTrace(System.err); 154 } 155 } 156 } 157 158 public JarPlan getPlan() throws ArchiveException { 159 if (plan == null) { 160 initPlan(); 161 } 162 return plan; 163 } 164 165 abstract protected String getSelectionName(); 167 abstract protected String getDescription(); 168 abstract protected String getExtension(); 169 abstract protected String getType(); 170 abstract protected void initPanels() throws ArchiveException; 171 abstract protected void initPlan() throws ArchiveException; 172 protected void setPlan(JarPlan p) { 173 plan = p; 174 } 175 176 protected String getClassIncludeRoot() { 178 return new String (); 179 } 180 181 protected boolean readArchive(Manifest manifest, String [] entries) { 182 boolean read = true; 183 PathHandle root = null; 184 PathHandle cursor = null; 185 String [] files = new String [0]; 186 Descriptor[] dd = new Descriptor[0]; 187 ArrayList list = new ArrayList (); 188 189 if ((manifest == null)) { 190 read = false; 191 } else { 192 String value = null; 193 194 try { 195 196 value = manifest.getMainAttributes().getValue(CLASS_ROOT); 198 root = PathHandle.createPathHandle(value); 199 if (root.isDirectory()) { 200 getPlan().setClassRoot(root.getPath()); 201 for (int i = 0; i < entries.length; i++) { 202 if (entries[i].toUpperCase().startsWith(getClassIncludeRoot())) { 203 cursor = 204 PathHandle.createPathHandle(root.getPath() 205 + File.separator 206 + entries[i].substring(getClassIncludeRoot().length())); 207 if (cursor.isFile()) { 208 list.add(cursor.getPath()); 209 } 210 } 211 } 212 list.trimToSize(); 213 files = new String [list.size()]; 214 files = (String []) list.toArray(files); 215 list.clear(); 216 getPlan().setClassFiles(files); 217 } 218 219 for (int i = 0; i < 100; i++) { 221 value = manifest.getMainAttributes().getValue(LIBRARY 222 + i); 223 cursor = PathHandle.createPathHandle(value); 224 if (cursor.isFile()) { 225 list.add(cursor.getPath()); 226 } 227 } 228 files = new String [list.size()]; 229 files = (String []) list.toArray(files); 230 list.clear(); 231 getPlan().setLibFiles(files); 232 233 dd = getPlan().getDescriptors(); 235 for (int i = 0; i < dd.length; i++) { 236 value = 237 manifest.getMainAttributes().getValue(dd[i].getType()); 238 cursor = PathHandle.createPathHandle(value); 239 if (cursor.isFile()) { 240 dd[i].setPath(cursor.getPath()); 241 } 242 } 243 getPlan().setDescriptors(dd); 244 } catch (ArchiveException e) { 245 e.printStackTrace(System.err); 246 } 247 } 248 return read; 249 } 250 251 protected String getWizardTitle() { 253 StringBuffer buf = new StringBuffer (); 254 255 buf.append(getSelectionName()); 256 buf.append(" Wizard"); 257 return buf.toString(); 258 } 259 260 protected ArchivePanel[] getWizardPanels() throws ArchiveException { 262 if (panels.length == 0) { 263 initPanels(); 264 } 265 return panels; 266 } 267 268 protected void setWizardPanels(ArchivePanel[] p) throws ArchiveException { 269 panels = p; 270 for (int i = 0; i < panels.length; i++) { 271 panels[i].readPlan(getPlan()); 272 } 273 } 274 275 protected File build() throws ArchiveException { 276 ArchiveTool tool = new ArchiveTool(); 277 278 return tool.buildArchive(getPlan()); 279 } 280 281 protected void saveDefaults() { 282 saveDefaultArchivePath(); 283 } 284 285 private void saveDefaultArchivePath() { 286 Properties props = null; 287 288 try { 289 props = ToolBoxInfo.loadProperties(); 290 props.setProperty(getType() + ".archive", 291 plan.getArchivePath()); 292 ToolBoxInfo.storeProperties(props); 293 } catch (ToolException e) { 294 e.printStackTrace(System.err); 295 props = new Properties (); 296 } 297 } 298 299 protected String getDefaultArchivePath() { 300 Properties props = null; 301 StringBuffer def = new StringBuffer (); 302 String path = null; 303 304 def.append(System.getProperty("user.dir")); 305 def.append(File.separator); 306 def.append("untitled."); 307 def.append(getExtension()); 308 try { 309 props = ToolBoxInfo.loadProperties(); 310 } catch (ToolException e) { 311 e.printStackTrace(System.err); 312 props = new Properties (); 313 } 314 if (isMostRecentlyUsed()) { 315 path = props.getProperty(getType() + ".archive", def.toString()); 316 } else { 317 path = def.toString(); 318 } 319 path = PathHandle.createPathString(path); 320 return path; 321 } 322 323 } 324 | Popular Tags |