MobileAppsConfigUpdateHandler.java
5.71 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.contentsync.config.ConfigEntry
* com.day.cq.contentsync.handler.ContentUpdateHandler
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferenceCardinality
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.References
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.commons.json.io.JSONWriter
* org.apache.sling.jcr.resource.JcrResourceResolverFactory
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.platform.impl.contentsync.handler;
import com.adobe.cq.mobile.platform.MobileAppsInfoProvider;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.impl.contentsync.handler.MobileAppsUpdateHandler;
import com.adobe.cq.mobile.platform.impl.contentsync.handler.MobileContentPackagesListingHandler;
import com.day.cq.contentsync.config.ConfigEntry;
import com.day.cq.contentsync.handler.ContentUpdateHandler;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.References;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.io.JSONWriter;
import org.apache.sling.jcr.resource.JcrResourceResolverFactory;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(factory="com.day.cq.contentsync.handler.ContentUpdateHandler/mobileappconfig")
@Service(value={ContentUpdateHandler.class})
@References(value={@Reference(name="MobileAppsInfoProvider", cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, referenceInterface=MobileAppsInfoProvider.class, policy=ReferencePolicy.DYNAMIC)})
public class MobileAppsConfigUpdateHandler
extends MobileAppsUpdateHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(MobileContentPackagesListingHandler.class);
public static final String APP_INIT_FILE = "MobileAppsConfig.json";
public static final String PROPERTY_PROVIDER = "providers";
public static final String PROPERTY_FILENAME = "fileName";
private Map<String, MobileAppsInfoProvider> providers = new HashMap<String, MobileAppsInfoProvider>();
private ComponentContext componentContext;
@Override
public boolean updateCacheEntry(ConfigEntry configEntry, Long lastUpdated, String configCacheRoot, Session adminSession, Session userSession) {
configCacheRoot = this.getConfigCacheRoot(configEntry, configCacheRoot);
String[] providerNames = configEntry.getValues("providers");
String fileName = configEntry.getValue("fileName");
if (providerNames == null || providerNames.length == 0) {
LOGGER.debug("The providers property is empty, skipping handler");
return false;
}
if (StringUtils.isBlank((String)fileName)) {
LOGGER.warn("The property {} is missing from the config entry, defaulting to {}", (Object)"fileName", (Object)"MobileAppsConfig.json");
fileName = "MobileAppsConfig.json";
}
ResourceResolver resourceResolver = this.resolverFactory.getResourceResolver(adminSession);
Resource shellResource = resourceResolver.getResource(this.getResolvedContentPath(configEntry));
ByteArrayOutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
try {
outputStream = new ByteArrayOutputStream();
outputStreamWriter = new OutputStreamWriter(outputStream);
JSONWriter writer = new JSONWriter((Writer)outputStreamWriter);
writer.setTidy(true);
writer.object();
for (String providerName : providerNames) {
MobileAppsInfoProvider provider = this.providers.get(providerName);
if (provider == null) continue;
provider.updateInfo(writer, (MobileResource)shellResource.adaptTo(MobileResource.class));
}
writer.endObject();
outputStream.close();
outputStreamWriter.close();
this.writeToCacheFile(outputStream, configCacheRoot + "/" + fileName, adminSession);
adminSession.save();
}
catch (Exception e) {
LOGGER.error("Unexpected error while updating cache for config: " + configEntry.getPath(), (Throwable)e);
}
return false;
}
protected void activate(ComponentContext context) throws Exception {
this.componentContext = context;
}
protected void deactivate(ComponentContext context) throws RepositoryException {
this.componentContext = null;
}
public void bindMobileAppsInfoProvider(MobileAppsInfoProvider provider) {
this.providers.put(provider.getClass().getName(), provider);
}
public void unbindMobileAppsInfoProvider(MobileAppsInfoProvider provider) {
this.providers.remove(provider.getClass().getName());
}
}