LicenseProvider.java
6.86 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.settings.SlingSettingsService
* org.osgi.framework.BundleContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.license.impl;
import com.adobe.granite.license.License;
import com.adobe.granite.license.impl.LicenseImpl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.settings.SlingSettingsService;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
@Component
@Service(value={LicenseProvider.class})
public class LicenseProvider {
private final Logger log = LoggerFactory.getLogger(LicenseProvider.class);
private static final String LICENSE_PROPERTIES = "license.properties";
private static final String PROP_GRANITE_LICENSE_URL = "granite.license.url";
@Reference
private SlingSettingsService slingSettings;
private License license;
public License getLicense() {
return this.license;
}
@Activate
protected void activate(BundleContext bundleContext) {
if (!this.checkLicenseUrl(bundleContext)) {
this.readLicenseFile();
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void readLicenseFile() {
block14 : {
File licenseFile = this.getLicenseFile();
if (licenseFile.exists()) {
InputStream in = null;
try {
if (licenseFile.canRead()) {
in = new FileInputStream(licenseFile);
Properties licenseProps = new Properties();
licenseProps.load(in);
this.setLicense(LicenseImpl.createLicense(this.getMap(licenseProps)));
break block14;
}
this.log.error("Could not validate license. Please check " + licenseFile.getAbsolutePath() + ". ");
}
catch (IOException e) {
this.log.error("Could not validate license. Please check " + licenseFile.getAbsolutePath() + ". ", (Throwable)e);
}
finally {
if (in != null) {
try {
in.close();
}
catch (IOException ignore) {}
}
}
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private boolean checkLicenseUrl(BundleContext bundleContext) {
String urlValue = bundleContext.getProperty("granite.license.url");
if (urlValue != null) {
InputStream is = null;
try {
URL url = LicenseProvider.getLicenseUrl(this.slingSettings.getSlingHomePath(), urlValue);
is = url.openStream();
Properties licenseProps = new Properties();
licenseProps.load(is);
this.setLicense(LicenseImpl.createLicense(this.getMap(licenseProps)));
boolean bl = true;
return bl;
}
catch (IOException ioe) {
this.log.warn("Unable to read license properties from {}. Trying default location.", (Object)urlValue);
}
finally {
if (is != null) {
try {
is.close();
}
catch (IOException ignore) {}
}
}
}
return false;
}
public static URL getLicenseUrl(String slingHome, String urlValue) throws IOException {
URL baseUrl = slingHome.endsWith("/") ? new URL(slingHome) : new URL(slingHome + "/");
return new URL(baseUrl, urlValue);
}
public void saveLicense(License license) throws IOException {
File file = this.getLicenseFile();
file.getParentFile().mkdirs();
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
this.getProperties(license).store(out, "Adobe Granite License Properties");
}
catch (IOException e) {
this.log.error("Error while saving license properties.", (Throwable)e);
throw e;
}
finally {
if (out != null) {
try {
out.close();
}
catch (IOException e) {}
}
}
this.setLicense(license);
}
private File getLicenseFile() {
String slingHome = this.slingSettings.getSlingHomePath();
File slingHomeDir = new File(slingHome);
try {
slingHomeDir = slingHomeDir.getCanonicalFile();
}
catch (IOException ioe) {
// empty catch block
}
File outsideDir = slingHomeDir.getParentFile();
return new File(outsideDir, "license.properties");
}
private Map<String, String> getMap(Properties props) {
HashMap<String, String> map = new HashMap<String, String>();
for (Map.Entry entry : props.entrySet()) {
map.put(entry.getKey().toString(), entry.getValue().toString());
}
return map;
}
private Properties getProperties(Map<String, String> map) {
Properties props = new Properties();
for (Map.Entry<String, String> entry : map.entrySet()) {
props.put(entry.getKey(), entry.getValue());
}
return props;
}
private void setLicense(License license) {
this.license = license;
if (license != null) {
this.log.info(license.getProductName() + " " + license.getProductVersion() + " licensed by " + license.getCustomerName());
this.log.info("DownloadID:" + license.getDownloadId());
}
}
protected void bindSlingSettings(SlingSettingsService slingSettingsService) {
this.slingSettings = slingSettingsService;
}
protected void unbindSlingSettings(SlingSettingsService slingSettingsService) {
if (this.slingSettings == slingSettingsService) {
this.slingSettings = null;
}
}
}