1 6 7 package SOFA.SOFAnet.Repository; 8 9 import java.io.*; 10 import java.util.*; 11 import SOFA.SOFAnode.TR.Impl.ComponentInfoImpl; 12 import SOFA.SOFAnode.TR.Impl.BundleImpl; 13 import SOFA.SOFAnet.Transport.BundleData; 14 import SOFA.Util.XML; 15 import org.w3c.dom.*; 16 import org.xml.sax.SAXException ; 17 18 30 public class BundleOffer extends XMLStorageItem implements StorageItem, Serializable 31 { 32 private String nodeName; 33 private List components; 34 35 36 public BundleOffer(String name, File file) 37 { 38 super(name, file, "bundle_offer"); 39 components = Collections.synchronizedList(new LinkedList()); 40 } 41 42 public List getComponents() 43 { 44 return components; 45 } 46 47 50 protected void reset() 51 { 52 nodeName = ""; 53 components.clear(); 54 } 55 56 75 public void loadFromXML(Element element) throws SAXException 76 { 77 synchronized (components) 78 { 79 components.clear(); 80 NodeList nl = element.getChildNodes(); 81 for (int i = 0; i < nl.getLength(); i++) 82 { 83 Node node = nl.item(i); 84 if (node.getNodeType() == Node.ELEMENT_NODE) 85 { 86 Element el = (Element)node; 87 String nodeN = node.getNodeName(); 88 89 90 if (nodeN.compareTo("node") == 0) 91 { 92 nodeName = el.getAttribute("name"); 93 } 94 else 95 if (nodeN.compareTo("component") == 0) 96 { 97 String name = el.getAttribute("name"); 98 String version = el.getAttribute("implementation_version"); 99 if (name.length() == 0) throw new SAXException ("Missing 'name' attribute in 'component' tag"); 100 if (version.length() == 0) throw new SAXException ("Missing 'implementation_version' attribute in 'component' tag"); 101 components.add(new ComponentInfoImpl(name, version)); 102 } 103 } 104 } 105 } 106 } 107 108 111 public void saveToXML(Element element) 112 { 113 114 synchronized (components) 115 { 116 Document doc = element.getOwnerDocument(); 117 Element el; 118 119 el = doc.createElement("node"); 120 el.setAttribute("name", nodeName); 121 element.appendChild(el); 122 123 Iterator it = components.iterator(); 124 while (it.hasNext()) 125 { 126 ComponentInfoImpl ci = (ComponentInfoImpl)it.next(); 127 el = doc.createElement("component"); 128 el.setAttribute("name", ci.getName()); 129 el.setAttribute("implementation_version", ci.getImplementationVersion()); 130 element.appendChild(el); 131 } 132 } 133 } 134 135 136 public String getNodeName() 137 { 138 return nodeName; 139 } 140 141 public void setNodeName(String nodeName) 142 { 143 this.nodeName = nodeName; 144 } 145 146 147 152 153 public BundleData generateBundleData() 154 { 155 BundleImpl bundleOff = new BundleImpl(); 156 synchronized (components) 157 { 158 Iterator it = components.iterator(); 159 while (it.hasNext()) 160 { 161 ComponentInfoImpl ci = (ComponentInfoImpl)it.next(); 162 bundleOff.add(new ComponentInfoImpl(ci.getName(), ci.getImplementationVersion()), BundleImpl.NO_FILES); 163 } 164 } 165 166 File tempFile = new File(BundleData.generateFilename()); 167 168 try 169 { 170 FileOutputStream os = new FileOutputStream(tempFile); 171 bundleOff._write(os); 172 os.close(); 173 } 174 catch (Exception e) 175 { 176 bundleOff = null; 177 return null; 178 } 179 180 bundleOff = null; 181 182 return new BundleData(tempFile, false); 183 } 184 185 } 186 | Popular Tags |