1 7 package org.jahia.security.license; 8 9 import java.io.File ; 10 import java.io.FileWriter ; 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.Properties ; 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 ; 21 22 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 licensePackages = new ArrayList (); 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 xmlLicenseFileName) 49 throws IOException , SAXException { 50 licensePackages.clear(); 51 initDigester(); 52 File xmlLicenseFile = new File (xmlLicenseFileName); 53 this.digester.parse(xmlLicenseFile); 54 } 55 56 public void save(String xmlLicenseFileName) throws IOException { 57 FileWriter fileWriter = new FileWriter (xmlLicenseFileName); 58 XmlWriter xmlWriter = new XmlWriter(fileWriter); 59 xmlWriter.enablePrettyPrint(true); 60 xmlWriter.writeEntity("jahia-licenses"); 61 Iterator 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 componentName) { 73 Iterator licensePackageIter = licensePackages.iterator(); 74 while (licensePackageIter.hasNext()) { 75 LicensePackage curLicensePackage = 76 (LicensePackage) licensePackageIter.next(); 77 ArrayList licenses = curLicensePackage.getLicenses(); 78 Iterator 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 productName) { 90 Iterator 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 keystoreIn, 103 String keystorePassword) 104 throws IOException { 105 Iterator 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 keystoreIn, 119 String keystorePassword, 120 String privateKeyAlias, 121 String privateKeyPassword, 122 String certAlias) 123 throws IOException { 124 Iterator 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 licenses = new ArrayList (); 159 AddLicenseRule addLicenseRule = new AddLicenseRule(); 160 161 private Properties properties = new Properties (); 162 163 public void begin( 164 String namespace, 165 String name, 166 org.xml.sax.Attributes attributes) 167 throws Exception { 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 namespace, String name) throws Exception { 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 (); 184 properties = new Properties (); 185 } 186 187 final class AddLicenseRule extends Rule { 188 private ArrayList limits = new ArrayList (); 189 private Properties licenseProperties = new Properties (); 190 AddLimitRule addLimitRule = new AddLimitRule(); 191 AddPropertyRule addPropertyRule = new AddPropertyRule(); 192 193 private Properties properties = new Properties (); 194 private License currentLicense = null; 195 196 public void begin( 197 String namespace, 198 String name, 199 org.xml.sax.Attributes attributes) 200 throws Exception { 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 namespace, String name) throws Exception { 217 licenses.add(currentLicense); 218 currentLicense = null; 219 limits = new ArrayList (); 220 licenseProperties = new Properties (); 221 properties = new Properties (); 222 } 223 224 final class AddLimitRule extends Rule { 225 226 Properties properties = new Properties (); 227 228 public void begin( 229 String namespace, 230 String name, 231 org.xml.sax.Attributes attributes) 232 throws Exception { 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 namespace, String name) 240 throws Exception { 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 (); 249 } 250 251 } 252 253 final class AddPropertyRule extends Rule { 254 255 Properties properties = new Properties (); 256 257 public void begin( 258 String namespace, 259 String name, 260 org.xml.sax.Attributes attributes) 261 throws Exception { 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 namespace, String name) 269 throws Exception { 270 licenseProperties.setProperty(properties.getProperty("name"), properties.getProperty("value")); 271 properties = new Properties (); 272 } 273 274 } 275 } 276 } 277 278 } 279 | Popular Tags |