1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.pddescriptor;
35
36 import info.magnolia.module.ModuleRegistry;
37 import info.magnolia.module.model.ModuleDefinition;
38 import info.magnolia.objectfactory.Components;
39
40 import java.io.InputStream;
41 import java.util.HashMap;
42 import java.util.Iterator;
43 import java.util.List;
44 import java.util.Map;
45
46 import javax.inject.Singleton;
47
48 import org.apache.commons.io.IOUtils;
49 import org.apache.commons.lang3.StringUtils;
50 import org.jdom.Document;
51 import org.jdom.Element;
52 import org.jdom.input.SAXBuilder;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56
57
58
59 @Singleton
60 public class ProductDescriptorExtractor {
61
62 private static final Logger log = LoggerFactory.getLogger(ProductDescriptorExtractor.class);
63
64 public static final String VERSION_NUMBER = "VersionNumber";
65
66
67
68
69 public static final String IMPLEMENTATION_VERSION = "ImplementationVersion";
70
71
72
73
74 public static final String CORE_VERSION = "CoreVersion";
75
76 public static final String BUILD_NUMBER = "BuildNumber";
77
78 public static final String PROVIDER = "Provider";
79
80 public static final String PROVIDER_ADDRESS = "ProviderAddress";
81
82 public static final String PROVIDER_EMAIL = "ProviderEmail";
83
84 public static final String EDITION = "Edition";
85
86 public static final String PRODUCT_DOMAIN = "ProductDomain";
87
88 private static final String LICENSE_FILE_PATH = "info/magnolia/cms/pddescriptor/pddescriptor.xml";
89
90 private static final String ELEMENT_META = "Meta";
91
92 private static final String NOT_DEFINED = "Not Defined";
93
94 private static final String OS_NAME = "OSName";
95
96 private Map<String, String> values = new HashMap<String, String>();
97
98 public String get(String id) {
99 if (values.containsKey(id)) {
100 return values.get(id);
101 }
102 return NOT_DEFINED;
103 }
104
105 public Map<String, String> getEntries() {
106 return values;
107 }
108
109 public String getOSName() {
110 String osName = System.getProperty("os.name");
111 return osName.replaceAll(" ", "-");
112 }
113
114 public void init() {
115 InputStream in = getClass().getClassLoader().getResourceAsStream(LICENSE_FILE_PATH);
116 this.init(in);
117 }
118
119 public void init(InputStream in) {
120 try {
121 SAXBuilder builder = new SAXBuilder();
122 Document document = builder.build(in);
123 this.load(document);
124 } catch (Exception e) {
125 log.error("failed to load license information");
126 log.error(e.getMessage(), e);
127 } finally {
128 IOUtils.closeQuietly(in);
129 }
130 }
131
132
133
134
135
136
137
138 public String getVersionFromManifest() {
139 Package p = this.getClass().getPackage();
140 if (p != null) {
141 return StringUtils.defaultString(p.getImplementationVersion());
142 }
143 return StringUtils.EMPTY;
144 }
145
146
147
148
149 public String getCoreVersion() {
150 try {
151 ModuleDefinition coreModuleDefinition = Components.getComponent(ModuleRegistry.class).getDefinition("core");
152 return coreModuleDefinition.getVersion().toString();
153 } catch (IllegalArgumentException e) {
154 return StringUtils.EMPTY;
155 }
156 }
157
158
159
160
161 private void load(Document document) {
162 Element metaElement = document.getRootElement().getChild(ELEMENT_META);
163
164 @SuppressWarnings("unchecked")
165 List<Element> elements = metaElement.getChildren();
166
167 values = new HashMap<String, String>(10);
168 Iterator<Element> iterator = elements.iterator();
169 while (iterator.hasNext()) {
170 Element element = iterator.next();
171 values.put(element.getName(), element.getText());
172 }
173
174 String osName = System.getProperty("os.name");
175 values.put(OS_NAME, osName.replaceAll(" ", "-"));
176
177 values.put(IMPLEMENTATION_VERSION, getVersionFromManifest());
178 values.put(CORE_VERSION, getCoreVersion());
179 }
180
181
182
183
184 public void printVersionInfo() {
185 System.out.println("---------------------------------------------");
186 System.out.println("MAGNOLIA LICENSE");
187 System.out.println("---------------------------------------------");
188 System.out.println("Version number : " + get(ProductDescriptorExtractor.VERSION_NUMBER));
189 System.out.println("Core version : " + get(ProductDescriptorExtractor.CORE_VERSION));
190 System.out.println("Build : " + get(ProductDescriptorExtractor.BUILD_NUMBER));
191 System.out.println("Edition : " + get(ProductDescriptorExtractor.EDITION));
192 System.out.println("Provider : " + get(ProductDescriptorExtractor.PROVIDER) + " (" + get(ProductDescriptorExtractor.PROVIDER_EMAIL) + ")");
193 }
194 }