summaryrefslogtreecommitdiffstats
path: root/tags/java/sca/1.5.1/itest/contribution-classloader/contribution-test/src/test/java/org/apache/tuscany/sca/test/contribution/SupplyChain.java
blob: 1ec7aaaa3af1e93dd1236baa75500168a18cc110 (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
/*
 * 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.net.URL;
import java.util.Hashtable;

import org.apache.tuscany.sca.assembly.Composite;
import org.apache.tuscany.sca.contribution.Contribution;
import org.apache.tuscany.sca.contribution.service.ContributionService;
import org.apache.tuscany.sca.host.embedded.impl.EmbeddedSCADomain;
import org.junit.Assert;

/*
 * 
 * Contribution ClassLoading integration tests
 */

public class SupplyChain {
    
    public static final int SUPPLYCHAIN = 0;
    public static final int SUPPLYCHAIN_ILLEGAL_1 = 1;
    public static final int SUPPLYCHAIN_ILLEGAL_2 = 2;
    public static final int SUPPLYCHAIN_SELFCONTAINED = 3;
    public static final int SUPPLYCHAIN_SPLITPACKAGE = 4;
    
    private String folderName = "../contribution-classes/target/classes";

    private String customerJarName = "Customer";
    private String retailerJarName = "Retailer";
    private String warehouseJarName = "Warehouse";
    private String shipperJarName = "Shipper";
    private String supplychainJarName = "SupplyChain";
    private String illegalSupplyChain1JarName = "IllegalSupplyChain1";
    private String illegalSupplyChain2JarName = "IllegalSupplyChain2";
    private String illegalCustomerJarName = "IllegalCustomer";
    private String completeSupplychainJarName = "CompleteSupplyChain";
    private String customerInterfaceJarName = "CustomerInterface";
    private String customerImplJarName = "CustomerImpl";
    
    
    private  EmbeddedSCADomain domain;
    private ContributionService contributionService;
    private int supplyChainVersion;
        
    private Hashtable<String, Contribution> contributions = new Hashtable<String, Contribution>();
    
    private URL customerContribURL;
    private URL retailerContribURL;
    private URL warehouseContribURL;
    private URL shipperContribURL;
    private URL supplyChainContribURL;
    private URL illegalSupplyChain1ContribURL;
    private URL illegalSupplyChain2ContribURL;
    private URL illegalCustomerContribURL;
    private URL completeSupplyChainContribURL;
    private URL customerInterfaceContribURL;
    private URL customerImplContribURL;
    
    public SupplyChain() throws Exception {
        
        customerContribURL = new File(folderName + "/" + customerJarName + ".jar").toURI().toURL();
        retailerContribURL = new File(folderName + "/" + retailerJarName + ".jar").toURI().toURL();
        warehouseContribURL = new File(folderName + "/" + warehouseJarName + ".jar").toURI().toURL();
        shipperContribURL = new File(folderName + "/" + shipperJarName + ".jar").toURI().toURL();
        supplyChainContribURL = new File(folderName + "/" + supplychainJarName + ".jar").toURI().toURL();
        illegalSupplyChain1ContribURL = new File(folderName + "/" + illegalSupplyChain1JarName + ".jar").toURI().toURL();
        illegalSupplyChain2ContribURL = new File(folderName + "/" + illegalSupplyChain2JarName + ".jar").toURI().toURL();
        illegalCustomerContribURL = new File(folderName + "/" + illegalCustomerJarName + ".jar").toURI().toURL();
        completeSupplyChainContribURL = new File(folderName + "/" + completeSupplychainJarName + ".jar").toURI().toURL();
        customerInterfaceContribURL = new File(folderName + "/" + customerInterfaceJarName + ".jar").toURI().toURL();
        customerImplContribURL = new File(folderName + "/" + customerImplJarName + ".jar").toURI().toURL();
    }
    
    public void setUp(ClassLoader parentClassLoader) throws Exception  {
        this.setUp(parentClassLoader, SUPPLYCHAIN);
    }
    
    public void setUp(ClassLoader parentClassLoader, int supplyChainVersion) throws Exception {
        
        this.supplyChainVersion = supplyChainVersion;
        
        Thread.currentThread().setContextClassLoader(parentClassLoader);
        
        //Create an embedded SCA domain
        domain = new EmbeddedSCADomain(parentClassLoader, "http://localhost");

        //Start the domain
        domain.start();

        this.contributionService = domain.getContributionService();
        
        addContributions(supplyChainVersion);
    }
        
    protected void addContributions(int supplyChainVersion) throws Exception {
            
        Contribution contribution;
        
        if (supplyChainVersion != SUPPLYCHAIN_SELFCONTAINED) {
            contribution = contributionService.contribute("Shipper", shipperContribURL, true);
            contributions.put("Shipper", contribution);
            contribution = contributionService.contribute("Warehouse", warehouseContribURL, true);
            contributions.put("Warehouse", contribution);
            contribution = contributionService.contribute("Retailer", retailerContribURL, true);
            contributions.put("Retailer", contribution);
        }
        
        switch (supplyChainVersion) {
        case SUPPLYCHAIN:
            contribution = contributionService.contribute("Customer", customerContribURL, true);
            contributions.put("Customer", contribution);
            
            contribution = contributionService.contribute("SupplyChain", supplyChainContribURL, true);
            contributions.put("SupplyChain", contribution);
            break;
        case SUPPLYCHAIN_ILLEGAL_1:
            contribution = contributionService.contribute("Customer", customerContribURL, true);
            contributions.put("Customer", contribution);
            
            contribution = contributionService.contribute("SupplyChain", illegalSupplyChain1ContribURL, true);
            contributions.put("SupplyChain", contribution);
            break;
        case SUPPLYCHAIN_ILLEGAL_2:
            contribution = contributionService.contribute("Customer", illegalCustomerContribURL, true);
            contributions.put("Customer", contribution);
            
            contribution = contributionService.contribute("SupplyChain", illegalSupplyChain2ContribURL, true);
            contributions.put("SupplyChain", contribution);
            break;
        case SUPPLYCHAIN_SELFCONTAINED:
            contribution = contributionService.contribute("SupplyChain", completeSupplyChainContribURL, true);
            contributions.put("SupplyChain", contribution);
            break;
        case SUPPLYCHAIN_SPLITPACKAGE:
            contribution = contributionService.contribute("Customer", customerInterfaceContribURL, true);
            contributions.put("Customer", contribution);
            
            contribution = contributionService.contribute("CustomerImpl", customerImplContribURL, true);
            contributions.put("CustomerImpl", contribution);
            
            contribution = contributionService.contribute("SupplyChain", supplyChainContribURL, true);
            contributions.put("SupplyChain", contribution);
            break;
        }
        
        // SUPPLYCHAIN_ILLEGAL_1 should throw an exception when the composite is resolved, and hence
        // should not get this far.
        Assert.assertTrue(supplyChainVersion != SUPPLYCHAIN_ILLEGAL_1);
            
        
        for (Contribution c : contributions.values()) {

            for (Composite deployable : c.getDeployables()) {
                domain.getDomainComposite().getIncludes().add(deployable);
                domain.buildComposite(deployable);
            }
            
        }
            
        // Start Components from my composite
        for (Composite deployable : contributions.get("SupplyChain").getDeployables() ) {
            domain.getCompositeActivator().activate(deployable);
            domain.getCompositeActivator().start(deployable);
        }
    }
   
    public void tearDown() throws Exception {
        
        if (domain == null)
            return;
        
        for (String contributionURI : contributions.keySet()) {
            contributionService.remove(contributionURI);
        }


        if (contributions.get("SupplyChain") != null) {
            // Stop Components from my composite
            for (Composite deployable : contributions.get("SupplyChain").getDeployables() ) {
                domain.getCompositeActivator().stop(deployable);
                domain.getCompositeActivator().deactivate(deployable);
            }
        }

        domain.stop();

        domain.close();
    }

    public Contribution getContribution(String uri) {
        if (supplyChainVersion == SUPPLYCHAIN_SELFCONTAINED)
            return contributions.get("SupplyChain");
        else
            return contributions.get(uri);
    }
    
    public Object getCustomer(Class<?> customerClass) {
        return domain.getService(customerClass, "CustomerComponent");
    }
      
    public URL[] getContributionURLs() {
        return new URL[] {
                customerContribURL,
                retailerContribURL,
                warehouseContribURL,
                shipperContribURL,
                supplyChainContribURL
        };
    }
    
    
}