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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.tuscany.sca.domain;
import java.net.URL;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.security.AccessController;
import java.security.PrivilegedAction;
import org.osoa.sca.CallableReference;
import org.osoa.sca.ServiceReference;
import org.osoa.sca.ServiceRuntimeException;
/**
* A handle to an SCA domain.
*
* @version $Rev: 556897 $ $Date: 2007-09-07 12:41:52 +0100 (Fri, 07 Sep 2007) $
*/
public abstract class SCADomain {
final static String LOCAL_DOMAIN_URI = "standalonedomain";
final static String LOCAL_NODE_URI = "standalonenode";
/**
* Returns a new instance of a local SCA domain.
*
* @return
*/
public static SCADomain newInstance() {
return createNewInstance(LOCAL_DOMAIN_URI, LOCAL_NODE_URI, null);
}
/**
* Returns a new instance of a local SCA domain. The specified deployable
* composite will be included in the SCA domain.
*
* @param composite the deployable composite to include in the SCA domain.
* @return
*/
public static SCADomain newInstance(String composite) {
return createNewInstance(LOCAL_DOMAIN_URI, LOCAL_NODE_URI, "/", composite);
}
/**
* Returns a new instance of a local SCA domain. The specified deployable
* composites will be included in the SCA domain.
*
* @param domainURI the URI of the SCA domain identifies where the domain service is running
* @param nodeURI the URI of the node - either a URL used to describe the node endpoints or a URI just used identify the node
* @param contributionLocation the location of an SCA contribution
* @param composites the deployable composites to include in the SCA domain.
* @return
*/
public static SCADomain newInstance(String domainURI, String nodeURI, String contributionLocation, String... composites) {
return createNewInstance(domainURI, nodeURI, contributionLocation, composites);
}
/**
* Removes the specified local SCA Domain instance
*
* @param domainInstance the instance to be removed
*/
// FIXME: Adding this as temporary support for the "connect" API
// public static void removeInstance(SCADomain domainInstance) {
// }
/**
* Returns an SCADomain representing a remote SCA domain.
*
* @param domainURI the URI of the SCA domain
* @return
*/
// FIXME : this is a temporary implementation to get the capability working
// public static SCADomain connect(String domainURI) {
// return null;
// }
/**
* Close the SCA domain.
*/
public abstract void close();
/**
* Returns the URI of the SCA Domain.
*
* @return the URI of the SCA Domain
*/
public abstract String getURI();
/**
* Cast a type-safe reference to a CallableReference. Converts a type-safe
* reference to an equivalent CallableReference; if the target refers to a
* service then a ServiceReference will be returned, if the target refers to
* a callback then a CallableReference will be returned.
*
* @param target a reference proxy provided by the SCA runtime
* @param <B> the Java type of the business interface for the reference
* @param <R> the type of reference to be returned
* @return a CallableReference equivalent for the proxy
* @throws IllegalArgumentException if the supplied instance is not a
* reference supplied by the SCA runtime
*/
public abstract <B, R extends CallableReference<B>> R cast(B target) throws IllegalArgumentException;
/**
* Returns a proxy for a service provided by a component in the SCA domain.
*
* @param businessInterface the interface that will be used to invoke the
* service
* @param serviceName the name of the service
* @param <B> the Java type of the business interface for the service
* @return an object that implements the business interface
*/
public abstract <B> B getService(Class<B> businessInterface, String serviceName);
/**
* Returns a ServiceReference for a service provided by a component in the
* SCA domain.
*
* @param businessInterface the interface that will be used to invoke the
* service
* @param serviceName the name of the service
* @param <B> the Java type of the business interface for the service
* @return a ServiceReference for the designated service
*/
public abstract <B> ServiceReference<B> getServiceReference(Class<B> businessInterface, String referenceName);
/**
* Read the service name from a configuration file
*
* @param classLoader
* @param name The name of the service class
* @return A class name which extends/implements the service class
* @throws IOException
*/
private static String getServiceName(ClassLoader classLoader, String name) throws IOException {
InputStream is = classLoader.getResourceAsStream("META-INF/services/" + name);
if (is == null) {
return null;
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(is));
while (true) {
String line = reader.readLine();
if (line == null) {
break;
} else if (!line.startsWith("#")) {
return line.trim();
}
}
} finally {
if (reader != null) {
reader.close();
}
}
return null;
}
/**
* Returns an SCADomain instance. If the system property
* "org.apache.tuscany.sca.host.embedded.SCADomain" is set, its value is used as
* the name of the implementation class. Otherwise, if the resource
* "META-INF/services/org.apache.tuscany.sca.host.embedded.SCADomain" can be
* loaded from the supplied classloader. Otherwise, it will use
* "org.apache.tuscany.sca.host.embedded.impl.DefaultSCADomain" as the default.
* The named class is loaded from the supplied classloader.
*
* @param classLoader
* @param domainURI
* @param contributionLocation
* @param composites
* @return
*/
static SCADomain createNewInstance(String domainURI, String nodeURI, String contributionLocation, String... composites) {
SCADomain node = null;
try {
// Determine the runtime and application classloader
final ClassLoader runtimeClassLoader = SCADomain.class.getClassLoader();
final ClassLoader applicationClassLoader = Thread.currentThread().getContextClassLoader();
// Discover the SCADomain implementation
final String name = SCADomain.class.getName();
String className = AccessController.doPrivileged(new PrivilegedAction<String>() {
public String run() {
return System.getProperty(name);
}
});
if (className == null) {
className = getServiceName(runtimeClassLoader, name);
}
if (className == null) {
className = "org.apache.tuscany.sca.node.impl.SCANodeImpl";
}
// Create an instance of the discovered SCA domain implementation
Class cls = Class.forName(className, true, runtimeClassLoader);
Constructor<?> constructor = null;
try {
constructor = cls.getConstructor(String.class,
String.class,
ClassLoader.class,
ClassLoader.class,
String.class,
String[].class);
} catch (NoSuchMethodException e) {}
if (constructor != null) {
node = (SCADomain)constructor.newInstance(domainURI,
nodeURI,
runtimeClassLoader,
applicationClassLoader,
contributionLocation,
composites);
}
return node;
} catch (Exception e) {
throw new ServiceRuntimeException(e);
}
}
// public ComponentManager getComponentManager() {
// return null;
// }
}
|