1 6 7 package SOFA.SOFAnet.Repository; 8 9 import java.io.*; 10 import java.util.*; 11 import SOFA.SOFAnet.Core.Reporter; 12 13 19 public class Contracts extends StorageBase 20 { 21 22 23 public Contracts(File baseDir) 24 { 25 super(baseDir, "contract", false); 26 init(); 27 } 28 29 protected StorageItem newItem(String name, File file) 30 { 31 return new Contract(name, file); 32 } 33 34 41 public Contract getContract(String name) 42 { 43 Contract contract = (Contract)map.get(name); 44 if (contract != null && !contract.isLoaded()) contract.loadFromStorage(); 45 return contract; 46 } 47 48 56 public ContractRule getContractRule(String contractID, String 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 71 public boolean deleteContract(String 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 111 public Contract addContract(String name, 112 boolean customizableName, 113 String description, 114 boolean autoDelete, 115 Date validFrom, 116 Date validTo, 117 NodeNameLicList defaultPushDestinationNodes, 118 NodeNameFilter defaultPushNodeFilter, 119 NodeNameFilter defaultPullNodeFilter, 120 String 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 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 204 213 public Licence performPush(String bundleName, String nodeName, String contractID, String 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 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 324 public ContractRule passPull(BundleInfo bundleInfo, NodeInfo nodeInfo, String 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 351 public ContractRule passPull(BundleInfo bundleInfo, NodeInfo nodeInfo, String contractID, String 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 |