KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * InputTrigger.java
3  *
4  * Created on 1. prosinec 2003, 20:47
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 input trigger.
18  * One item of persistent storage.
19  * <p>
20  * The input trigger contains:
21  * <ul>
22  * <li>at which operations the trigger should be used (push/pull/made)
23  * <li>whether is should be used on classical bundles, bundle offers or both
24  * <li>text description of the trigger
25  * <li>filter that specifies the bundles on which the trigger should be used (for push operations it also says which bundles can be accepted at node)
26  * <li>filter of nodes that must be passed to use the trigger (for push operations it also says from which nodes the bundles can be accepted)
27  * <li>time validity
28  * <li>(optional) ID and Rule of contract that is connected with the input trigger
29  * <li>actions that should be done when the trigger is "executed"
30  * <ul>
31  * <li>install
32  * <li>share
33  * <li>allow pull
34  * <li>push
35  * <li>push_offer
36  * <li>delete
37  * </ul>
38  * </ul>
39  *
40  * @author Ladislav Sobr
41  */

42 public class InputTrigger extends XMLStorageItem implements StorageItem, Serializable
43 {
44   final static public int SOURCE_NONE = 0;
45   final static public int SOURCE_PUSH = 1;
46   final static public int SOURCE_PULL = 2;
47   final static public int SOURCE_MADE = 4;
48
49   final static public int BUNDLETYPE_FULL = 0;
50   final static public int BUNDLETYPE_OFFER = 1;
51   final static public int BUNDLETYPE_ALL = 2;
52   
53   final static public int ACTION_INSTALL = 1;
54   final static public int ACTION_SHARE = 2;
55   final static public int ACTION_ALLOW_PULL = 4;
56   final static public int ACTION_PUSH = 8;
57   final static public int ACTION_PUSH_OFFER = 16;
58   final static public int ACTION_DELETE = 32;
59   
60   private int source;
61   private int bundleType;
62   private String JavaDoc description;
63   private boolean autoDelete;
64   private BundleNameFilter bundleFilter; //can be null
65
private NodeNameFilter nodeFilter; //can be null
66
private Date validFrom; //can be null
67
private Date validTo; //can be null
68
private boolean isContract;
69   private String JavaDoc contractID;
70   private String JavaDoc contractRule;
71   private int actions;
72   
73   //parameters of INSTALL action
74
//parameters of SHARE action
75
private int asNoLicences; // Licence.ONE_IMPLICIT_LICENCE means "one licence", Licence.ALL_LICENCES means "all remaining licences"
76
private ShareGroups asShareGroups; // empty list means "all groups"
77
private NodeNameFilter asNodeFilter;
78   private boolean asEquality;
79   //parameters of ALLOW_PULL action
80
private OutputTrigger alpOutputTrigger;
81   //parameters of PUSH action
82
private NodeNameLicList apNodeNameLicList;
83   //parameters of PUSH_OFFER action
84
private NodeNameList apoNodeNameList;
85   //parameters of PUSH_DELETE action
86

87   
88   /** Creates a new instance of InputTrigger */
89   public InputTrigger(String JavaDoc name, File file)
90   {
91     super(name, file, "input_trigger");
92     reset();
93   }
94   
95   public InputTrigger()
96   {
97     super("", null, "input_trigger");
98     reset();
99   }
100   
101   public boolean toBeDeleted()
102   {
103     return validTo != null && autoDelete && validTo.before(Calendar.getInstance().getTime());
104   }
105
106   /**
107    * Resets content of the item.
108    */

109   protected void reset()
110   {
111     source = SOURCE_NONE;
112     bundleType = BUNDLETYPE_FULL;
113     bundleFilter = null;
114     nodeFilter = null;
115     description = "";
116     validFrom = null;
117     validTo = null;
118     autoDelete = false;
119     isContract = false;
120     contractID = "";
121     contractRule = "";
122     actions = 0;
123     asNoLicences = Licence.ONE_IMPLICIT_LICENCE;
124     asShareGroups = null;
125     asNodeFilter = null;
126     asEquality = false;
127     alpOutputTrigger = null;
128     apNodeNameLicList = null;
129     apoNodeNameList = null;
130   }
131   
132   /**
133    * Loads the item from the XML DOM tree.
134    * <p>
135    * XML format of storage item:
136    * <p>
137    * <pre>
138    * &lt;input_trigger&gt;
139    * InputTrigger XML format
140    * &lt;/input_trigger&gt;
141    * </pre>
142    * <p>
143    * XML format:
144    * <p>
145    * <pre>
146    * &lt;source push="boolean" pull="boolean" made="boolean"/&gt;
147    * &lt;bundle_type&gt;FULL or OFFER or ALL&lt;/bundle_type&gt;
148    * &lt;description&gt;string&lt;/description&gt;
149    * ?&lt;bundle_filter&gt;
150    * BundleNameFilter XML format
151    * &lt;/bundle_filter&gt;
152    * ?&lt;node_filter&gt;
153    * NodeNameFilter XML format
154    * &lt;/node_filter&gt;
155    * ?&lt;validity ?from="date" ?to="date" ?autodelete="boolean"/&gt;
156    * ?&lt;contract cid="string" rule="string"/&gt;
157    * &lt;actions&gt;
158    * ?&lt;install/&gt;
159    * ?&lt;share ?number_of_licences="positive-integer or *" equality="boolean"&gt;
160    * ?&lt;share_groups&gt;
161    * ShareGroups XML format
162    * &lt;/share_groups&gt;
163    * ?&lt;node_filter&gt;
164    * NodeNameFilter XML format
165    * &lt;/node_filter&gt;
166    * &lt;/share&gt;
167    * ?&lt;allow_pull&gt;
168    * OutputTrigger XML format
169    * &lt;/allow_pull&gt;
170    * ?&lt;push&gt;
171    * NodeNameLicList XML format
172    * &lt;/push&gt;
173    * ?&lt;push_offer&gt;
174    * NodeNameList XML format
175    * &lt;/push_offer&gt;
176    * ?&lt;delete/&gt;
177    * &lt;/actions&gt;
178    * </pre>
179    */

180   public void loadFromXML(Element element) throws SAXException JavaDoc
181   {
182     reset();
183     NodeList nl = element.getChildNodes();
184     for (int i = 0; i < nl.getLength(); i++)
185     {
186       Node node = nl.item(i);
187       if (node.getNodeType() == Node.ELEMENT_NODE)
188       {
189         Element el = (Element)node;
190         String JavaDoc nodeName = node.getNodeName();
191         
192         if (nodeName.compareTo("source") == 0)
193         {
194           String JavaDoc sourcePush = el.getAttribute("push");
195           String JavaDoc sourcePull = el.getAttribute("pull");
196           String JavaDoc sourceMade = el.getAttribute("made");
197           
198           if (sourcePush.length() > 0)
199           {
200             sourcePush = sourcePush.trim().toLowerCase();
201             if (sourcePush.compareTo("1") == 0 || sourcePush.compareTo("true") == 0) source |= SOURCE_PUSH;
202           }
203           
204           if (sourcePull.length() > 0)
205           {
206             sourcePull = sourcePull.trim().toLowerCase();
207             if (sourcePull.compareTo("1") == 0 || sourcePull.compareTo("true") == 0) source |= SOURCE_PULL;
208           }
209           
210           if (sourceMade.length() > 0)
211           {
212             sourceMade = sourceMade.trim().toLowerCase();
213             if (sourceMade.compareTo("1") == 0 || sourceMade.compareTo("true") == 0) source |= SOURCE_MADE;
214           }
215         }
216         else
217         if (nodeName.compareTo("bundle_type") == 0)
218         {
219           String JavaDoc t = XML.getTextContent(el).trim().toLowerCase();
220           if (t.compareTo("full") == 0) bundleType = BUNDLETYPE_FULL;
221           else if (t.compareTo("offer") == 0) bundleType = BUNDLETYPE_OFFER;
222           else if (t.compareTo("all") == 0) bundleType = BUNDLETYPE_ALL;
223           else throw new SAXException JavaDoc("Invalid type of input trigger ('bundle_type' tag)");
224         }
225         else
226         if (nodeName.compareTo("description") == 0)
227         {
228           description = XML.getTextContent(el);
229         }
230         else
231         if (nodeName.compareTo("bundle_filter") == 0)
232         {
233           bundleFilter = new BundleNameFilter();
234           bundleFilter.loadFromXML(el);
235         }
236         else
237         if (nodeName.compareTo("node_filter") == 0)
238         {
239           nodeFilter = new NodeNameFilter();
240           nodeFilter.loadFromXML(el);
241         }
242         else
243         if (nodeName.compareTo("validity") == 0)
244         {
245           String JavaDoc from = el.getAttribute("from");
246           String JavaDoc to = el.getAttribute("to");
247           String JavaDoc ad = el.getAttribute("autodelete");
248           DateFormat dateFormat = DateFormat.getInstance();
249           if (from.length() > 0)
250           {
251             try
252             {
253               validFrom = dateFormat.parse(from);
254             }
255             catch (ParseException e)
256             {
257               throw new SAXException JavaDoc("Cannot parse date/time format of 'from' attribute of 'validity' tag", e);
258             }
259           }
260           
261           if (to.length() > 0)
262           {
263             try
264             {
265               validTo = dateFormat.parse(to);
266             }
267             catch (ParseException e)
268             {
269               throw new SAXException JavaDoc("Cannot parse date/time format of 'to' attribute of 'validity' tag", e);
270             }
271           }
272           
273           if (ad.length() > 0)
274           {
275             ad = ad.trim().toLowerCase();
276             if (ad.compareTo("1") == 0 || ad.compareTo("true") == 0) autoDelete = true;
277           }
278         }
279         else
280         if (nodeName.compareTo("contract") == 0)
281         {
282           isContract = true;
283           contractID = el.getAttribute("cid");
284           contractRule = el.getAttribute("rule");
285         }
286         else
287         if (nodeName.compareTo("actions") == 0)
288         {
289           NodeList nl2 = el.getChildNodes();
290           for (int i2 = 0; i2 < nl2.getLength(); i2++)
291           {
292             Node node2 = nl2.item(i2);
293             if (node2.getNodeType() == Node.ELEMENT_NODE)
294             {
295               Element el2 = (Element)node2;
296               String JavaDoc nodeName2 = node2.getNodeName();
297
298               if (nodeName2.compareTo("install") == 0)
299               {
300                 actions |= ACTION_INSTALL;
301               }
302               else
303               if (nodeName2.compareTo("share") == 0)
304               {
305                 actions |= ACTION_SHARE;
306                 
307                 String JavaDoc s = el2.getAttribute("number_of_licences");
308                 if (s.compareTo("*") == 0) asNoLicences = Licence.ALL_LICENCES;
309                 else
310                 if (s.length() != 0)
311                 {
312                   try
313                   {
314                     int l = Integer.parseInt(s);
315                     if (l <= 0) throw new NumberFormatException JavaDoc();
316                     asNoLicences = l;
317                   }
318                   catch (NumberFormatException JavaDoc e)
319                   {
320                     throw new SAXException JavaDoc("Invalid 'positive-integer or *' format of 'number_of_licences' attribute of 'share' tag", e);
321                   }
322                 }
323                 
324                 String JavaDoc equality = el2.getAttribute("equality").trim().toLowerCase();
325                 if (equality.compareTo("1") == 0 ||
326                     equality.compareTo("true") == 0) asEquality = true;
327                 
328                 NodeList nl3 = el2.getChildNodes();
329                 for (int i3 = 0; i3 < nl3.getLength(); i3++)
330                 {
331                   Node node3 = nl3.item(i3);
332                   if (node3.getNodeType() == Node.ELEMENT_NODE)
333                   {
334                     Element el3 = (Element)node3;
335                     String JavaDoc nodeName3 = node3.getNodeName();
336
337                     if (nodeName3.compareTo("share_groups") == 0)
338                     {
339                       asShareGroups = new ShareGroups();
340                       asShareGroups.loadFromXML(el3);
341                     }
342                     else
343                     if (nodeName3.compareTo("node_filter") == 0)
344                     {
345                       asNodeFilter = new NodeNameFilter();
346                       asNodeFilter.loadFromXML(el3);
347                     }
348                   }
349                 }
350               }
351               else
352               if (nodeName2.compareTo("allow_pull") == 0)
353               {
354                 actions |= ACTION_ALLOW_PULL;
355                 
356                 alpOutputTrigger = new OutputTrigger();
357                 alpOutputTrigger.loadFromXML(el2);
358               }
359               else
360               if (nodeName2.compareTo("push") == 0)
361               {
362                 actions |= ACTION_PUSH;
363                 
364                 apNodeNameLicList = new NodeNameLicList();
365                 apNodeNameLicList.loadFromXML(el2);
366               }
367               else
368               if (nodeName2.compareTo("push_offer") == 0)
369               {
370                 actions |= ACTION_PUSH_OFFER;
371                 
372                 apoNodeNameList = new NodeNameList();
373                 apoNodeNameList.loadFromXML(el2);
374               }
375               else
376               if (nodeName2.compareTo("delete") == 0)
377               {
378                 actions |= ACTION_DELETE;
379               }
380             }
381           }
382         } //actions tag
383
}
384     }
385   }
386   
387   /**
388    * Saves the item to XML DOM tree.
389    */

390   public void saveToXML(Element element)
391   {
392     Document doc = element.getOwnerDocument();
393     Element el;
394     
395     el = doc.createElement("source");
396     if ((source & SOURCE_PUSH) != 0) el.setAttribute("push", "true");
397     else el.setAttribute("push", "false");
398     if ((source & SOURCE_PULL) != 0) el.setAttribute("pull", "true");
399     else el.setAttribute("pull", "false");
400     if ((source & SOURCE_MADE) != 0) el.setAttribute("made", "true");
401     else el.setAttribute("made", "false");
402     element.appendChild(el);
403     
404     String JavaDoc bundleTypeText;
405     switch (bundleType)
406     {
407       case BUNDLETYPE_FULL: bundleTypeText = "full"; break;
408       case BUNDLETYPE_OFFER: bundleTypeText = "offer"; break;
409       case BUNDLETYPE_ALL: bundleTypeText = "all"; break;
410       default: bundleTypeText = "full"; break;
411     }
412     
413     XML.newTextElement(element, "bundle_type", bundleTypeText);
414     XML.newTextElement(element, "description", description);
415     
416     if (bundleFilter != null)
417     {
418       el = doc.createElement("bundle_filter");
419       bundleFilter.saveToXML(el);
420       element.appendChild(el);
421     }
422     
423     if (nodeFilter != null)
424     {
425       el = doc.createElement("node_filter");
426       nodeFilter.saveToXML(el);
427       element.appendChild(el);
428     }
429     
430     if (validFrom != null || validTo != null || autoDelete)
431     {
432       DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
433       el = doc.createElement("validity");
434       if (validFrom != null) el.setAttribute("from", dateFormat.format(validFrom));
435       if (validTo != null) el.setAttribute("to", dateFormat.format(validTo));
436       if (autoDelete) el.setAttribute("autodelete", "true");
437       element.appendChild(el);
438     }
439     
440     if (isContract)
441     {
442       el = doc.createElement("contract");
443       el.setAttribute("cid", contractID);
444       if (contractRule.length() > 0) el.setAttribute("rule", contractRule);
445       element.appendChild(el);
446     }
447     
448     el = doc.createElement("actions");
449     
450     if ((actions & ACTION_INSTALL) != 0)
451     {
452       Element el2 = doc.createElement("install");
453       el.appendChild(el2);
454     }
455     
456     if ((actions & ACTION_SHARE) != 0)
457     {
458       Element el2 = doc.createElement("share");
459       String JavaDoc s = null;
460       if (asNoLicences == Licence.ALL_LICENCES) s = "*";
461       else if (asNoLicences > 0) s = Integer.toString(asNoLicences);
462       if (s != null) el2.setAttribute("number_of_licences", s);
463
464       if (asEquality) el2.setAttribute("equality", "true");
465       else el2.setAttribute("equality", "false");
466       
467       if (asShareGroups != null)
468       {
469         Element el3 = doc.createElement("share_groups");
470         asShareGroups.saveToXML(el3);
471         el2.appendChild(el3);
472       }
473       
474       if (asNodeFilter != null)
475       {
476         Element el3 = doc.createElement("node_filter");
477         asNodeFilter.saveToXML(el3);
478         el2.appendChild(el3);
479       }
480
481       el.appendChild(el2);
482     }
483     
484     if ((actions & ACTION_ALLOW_PULL) != 0)
485     {
486       Element el2 = doc.createElement("allow_pull");
487       alpOutputTrigger.saveToXML(el2);
488       el.appendChild(el2);
489     }
490     
491     if ((actions & ACTION_PUSH) != 0)
492     {
493       Element el2 = doc.createElement("push");
494       apNodeNameLicList.saveToXML(el2);
495       el.appendChild(el2);
496     }
497
498     if ((actions & ACTION_PUSH_OFFER) != 0)
499     {
500       Element el2 = doc.createElement("push_offer");
501       apoNodeNameList.saveToXML(el2);
502       el.appendChild(el2);
503     }
504
505     if ((actions & ACTION_DELETE) != 0)
506     {
507       Element el2 = doc.createElement("delete");
508       el.appendChild(el2);
509     }
510         
511     element.appendChild(el);
512   }
513
514   public int getSource()
515   {
516     return source;
517   }
518   
519   public boolean isPushSource()
520   {
521     return (source & SOURCE_PUSH) != 0;
522   }
523   
524   public boolean isPullSource()
525   {
526     return (source & SOURCE_PULL) != 0;
527   }
528   
529   public boolean isMadeSource()
530   {
531     return (source & SOURCE_MADE) != 0;
532   }
533   
534   public int getBundleType()
535   {
536     return bundleType;
537   }
538
539   public String JavaDoc getDescription()
540   {
541     return description;
542   }
543
544   public boolean getAutoDelete()
545   {
546     return autoDelete;
547   }
548   
549   public BundleNameFilter getBundleFilter()
550   {
551     return bundleFilter;
552   }
553   
554   public NodeNameFilter getNodeFilter()
555   {
556     return nodeFilter;
557   }
558   
559   public Date getValidFrom() //can be null
560
{
561     return validFrom;
562   }
563   
564   public Date getValidTo() //can be null
565
{
566     return validTo;
567   }
568   
569   public boolean isValid()
570   {
571     Date now = Calendar.getInstance().getTime();
572     return (validFrom == null || !validFrom.after(now)) && (validTo == null || !validTo.before(now));
573   }
574
575   public boolean isContract()
576   {
577     return isContract;
578   }
579   
580   public String JavaDoc getContractID()
581   {
582     return contractID;
583   }
584   
585   public String JavaDoc getContractRule()
586   {
587     return contractRule;
588   }
589
590   public int getActions()
591   {
592     return actions;
593   }
594
595   public int getAsNoLicences()
596   {
597     return asNoLicences;
598   }
599   
600   public ShareGroups getAsShareGroups()
601   {
602     return asShareGroups;
603   }
604   
605   public NodeNameFilter getAsNodeFilter()
606   {
607     return asNodeFilter;
608   }
609   
610   public boolean getAsEquality()
611   {
612     return asEquality;
613   }
614   
615   public OutputTrigger getAlpOutputTrigger()
616   {
617     return alpOutputTrigger;
618   }
619   
620   public NodeNameLicList getApNodeNameLicList()
621   {
622     return apNodeNameLicList;
623   }
624   
625   public NodeNameList getApoNodeNameList()
626   {
627     return apoNodeNameList;
628   }
629
630   public void setSource(int source)
631   {
632     this.source = source;
633   }
634   
635   public void setPushSource(boolean isPush)
636   {
637     if (isPush) source |= SOURCE_PUSH;
638     else source &= ~SOURCE_PUSH;
639   }
640   
641   public void setPullSource(boolean isPull)
642   {
643     if (isPull) source |= SOURCE_PULL;
644     else source &= ~SOURCE_PULL;
645   }
646   
647   public void setMadeSource(boolean isMade)
648   {
649     if (isMade) source |= SOURCE_MADE;
650     else source &= ~SOURCE_MADE;
651   }
652   
653   public void setBundleType(int bundleType)
654   {
655     if (bundleType != BUNDLETYPE_FULL && bundleType != BUNDLETYPE_OFFER && bundleType != BUNDLETYPE_ALL) bundleType = BUNDLETYPE_FULL;
656     this.bundleType = bundleType;
657   }
658   
659   public void setDescription(String JavaDoc description)
660   {
661     this.description = description;
662   }
663   
664   public void setAutoDelete(boolean autoDelete)
665   {
666     this.autoDelete = autoDelete;
667   }
668   
669   public void setBundleFilter(BundleNameFilter bundleFilter)
670   {
671     this.bundleFilter = bundleFilter;
672   }
673   
674   public void setNodeFilter(NodeNameFilter nodeFilter)
675   {
676     this.nodeFilter = nodeFilter;
677   }
678   
679   public void setValidFrom(Date validFrom) //can be null
680
{
681     this.validFrom = validFrom;
682   }
683   
684   public void setValidTo(Date validTo) //can be null
685
{
686     this.validTo = validTo;
687   }
688   
689   public void setContract(String JavaDoc cid, String JavaDoc rule)
690   {
691     if (cid == null && rule == null)
692     {
693       contractID = "";
694       contractRule = "";
695       isContract = false;
696     }
697     else
698     {
699       contractID = cid;
700       contractRule = rule;
701       isContract = true;
702     }
703   }
704   
705   public void setActions(int actions)
706   {
707     this.actions = actions;
708   }
709
710   public void setAsNoLicences(int asNoLicences)
711   {
712     if (asNoLicences <= 0 && asNoLicences != Licence.ALL_LICENCES) asNoLicences = Licence.ONE_IMPLICIT_LICENCE;
713     this.asNoLicences = asNoLicences;
714   }
715
716   public void setAsShareGroups(ShareGroups asShareGroups)
717   {
718     this.asShareGroups = asShareGroups;
719   }
720   
721   public void setAsNodeFilter(NodeNameFilter asNodeFilter)
722   {
723     this.asNodeFilter = asNodeFilter;
724   }
725   
726   public void setAsEquality(boolean asEquality)
727   {
728     this.asEquality = asEquality;
729   }
730   
731   public void setAlpOutputTrigger(OutputTrigger alpOutputTrigger)
732   {
733     this.alpOutputTrigger = alpOutputTrigger;
734   }
735   
736   public void setApNodeNameLicList(NodeNameLicList apNodeNameLicList)
737   {
738     if (apNodeNameLicList == null) this.apNodeNameLicList = null;
739     else this.apNodeNameLicList = apNodeNameLicList;
740   }
741   
742   public void setApoNodeNameList(NodeNameList apoNodeNameList)
743   {
744     if (apoNodeNameList == null) this.apoNodeNameList = null;
745     else this.apoNodeNameList = apoNodeNameList;
746   }
747   
748 }
749
Popular Tags