KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

42   public Licence getLicence(String JavaDoc bundleName)
43   {
44     Licence licence = (Licence)map.get(bundleName);
45     if (licence != null && !licence.isLoaded()) licence.loadFromStorage();
46     return licence;
47   }
48
49   /**
50    * Deletes licence (licence file).
51    *
52    * @param bundleName name of bundle of which we want to delete licence (licence file)
53    * @return true, if licence was found (and deleted)
54    */

55   public boolean deleteLicence(String JavaDoc bundleName)
56   {
57     synchronized (map)
58     {
59       Licence licence = (Licence)map.remove(bundleName);
60       if (licence != null)
61       {
62         licence.deleteFromStorage();
63         return true;
64       }
65       else return false;
66     }
67   }
68   
69   /**
70    * Adds licence (licence file) for specified bundle.
71    *
72    * For description of all parameters see Licence class
73    *
74    * @param bundleName name of bundle
75    * @param type see
76    * @param numberOfCopies either Licence.WITHOUT_COPIES or non-negative integer
77    * @param validFrom can be null
78    * @param validTo can be null
79    * @return licence, if licence was added (bundleName was correct and there was not already the one with the same name), null otherwise
80    */

81   public Licence addLicence(String JavaDoc bundleName,
82                             int type,
83                             int numberOfCopies,
84                             Date validFrom,
85                             Date validTo,
86                             String JavaDoc text)
87   {
88     synchronized (map)
89     {
90       if (map.get(bundleName) != null) return null;
91       String JavaDoc fileName = BundleInfo.bundleNameToFileName(bundleName);
92       if (fileName == null) return null;
93       Licence lic = new Licence(bundleName, new File(baseDir, fileName + ".licence"));
94       lic.setType(type);
95       lic.setNumberOfCopies(numberOfCopies);
96       lic.setValidFrom(validFrom);
97       lic.setValidTo(validTo);
98       lic.setText(text);
99
100       lic.saveToStorage();
101       map.put(bundleName, lic);
102       return lic;
103     }
104   }
105 }
106
Popular Tags