summaryrefslogtreecommitdiffstats
path: root/sandbox/ant/sca/trunk/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/ComponentReferenceEndpointReferenceBuilderImpl.java
blob: e773df2140d675da4c9d4d4a15e1188c28f2082c (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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
 * 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.assembly.builder.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.tuscany.sca.assembly.AssemblyFactory;
import org.apache.tuscany.sca.assembly.Binding;
import org.apache.tuscany.sca.assembly.Component;
import org.apache.tuscany.sca.assembly.ComponentReference;
import org.apache.tuscany.sca.assembly.ComponentService;
import org.apache.tuscany.sca.assembly.Composite;
import org.apache.tuscany.sca.assembly.Endpoint2;
import org.apache.tuscany.sca.assembly.EndpointReference2;
import org.apache.tuscany.sca.assembly.Implementation;
import org.apache.tuscany.sca.assembly.Multiplicity;
import org.apache.tuscany.sca.assembly.builder.CompositeBuilder;
import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException;
import org.apache.tuscany.sca.definitions.Definitions;
import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper;
import org.apache.tuscany.sca.monitor.Monitor;

/**
 * A composite builder that creates endpoint reference models.
 *
 * @version $Rev$ $Date$
 */
public class ComponentReferenceEndpointReferenceBuilderImpl extends BaseBuilderImpl implements CompositeBuilder {


    public ComponentReferenceEndpointReferenceBuilderImpl(AssemblyFactory assemblyFactory, InterfaceContractMapper interfaceContractMapper) {
        super(assemblyFactory, null, null, null, interfaceContractMapper);
    }

    public String getID() {
        return "org.apache.tuscany.sca.assembly.builder.ComponentReferenceEndpointReferenceBuilder";
    }

    /**
     * Create endpoint references for all component references.
     * 
     * @param composite
     */
    public void build(Composite composite, Definitions definitions, Monitor monitor) throws CompositeBuilderException 
    {
        // process top level composite references
        // TODO - I don't think OASIS allows for these
        //
        //processCompositeReferences(composite);
        
        // process component services
        processComponentReferences(composite, monitor);  
    }
    
    private void processCompositeReferences(Composite composite) {
        // TODO do we need this for OASIS?
    }
    
    private void processComponentReferences(Composite composite, Monitor monitor) {
        
        // index all of the components in the composite
        Map<String, Component> components = new HashMap<String, Component>();
        indexComponents(composite, components);
        
        // index all of the services in the composite
        Map<String, ComponentService> componentServices = new HashMap<String, ComponentService>();
        indexServices(composite, componentServices);
        
        // create endpoint references for each component's references
        for (Component component : composite.getComponents()) {
            // recurse for composite implementations
            Implementation implementation = component.getImplementation();
            if (implementation instanceof Composite) {
                processComponentReferences((Composite)implementation, monitor);
            }
            
            // create endpoint references to represent the component reference
            for (ComponentReference reference : component.getReferences()) {
                createReferenceEndpointReferences(composite, component, reference, components, componentServices, monitor);
            }
        }
    }    
    
    private void createReferenceEndpointReferences(Composite composite, 
                                                   Component component, 
                                                   ComponentReference reference, 
                                                   Map<String, Component> components,
                                                   Map<String, ComponentService> componentServices, 
                                                   Monitor monitor)
    {
        if (reference.getAutowire() == Boolean.TRUE && 
            reference.getTargets().isEmpty()) {

            // Find suitable targets in the current composite for an
            // autowired reference
            Multiplicity multiplicity = reference.getMultiplicity();
            for (Component targetComponent : composite.getComponents()) {
                
                // prevent autowire connecting to self
                boolean skipSelf = false;
                for (ComponentReference targetComponentReference : targetComponent.getReferences()) {
                    if (reference == targetComponentReference) {
                        skipSelf = true;
                    }
                }

                if (!skipSelf) {
                    for (ComponentService targetComponentService : targetComponent.getServices()) {
                        if (reference.getInterfaceContract() == null || 
                            interfaceContractMapper.isCompatible(reference.getInterfaceContract(),
                                                                 targetComponentService.getInterfaceContract())) {
                            // create endpoint reference
                            EndpointReference2 endpointRef = assemblyFactory.createEndpointReference();
                            endpointRef.setComponent(component);
                            endpointRef.setReference(reference);
                            endpointRef.setTargetName(targetComponentService.getName());
                            endpointRef.setUnresolved(false);
                            
                            // create dummy endpoint. This will be replaced when policies
                            // are matched and bindings are configured later
                            Endpoint2 endpoint = assemblyFactory.createEndpoint();
                            endpoint.setComponent(targetComponent);
                            endpoint.setService(targetComponentService);
                            endpoint.setUnresolved(true);
                            endpointRef.setTargetEndpoint(endpoint);
                            
                            reference.getEndpointReferences().add(endpointRef);

                            if (multiplicity == Multiplicity.ZERO_ONE || 
                                multiplicity == Multiplicity.ONE_ONE) {
                                break;
                            }
                        }
                    }
                }
            }

            if (multiplicity == Multiplicity.ONE_N || 
                multiplicity == Multiplicity.ONE_ONE) {
                if (reference.getEndpointReferences().size() == 0) {
                    warning(monitor, 
                            "NoComponentReferenceTarget",
                            reference, 
                            reference.getName());
                }
            }

        } else if (!reference.getTargets().isEmpty()) {

            // Check that the component reference does not mix the use of
            // endpoint references specified via the target attribute with 
            // the presence of binding elements 
            if (reference.getBindings().size() > 0) {
                warning(monitor, "ReferenceEndPointMixWithTarget",
                        composite, reference.getName());
            }

            // Resolve targets specified on the component reference
            for (ComponentService target : reference.getTargets()) {               
                
                String targetName = target.getName();
                ComponentService targetComponentService = componentServices.get(targetName);
                
                Component targetComponent;
                int s = targetName.indexOf('/');
                if (s == -1) {
                    targetComponent = components.get(targetName);
                } else {
                    targetComponent = components.get(targetName.substring(0, s));
                }

                if (targetComponentService != null) {

                    // Check that the target component service provides
                    // a superset of the component reference interface
                    if (reference.getInterfaceContract() == null || 
                        interfaceContractMapper.isCompatible(reference.getInterfaceContract(),
                                                             targetComponentService.getInterfaceContract())) {

                        // create endpoint reference
                        EndpointReference2 endpointRef = assemblyFactory.createEndpointReference();
                        endpointRef.setComponent(component);
                        endpointRef.setReference(reference);
                        endpointRef.setTargetName(targetComponentService.getName());
                        endpointRef.setUnresolved(false);

                        // create dummy endpoint. This will be replaced when policies
                        // are matched and bindings are configured later
                        Endpoint2 endpoint = assemblyFactory.createEndpoint();
                        endpoint.setComponent(targetComponent);
                        endpoint.setService(targetComponentService);
                        endpoint.setUnresolved(true);
                        endpointRef.setTargetEndpoint(endpoint);
                        
                        reference.getEndpointReferences().add(endpointRef);
                    } else {
                        warning(monitor, 
                                "ReferenceIncompatibleInterface",
                                composite, 
                                composite.getName().toString(),
                                reference.getName(), 
                                targetName);
                    }
                } else {
                    // add an unresolved endpoint reference
                    EndpointReference2 endpointRef = assemblyFactory.createEndpointReference();
                    endpointRef.setComponent(component);
                    endpointRef.setReference(reference);
                    endpointRef.setTargetName(targetName);
                    endpointRef.setUnresolved(true);
                                
                    // create an unresolved endpoint to go with it
                    Endpoint2 endpoint = assemblyFactory.createEndpoint();
                    endpoint.setUnresolved(true);
                    endpointRef.setTargetEndpoint(endpoint);
                    
                    warning(monitor, 
                            "ComponentReferenceTargetNotFound",
                            composite, 
                            composite.getName().toString(),
                            targetName);
                }
            }
        } else if ((reference.getReference() != null)
                && (!reference.getReference().getTargets().isEmpty())) {

            // Resolve targets from the corresponding reference in the
            // componentType
            for (ComponentService target : reference.getReference().getTargets()) {

                String targetName = target.getName();
                ComponentService targetComponentService = componentServices.get(targetName);
                
                Component targetComponent;
                int s = targetName.indexOf('/');
                if (s == -1) {
                    targetComponent = components.get(targetName);
                } else {
                    targetComponent = components.get(targetName.substring(0, s));
                }

                if (targetComponentService != null) {

                    // Check that the target component service provides
                    // a superset of the component reference interface
                    if (reference.getInterfaceContract() == null || 
                        interfaceContractMapper.isCompatible(reference.getInterfaceContract(),
                                                             targetComponentService.getInterfaceContract())) {

                        // create endpoint reference
                        EndpointReference2 endpointRef = assemblyFactory.createEndpointReference();
                        endpointRef.setComponent(component);
                        endpointRef.setReference(reference);
                        endpointRef.setTargetName(targetComponentService.getName());
                        endpointRef.setUnresolved(false);
                        
                        // create dummy endpoint. This will be replaced when policies
                        // are matched and bindings are configured later
                        Endpoint2 endpoint = assemblyFactory.createEndpoint();
                        endpoint.setComponent(targetComponent);
                        endpoint.setService(targetComponentService);
                        endpoint.setUnresolved(true);
                        endpointRef.setTargetEndpoint(endpoint);
                        
                        reference.getEndpointReferences().add(endpointRef);
                    } else {
                        warning(monitor, 
                                "ReferenceIncompatibleInterface",
                                composite, 
                                composite.getName().toString(),
                                reference.getName(), 
                                targetName);
                    }
                } else {
                    // add an unresolved endpoint reference
                    EndpointReference2 endpointRef = assemblyFactory.createEndpointReference();
                    endpointRef.setComponent(component);
                    endpointRef.setReference(reference);
                    endpointRef.setTargetName(targetName);
                    endpointRef.setUnresolved(true);
                    
                    // create an unresolved endpoint to go with it
                    Endpoint2 endpoint = assemblyFactory.createEndpoint();
                    endpoint.setUnresolved(true);
                    endpointRef.setTargetEndpoint(endpoint);                    
                    
                    warning(monitor, 
                            "ComponentReferenceTargetNotFound",
                            composite, 
                            composite.getName().toString(),
                            targetName);
                }
            }
        } 


        // if no endpoints have found so far the bindings become targets. 
        if (reference.getEndpointReferences().isEmpty()) {
            for (Binding binding : reference.getBindings()) {

                String uri = binding.getURI();

                // user hasn't put a uri on the binding so it's not a target
                // name
                if (uri == null) {
                    // create endpoint reference for manually configured bindings
                    EndpointReference2 endpointRef = assemblyFactory.createEndpointReference();
                    endpointRef.setComponent(component);
                    endpointRef.setReference(reference);
                    endpointRef.setBinding(binding);
                    endpointRef.setTargetName(null);
                    endpointRef.setTargetEndpoint(null);
                    endpointRef.setUnresolved(false);
                    
                    // create a resolved endpoint to signify that this 
                    // reference is pointing at some unwired endpoint
                    Endpoint2 endpoint = assemblyFactory.createEndpoint();
                    endpoint.setUnresolved(false);
                    endpointRef.setTargetEndpoint(endpoint);
                    
                    reference.getEndpointReferences().add(endpointRef);
                    
                    continue;
                }

                // user might have put a local target name in the uri 
                // see if it refers to a target we know about
                // - if it does the reference binding will be matched with a
                // service binding
                // - if it doesn't it is assumed to be an external reference
                Component targetComponent = null;
                ComponentService targetComponentService = null;


                if (uri.startsWith("/")) {
                    uri = uri.substring(1);
                }

                // Resolve the target component and service
                targetComponentService = componentServices.get(uri);
                int s = uri.indexOf('/');
                if (s == -1) {
                    targetComponent = components.get(uri);
                } else {
                    targetComponent = components.get(uri.substring(0, s));
                }

                // if the binding URI matches a component in the
                // composite then configure an endpoint reference with this component as
                // the target. If not then the binding URI will be assumed to reference an
                // external service
                if (targetComponentService != null) {

                    // Check that the target component service provides
                    // a superset of the component reference interface
                    if (reference.getInterfaceContract() == null || 
                        interfaceContractMapper.isCompatible(reference.getInterfaceContract(),
                                                             targetComponentService.getInterfaceContract())) {
                        // create enpoint reference
                        EndpointReference2 endpointRef = assemblyFactory.createEndpointReference();
                        endpointRef.setComponent(component);
                        endpointRef.setReference(reference);
                        endpointRef.setBinding(binding);
                        endpointRef.setTargetName(targetComponentService.getName());
                        endpointRef.setUnresolved(false);

                        // create dummy endpoint. This will be replaced when policies
                        // are matched and bindings are configured later
                        Endpoint2 endpoint = assemblyFactory.createEndpoint();
                        endpoint.setComponent(targetComponent);
                        endpoint.setService(targetComponentService);
                        endpoint.setUnresolved(true);
                        endpointRef.setTargetEndpoint(endpoint);
                        
                        reference.getEndpointReferences().add(endpointRef);
                    } else {
                        warning(monitor, 
                                "ReferenceIncompatibleInterface",
                                composite, 
                                composite.getName().toString(),
                                reference.getName(), 
                                uri);
                    }
                } else {
                    // create endpoint reference for manually configured bindings
                    EndpointReference2 endpointRef = assemblyFactory.createEndpointReference();
                    endpointRef.setComponent(component);
                    endpointRef.setReference(reference);
                    endpointRef.setBinding(binding);
                    endpointRef.setTargetName(null);
                    endpointRef.setTargetEndpoint(null);
                    endpointRef.setUnresolved(false);
                    
                    // create a resolved endpoint to signify that this 
                    // reference is pointing at some unwired endpoint
                    Endpoint2 endpoint = assemblyFactory.createEndpoint();
                    endpoint.setUnresolved(false);
                    endpointRef.setTargetEndpoint(endpoint);
                    
                    reference.getEndpointReferences().add(endpointRef);
                }
            }
        }
    } 
}