summaryrefslogtreecommitdiffstats
path: root/tags/java/sca/1.5.1/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/SCADomain.java
blob: bb8b8ad0e058453c8e41d7dbb1bbb5f6352957a0 (plain)
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
/*
 * 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.host.embedded;

import java.lang.reflect.Constructor;

import org.apache.tuscany.sca.extensibility.ServiceDiscovery;
import org.apache.tuscany.sca.host.embedded.impl.DefaultSCADomain;
import org.apache.tuscany.sca.host.embedded.management.ComponentManager;
import org.osoa.sca.CallableReference;
import org.osoa.sca.ServiceReference;
import org.osoa.sca.ServiceRuntimeException;

/**
 * A handle to an SCA domain.
 * 
 * @version $Rev$ $Date$
 */
public abstract class SCADomain {
    
    static final String LOCAL_DOMAIN_URI = "http://localhost";

    /**
     * Static variable to hold the most recent instance of SCADomain
     */
    // TODO: Temporary support for SCADomain.connect() API
    protected static SCADomain theDomain;

    
    /**
     * Returns a new instance of a local SCA domain.
     *  
     * @return
     */
    public static SCADomain newInstance() {
        return createNewInstance(LOCAL_DOMAIN_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, "/", 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
     * @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 contributionLocation, String... composites) {
        return createNewInstance(domainURI, 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) {
        theDomain = null;
    }

    /**
     * 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 theDomain;
    }

    /**
     * Close the SCA domain.
     */
    public void close() {
        // TODO: temporary to support initial SCADomain.connect capability
        SCADomain.removeInstance(this);
    }

    /**
     * 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 serviceName);

    /**
     * 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 contributionLocation, String... composites) {

        SCADomain domain = null;

        try {
            // Determine the runtime and application ClassLoader
            final ClassLoader runtimeClassLoader = SCADomain.class.getClassLoader();
            final ClassLoader applicationClassLoader = Thread.currentThread().getContextClassLoader();

            Class<?> implClass = ServiceDiscovery.getInstance().loadFirstServiceClass(SCADomain.class);

            if (implClass == null) {

                // Create a default SCA domain implementation
                domain =
                    new DefaultSCADomain(runtimeClassLoader, applicationClassLoader, domainURI, contributionLocation,
                                         composites);
            } else {

                // Create an instance of the discovered SCA domain implementation
                Constructor<?> constructor = null;
                try {
                    constructor =
                        implClass.getConstructor(ClassLoader.class,
                                                 ClassLoader.class,
                                                 String.class,
                                                 String.class,
                                                 String[].class);
                } catch (NoSuchMethodException e) {
                }
                if (constructor != null) {
                    domain =
                        (SCADomain)constructor.newInstance(runtimeClassLoader,
                                                           applicationClassLoader,
                                                           domainURI,
                                                           contributionLocation,
                                                           composites);
                } else {

                    constructor = implClass.getConstructor(ClassLoader.class, String.class);
                    domain = (SCADomain)constructor.newInstance(runtimeClassLoader, domainURI);
                }
            }

            // FIXME: temporary support for connect() API
            theDomain = domain;

            return domain;

        } catch (ServiceRuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceRuntimeException(e);
        }
    }

    public ComponentManager getComponentManager() {
        return null; 
    }

}