summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-20060518/java/sca/model/src/main/java/org/apache/tuscany/model/assembly/impl/CompositeImpl.java
blob: fff32e33986e3d4e39a6fb63b7b1623402492225 (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
/**
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed 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.model.assembly.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.tuscany.model.assembly.AssemblyContext;
import org.apache.tuscany.model.assembly.AssemblyFactory;
import org.apache.tuscany.model.assembly.AssemblyVisitor;
import org.apache.tuscany.model.assembly.Component;
import org.apache.tuscany.model.assembly.ComponentType;
import org.apache.tuscany.model.assembly.Composite;
import org.apache.tuscany.model.assembly.ConfiguredProperty;
import org.apache.tuscany.model.assembly.ConfiguredReference;
import org.apache.tuscany.model.assembly.ConfiguredService;
import org.apache.tuscany.model.assembly.EntryPoint;
import org.apache.tuscany.model.assembly.ExternalService;
import org.apache.tuscany.model.assembly.Implementation;
import org.apache.tuscany.model.assembly.ImportWSDL;
import org.apache.tuscany.model.assembly.Multiplicity;
import org.apache.tuscany.model.assembly.OverrideOption;
import org.apache.tuscany.model.assembly.Part;
import org.apache.tuscany.model.assembly.Reference;
import org.apache.tuscany.model.assembly.Service;
import org.apache.tuscany.model.assembly.ServiceContract;
import org.apache.tuscany.model.assembly.ServiceURI;
import org.apache.tuscany.model.assembly.Wire;
import org.apache.tuscany.model.util.NotifyingList;

/**
 * An implementation of Composite.
 */
public abstract class CompositeImpl extends ExtensibleImpl implements Composite {

    private String name;
    private ComponentType componentType;
    private Class<?> implementationClass;

    /**
     * A list of parts synchronized with a map
     */
    private class PartList<E extends Part> extends NotifyingList<E> {
        protected void added(E element) {
            partsMap.put(element.getName(), element);
            element.setComposite(CompositeImpl.this);
        }

        protected void removed(E element) {
            partsMap.remove(element.getName());
            element.setComposite(null);
        }
    }

    private Map<String, Part> partsMap = new HashMap<String, Part>();

    private List<Component> components = new PartList<Component>();
    private List<EntryPoint> entryPoints = new PartList<EntryPoint>();
    private List<ExternalService> externalServices = new PartList<ExternalService>();

    private List<Wire> wires = new ArrayList<Wire>();

    /**
     * A list of WSDL imports synchronized with a map
     */
    private class ImportWSDLList extends NotifyingList<ImportWSDL> {
        protected void added(ImportWSDL element) {
            List<ImportWSDL> importList = wsdlImportsMap.get(element.getNamespace());
            if (importList == null) {
                importList = new ArrayList<ImportWSDL>();
                wsdlImportsMap.put(element.getNamespace(), importList);
            }
            importList.add(element);
        }

        protected void removed(ImportWSDL element) {
            List<ImportWSDL> importList = wsdlImportsMap.get(element.getNamespace());
            if (importList != null) {
                importList.remove(element);
                if (importList.isEmpty())
                    wsdlImportsMap.remove(element.getNamespace());
            }
        }
    }

    private Map<String, List<ImportWSDL>> wsdlImportsMap = new HashMap<String, List<ImportWSDL>>();

    private List<ImportWSDL> wsdlImports = new ImportWSDLList();

    protected CompositeImpl() {
    }

    public String getName() {
        return name;
    }

    public void setName(String newName) {
        checkNotFrozen();
        name = newName;
    }

    public List<Component> getComponents() {
        return components;
    }

    public List<EntryPoint> getEntryPoints() {
        return entryPoints;
    }

    public List<ExternalService> getExternalServices() {
        return externalServices;
    }

    public Part getPart(String name) {
        return partsMap.get(name);
    }

    public List<Wire> getWires() {
        return wires;
    }

    public List<ImportWSDL> getWSDLImports() {
        return wsdlImports;
    }

    public List<ImportWSDL> getWSDLImports(String namespace) {
        return wsdlImportsMap.get(namespace);
    }

    public ComponentType getComponentType() {
        return componentType;
    }

    public void setComponentType(ComponentType componentType) {
        checkNotFrozen();
        this.componentType = componentType;
    }

    public ConfiguredService getConfiguredService(ServiceURI address) {
        String partName = address.getPartName();
        String serviceName = address.getServiceName();
        Part part = getPart(partName);
        if (part instanceof Component) {
            Component<?> component = (Component<?>) part;
            if (serviceName != null) {
                return component.getConfiguredService(serviceName);
            } else {
                if (!component.getConfiguredServices().isEmpty()) {
                    return component.getConfiguredServices().get(0);
                } else {
                    return null;
                }
            }

        }
        if (part instanceof ExternalService) {
            ExternalService externalService = (ExternalService) part;
            return externalService.getConfiguredService();
        } else
            return null;
    }

    public void initialize(AssemblyContext modelContext) {
        if (isInitialized())
            return;
        super.initialize(modelContext);

        // Initialize WSDL imports
        for (ImportWSDL importWSDL : wsdlImports) {
            importWSDL.initialize(modelContext);
        }

        // Initialize parts
        for (Part part : externalServices) {
            part.initialize(modelContext);
        }
        for (Part part : components) {
            part.initialize(modelContext);
        }
        for (Part part : entryPoints) {
            part.initialize(modelContext);
        }

        // Derive the component type from the entry points and external services in the composite
        // Also derive properties from the overridable properties of the components in the composite
        if (componentType == null) {
            AssemblyFactory factory = modelContext.getAssemblyFactory();
            componentType = factory.createComponentType();
            for (EntryPoint entryPoint : getEntryPoints()) {
                Service service = factory.createService();
                service.setName(entryPoint.getName());
                ServiceContract serviceContract = entryPoint.getConfiguredService().getPort().getServiceContract();
                if (serviceContract != null)
                    service.setServiceContract(serviceContract);
                componentType.getServices().add(service);

                ConfiguredReference configuredReference = entryPoint.getConfiguredReference();
                ServiceURI sourceURI = factory.createServiceURI(null, entryPoint, configuredReference);
                for (String target : configuredReference.getTargets()) {
                    ServiceURI targetURI = factory.createServiceURI(null, target);
                    Wire wire = factory.createWire();
                    wire.setSource(sourceURI);
                    wire.setTarget(targetURI);
                    getWires().add(wire);
                }
            }
            for (ExternalService externalService : getExternalServices()) {
                if (externalService.getOverrideOption() == null || externalService.getOverrideOption() == OverrideOption.NO)
                    continue;
                Reference reference = factory.createReference();
                reference.setName(externalService.getName());
                ServiceContract serviceContract = externalService.getConfiguredService().getPort().getServiceContract();
                if (serviceContract != null)
                    reference.setServiceContract(serviceContract);
                componentType.getReferences().add(reference);
            }
            for (Component<Implementation> component : getComponents()) {
                for (ConfiguredProperty configuredProperty : component.getConfiguredProperties()) {
                    if (configuredProperty.getOverrideOption() == null || configuredProperty.getOverrideOption() == OverrideOption.NO)
                        continue;
                    componentType.getProperties().add(configuredProperty.getProperty());
                }

                for (ConfiguredReference configuredReference : component.getConfiguredReferences()) {
                    // Create a wire
                    ServiceURI sourceURI = factory.createServiceURI(null, component, configuredReference);
                    for (String target : configuredReference.getTargets()) {
                        ServiceURI targetURI = factory.createServiceURI(null, target);
                        Wire wire = factory.createWire();
                        wire.setSource(sourceURI);
                        wire.setTarget(targetURI);
                        getWires().add(wire);
                    }
                }
            }
        }
        componentType.initialize(modelContext);

        // Wire the parts in this composite
        wire(modelContext);
    }

    /**
     * Wire the parts in this composite.
     * @param modelContext
     */
    protected void wire(AssemblyContext modelContext) {
        for (Wire wire : getWires()) {

            // Get the source reference
            ServiceURI sourceURI = wire.getSource();
            ConfiguredReference configuredReference = null;
            String partName = sourceURI.getPartName();
            String referenceName = sourceURI.getServiceName();
            if (referenceName != null) {
                //Component<?> component = (Component<?>)getPart(partName);
//                if (component != null) {
                Part part = getPart(partName);
                if (part instanceof Component) {
                    configuredReference = ((Component) part).getConfiguredReference(referenceName);
                } else if (part instanceof EntryPoint) {
                    configuredReference = ((EntryPoint) part).getConfiguredReference();
                }
            } else {
                EntryPoint entryPoint = (EntryPoint) getPart(partName);
                if (entryPoint != null) {
                    configuredReference = entryPoint.getConfiguredReference();
                }
            }
            if (configuredReference == null) {
                throw new IllegalArgumentException("Cannot find wire source " + sourceURI.getPath());
            } else {

                // Resolve the target service endpoint
                ServiceURI targetURI = wire.getTarget();
                ConfiguredService configuredService = getConfiguredService(targetURI);
                if (configuredService != null) {

                    // Wire the reference to the target
                    Multiplicity multiplicity = configuredReference.getPort().getMultiplicity();
                    if (multiplicity == Multiplicity.ZERO_N || multiplicity == Multiplicity.ONE_N) {
                        configuredReference.getTargetConfiguredServices().add(configuredService);
                    } else {
                        configuredReference.getTargetConfiguredServices().clear();
                        configuredReference.getTargetConfiguredServices().add(configuredService);
                    }
                } else {
                    throw new IllegalArgumentException("Cannot find service '" + targetURI.getPath() +"'.");
                }
            }
        }
    }

    public Class<?> getImplementationClass() {
        return implementationClass;
    }

    public void setImplementationClass(Class<?> clazz) {
        checkNotFrozen();
        this.implementationClass = clazz;
    }

    public void freeze() {
        if (isFrozen())
            return;
        super.freeze();

        // Freeze component type
        if (componentType != null)
            componentType.freeze();

        // Freeze lists
        wsdlImports = freeze(wsdlImports);
        components = freeze(components);
        entryPoints = freeze(entryPoints);
        externalServices = freeze(externalServices);
        wires = freeze(wires);
    }

    public boolean accept(AssemblyVisitor visitor) {
        if (!super.accept(visitor))
            return false;

        if (!accept(wsdlImports, visitor))
            return false;

        if (!accept(partsMap.values(), visitor))
            return false;

        if (!accept(wires, visitor))
            return false;

        if (componentType != null) {
            if (!componentType.accept(visitor))
                return false;
        }

        return true;
    }

}