KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnet > Repository > BundleOffer


1 /*
2  * BundleOffer.java
3  *
4  * Created on 19. bøezen 2004, 20:32
5  */

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 JavaDoc;
17
18 /**
19  * Representation of the bundle offer.
20  * One item of persistent storage.
21  * <p>
22  * The bundle offer contains:
23  * <ul>
24  * <li>name of node that can provide the bundle (that is offering the bundle)
25  * <li>list of components contained in the bundle
26  * </ul>
27  *
28  * @author Ladislav Sobr
29  */

30 public class BundleOffer extends XMLStorageItem implements StorageItem, Serializable
31 {
32   private String JavaDoc nodeName;
33   private List components;
34   
35   /** Creates a new instance of BundleOffer */
36   public BundleOffer(String JavaDoc 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   /**
48    * Resets content of the item.
49    */

50   protected void reset()
51   {
52     nodeName = "";
53     components.clear();
54   }
55   
56   /**
57    * Loads the item from the XML DOM tree.
58    * <p>
59    * XML format of storage item:
60    * <p>
61    * <pre>
62    * &lt;bundle_offer&gt;
63    * BundleOffer XML format
64    * &lt;/bundle_offer&gt;
65    * </pre>
66    * <p>
67    * XML format:
68    * <p>
69    * <pre>
70    * &lt;node name="string"&gt;
71    * *&lt;component name="string" implementation_version="string"/&gt;
72    * </pre>
73    *
74    */

75   public void loadFromXML(Element element) throws SAXException JavaDoc
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 JavaDoc 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 JavaDoc name = el.getAttribute("name");
98             String JavaDoc version = el.getAttribute("implementation_version");
99             if (name.length() == 0) throw new SAXException JavaDoc("Missing 'name' attribute in 'component' tag");
100             if (version.length() == 0) throw new SAXException JavaDoc("Missing 'implementation_version' attribute in 'component' tag");
101             components.add(new ComponentInfoImpl(name, version));
102           }
103         }
104       }
105     }
106   }
107   
108   /**
109    * Saves the item to XML DOM tree.
110    */

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 JavaDoc getNodeName()
137   {
138     return nodeName;
139   }
140   
141   public void setNodeName(String JavaDoc nodeName)
142   {
143     this.nodeName = nodeName;
144   }
145   
146   
147   /**
148    * Generates BundleData structure (with "binary bundle file" in temp)
149    *
150    * @return null on error
151    */

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 JavaDoc 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