KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * OutputTriggers.java
3  *
4  * Created on 1. prosinec 2003, 18:15
5  */

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

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

40   public OutputTrigger getOutputTrigger(String JavaDoc name)
41   {
42     OutputTrigger outputTrigger = (OutputTrigger)map.get(name);
43     if (outputTrigger != null && !outputTrigger.isLoaded()) outputTrigger.loadFromStorage();
44     return outputTrigger;
45   }
46
47   /**
48    * Deletes output trigger.
49    *
50    * @param name name of output trigger
51    * @return true, if output trigger was found (and deleted)
52    */

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

93   public OutputTrigger addOutputTrigger(String JavaDoc name,
94                                         boolean customizableName,
95                                         String JavaDoc description,
96                                         boolean autoDelete,
97                                         BundleNameFilter bundleFilter,
98                                         NodeNameFilter nodeFilter,
99                                         Date validFrom,
100                                         Date validTo,
101                                         String JavaDoc contractID,
102                                         String JavaDoc contractRule)
103   {
104     synchronized (map)
105     {
106       if (customizableName || name == null)
107       {
108         if (name == null || name.length() == 0)
109         {
110           name = "auto";
111           Random random = new Random();
112           for (int i = 0; i < 10; i++) name += Integer.toString(random.nextInt(16), 16);
113         }
114         
115         String JavaDoc postfix = "";
116         for (int i = 0; ; i++)
117         {
118           if (map.get(name + postfix) == null)
119           {
120             name += postfix;
121             break;
122           }
123           postfix = Integer.toString(i + 1);
124         }
125       }
126       else
127       {
128         if (map.get(name) != null) return null;
129       }
130
131       OutputTrigger outputTrigger = new OutputTrigger(name, new File(baseDir, name + ".ot"));
132       outputTrigger.setDescription(description);
133       outputTrigger.setAutoDelete(autoDelete);
134       outputTrigger.setBundleFilter(bundleFilter);
135       outputTrigger.setNodeFilter(nodeFilter);
136       outputTrigger.setValidFrom(validFrom);
137       outputTrigger.setValidTo(validTo);
138       outputTrigger.setContract(contractID, contractRule);
139       
140       outputTrigger.saveToStorage();
141       map.put(name, outputTrigger);
142       return outputTrigger;
143     }
144   }
145 }
Popular Tags