1 13 package info.magnolia.cms.license; 14 15 import info.magnolia.cms.util.FactoryUtil; 16 17 import java.io.InputStream ; 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.apache.commons.io.IOUtils; 24 import org.apache.commons.lang.StringUtils; 25 import org.jdom.Document; 26 import org.jdom.Element; 27 import org.jdom.input.SAXBuilder; 28 import org.slf4j.Logger; 29 import org.slf4j.LoggerFactory; 30 31 32 37 public class LicenseFileExtractor { 38 39 public static final String VERSION_NUMBER = "VersionNumber"; 41 44 public static final String IMPLEMENTATION_VERSION = "ImplementationVersion"; 46 public static final String BUILD_NUMBER = "BuildNumber"; 48 public static final String PROVIDER = "Provider"; 50 public static final String PROVIDER_ADDRESS = "ProviderAddress"; 52 public static final String PRIVIDER_EMAIL = "ProviderEmail"; 54 public static final String EDITION = "Edition"; 56 public static final String PRODUCT_DOMAIN = "ProductDomain"; 58 public static final String VERSION_PAGE_HANDLE = "VersionPageHandle"; 60 private static final String LICENSE_FILE_PATH = "info/magnolia/cms/license/license.xml"; 62 private static final String ELEMENT_META = "Meta"; 64 private static final String NOT_DEFINED = "Not Defined"; 66 private static final String OS_NAME = "OSName"; 68 71 private static Logger log = LoggerFactory.getLogger(LicenseFileExtractor.class); 72 73 private Map values; 74 75 public static LicenseFileExtractor getInstance() { 76 return (LicenseFileExtractor) FactoryUtil.getSingleton(LicenseFileExtractor.class); 77 } 78 79 public String get(String id) { 80 if (values.containsKey(id)) { 81 return (String ) values.get(id); 82 } 83 return NOT_DEFINED; 84 } 85 86 public Map getEntries() { 87 return values; 88 } 89 90 public String getOSName() { 91 String osName = System.getProperty("os.name"); return osName.replaceAll(" ", "-"); } 94 95 public void init() { 96 InputStream in = getClass().getClassLoader().getResourceAsStream(LICENSE_FILE_PATH); 97 this.init(in); 98 } 99 100 public void init(InputStream in) { 101 try { 102 SAXBuilder builder = new SAXBuilder(); 103 Document document = builder.build(in); 104 this.load(document); 105 } 106 catch (Exception e) { 107 log.error("failed to load license information"); log.error(e.getMessage(), e); 109 } 110 finally { 111 IOUtils.closeQuietly(in); 112 } 113 } 114 115 120 public String getVersionFromManifest() { 121 Package p = this.getClass().getPackage(); 122 if (p != null) { 123 return StringUtils.defaultString(p.getImplementationVersion()); 124 } 125 return StringUtils.EMPTY; 126 } 127 128 131 private void load(Document document) { 132 Element metaElement = document.getRootElement().getChild(ELEMENT_META); 133 134 List elements = metaElement.getChildren(); 135 136 values = new HashMap (10); 137 Iterator iterator = elements.iterator(); 138 while (iterator.hasNext()) { 139 Element element = (Element) iterator.next(); 140 values.put(element.getName(), element.getText()); 141 } 142 143 String osName = System.getProperty("os.name"); values.put(OS_NAME, osName.replaceAll(" ", "-")); 146 values.put(IMPLEMENTATION_VERSION, getVersionFromManifest()); 147 148 } 149 } 150 | Popular Tags |