summaryrefslogtreecommitdiffstats
path: root/sandbox/mobile-android/tuscany-assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/DomainWireBuilderImpl.java
blob: b2f96c30cc51b28df4d994c4f783b98dfd45c8fc (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
/*
 * 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.ArrayList;
import java.util.List;

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.Reference;
import org.apache.tuscany.sca.assembly.SCABindingFactory;
import org.apache.tuscany.sca.assembly.Service;
import org.apache.tuscany.sca.assembly.builder.CompositeBuilderMonitor;
import org.apache.tuscany.sca.assembly.builder.DomainBuilder;
import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper;
import org.apache.tuscany.sca.policy.IntentAttachPointTypeFactory;

public class DomainWireBuilderImpl implements DomainBuilder {
    
    private CompositeWireBuilderImpl wireBuilder;
    
    public DomainWireBuilderImpl(AssemblyFactory assemblyFactory,
            SCABindingFactory scaBindingFactory,
            IntentAttachPointTypeFactory  intentAttachPointTypeFactory,
            InterfaceContractMapper interfaceContractMapper,
            CompositeBuilderMonitor monitor) {
        wireBuilder = new CompositeWireBuilderImpl(assemblyFactory, interfaceContractMapper, monitor);
    }
    
    public String getComponentNameFromReference(String referenceName){
        // Extract the component name
        String componentName = referenceName;
        int i = referenceName.indexOf('/');
        if (i != -1) {
            componentName = referenceName.substring(0, i);
        } 
        
        return componentName;
    }
    
    public String getServiceNameFromReference(String referenceName){
        // Extract the component name
        String serviceName = null;
        int i = referenceName.indexOf('/');
        if (i != -1) {
            serviceName = referenceName.substring(i + 1);

        } 
        return serviceName;
    }
    
    
    public List<Reference> findReferenceForService(Composite composite, String serviceName){

        List<Reference> referenceList = new ArrayList<Reference>();
        
        String componentName = getComponentNameFromReference(serviceName);
        
        for (Reference reference: composite.getReferences()) {
            for (ComponentService componentService : reference.getTargets()){
                if (componentService.getName().equals(serviceName) || 
                    componentService.getName().equals(componentName)) {
                    referenceList.add(reference);                    
                }
            }
        }
        
        for (Component component: composite.getComponents()) {
            for (ComponentReference reference: component.getReferences()) {
                for (ComponentService componentService : reference.getTargets()){
                    if (componentService.getName().equals(serviceName) || 
                        componentService.getName().equals(componentName)) {
                        referenceList.add(reference);                 
                    }
                }
            }
        }          

        return referenceList;
    }
    
    public List<Reference> findDomainLevelReferenceForService(Composite composite, String referenceName){
        List<Reference> referenceList = new ArrayList<Reference>();
        
        for (Composite tmpComposite : composite.getIncludes()) { 
            List<Reference> tmpReferenceList = findReferenceForService(tmpComposite, referenceName);
            
            referenceList.addAll(tmpReferenceList);
        } 
        
        return referenceList;
    }    

    public Service findServiceForReference(Composite composite, String referenceName){
        
        String componentName = getComponentNameFromReference(referenceName);
        String serviceName = getServiceNameFromReference(referenceName);

        for (Service service: composite.getServices()) {
            if (service.getName().equals(serviceName)){
                return service; 
            }
        }
        
        for (Component component: composite.getComponents()) {
            if (component.getName().equals(componentName)){
                if (component.getServices().size() > 1) {
                    for (Service service: component.getServices()) {
                        if (service.getName().equals(serviceName)){
                            return service; 
                        }
                    }
                } else if (component.getServices().size() == 1) {
                    return component.getServices().get(0);
                } 
            }
        }          

        return null;
    } 
    
    public Service findDomainLevelService(Composite composite, String referenceName){
        Service service = null;
        
        for (Composite tmpComposite : composite.getIncludes()) { 
            service = findServiceForReference(tmpComposite, referenceName);
            if (service != null) {
                break;
            }
        } 
        
        return service;
    }
    
    public void updateDomainLevelServiceURI(Composite domainLevelComposite, String referenceName, String bindingClassName, String URI){
        
        String componentName = getComponentNameFromReference(referenceName);
        String serviceName = getServiceNameFromReference(referenceName);
        
        // get the named service 
        Service service = null;
        for(Composite composite : domainLevelComposite.getIncludes()) {
            service = findServiceForReference(composite, referenceName);                
            if (service != null){
                break;
            }
        }

        if (service != null) {
            // find the named binding
            for (Binding binding : service.getBindings()){
                if (binding.getClass().getName().equals(bindingClassName)){
                    binding.setURI(URI);
                    break;
                }
            }        
        }   
    }

    public List<Composite> wireDomain(Composite domainLevelComposite){
        List<Composite> changedComposites = new ArrayList<Composite>();
        
        // process wires
        
        // autowire
        
        // wire by impl?
        
        // process all wired references
        for(Composite composite : domainLevelComposite.getIncludes()) {
            boolean compositeChanged = false;
            for(Component component : composite.getComponents()){
                for (Reference reference : component.getReferences()){
                    for (ComponentService targetService : reference.getTargets()){
                        String targetName = targetService.getName();
                        String componentName = getComponentNameFromReference(targetName);
                        String serviceName = getServiceNameFromReference(targetName);
                        
                        Service service = null;
                        Component serviceComponent = null;
                        
                        // find the real target service in the domain
                        for(Composite tmpComposite : domainLevelComposite.getIncludes()) {
                            for (Component tmpComponent: tmpComposite.getComponents()) {
                                if (tmpComponent.getName().equals(componentName)){
                                    serviceComponent = tmpComponent;
                                    if (tmpComponent.getServices().size() > 1) {
                                        for (Service tmpService: tmpComponent.getServices()) {
                                            if (tmpService.getName().equals(serviceName)){
                                                service = tmpService; 
                                                break;
                                            }
                                        }
                                    } else if (tmpComponent.getServices().size() == 1) {
                                        service = tmpComponent.getServices().get(0);
                                        break;
                                    }
                                }
                            }
                        }
                        
                        if ( targetService.isUnresolved()){
                            
                            if (service != null){
                                // Find the binding already in use for this target
                                Binding binding = null;
                                
                                for (Binding tmpBinding : reference.getBindings()){
                                    if ((tmpBinding.getName() != null) &&
                                        (tmpBinding.getName().startsWith(reference.getName() + "#" + targetName))){
                                        binding = tmpBinding;
                                    }
                                }

                                // Resolve the binding that should be used for this target
                                // TODO - hang onto the old bindings at the domain level, i.e. 
                                //        don't rely on the target objects as we still need the
                                //        bindings if we are going to do autowiring 
                                List<Binding> source = targetService.getBindings();
                                List<Binding> target = service.getBindings();
                                Binding newBinding = BindingUtil.matchBinding(serviceComponent, (ComponentService)service, source, target);
                                
                                // update the existing binding to the new binding if required
                                if (newBinding != null) {
                                    if (binding != null){
                                        // there is a binding already so see if the URI has changed
                                        if ((binding.getURI() == null) ||
                                            (!binding.getURI().equals(newBinding.getURI()))){
                                            binding.setURI(newBinding.getURI());
                                            compositeChanged = true;
                                        }
                                    } else {
                                        // this is a newly configured binding so add it
                                        reference.getBindings().add(newBinding);
                                        compositeChanged = true;
                                    }
                                }
                            } else {
                                // Do nothing - the target service hasn't been contributed yet
                            }
                        } else {
                            // find the reference binding with the right name 
                            for (Binding refBinding : reference.getBindings()){
                                if ((refBinding.getName() != null) &&
                                    (refBinding.getName().startsWith(reference.getName() + "#" + targetName))){
                                    // find the matching service binding
                                    for (Binding serviceBinding : service.getBindings()){
                                        if (refBinding.getClass() == serviceBinding.getClass()){
                                            refBinding.setURI(serviceBinding.getURI());
                                        }
                                    }
                                }  
                            }
                        }
                    }
                }
            }
          
            if (compositeChanged) {
                changedComposites.add(composite);
            }
        }       
        
        return changedComposites;
    }
    

}