KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * InputTriggers.java
3  *
4  * Created on 1. prosinec 2003, 23:24
5  */

6
7 package SOFA.SOFAnet.Repository;
8
9 import java.io.*;
10 import java.util.*;
11
12 /**
13  * Persistent storage of input triggers.
14  * Implemented as directory of XML files.
15  *
16  * @author Ladislav Sobr
17  */

18 public class InputTriggers extends StorageBase
19 {
20   
21   /** Creates a new instance of InputTriggers */
22   public InputTriggers(File baseDir)
23   {
24     super(baseDir, "it", true);
25     init();
26   }
27   
28   protected StorageItem newItem(String JavaDoc name, File file)
29   {
30     return new InputTrigger(name, file);
31   }
32   
33   /**
34    * Returns specified input trigger
35    * The function assures that the returned InputTrigger is loaded in the memory.
36    *
37    * @param name name of input trigger
38    * @return requested input trigger; null if not present
39    */

40   public InputTrigger getInputTrigger(String JavaDoc name)
41   {
42     return (InputTrigger)map.get(name);
43   }
44
45   /**
46    * Deletes specified input trigger
47    *
48    * @param name name of input trigger
49    * @return true, if input trigger was found (and deleted)
50    */

51   public boolean deleteInputTrigger(String JavaDoc name)
52   {
53     synchronized (map)
54     {
55       InputTrigger inputTrigger = (InputTrigger)map.remove(name);
56       if (inputTrigger != null)
57       {
58         inputTrigger.deleteFromStorage();
59         return true;
60       }
61       else return false;
62     }
63   }
64   
65   /**
66    * Adds input trigger.
67    * <p>
68    * If contract is not present, set contractID and contractRule to null
69    * <p>
70    * <ul>
71    * <li>If customizableName == false:
72    * <ul>
73    * <li>name is full name of input trigger and is not changed in any way
74    * <li>function can fail
75    * </ul>
76    * <li>If customizableName == true:
77    * <ul>
78    * <li>name is prefix of the name, postfix is generated as sequent numbers
79    * <li>if name is empty, prefix is "auto" + randomly generated string
80    * <li>function never fail
81    * </ul>
82    * </ul>
83    * <p>
84    * For description of all parameters see InputTrigger class
85    *
86    * @param name can be null - in that case the name will be generated and function always succeed
87    * @param validFrom can be null
88    * @param validTo can be null
89    * @param asNoLicences Licence.ONE_IMPLICIT_LICENCE means "one licence", Licence.ALL_LICENCES means "all remaining licences"
90    * @return added input trigger, if input trigger was added (there was not already the one with the same name); null otherwise
91    */

92   public InputTrigger addInputTrigger(String JavaDoc name,
93                                       boolean customizableName,
94                                       int source,
95                                       int bundleType,
96                                       String JavaDoc description,
97                                       boolean autoDelete,
98                                       BundleNameFilter bundleFilter,
99                                       NodeNameFilter nodeFilter,
100                                       Date validFrom,
101                                       Date validTo,
102                                       String JavaDoc contractID,
103                                       String JavaDoc contractRule,
104                                       int actions,
105                                       int asNoLicences, // Licence.ONE_IMPLICIT_LICENCE means "one licence", Licence.ALL_LICENCES means "all remaining licences"
106
ShareGroups asShareGroups,
107                                       NodeNameFilter asNodeFilter,
108                                       boolean asEquality,
109                                       OutputTrigger alpOutputTrigger,
110                                       NodeNameLicList apNodeNameLicList,
111                                       NodeNameList apoNodeNameList)
112   {
113     synchronized (map)
114     {
115       if (customizableName || name == null)
116       {
117         if (name == null || name.length() == 0)
118         {
119           name = "auto";
120           Random random = new Random();
121           for (int i = 0; i < 10; i++) name += Integer.toString(random.nextInt(16), 16);
122         }
123         
124         String JavaDoc postfix = "";
125         for (int i = 0; ; i++)
126         {
127           if (map.get(name + postfix) == null)
128           {
129             name += postfix;
130             break;
131           }
132           postfix = Integer.toString(i + 1);
133         }
134       }
135       else
136       {
137         if (map.get(name) != null) return null;
138       }
139
140       InputTrigger inputTrigger = new InputTrigger(name, new File(baseDir, name + ".it"));
141       inputTrigger.setSource(source);
142       inputTrigger.setBundleType(bundleType);
143       inputTrigger.setDescription(description);
144       inputTrigger.setAutoDelete(autoDelete);
145       inputTrigger.setBundleFilter(bundleFilter);
146       inputTrigger.setNodeFilter(nodeFilter);
147       inputTrigger.setValidFrom(validFrom);
148       inputTrigger.setValidTo(validTo);
149       inputTrigger.setContract(contractID, contractRule);
150       inputTrigger.setActions(actions);
151       inputTrigger.setAsNoLicences(asNoLicences);
152       inputTrigger.setAsShareGroups(asShareGroups);
153       inputTrigger.setAsNodeFilter(asNodeFilter);
154       inputTrigger.setAsEquality(asEquality);
155       inputTrigger.setAlpOutputTrigger(alpOutputTrigger);
156       inputTrigger.setApNodeNameLicList(apNodeNameLicList);
157       inputTrigger.setApoNodeNameList(apoNodeNameList);
158       
159       inputTrigger.saveToStorage();
160       map.put(name, inputTrigger);
161       return inputTrigger;
162     }
163   }
164 }
165
Popular Tags