1 2 24 package org.enhydra.tool.archive; 25 26 import org.enhydra.tool.archive.xml.DescriptorHandler; 28 import org.enhydra.tool.archive.xml.EjbDescriptorHandler; 29 import org.enhydra.tool.common.PathHandle; 30 31 import java.io.InputStream ; 33 import java.io.BufferedInputStream ; 34 import java.io.FileInputStream ; 35 import java.io.FileNotFoundException ; 36 37 public class Descriptor { 39 public static final String EJB = "ejb-jar"; 40 public static final String JONAS_EJB = "jonas-ejb-jar"; 41 public static final String WEB = "web"; 42 public static final String ENHYDRA_WEB = "enhydra-web"; 43 public static final String ENHYDRA_SERVICE = "enhydra-service"; 44 45 private boolean required = false; 47 private String type = new String (); 48 private String path = new String (); 49 50 public static String getArchivePath(String type) { 51 StringBuffer buf = new StringBuffer (); 52 53 if (type.endsWith("web")) { 54 buf.append("WEB-INF/"); 55 } else { 56 buf.append("META-INF/"); 57 } 58 buf.append(type); 59 buf.append(".xml"); 60 return buf.toString(); 61 } 62 63 public Descriptor(boolean r, String t) { 64 required = r; 65 type = t; 66 } 67 68 public Descriptor(String p) { 69 PathHandle ph = null; 70 setPath(p); 71 required = false; 72 ph = PathHandle.createPathHandle(getPath()); 73 type = ph.getFile().getName(); 74 type = type.substring(0, type.indexOf('.')); 75 } 76 77 public boolean isRequired() { 78 return required; 79 } 80 81 public String getType() { 82 return type; 83 } 84 85 public String getPath() { 86 return path; 87 } 88 89 public void setPath(String p) { 90 if (p == null) { 91 path = new String (); 92 } else { 93 PathHandle ph = PathHandle.createPathHandle(p); 94 95 path = ph.getPath(); 96 } 97 } 98 99 public InputStream getInputStream() throws ArchiveException { 100 DescriptorHandler dh = null; 101 InputStream stream = null; 102 103 if (getType().equals(Descriptor.EJB)) { 104 dh = new EjbDescriptorHandler(); 105 } 106 if (dh != null) { 107 dh.setSource(getPath()); 108 dh.prep(); 109 stream = dh.getInputStream(); 110 } else { 111 FileInputStream fileStream = null; 112 BufferedInputStream bufStream = null; 113 114 try { 115 fileStream = new FileInputStream (getPath()); 116 bufStream = new BufferedInputStream (fileStream); 117 stream = bufStream; 118 } catch (FileNotFoundException e) { 119 throw new ArchiveException(e, 120 "Invalid descriptor: " 121 + getPath()); 122 } 123 } 124 return stream; 125 } 126 127 protected String getArchivePath() { 128 return Descriptor.getArchivePath(getType()); 129 } 130 131 protected boolean isValid() { 132 boolean valid = true; 133 134 try { 135 validate(); 136 } catch (ArchiveException e) { 137 valid = false; 138 } 139 return valid; 140 } 141 142 protected void validate() throws ArchiveException { 143 PathHandle ph = PathHandle.createPathHandle(getPath()); 144 145 if (!ph.isFile()) { 146 if (isRequired()) { 147 throw new ArchiveException("Required descriptor not found: " 148 + ph.getPath()); 149 } else if (!ph.isEmpty()) { 150 throw new ArchiveException("Invalid descriptor path: " 151 + ph.getPath()); 152 } 153 } 154 } 155 156 public boolean equals(Object comp) { 157 Descriptor d = null; 158 boolean equal = false; 159 160 if (comp instanceof Descriptor) { 161 d = (Descriptor) comp; 162 equal = d.isRequired() == isRequired(); 163 if (equal) { 164 equal = d.getType().equals(getType()); 165 } 166 if (equal) { 167 equal = d.getPath().equals(getPath()); 168 } 169 } 170 return equal; 171 } 172 173 } 174 | Popular Tags |