KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * LocalInfo.java
3  *
4  * Created on 18. b°ezen 2004, 23:03
5  */

6
7 package SOFA.SOFAnet.Repository;
8
9 import java.io.*;
10 import java.util.*;
11 import java.text.*;
12 import SOFA.Util.XML;
13 import org.w3c.dom.*;
14 import org.xml.sax.SAXException JavaDoc;
15
16 /**
17  * Representation of the local informations about the bundle.
18  * One item of persistent storage.
19  * <p>
20  * The local info contains:
21  * <ul>
22  * <li>state of the bundle
23  * <ul>
24  * <li>installed
25  * <li>in the memory (+ nonpersistent counter of usage of bundle)
26  * <li>shared (share manager or share client)
27  * </ul>
28  * <li>local licence of the bundle (used also for sharing)
29  * <li>share manager information:
30  * <ul>
31  * <li>list of clients
32  * <li>share groups
33  * <li>filter of nodes that can become clients
34  * <li>whether share manager is equal to share clients (if so, all copies of bundle can be gone to clients and no copy remains on the share manager)
35  * </ul>
36  * <li>share client information:
37  * <ul>
38  * <li>name of node with share manager
39  * </ul>
40  * </ul>
41  *
42  * @author Ladislav Sobr
43  */

44 public class LocalInfo extends XMLStorageItem implements StorageItem, Serializable
45 {
46   final static public int STATE_NONE = 0;
47   final static public int STATE_INSTALLED = 1;
48   final static public int STATE_SHARE_MANAGER = 2;
49   final static public int STATE_SHARE_CLIENT = 4;
50   final static public int STATE_SHARE_PRECLIENT = 8; //temporaty state during instatiating of share client
51

52   private Licence licence;
53   private int state;
54   private ShareGroups smShareGroups; // for STATE_SHARE_MANAGER
55
private NodeNameFilter smNodeFilter; // for STATE_SHARE_MANAGER
56
private NodeNameList smClients; // for STATE_SHARE_MANAGER
57
private boolean smEquality; // for STATE_SHARE_MANAGER
58
private String JavaDoc scManager; // for STATE_SHARE_CLIENT
59
private int inMemoryCounter;
60   private boolean inMemory;
61   
62   /** Creates a new instance of LocalInfo */
63   public LocalInfo(String JavaDoc name, File file)
64   {
65     super(name, file, "local_info");
66     reset();
67   }
68
69   /**
70    * Resets content of the item.
71    */

72   protected void reset()
73   {
74     licence = new Licence();
75     state = STATE_NONE;
76     smShareGroups = null;
77     smNodeFilter = null;
78     smClients = null;
79     smEquality = false;
80     scManager = "";
81     inMemoryCounter = 0;
82     inMemory = false;
83   }
84
85   /**
86    * Loads the item from the XML DOM tree.
87    * <p>
88    * XML format of storage item:
89    * <p>
90    * <pre>
91    * &lt;local_info&gt;
92    * LocalInfo XML format
93    * &lt;/local_info&gt;
94    * </pre>
95    * <p>
96    * XML format:
97    * <p>
98    * <pre>
99    * &lt;licence&gt;
100    * Licence XML format
101    * &lt;/licence&gt;
102    * &lt;state ?installed="boolean" ?sharing="MANAGER or CLIENT or PRECLIENT"/ ?in_memory="boolean"&gt;
103    * ?&lt;share_manager equality="boolean"&gt;
104    * ?&lt;share_groups&gt;
105    * ShareGroups XML format
106    * &lt;/share_groups&gt;
107    * ?&lt;node_filter&gt;
108    * NodeNameFilter XML format
109    * &lt;/node_filter&gt;
110    * &lt;clients&gt;
111    * NodeNameList XML format
112    * &lt;/clients&gt;
113    * &lt;/share_manager&gt;
114    * ?&lt;share_client manager="string"/&gt;
115    * </pre>
116    *
117    */

118   public void loadFromXML(Element element) throws SAXException JavaDoc
119   {
120     reset();
121     NodeList nl = element.getChildNodes();
122     for (int i = 0; i < nl.getLength(); i++)
123     {
124       Node node = nl.item(i);
125       if (node.getNodeType() == Node.ELEMENT_NODE)
126       {
127         Element el = (Element)node;
128         String JavaDoc nodeName = node.getNodeName();
129         
130         if (nodeName.compareTo("licence") == 0)
131         {
132           licence.loadFromXML(el);
133         }
134         else
135         if (nodeName.compareTo("state") == 0)
136         {
137           String JavaDoc installed = el.getAttribute("installed").trim().toLowerCase();
138           if (installed.compareTo("1") == 0 ||
139               installed.compareTo("true") == 0) state |= STATE_INSTALLED;
140           
141           String JavaDoc sharing = el.getAttribute("sharing").trim().toLowerCase();
142           if (sharing.length() > 0)
143           {
144             if (sharing.compareTo("manager") == 0) state |= STATE_SHARE_MANAGER;
145             else if (sharing.compareTo("client") == 0) state |= STATE_SHARE_CLIENT;
146             else if (sharing.compareTo("preclient") == 0) state |= STATE_SHARE_PRECLIENT;
147             else throw new SAXException JavaDoc("Invalid content of 'sharing' attribute ('state' tag)");
148           }
149           
150           String JavaDoc in_memory = el.getAttribute("in_memory").trim().toLowerCase();
151           if (in_memory.compareTo("1") == 0 ||
152               in_memory.compareTo("true") == 0)
153           {
154             inMemory = true;
155             inMemoryCounter = 1; //fake value
156
}
157         }
158         else
159         if (nodeName.compareTo("share_manager") == 0)
160         {
161           String JavaDoc equality = el.getAttribute("equality").trim().toLowerCase();
162           if (equality.compareTo("1") == 0 ||
163               equality.compareTo("true") == 0) smEquality = true;
164           
165           NodeList nl2 = el.getChildNodes();
166           for (int i2 = 0; i2 < nl2.getLength(); i2++)
167           {
168             Node node2 = nl2.item(i2);
169             if (node2.getNodeType() == Node.ELEMENT_NODE)
170             {
171               Element el2 = (Element)node2;
172               String JavaDoc nodeName2 = node2.getNodeName();
173
174               if (nodeName2.compareTo("share_groups") == 0)
175               {
176                 smShareGroups = new ShareGroups();
177                 smShareGroups.loadFromXML(el2);
178               }
179               else
180               if (nodeName2.compareTo("node_filter") == 0)
181               {
182                 smNodeFilter = new NodeNameFilter();
183                 smNodeFilter.loadFromXML(el2);
184               }
185               else
186               if (nodeName2.compareTo("clients") == 0)
187               {
188                 smClients = new NodeNameList();
189                 smClients.loadFromXML(el2);
190               }
191             }
192           }
193         }
194         else
195         if (nodeName.compareTo("share_client") == 0)
196         {
197           scManager = el.getAttribute("manager");
198         }
199       }
200     }
201     
202     if (isShareManager() && smClients == null) smClients = new NodeNameList();
203   }
204   
205   /**
206    * Saves the item to XML DOM tree.
207    */

208   public void saveToXML(Element element)
209   {
210     Document doc = element.getOwnerDocument();
211     Element el;
212   
213     el = doc.createElement("licence");
214     licence.saveToXML(el);
215     element.appendChild(el);
216     
217     el = doc.createElement("state");
218     
219     if ((state & STATE_INSTALLED) != 0) el.setAttribute("installed", "true");
220     else el.setAttribute("installed", "false");
221
222     if ((state & STATE_SHARE_MANAGER) != 0) el.setAttribute("sharing", "manager");
223     else if ((state & STATE_SHARE_CLIENT) != 0) el.setAttribute("sharing", "client");
224     else if ((state & STATE_SHARE_PRECLIENT) != 0) el.setAttribute("sharing", "preclient");
225     
226     if (inMemory) el.setAttribute("in_memory", "true");
227     
228     element.appendChild(el);
229
230     if (isShareManager())
231     {
232       el = doc.createElement("share_manager");
233       
234       if (smEquality) el.setAttribute("equality", "true");
235       else el.setAttribute("equality", "false");
236       
237       if (smShareGroups != null)
238       {
239         Element el2 = doc.createElement("share_groups");
240         smShareGroups.saveToXML(el2);
241         el.appendChild(el2);
242       }
243       
244       if (smNodeFilter != null)
245       {
246         Element el2 = doc.createElement("node_filter");
247         smNodeFilter.saveToXML(el2);
248         el.appendChild(el2);
249       }
250       
251       if (smClients != null)
252       {
253         Element el2 = doc.createElement("clients");
254         smClients.saveToXML(el2);
255         el.appendChild(el2);
256       }
257       
258       element.appendChild(el);
259     }
260     
261     if (isShareClient())
262     {
263       el = doc.createElement("share_client");
264       el.setAttribute("manager", scManager);
265       element.appendChild(el);
266     }
267   }
268   
269   public Licence getLicence()
270   {
271     return licence;
272   }
273   
274   public int getState()
275   {
276     return state;
277   }
278   
279   public ShareGroups getSMShareGroups()
280   {
281     return smShareGroups;
282   }
283   
284   public NodeNameFilter getSMNodeFilter()
285   {
286     return smNodeFilter;
287   }
288   
289   public NodeNameList getSMClients()
290   {
291     return smClients;
292   }
293   
294   public boolean isSMEquality()
295   {
296     return smEquality;
297   }
298   
299   public String JavaDoc getSCManager()
300   {
301     return scManager;
302   }
303   
304   public int getInMemoryCounter()
305   {
306     return inMemoryCounter;
307   }
308   
309   public void setLicence(Licence licence)
310   {
311     this.licence = licence;
312   }
313   
314   public void setState(int state)
315   {
316     this.state = state;
317   }
318   
319   public void setSMShareGroups(ShareGroups shareGroups)
320   {
321     this.smShareGroups = shareGroups;
322   }
323   
324   public void setSMNodeFilter(NodeNameFilter nodeFilter)
325   {
326     this.smNodeFilter = nodeFilter;
327   }
328   
329   public void setSMClients(NodeNameList clients)
330   {
331     this.smClients = clients;
332   }
333   
334   public void setSMEquality(boolean smEquality)
335   {
336     this.smEquality = smEquality;
337   }
338   
339   public void setSCManager(String JavaDoc manager)
340   {
341     this.scManager = manager;
342   }
343   
344   /**
345    * @return true, if inMemory flag has been changed and local info should be saved to storage
346    */

347   public boolean setInMemoryCounter(int inMemoryCounter)
348   {
349     this.inMemoryCounter = inMemoryCounter;
350     boolean oldInMemory = inMemory;
351     inMemory = inMemoryCounter > 0;
352     return oldInMemory != inMemory;
353   }
354   
355   public void increaseInMemoryCounter()
356   {
357     if (inMemoryCounter == 0) inMemory = true;
358     inMemoryCounter++;
359   }
360   
361   public void decreaseInMemoryCounter()
362   {
363     inMemoryCounter--;
364     if (inMemoryCounter < 0) inMemoryCounter = 0;
365     if (inMemoryCounter == 0) inMemory = false;
366   }
367   
368   public boolean isInstalled()
369   {
370     return (state & STATE_INSTALLED) != 0;
371   }
372   
373   public void setInstalled(boolean installed)
374   {
375     if (installed) state |= STATE_INSTALLED;
376     else state &= ~STATE_INSTALLED;
377   }
378   
379   public boolean isShareManager()
380   {
381     return (state & STATE_SHARE_MANAGER) != 0;
382   }
383   
384   public void setShareManager(boolean shareManager)
385   {
386     if (shareManager) state |= STATE_SHARE_MANAGER;
387     else state &= ~STATE_SHARE_MANAGER;
388   }
389   
390   public boolean isShareClient()
391   {
392     return (state & STATE_SHARE_CLIENT) != 0;
393   }
394
395   public boolean areShareClientModes()
396   {
397     return (state & (STATE_SHARE_CLIENT | STATE_SHARE_PRECLIENT)) != 0;
398   }
399
400   public boolean isSharePreClient()
401   {
402     return (state & STATE_SHARE_PRECLIENT) != 0;
403   }
404
405   public void setShareClient(boolean shareClient)
406   {
407     if (shareClient) state |= STATE_SHARE_CLIENT;
408     else state &= ~STATE_SHARE_CLIENT;
409   }
410   
411   public void setSharePreClient(boolean sharePreClient)
412   {
413     if (sharePreClient) state |= STATE_SHARE_PRECLIENT;
414     else state &= ~STATE_SHARE_PRECLIENT;
415   }
416   
417   public boolean isInSharing()
418   {
419     return (state & (STATE_SHARE_MANAGER | STATE_SHARE_CLIENT | STATE_SHARE_PRECLIENT)) != 0;
420   }
421   
422   public boolean isInMemory()
423   {
424     return inMemory;
425   }
426   
427   public void setInMemoryFlag(boolean inMemory)
428   {
429     this.inMemory = inMemory;
430   }
431
432 }
433
Popular Tags