BedrockInstaller.java
9.41 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemds.bedrock.CoreConfigService
* org.apache.commons.io.IOUtils
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Deactivate
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.installer.api.tasks.InstallTask
* org.apache.sling.installer.api.tasks.InstallTaskFactory
* org.apache.sling.installer.api.tasks.RegisteredResource
* org.apache.sling.installer.api.tasks.ResourceTransformer
* org.apache.sling.installer.api.tasks.TaskResource
* org.apache.sling.installer.api.tasks.TaskResourceGroup
* org.apache.sling.installer.api.tasks.TransformationResult
* org.apache.sling.settings.SlingSettingsService
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.aemds.bedrock.installer.internal;
import com.adobe.aemds.bedrock.CoreConfigService;
import com.adobe.aemds.bedrock.installer.internal.ArtifactType;
import com.adobe.aemds.bedrock.installer.internal.BMCDeployer;
import com.adobe.aemds.bedrock.installer.internal.DeployBMCTask;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.installer.api.tasks.InstallTask;
import org.apache.sling.installer.api.tasks.InstallTaskFactory;
import org.apache.sling.installer.api.tasks.RegisteredResource;
import org.apache.sling.installer.api.tasks.ResourceTransformer;
import org.apache.sling.installer.api.tasks.TaskResource;
import org.apache.sling.installer.api.tasks.TaskResourceGroup;
import org.apache.sling.installer.api.tasks.TransformationResult;
import org.apache.sling.settings.SlingSettingsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
public class BedrockInstaller
implements ResourceTransformer,
InstallTaskFactory {
public static final String AEMDS_ARTIFACT_TYPE = "AEMDS-Artifact-Type";
public static final String AEMDS_ARTIFACT_NAME = "AEMDS-Artifact-Name";
public static final String BEDROCK_SERVICE_NAME = "Bedrock-Service-Name";
public static final String BEDROCK_OS_NAME = "Bedrock-Operating-System";
public static final String BEDROCK_NATIVE_DEPENDENCIES = "Bedrock-Native-Dependencies";
public static final String BUNDLE_VERSION = "Bundle-Version";
private final Logger logger;
private BMCDeployer bmcDeployer;
private static final Pattern VERSION_PATTERN = Pattern.compile("\\d+\\.\\d+\\.\\d+(-SNAPSHOT)?");
@Reference
private CoreConfigService coreConfigService;
@Reference
private SlingSettingsService settingsService;
public BedrockInstaller() {
this.logger = LoggerFactory.getLogger(this.getClass());
this.bmcDeployer = new BMCDeployer();
}
public TransformationResult[] transform(RegisteredResource resource) {
if (resource.getType().equals("file")) {
try {
Manifest m = this.getManifest(resource.getInputStream());
if (m != null) {
String artifactTypeCode = m.getMainAttributes().getValue("AEMDS-Artifact-Type");
String serviceName = m.getMainAttributes().getValue("Bedrock-Service-Name");
if (artifactTypeCode == null && serviceName != null) {
artifactTypeCode = ArtifactType.BMC_NATIVE.getTypeCode();
}
if (artifactTypeCode == null) {
return null;
}
ArtifactType artifactType = ArtifactType.fromCode(artifactTypeCode);
if (artifactType == null) {
return null;
}
TransformationResult tr = null;
switch (artifactType) {
case BMC_NATIVE:
case BMC_CONFIG: {
tr = this.transformAsBMC(resource, m, artifactType);
}
}
if (tr != null) {
return new TransformationResult[]{tr};
}
}
}
catch (IOException ignore) {
this.logger.warn("Error encountered during transformation of installable resource " + (Object)resource, (Throwable)ignore);
}
}
return null;
}
private String sniffVersionFromArtifactFileName(String path) {
if (path == null) {
return null;
}
Matcher m = VERSION_PATTERN.matcher(path);
if (m.find()) {
String gr = m.group(0);
if (gr == null || gr.length() == 0) {
return null;
}
gr = gr.replace('-', '.');
return gr;
}
return null;
}
private String getVersion(RegisteredResource r, Manifest m) {
String result = m.getMainAttributes().getValue("Bundle-Version");
if (result == null) {
result = this.sniffVersionFromArtifactFileName(r.getURL());
}
return result;
}
private TransformationResult transformAsBMC(RegisteredResource resource, Manifest m, ArtifactType type) {
String serviceName = m.getMainAttributes().getValue("Bedrock-Service-Name");
if (serviceName == null) {
serviceName = m.getMainAttributes().getValue("AEMDS-Artifact-Name");
}
if (serviceName != null) {
String nativeDependencies;
String osName = m.getMainAttributes().getValue("Bedrock-Operating-System");
if (osName == null && type == ArtifactType.BMC_NATIVE) {
this.logger.warn("OS Name(Bedrock-Operating-System) not specified for resource {} with Service Name {}", (Object)resource, (Object)serviceName);
}
if ((nativeDependencies = m.getMainAttributes().getValue("Bedrock-Native-Dependencies")) == null && type == ArtifactType.BMC_NATIVE) {
this.logger.warn("Mandatory attribute 'Bedrock-Native-Dependencies' not specified for resource {} with Service Name {}", (Object)resource, (Object)serviceName);
}
HashMap<String, String> attr = new HashMap<String, String>();
attr.put("AEMDS-Artifact-Name", serviceName);
attr.put("Bedrock-Service-Name", serviceName);
attr.put("Bedrock-Operating-System", osName);
attr.put("Bedrock-Native-Dependencies", nativeDependencies == null ? "" : nativeDependencies);
String version = this.getVersion(resource, m);
if (version != null) {
attr.put("Bundle-Version", version);
}
TransformationResult tr = new TransformationResult();
String id = serviceName + "-" + osName;
if (type == ArtifactType.BMC_CONFIG) {
id = serviceName + "-" + type.getTypeCode();
}
tr.setId(id);
tr.setResourceType(type.getTypeCode());
tr.setAttributes(attr);
return tr;
}
return null;
}
public InstallTask createTask(TaskResourceGroup toActivate) {
String typeCode = toActivate.getActiveResource().getType();
ArtifactType type = ArtifactType.fromCode(typeCode);
if (ArtifactType.BMC_NATIVE == type || ArtifactType.BMC_CONFIG == type) {
return new DeployBMCTask(toActivate, this.bmcDeployer, type);
}
return null;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private Manifest getManifest(InputStream ins) throws IOException {
Manifest result = null;
if (ins != null) {
JarInputStream jis = null;
try {
jis = new JarInputStream(ins);
result = jis.getManifest();
}
finally {
if (jis != null) {
IOUtils.closeQuietly((InputStream)jis);
} else {
IOUtils.closeQuietly((InputStream)ins);
}
}
}
return result;
}
@Activate
private void activate() {
this.bmcDeployer.setConfigService(this.coreConfigService);
}
@Deactivate
private void deactivate() {
this.bmcDeployer.setConfigService(null);
}
protected void bindCoreConfigService(CoreConfigService coreConfigService) {
this.coreConfigService = coreConfigService;
}
protected void unbindCoreConfigService(CoreConfigService coreConfigService) {
if (this.coreConfigService == coreConfigService) {
this.coreConfigService = null;
}
}
protected void bindSettingsService(SlingSettingsService slingSettingsService) {
this.settingsService = slingSettingsService;
}
protected void unbindSettingsService(SlingSettingsService slingSettingsService) {
if (this.settingsService == slingSettingsService) {
this.settingsService = null;
}
}
}