KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Contracts.java
3  *
4  * Created on 14. prosinec 2003, 21:32
5  */

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

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

41   public Contract getContract(String JavaDoc name)
42   {
43     Contract contract = (Contract)map.get(name);
44     if (contract != null && !contract.isLoaded()) contract.loadFromStorage();
45     return contract;
46   }
47
48   /**
49    * Returns contract rule of the contract.
50    * The function assures that parent Contract of the returned ContractRule is loaded in the memory.
51    *
52    * @param contractID contractID od the contract
53    * @param contractRule name of rule in the contract
54    * @return requested contract rule; null if not present
55    */

56   public ContractRule getContractRule(String JavaDoc contractID, String JavaDoc contractRule)
57   {
58     Contract contract = (Contract)map.get(contractID);
59     if (contract == null) return null;
60     if (!contract.isLoaded()) contract.loadFromStorage();
61     ContractRule rule = (ContractRule)contract.getRules().get(contractRule);
62     return rule;
63   }
64   
65   /**
66    * Deletes specified contract
67    *
68    * @param name name of contract
69    * @return true, if contract was found (and deleted)
70    */

71   public boolean deleteContract(String JavaDoc name)
72   {
73     synchronized (map)
74     {
75       Contract contract = (Contract)map.remove(name);
76       if (contract != null)
77       {
78         contract.deleteFromStorage();
79         return true;
80       }
81       else return false;
82     }
83   }
84
85   /**
86    * Adds contract.
87    * <p>
88    * Name of contract is identical to Contract ID.
89    * <p>
90    * <ul>
91    * <li>If customizableName == false:
92    * <ul>
93    * <li>name is full name(id) of contract and is not changed in any way
94    * <li>function can fail
95    * </ul>
96    * <li>If customizableName == true:
97    * <ul>
98    * <li>name(id) is prefix of the name, postfix is generated as sequent numbers
99    * <li>if name(id) is empty, name will be randomly generated string
100    * <li>function never fail
101    * </ul>
102    * </ul>
103    * <p>
104    * For description of all parameters see Contract class
105    *
106    * @param name can be null - in that case the name will be generated and function always succeed
107    * @param validFrom can be null
108    * @param validTo can be null
109    * @return contract, if contract was added (there was not already the one with the same name); null otherwise
110    */

111   public Contract addContract(String JavaDoc name,
112                               boolean customizableName,
113                               String JavaDoc description,
114                               boolean autoDelete,
115                               Date validFrom,
116                               Date validTo,
117                               NodeNameLicList defaultPushDestinationNodes,
118                               NodeNameFilter defaultPushNodeFilter,
119                               NodeNameFilter defaultPullNodeFilter,
120                               String JavaDoc defaultLicenceText,
121                               Collection rules)
122   {
123     synchronized (map)
124     {
125       if (customizableName || name == null)
126       {
127         if (name == null || name.length() == 0)
128         {
129           name = "";
130           Random random = new Random();
131           for (int i = 0; i < 20; i++) name += Integer.toString(random.nextInt(16), 16);
132         }
133         
134         String JavaDoc postfix = "";
135         for (int i = 0; ; i++)
136         {
137           if (map.get(name + postfix) == null)
138           {
139             name += postfix;
140             break;
141           }
142           postfix = Integer.toString(i + 1);
143         }
144       }
145       else
146       {
147         if (map.get(name) != null) return null;
148       }
149
150       Contract contract = new Contract(name, new File(baseDir, name + ".contract"));
151       contract.setContractID(name);
152       contract.setDescription(description);
153       contract.setAutoDelete(autoDelete);
154       contract.setValidFrom(validFrom);
155       contract.setValidTo(validTo);
156       contract.setDefaultPushDestinationNodes(defaultPushDestinationNodes);
157       contract.setDefaultPushNodeFilter(defaultPushNodeFilter);
158       contract.setDefaultPullNodeFilter(defaultPullNodeFilter);
159       contract.setDefaultLicenceText(defaultLicenceText);
160       
161       Iterator it = rules.iterator();
162       while (it.hasNext())
163       {
164         contract.addRule((ContractRule)it.next());
165       }
166       
167       contract.saveToStorage();
168       map.put(name, contract);
169       return contract;
170     }
171   }
172   
173 //
174
//
175
// /**
176
// * Tries to find a contract and its rule, that can accept "pull" and
177
// * performs "pull" on it.
178
// *
179
// * @param bundleInfo describes bundle that should be "pulled"
180
// * @param nodeInfo describes node trying to "pull"
181
// * @return extracted licence or null if no contract(rule) accepts "pull"
182
// */
183
// public Licence performPull(BundleInfo bundleInfo, NodeInfo nodeInfo)
184
// {
185
// synchronized (map)
186
// {
187
// Iterator it = map.values().iterator();
188
// while (it.hasNext())
189
// {
190
// Contract contract = (Contract)it.next();
191
// if (!contract.isLoaded()) contract.loadFromStorage();
192
// ContractRule contractRule = contract.passPull(bundleInfo, nodeInfo);
193
// if (contractRule != null)
194
// {
195
// //we found the contract rule that can accept "pull"
196
// return contractRule.performPull(contract.getDefaultLicenceText());
197
// }
198
// }
199
// }
200
//
201
// return null;
202
// }
203

204   /**
205    * Performs 'push' action on specified contract and rule.
206    *
207    * @param bundleName name of bundle
208    * @param nodeName exact(!!! as it is stated in contract) name of node, which the bundle should be pushed to
209    * @param contractID contractID od the contract
210    * @param contractRule name of rule in the contract (must be specified!!!)
211    * @return licence for the bundle and node ; null on error
212    */

213   public Licence performPush(String JavaDoc bundleName, String JavaDoc nodeName, String JavaDoc contractID, String JavaDoc contractRule)
214   {
215     if (contractID.length() == 0) return null;
216     
217     synchronized (map)
218     {
219       Contract contract = (Contract)map.get(contractID);
220       if (contract == null) return null;
221       if (!contract.isLoaded()) contract.loadFromStorage();
222       if (contract.getContractID().compareTo(contractID) != 0)
223       {
224         Reporter.warning("Incosistent name and contractID of contract. name: '" + contract.getName() + "' contractID: '" + contract.getContractID() + "'");
225         return null;
226       }
227       
228       if (!contract.isValid())
229       {
230         Reporter.warning("Attempt to use Contract that is not valid. contractID: " + contract.getName());
231         return null;
232       }
233       
234       ContractRule rule = (ContractRule)contract.getRules().get(contractRule);
235       if (rule == null) return null;
236       if (!rule.isValid())
237       {
238         Reporter.warning("Attempt to use ContractRule that is not valid. contractID: " + contract.getName() + " ; contractRule: " + rule.getName());
239         return null;
240       }
241       
242       if (!rule.isPush())
243       {
244         Reporter.warning("Attempt to use ContractRule without 'push' statement. contractID: " + contract.getName() + " ; contractRule: " + rule.getName());
245         return null;
246       }
247       
248       BundleNameFilter bundleFilter = rule.getSoftware();
249       if (bundleFilter == null || !bundleFilter.pass(bundleName))
250       {
251         Reporter.warning("Attempt to use ContractRule on impropper bundle. contractID: " + contract.getName() + " ; contractRule: " + rule.getName() + " ; bundleName: " + bundleName);
252         return null;
253       }
254
255       NodeNameLicList destinations = rule.getPushDestinationNodes();
256       if (destinations == null) destinations = contract.getDefaultPushDestinationNodes();
257       
258       if (destinations == null)
259       {
260         Reporter.warning("Attempt to use ContractRule for 'push' without destination nodes. contractID: " + contract.getName() + " ; contractRule: " + rule.getName());
261         return null;
262       }
263
264       NodeNameLicList.NodeLicPair nlp = destinations.find(nodeName);
265       if (nlp == null)
266       {
267         Reporter.warning("Cannot find push destination node in ContractRule. contractID: " + contract.getName() + " ; contractRule: " + rule.getName() + " ; nodeName: " + nodeName);
268         return null;
269       }
270
271       int pushCounter = rule.getPushCounter();
272       if (pushCounter == 0)
273       {
274         Reporter.warning("Cannot find push destination node in ContractRule. contractID: " + contract.getName() + " ; contractRule: " + rule.getName() + " ; nodeName: " + nodeName);
275         return null;
276       }
277       
278       if (pushCounter > 0)
279       {
280         rule.decrementPushCounter();
281         contract.saveToStorage();
282       }
283       
284       Licence licence = (Licence)rule.getLicence().clone();
285       
286       if (nlp.numberOfLicences != Licence.ONE_IMPLICIT_LICENCE) licence.setNumberOfCopies(nlp.numberOfLicences);
287       if (licence.getText().length() == 0) licence.setText(contract.getDefaultLicenceText());
288       return licence;
289     }
290   }
291
292   /**
293    * Tries to find a contract and its rule, that can accept "push".
294    *
295    * @param bundleInfo describes bundle that should be "pushed"
296    * @param nodeInfo destination node
297    * @return ContractRule that can accept "push" or null if no such rule exists
298    */

299   public ContractRule passPush(BundleInfo bundleInfo, NodeInfo nodeInfo)
300   {
301     synchronized (map)
302     {
303       Iterator it = map.values().iterator();
304       while (it.hasNext())
305       {
306         Contract contract = (Contract)it.next();
307         if (!contract.isLoaded()) contract.loadFromStorage();
308         ContractRule contractRule = contract.passPush(bundleInfo, nodeInfo);
309         if (contractRule != null) return contractRule;
310       }
311     }
312     
313     return null;
314   }
315   
316   /**
317    * Tests whether 'pull' action can be done on specified contract.
318    *
319    * @param bundleInfo "name of bundle"
320    * @param nodeInfo node, which requests the bundle
321    * @param contractID contractID od the contract
322    * @return ContractRule that can accept "pull" or null if no such rule exists
323    */

324   public ContractRule passPull(BundleInfo bundleInfo, NodeInfo nodeInfo, String JavaDoc contractID)
325   {
326     if (contractID.length() == 0) return null;
327     
328     synchronized (map)
329     {
330       Contract contract = (Contract)map.get(contractID);
331       if (contract == null) return null;
332       if (!contract.isLoaded()) contract.loadFromStorage();
333       if (contract.getContractID().compareTo(contractID) != 0)
334       {
335         Reporter.warning("Incosistent name and contractID of contract. name: '" + contract.getName() + "' contractID: '" + contract.getContractID() + "'");
336         return null;
337       }
338       return contract.passPull(bundleInfo, nodeInfo);
339     }
340   }
341
342   /**
343    * Tests whether 'pull' action can be done on specified contract and rule.
344    *
345    * @param bundleInfo "name of bundle"
346    * @param nodeInfo node, which requests the bundle
347    * @param contractID contractID od the contract
348    * @param contractRule name of rule in the contract
349    * @return ContractRule if it can accept "pull" or null if it can't
350    */

351   public ContractRule passPull(BundleInfo bundleInfo, NodeInfo nodeInfo, String JavaDoc contractID, String JavaDoc contractRule)
352   {
353     if (contractID.length() == 0) return null;
354     
355     synchronized (map)
356     {
357       Contract contract = (Contract)map.get(contractID);
358       if (contract == null) return null;
359       if (!contract.isLoaded()) contract.loadFromStorage();
360       if (contract.getContractID().compareTo(contractID) != 0)
361       {
362         Reporter.warning("Incosistent name and contractID of contract. name: '" + contract.getName() + "' contractID: '" + contract.getContractID() + "'");
363         return null;
364       }
365       return contract.passPull(bundleInfo, nodeInfo, contractRule);
366     }
367   }
368   
369 }
370
Popular Tags