KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > security > license > LicenseManager


1 /*
2  * Created on Sep 14, 2003
3  *
4  * To change the template for this generated file go to
5  * Window>Preferences>Java>Code Generation>Code and Comments
6  */

7 package org.jahia.security.license;
8
9 import java.io.File JavaDoc;
10 import java.io.FileWriter JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.InputStream JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Properties JavaDoc;
16
17 import org.apache.commons.digester.Digester;
18 import org.apache.commons.digester.Rule;
19 import org.jahia.utils.xml.XmlWriter;
20 import org.xml.sax.SAXException JavaDoc;
21
22 /**
23  * @author loom
24  *
25  * To change the template for this generated type comment go to
26  * Window>Preferences>Java>Code Generation>Code and Comments
27  */

28 public class LicenseManager {
29
30     private static org.apache.log4j.Logger logger =
31         org.apache.log4j.Logger.getLogger(LicenseManager.class);
32
33     private static LicenseManager singletonInstance = null;
34
35     private ArrayList JavaDoc licensePackages = new ArrayList JavaDoc();
36     private Digester digester;
37
38     private LicenseManager() {
39     }
40
41     public static LicenseManager getInstance() {
42         if (singletonInstance == null) {
43             singletonInstance = new LicenseManager();
44         }
45         return singletonInstance;
46     }
47
48     public void load(String JavaDoc xmlLicenseFileName)
49         throws IOException JavaDoc, SAXException JavaDoc {
50         licensePackages.clear();
51         initDigester();
52         File JavaDoc xmlLicenseFile = new File JavaDoc(xmlLicenseFileName);
53         this.digester.parse(xmlLicenseFile);
54     }
55
56     public void save(String JavaDoc xmlLicenseFileName) throws IOException JavaDoc {
57         FileWriter JavaDoc fileWriter = new FileWriter JavaDoc(xmlLicenseFileName);
58         XmlWriter xmlWriter = new XmlWriter(fileWriter);
59         xmlWriter.enablePrettyPrint(true);
60         xmlWriter.writeEntity("jahia-licenses");
61         Iterator JavaDoc licensePackageIter = licensePackages.iterator();
62         while (licensePackageIter.hasNext()) {
63             LicensePackage curLicensePackage =
64                 (LicensePackage) licensePackageIter.next();
65             curLicensePackage.toXML(xmlWriter);
66         }
67         xmlWriter.endEntity();
68         fileWriter.flush();
69         fileWriter.close();
70     }
71
72     public License getLicenseByComponentName(String JavaDoc componentName) {
73         Iterator JavaDoc licensePackageIter = licensePackages.iterator();
74         while (licensePackageIter.hasNext()) {
75             LicensePackage curLicensePackage =
76                 (LicensePackage) licensePackageIter.next();
77             ArrayList JavaDoc licenses = curLicensePackage.getLicenses();
78             Iterator JavaDoc licenseIter = licenses.iterator();
79             while (licenseIter.hasNext()) {
80                 License curLicense = (License) licenseIter.next();
81                 if (curLicense.getComponentName().equals(componentName)) {
82                     return curLicense;
83                 }
84             }
85         }
86         return null;
87     }
88
89     public LicensePackage getLicensePackage(String JavaDoc productName) {
90         Iterator JavaDoc licensePackageIter = licensePackages.iterator();
91         while (licensePackageIter.hasNext()) {
92             LicensePackage curLicensePackage =
93                 (LicensePackage) licensePackageIter.next();
94             if (curLicensePackage.getProductName().equals(productName)) {
95                 return curLicensePackage;
96             }
97         }
98         return null;
99     }
100
101     public boolean verifyAllSignatures(
102         InputStream JavaDoc keystoreIn,
103         String JavaDoc keystorePassword)
104         throws IOException JavaDoc {
105         Iterator JavaDoc licensePackageIter = licensePackages.iterator();
106         while (licensePackageIter.hasNext()) {
107             LicensePackage curLicensePackage =
108                 (LicensePackage) licensePackageIter.next();
109             if (!curLicensePackage
110                 .verifyAllSignatures(keystoreIn, keystorePassword)) {
111                 return false;
112             }
113         }
114         return true;
115     }
116
117     public boolean updateAllSignatures(
118         InputStream JavaDoc keystoreIn,
119         String JavaDoc keystorePassword,
120         String JavaDoc privateKeyAlias,
121         String JavaDoc privateKeyPassword,
122         String JavaDoc certAlias)
123         throws IOException JavaDoc {
124         Iterator JavaDoc licensePackageIter = licensePackages.iterator();
125         while (licensePackageIter.hasNext()) {
126             LicensePackage curLicensePackage =
127                 (LicensePackage) licensePackageIter.next();
128             curLicensePackage.updateAllSignatures(
129                 keystoreIn,
130                 keystorePassword,
131                 privateKeyAlias,
132                 privateKeyPassword,
133                 certAlias);
134         }
135         return true;
136     }
137
138     private void initDigester() {
139         this.digester = new Digester();
140
141         AddLicensePackageRule addLicensePackageRule =
142             new AddLicensePackageRule();
143         digester.addRule(
144             "jahia-licenses/license-package",
145             addLicensePackageRule);
146         digester.addRule(
147             "jahia-licenses/license-package/license",
148             addLicensePackageRule.addLicenseRule);
149         digester.addRule(
150             "jahia-licenses/license-package/license/limit",
151             addLicensePackageRule.addLicenseRule.addLimitRule);
152         digester.addRule(
153             "jahia-licenses/license-package/license/property",
154             addLicensePackageRule.addLicenseRule.addPropertyRule);
155     }
156
157     final class AddLicensePackageRule extends Rule {
158         private ArrayList JavaDoc licenses = new ArrayList JavaDoc();
159         AddLicenseRule addLicenseRule = new AddLicenseRule();
160
161         private Properties JavaDoc properties = new Properties JavaDoc();
162
163         public void begin(
164             String JavaDoc namespace,
165             String JavaDoc name,
166             org.xml.sax.Attributes JavaDoc attributes)
167             throws Exception JavaDoc {
168             for (int i = 0; i < attributes.getLength(); i++) {
169                 properties.setProperty(
170                     attributes.getQName(i),
171                     attributes.getValue(i));
172             }
173         }
174
175         public void end(String JavaDoc namespace, String JavaDoc name) throws Exception JavaDoc {
176             LicensePackage newLicensePackage =
177                 new LicensePackage(
178                     properties.getProperty("format"),
179                     properties.getProperty("product"),
180                     properties.getProperty("release"),
181                     licenses);
182             licensePackages.add(newLicensePackage);
183             licenses = new ArrayList JavaDoc();
184             properties = new Properties JavaDoc();
185         }
186
187         final class AddLicenseRule extends Rule {
188             private ArrayList JavaDoc limits = new ArrayList JavaDoc();
189             private Properties JavaDoc licenseProperties = new Properties JavaDoc();
190             AddLimitRule addLimitRule = new AddLimitRule();
191             AddPropertyRule addPropertyRule = new AddPropertyRule();
192
193             private Properties JavaDoc properties = new Properties JavaDoc();
194             private License currentLicense = null;
195
196             public void begin(
197                 String JavaDoc namespace,
198                 String JavaDoc name,
199                 org.xml.sax.Attributes JavaDoc attributes)
200                 throws Exception JavaDoc {
201                 for (int i = 0; i < attributes.getLength(); i++) {
202                     properties.setProperty(
203                         attributes.getQName(i),
204                         attributes.getValue(i));
205                 }
206                 currentLicense =
207                     new License(
208                     properties.getProperty("component"),
209                     properties.getProperty("licensee"),
210                     properties.getProperty("signature"),
211                     properties.getProperty("certAlias"),
212                     limits,
213                     licenseProperties);
214             }
215
216             public void end(String JavaDoc namespace, String JavaDoc name) throws Exception JavaDoc {
217                 licenses.add(currentLicense);
218                 currentLicense = null;
219                 limits = new ArrayList JavaDoc();
220                 licenseProperties = new Properties JavaDoc();
221                 properties = new Properties JavaDoc();
222             }
223
224             final class AddLimitRule extends Rule {
225
226                 Properties JavaDoc properties = new Properties JavaDoc();
227
228                 public void begin(
229                     String JavaDoc namespace,
230                     String JavaDoc name,
231                     org.xml.sax.Attributes JavaDoc attributes)
232                     throws Exception JavaDoc {
233                     for (int i = 0; i < attributes.getLength(); i++) {
234                         properties.setProperty(
235                             attributes.getQName(i),
236                             attributes.getValue(i));
237                     }
238                 }
239                 public void end(String JavaDoc namespace, String JavaDoc name)
240                     throws Exception JavaDoc {
241                     Limit limit =
242                         new Limit(
243                             properties.getProperty("class"),
244                             properties.getProperty("name"),
245                             properties.getProperty("value"),
246                             currentLicense);
247                     limits.add(limit);
248                     properties = new Properties JavaDoc();
249                 }
250
251             }
252
253             final class AddPropertyRule extends Rule {
254
255                 Properties JavaDoc properties = new Properties JavaDoc();
256
257                 public void begin(
258                     String JavaDoc namespace,
259                     String JavaDoc name,
260                     org.xml.sax.Attributes JavaDoc attributes)
261                     throws Exception JavaDoc {
262                     for (int i = 0; i < attributes.getLength(); i++) {
263                         properties.setProperty(
264                             attributes.getQName(i),
265                             attributes.getValue(i));
266                     }
267                 }
268                 public void end(String JavaDoc namespace, String JavaDoc name)
269                     throws Exception JavaDoc {
270                     licenseProperties.setProperty(properties.getProperty("name"), properties.getProperty("value"));
271                     properties = new Properties JavaDoc();
272                 }
273
274             }
275         }
276     }
277
278 }
279
Popular Tags