1 5 package SOFA.SOFAnode.TR.Impl; 6 7 import SOFA.SOFAnode.InOut.Bundle; 8 import SOFA.SOFAnode.InOut.InOutException; 9 import SOFA.SOFAnode.TR.ComponentInfo; 10 import SOFA.Util.Tools; 11 import SOFA.Util.VMProperties; 12 import cz.cuni.sofa.lib.InputStream; 13 import cz.cuni.sofa.lib.OutputStream; 14 import cz.cuni.sofa.lib.*; 15 16 import java.io.*; 17 import java.util.*; 18 import java.util.jar.*; 19 import java.lang.Object ; 20 21 29 public class BundleImpl implements Bundle, Serializable, Streamable { 30 31 34 private Hashtable components; 35 36 41 private static String sofaNodeName; 42 43 47 private File tempDir = null; 48 49 53 public static final ComponentFiles NO_FILES = new ComponentFiles(); 54 55 58 public BundleImpl () { 59 components = new Hashtable(); 60 sofaNodeName = "sofa://" + System.getProperty(VMProperties.NODE_NAME); 61 } 62 63 67 public ComponentInfo[] getComponents () { 68 Set keySet = components.keySet(); 69 int size = keySet.size(); 70 ComponentInfo[] comps = new ComponentInfo[size]; 71 keySet.toArray(comps); 72 return comps; 73 } 74 75 80 public boolean contains (ComponentInfo descr) { 81 return components.containsKey(descr); 82 } 83 84 90 public boolean containsComponent (ComponentInfo comp) { 91 ComponentFiles files = (ComponentFiles) components.get(comp); 92 return (files != null && files != NO_FILES); 93 } 94 95 102 public java.lang.Object getComponent (ComponentInfo descr) throws InOutException { 103 ComponentFiles files = (ComponentFiles) components.get(descr); 104 if (files == null) 105 throw new InOutException("Required component has not been stored in this bundle. " + descr); 106 return files; 107 } 108 109 114 public void add (ComponentInfoImpl comp, ComponentFiles files) { 115 comp.setLocation(comp.getName() + comp.getImplementationVersion()); 116 components.put(comp, files); 117 } 118 119 125 public void loadFromJar (BundleInputStream stream) throws IOException { 126 tempDir = stream.getTempRootDirectory(); Manifest manifest = stream.getManifest(); 128 129 Map entries = manifest.getEntries(); 130 for (BundleEntry entry = stream.getNextBundleEntry(); entry != null; entry = stream.getNextBundleEntry()) { 132 Attributes attrs = (Attributes) entries.remove(entry.getName()); 134 ComponentInfoImpl compInfo = getComponentInfo(attrs); 135 components.put(compInfo, entry.getFiles()); 136 137 } 138 stream.close(); 139 if (!entries.isEmpty()) { Iterator iter = entries.keySet().iterator(); 141 while (iter.hasNext()) { 142 String key = (String ) iter.next(); 143 Attributes attrs = (Attributes) entries.get(key); 144 145 147 ComponentInfoImpl compInfo = getComponentInfo(attrs); 148 components.put(compInfo, NO_FILES); 149 } 150 } 151 } 152 153 159 private ComponentInfoImpl getComponentInfo (Attributes attrs) { 160 String name = attrs.getValue("Component-Name"); 161 String version = attrs.getValue("Implementation-Version"); 162 String location = attrs.getValue("Component-Path"); 163 164 return new ComponentInfoImpl(name, version, location); 165 } 166 167 171 private String createManifest () { 172 173 StringBuffer manifest = new StringBuffer (); 174 manifest.append("Manifest-Version: 1.0\n"); 175 manifest.append("Created-By: "); 176 manifest.append(System.getProperty(VMProperties.NODE_NAME, "Unknown node")); 177 manifest.append("\n"); 178 179 manifest.append("\n"); 180 181 class MyVector extends Vector { String getComponentPath (ComponentInfo info) { 183 String name = info.getName(); 184 String shortName = name.substring(name.lastIndexOf("::") + 2); String finalName = shortName; 186 for (int i = 2; this.contains(finalName); i++) { finalName = shortName + Integer.toString(i); } 189 this.add(finalName); return finalName; 191 } 192 } 193 ; 194 195 MyVector compDirs = new MyVector(); 196 197 Enumeration comp = components.keys(); 198 while (comp.hasMoreElements()) { 199 ComponentInfoImpl info = (ComponentInfoImpl) comp.nextElement(); 200 ComponentFiles compFiles = (ComponentFiles) components.get(info); String name = compDirs.getComponentPath(info); 202 String location = (compFiles == NO_FILES) ? sofaNodeName : name; 203 info.setLocation(location); 204 manifest.append("Name: "); 205 manifest.append(name); 206 manifest.append("\n"); 207 manifest.append("Component-Name: "); 208 manifest.append(info.getName()); 209 manifest.append("\n"); 210 manifest.append("Implementation-Version: "); 211 manifest.append(info.getImplementationVersion()); 212 manifest.append("\n"); 213 manifest.append("Component-Path: "); 214 manifest.append(location); 215 manifest.append("\n"); 216 manifest.append("\n"); 217 manifest.append("\n"); 218 } 219 220 return manifest.toString(); 221 222 } 223 224 231 public void saveToJar (BundleOutputStream stream) throws IOException { 232 stream.putNextEntry(new BundleEntry(JarFile.MANIFEST_NAME)); 233 String manifest = createManifest(); 234 byte[] buffer = manifest.getBytes(); 235 stream.write(buffer); 236 237 Enumeration comp = components.keys(); 238 while (comp.hasMoreElements()) { 239 ComponentInfoImpl info = (ComponentInfoImpl) comp.nextElement(); ComponentFiles compFiles = (ComponentFiles) components.get(info); if (compFiles != NO_FILES) stream.putNextBundleEntry(new BundleEntry(info.getLocation(), compFiles)); } 244 245 stream.close(); 246 } 247 248 254 public void _read (InputStream is) throws IOException { 255 267 loadFromJar(new BundleInputStream(is)); 268 } 269 270 public void _read (java.io.InputStream is) throws IOException 271 { 272 loadFromJar(new BundleInputStream(is)); 273 } 274 275 281 public void _write (OutputStream os) throws IOException { 282 287 saveToJar(new BundleOutputStream(os)); 288 } 289 public void _write (java.io.OutputStream os) throws IOException 290 { 291 saveToJar(new BundleOutputStream(os)); 292 } 293 294 295 300 protected void finalize () throws Throwable { 301 if (tempDir != null) Tools.deleteSubTree(tempDir); 303 super.finalize(); 304 } 305 306 } 307 | Popular Tags |