FormDataXMLProviderRegistryImpl.java
7.18 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.lang3.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.Service
* org.apache.sling.commons.osgi.ServiceUtil
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.forms.common.service.impl;
import com.adobe.forms.common.service.DataXMLOptions;
import com.adobe.forms.common.service.DataXMLProvider;
import com.adobe.forms.common.service.FormDataXMLProvider;
import com.adobe.forms.common.service.FormDataXMLProviderRegistry;
import com.adobe.forms.common.service.FormsException;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
import org.apache.commons.lang3.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.Service;
import org.apache.sling.commons.osgi.ServiceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(immediate=1, label="Form Prefill Services Provider")
@Service(value={FormDataXMLProviderRegistry.class})
public class FormDataXMLProviderRegistryImpl
implements FormDataXMLProviderRegistry {
private static final String SERVICE_PROTOCOL = "service://";
@Reference(name="formDataXMLProvider", referenceInterface=FormDataXMLProvider.class, cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
private Map<Comparable<Object>, FormDataXMLProvider> oldProviders = new ConcurrentSkipListMap(Collections.reverseOrder());
@Reference(name="DataXMLProvider", referenceInterface=DataXMLProvider.class, cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
private Map<Comparable<Object>, DataXMLProvider> newProviders = new ConcurrentSkipListMap(Collections.reverseOrder());
private Map<String, DataXMLProvider> newProviderNameMap = new ConcurrentHashMap<String, DataXMLProvider>();
private Logger logger = LoggerFactory.getLogger(FormDataXMLProviderRegistryImpl.class);
@Override
public String getDataXMLFromService(String dataRef) throws FormsException {
String result;
result = null;
try {
for (FormDataXMLProvider provider : this.getOldProviders()) {
try {
String dataXML = provider.getDataXMLForDataRef(dataRef);
if (dataXML == null) continue;
result = dataXML;
break;
}
catch (Exception e) {
this.logger.error("The current provider could not return the the appropriate data XML" + e.getMessage(), (Throwable)e);
continue;
}
}
}
catch (Exception e) {
this.logger.error("Error in returning the service to provide the desired data XML", (Throwable)e);
throw new FormsException(e);
}
return result;
}
private Collection<FormDataXMLProvider> getOldProviders() {
return this.oldProviders.values();
}
protected void bindFormDataXMLProvider(FormDataXMLProvider provider, Map<String, Object> config) {
this.oldProviders.put(ServiceUtil.getComparableForServiceRanking(config), provider);
}
protected void unbindFormDataXMLProvider(FormDataXMLProvider provider, Map<String, Object> config) {
this.oldProviders.remove(ServiceUtil.getComparableForServiceRanking(config));
}
protected void bindDataXMLProvider(DataXMLProvider provider, Map<String, Object> config) {
this.newProviders.put(ServiceUtil.getComparableForServiceRanking(config), provider);
if (provider.getServiceName() != null) {
this.newProviderNameMap.put(provider.getServiceName(), provider);
}
}
protected void unbindDataXMLProvider(DataXMLProvider provider, Map<String, Object> config) {
this.newProviders.remove(ServiceUtil.getComparableForServiceRanking(config));
if (provider.getServiceName() != null) {
this.newProviderNameMap.remove(provider.getServiceName());
}
}
@Override
public InputStream getDataXMLStreamFromService(DataXMLOptions options) throws FormsException {
String dataRef;
InputStream result = null;
String _serviceName = options.getServiceName();
if (StringUtils.isBlank((CharSequence)_serviceName) && (dataRef = options.getDataRef()).startsWith("service://")) {
int protocolLength = "service://".length();
int index = dataRef.indexOf("/", protocolLength);
_serviceName = index == -1 ? dataRef.substring(protocolLength) : dataRef.substring(protocolLength, index);
if (StringUtils.isBlank((CharSequence)_serviceName)) {
this.logger.warn("Invalid service protocol found in " + dataRef + " .Protocol should be in the following format service://[SERVICE_NAME]/[IDENTIFIER]");
}
}
if (StringUtils.isNotBlank((CharSequence)_serviceName)) {
DataXMLProvider provider = this.newProviderNameMap.get(_serviceName);
if (provider != null) {
result = provider.getDataXMLForDataRef(options);
} else {
this.logger.warn("No registered service found for service name " + _serviceName);
}
} else {
String data;
for (DataXMLProvider provider : this.newProviders.values()) {
try {
InputStream dataXMLStream = provider.getDataXMLForDataRef(options);
if (dataXMLStream == null) continue;
result = dataXMLStream;
break;
}
catch (Exception e) {
this.logger.error("The current provider could not return the the appropriate data XML" + e.getMessage(), (Throwable)e);
continue;
}
}
if (result == null && (data = this.getDataXMLFromService(options.getDataRef())) != null) {
try {
result = new ByteArrayInputStream(data.getBytes("UTF-8"));
}
catch (UnsupportedEncodingException e) {
this.logger.error("Unable to read data using utf-8 encoding " + e.getMessage(), (Throwable)e);
}
}
}
return result;
}
@Override
public List<DataXMLProvider> getProviders() {
ArrayList<DataXMLProvider> result = new ArrayList<DataXMLProvider>();
for (DataXMLProvider provider : this.newProviders.values()) {
result.add(provider);
}
return result;
}
}