Utilities.java
4.32 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.osgi.framework.BundleContext
* org.osgi.framework.InvalidSyntaxException
* org.osgi.framework.ServiceReference
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.aemds.bedrock.internal;
import com.adobe.aemds.bedrock.internal.OSGiUtils;
import com.adobe.aemds.datamanager.impl.UrlUtil;
import com.adobe.service.ConnectionFactory;
import java.io.IOException;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.io.StringReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Map;
import java.util.Properties;
import javax.naming.NameNotFoundException;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class Utilities {
private static final Logger logger = LoggerFactory.getLogger(Utilities.class);
private static final String[] SERVICE_PREFIXES = new String[]{"", "com.adobe~", "com.adobe."};
public static Properties getEnvironment() {
Properties env = new Properties();
env.putAll(System.getenv());
return env;
}
public static ConnectionFactory serviceLookup(String inConnFactoryName) throws NameNotFoundException {
ConnectionFactory svcConnection = null;
for (int i = 0; i < SERVICE_PREFIXES.length; ++i) {
block7 : {
Object obj = null;
String serviceName = SERVICE_PREFIXES[i] + inConnFactoryName;
try {
BundleContext bc = OSGiUtils.getBundleContext();
String filter = "(bmc.service.name=" + serviceName + ")";
ServiceReference[] srs = null;
try {
srs = bc.getAllServiceReferences(ConnectionFactory.class.getName(), filter);
}
catch (InvalidSyntaxException e) {
throw new RuntimeException("Unexpected error parsing filter: " + filter, (Throwable)e);
}
if (srs == null || srs.length <= 0) break block7;
obj = bc.getService(srs[0]);
if (obj instanceof ConnectionFactory) {
svcConnection = (ConnectionFactory)obj;
break block7;
}
throw new IllegalArgumentException("Object instance is not of type ConnectionFactory: " + obj);
}
catch (ArrayIndexOutOfBoundsException exp) {
logger.debug("No service of type ConnectionFactory found with service-name " + serviceName + ", moving on...", (Throwable)exp);
}
}
if (svcConnection != null) break;
}
if (svcConnection == null) {
throw new NameNotFoundException(inConnFactoryName);
}
return svcConnection;
}
public static String replaceAll(String inOriginal, String inSubString, String inReplacement) {
if (inOriginal == null || inSubString == null || inReplacement == null) {
return inOriginal;
}
int last = 0;
int pos = 0;
StringBuffer stringValue = new StringBuffer(inOriginal);
while ((pos = stringValue.toString().indexOf(inSubString, last)) >= 0) {
last = pos;
stringValue.replace(pos, pos + inSubString.length(), inReplacement);
}
return stringValue.toString();
}
public static String[] tokenizeString(String inStr) {
StreamTokenizer st = new StreamTokenizer(new StringReader(inStr));
st.resetSyntax();
st.whitespaceChars(1, 32);
st.wordChars(33, 126);
st.quoteChar(34);
st.quoteChar(39);
ArrayList<String> al = new ArrayList<String>();
try {
while (st.nextToken() != -1) {
al.add(st.sval);
}
}
catch (IOException e) {
logger.warn("Unexpected error encountered fetching next token for string " + inStr, (Throwable)e);
}
String[] resStrArray = al.toArray(new String[al.size()]);
return resStrArray;
}
public static URL toURL(String url) throws IOException {
return UrlUtil.toURL(url);
}
}