summaryrefslogtreecommitdiffstats
path: root/sandbox/axis2-1.4/itest/contribution-classloader/contribution-test/src/test/java/org/apache/tuscany/sca/test/contribution/TuscanyClassloadingTestCaseFIXME.java
blob: b123951ef67097f186d48d01caf0cd12b3506767 (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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * 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.test.contribution;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.apache.tuscany.sca.assembly.Composite;
import org.apache.tuscany.sca.extensibility.ServiceDiscovery;
import org.apache.tuscany.sca.host.embedded.impl.EmbeddedSCADomain;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.osoa.sca.ServiceReference;

/*
 * 
 * Contribution ClassLoading integration tests
 */

//FIXME This test case needs some serious rework!
// First it is very dependent on the names of the Tuscany runtime JARs
// and this is going to be difficult to maintain
// Second its usage of reflection requires the Tuscany implementation classes
// to be made public and this breaks isolation between modules.
public class TuscanyClassloadingTestCaseFIXME {

    //	private static final int SCA_API = 1;
    //	private static final int TUSCANY_CORE_SPI = 2;
    //	private static final int TUSCANY_RUNTIME = 3;
    //	private static final int TUSCANY_EXTENSIONS = 4;
    //	private static final int TUSCANY_DEPENDENCIES = 0;

    private static final String[] scaApiJars = {"sca-api"};
    private static final String[] tuscanyCoreSpiJars =
        {"core-spi", "interface", "interface-java", "interface-wsdl", "assembly", "policy", "databinding",
         "contribution", "definitions"};
    private static final String[] tuscanyRuntimeJars =
        {

        "binding-sca-xml", "binding-sca", "assembly-java-dsl", "assembly-xml", "assembly-xsd", "contribution-impl",
         "contribution-java", "contribution-namespace", "core-databinding", "core-spring", "core", "definitions-xml",
         "domain-api", "domain-impl", "domain", "extension-helper", "host-embedded", "interface-java-xml",
         "interface-wsdl-xml", "java2wsdl", "node-api", "node-impl", "node", "osgi-runtime", "policy-logging",
         "policy-security", "policy-transaction", "policy-xml", "wsdl2java"};
    private static final String[] tuscanyExtensionJars =
        {"binding-dwr", "binding-ejb", "binding-feed", "binding-http", "binding-jms", "binding-jsonrpc",
         "binding-notification", "binding-rmi", "binding-sca-axis2", "binding-ws-axis2", "binding-ws-xml",
         "binding-ws", "databinding-axiom", "databinding-fastinfoset", "databinding-jaxb", "databinding-json",
         "databinding-saxon", "databinding-sdo-axiom", "databinding-sdo", "databinding-xmlbeans", "host-http",
         "host-jetty", "host-rmi", "host-tomcat", "host-webapp", "implementation-das.jar", "implementation-data.jar",
         "implementation-java-runtime", "implementation-java-xml", "implementation-java",
         "implementation-node-runtime", "implementation-node-xml", "implementation-node",
         "implementation-notification", "implementation-osgi", "implementation-resource", "implementation-script",
         "implementation-spring", "implementation-xquery", "contribution-osgi"};

    private Class<?> embeddedDomainClass;
    Object domain;

    @Before
    public void setUp() throws Exception {

        embeddedDomainClass = getEmbeddedDomainClass();

        Constructor c = embeddedDomainClass.getConstructor(ClassLoader.class, String.class);
        // Create an embedded domain
        domain = c.newInstance(embeddedDomainClass.getClassLoader(), "http://localhost");

        // Start the domain
        invokeNoArgsMethod(domain, "start");

    }

    @After
    public void tearDown() throws Exception {

        // Stop the domain
        invokeNoArgsMethod(domain, "stop");

    }

    /**
     * Create a ClassLoader hierarchy for Tuscany runtime
     *     Dependencies <- SCA-API <- Core-SPI+ Runtime <- Extensions
     * Load the embedded SCA domain class using the runtime ClassLoader
     * 
     * @return embedded SCA domain class
     * @throws Exception
     */
    private Class<?> getEmbeddedDomainClass() throws Exception {

        URL[] scaApiUrls;
        URL[] runtimeUrls;
        URL[] extensionUrls;
        URL[] dependencyUrls;

        // When the test is run under Eclipse, the ClassLoader for the test is
        // sun.misc.Launcher$AppClassLoader. The first code path is taken.
        // When the test is run under Maven, the ClassLoader for the test is
        // org.apache.maven.surefire.booter.IsolatedClassLoader, which is a subclass
        // of URLClassLoader. The second code path is taken.
        if (!(this.getClass().getClassLoader() instanceof URLClassLoader)) {
            String classPath = System.getProperty("java.class.path");
            String[] classPathEntries = classPath.split(System.getProperty("path.separator"));
            HashSet<String> dependentJars = new HashSet<String>();
            for (int i = 0; i < classPathEntries.length; i++) {
                dependentJars.add(classPathEntries[i]);
            }

            scaApiUrls = getTuscanyClassLoaderURLs(classPathEntries, dependentJars, scaApiJars);
            runtimeUrls =
                getTuscanyClassLoaderURLs(classPathEntries, dependentJars, tuscanyCoreSpiJars, tuscanyRuntimeJars);
            extensionUrls = getTuscanyClassLoaderURLs(classPathEntries, dependentJars, tuscanyExtensionJars);
            dependencyUrls = getTuscanyClassLoaderURLs(classPathEntries, dependentJars);
        } else {
            HashSet<URL> dependentJars = new HashSet<URL>();
            URL[] classPathEntries = ((URLClassLoader)this.getClass().getClassLoader()).getURLs();
            for (int i = 0; i < classPathEntries.length; i++) {
                dependentJars.add(classPathEntries[i]);
            }
            scaApiUrls = getTuscanyClassLoaderURLs(classPathEntries, dependentJars, scaApiJars);
            runtimeUrls =
                getTuscanyClassLoaderURLs(classPathEntries, dependentJars, tuscanyCoreSpiJars, tuscanyRuntimeJars);
            extensionUrls = getTuscanyClassLoaderURLs(classPathEntries, dependentJars, tuscanyExtensionJars);
            dependencyUrls = getTuscanyClassLoaderURLs(classPathEntries, dependentJars);

        }

        boolean useSingleClassLoader =
            (scaApiUrls == null || scaApiUrls.length == 0) || (runtimeUrls == null || runtimeUrls.length == 0)
                || (extensionUrls == null || extensionUrls.length == 0)
                || (dependencyUrls == null || dependencyUrls.length == 0);

        if (useSingleClassLoader) {
            return EmbeddedSCADomain.class;
        } else {

            ClassLoader dependencyLoader = new URLClassLoader(dependencyUrls, null);
            ClassLoader scaApiLoader = new URLClassLoader(scaApiUrls, dependencyLoader);
            ClassLoader runtimeClassLoader = new URLClassLoader(runtimeUrls, scaApiLoader);
            ClassLoader extensionClassLoader = new URLClassLoader(extensionUrls, runtimeClassLoader);

            Class<?> serviceDiscoveryClass = runtimeClassLoader.loadClass(ServiceDiscovery.class.getName());
            Method getInstanceMethod = serviceDiscoveryClass.getMethod("getInstance");
            Object serviceDiscoveryObj = getInstanceMethod.invoke(null);
            Method registerClassLoaderMethod =
                serviceDiscoveryClass.getMethod("registerClassLoader", ClassLoader.class);
            registerClassLoaderMethod.invoke(serviceDiscoveryObj, extensionClassLoader);

            Thread.currentThread().setContextClassLoader(extensionClassLoader);

            return runtimeClassLoader.loadClass(EmbeddedSCADomain.class.getName());

        }

    }

    /**
     * From the list of entries in the test ClassLoader, match
     * Tuscany jars corresponding to a ClassLoader, and return the list
     * of matching entries as URLs.
     * This method is used when the test is run under eclipse, using CLASSPATH
     * based application ClassLoader.
     * 
     * @param classPathEntries List of entries on CLASSPATH
     * @param dependentJars Complete set of jars, remove jars corresponding to this
     *                      ClassLoader from the set.
     * @param jars List of Tuscany jars corresponding to this ClassLoader
     * @return Matching URLs for the ClassLoader 
     * @throws IOException
     */
    private URL[] getTuscanyClassLoaderURLs(String[] classPathEntries, HashSet<String> dependentJars, String[]... jars)
        throws IOException {

        String pathSeparator = System.getProperty("file.separator");
        HashSet<String> classPathEntrySet;

        if (jars.length == 0)
            classPathEntrySet = dependentJars;
        else {
            classPathEntrySet = new HashSet<String>();

            for (int i = 0; i < classPathEntries.length; i++) {

                String classPathEntry = classPathEntries[i];
                for (int j = 0; j < jars.length; j++) {
                    String[] jarList = jars[j];
                    if (jarList != null) {
                        for (int k = 0; k < jarList.length; k++) {
                            String jarName = "tuscany-" + jarList[k];
                            String alternateJarName = "modules" + pathSeparator + jarList[k];
                            if (classPathEntry.indexOf(jarName) >= 0 || classPathEntry.indexOf(alternateJarName) >= 0) {
                                classPathEntrySet.add(classPathEntry);
                                dependentJars.remove(classPathEntry);
                            }
                        }
                    }
                }
            }

        }
        ArrayList<URL> urls = new ArrayList<URL>();

        for (String fileName : classPathEntrySet) {
            File file = new File((String)fileName);
            if (!file.exists()) {
                throw new FileNotFoundException(fileName);

            } else {
                urls.add(file.toURL());

            }
        }

        return (URL[])urls.toArray(new URL[urls.size()]);
    }

    /**
     * From the list of URLs of the test ClassLoader, match
     * Tuscany jars corresponding to a ClassLoader, and return the matching URLs
     * This method is used when the test is run under Maven. The test ClassLoader is
     * org.apache.maven.surefire.booter.IsolatedClassLoader, which is a subclass
     * of URLClassLoader
     * 
     * @param classPathEntries List of URLs from the test ClassLoader
     * @param dependentJars Complete set of jars, remove jars corresponding to this
     *                      ClassLoader from the set.
     * @param jars List of Tuscany jars corresponding to this ClassLoader
     * @return Matching URLs for the ClassLoader 
     * @throws IOException
     */
    private URL[] getTuscanyClassLoaderURLs(URL[] classPathEntries, HashSet<URL> dependentJars, String[]... jars)
        throws IOException {

        String pathSeparator = System.getProperty("file.separator");
        HashSet<URL> classPathEntrySet;

        if (jars.length == 0)
            classPathEntrySet = dependentJars;
        else {
            classPathEntrySet = new HashSet<URL>();

            for (int i = 0; i < classPathEntries.length; i++) {

                URL classPathEntry = classPathEntries[i];
                String classPathEntryStr = classPathEntry.getPath();
                for (int j = 0; j < jars.length; j++) {
                    String[] jarList = jars[j];
                    if (jarList != null) {
                        for (int k = 0; k < jarList.length; k++) {
                            String jarName = "tuscany-" + jarList[k];
                            String alternateJarName = "modules" + pathSeparator + jarList[k];
                            if (classPathEntryStr.indexOf(jarName) >= 0 || classPathEntryStr.indexOf(alternateJarName) >= 0) {
                                classPathEntrySet.add(classPathEntry);
                                dependentJars.remove(classPathEntry);
                            }
                        }
                    }
                }
            }

        }
        return (URL[])classPathEntrySet.toArray(new URL[classPathEntrySet.size()]);
    }

    private Object invokeNoArgsMethod(Object obj, String methodName) throws Exception {

        return obj.getClass().getMethod(methodName).invoke(obj);
    }

    private Object invokeOneArgMethod(Object obj, String methodName, Class argType, Object arg) throws Exception {

        return obj.getClass().getMethod(methodName, argType).invoke(obj, arg);
    }

    /**
     * 
     * Load Tuscany runtime using multiple ClassLoaders, and run supplychain
     * test. 
     * 
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @Test
    public void test() throws Exception {

        ClassLoader runtimeClassloader = embeddedDomainClass.getClassLoader();

        if (runtimeClassloader == this.getClass().getClassLoader()) {
            System.out.println("Runtime and test loaded using the same classloader " + runtimeClassloader);
        } else {
            System.out
                .println("Running test using separate Tuscany classloaders, runtime classloader=" + runtimeClassloader);
            ClassLoader apiClassLoader =
                runtimeClassloader.loadClass(ServiceReference.class.getName()).getClassLoader();
            Assert.assertTrue(apiClassLoader != runtimeClassloader);

            try {
                runtimeClassloader.loadClass("org.apache.tuscany.sca.implementation.java.JavaImplementation");
                Assert.fail("Loaded extension class incorrectly from runtimeClassLoader");
            } catch (ClassNotFoundException e) {
            }

        }

        // Contribute supplychain (as single contribution)
        Object contributionService = invokeNoArgsMethod(domain, "getContributionService");
        Method contributeMethod =
            contributionService.getClass().getMethod("contribute", String.class, URL.class, boolean.class);

        String folderName = "../contribution-classes/target/classes";
        String supplychainJarName = "CompleteSupplyChain";
        URL supplyChainContribURL = new File(folderName + "/" + supplychainJarName + ".jar").toURL();
        Object contribution = contributeMethod.invoke(contributionService, "SupplyChain", supplyChainContribURL, true);

        Object composite = ((List)invokeNoArgsMethod(contribution, "getDeployables")).get(0);
        Object domainComposite = invokeNoArgsMethod(domain, "getDomainComposite");
        List includes = (List)invokeNoArgsMethod(domainComposite, "getIncludes");
        includes.add(composite);
        //Object compositeBuilder = invokeNoArgsMethod(domain, "getCompositeBuilder");
        Object compositeActivator = invokeNoArgsMethod(domain, "getCompositeActivator");

        Class compositeClass = embeddedDomainClass.getClassLoader().loadClass(Composite.class.getName());
        invokeOneArgMethod(domain, "buildComposite", compositeClass, composite);
        invokeOneArgMethod(compositeActivator, "activate", compositeClass, composite);
        invokeOneArgMethod(compositeActivator, "start", compositeClass, composite);

        // Get customer service 
        Method getClassLoaderMethod = contribution.getClass().getMethod("getClassLoader");
        ClassLoader classLoader = (ClassLoader)getClassLoaderMethod.invoke(contribution);

        Class customerClass = classLoader.loadClass("supplychain.customer.Customer");
        Method getServiceMethod = embeddedDomainClass.getMethod("getService", Class.class, String.class);
        Object customer = getServiceMethod.invoke(domain, customerClass, "CustomerComponent");

        // Invoke purchaseGoods
        Method m = customerClass.getMethod("purchaseGoods");
        m.invoke(customer);

        m = customerClass.getMethod("outstandingOrderCount");

        int retries = 10;
        int outstandingCount = 1;
        while (retries-- > 0) {

            outstandingCount = (int)(Integer)m.invoke(customer);
            if (outstandingCount == 0)
                break;
            else
                Thread.sleep(100);
        }
        Assert.assertEquals(0, outstandingCount);

    }

}