summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/tags/1.0.1/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/SCADomainImpl.java
blob: 5e9a8c0407aaa59f3f34cdc2a46fad09bd8d1249 (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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
 /*
 * 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.node.impl;

import java.net.URI;
import java.net.URL;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.namespace.QName;

import org.apache.tuscany.sca.assembly.AssemblyFactory;
import org.apache.tuscany.sca.assembly.Component;
import org.apache.tuscany.sca.assembly.ComponentService;
import org.apache.tuscany.sca.assembly.Composite;
import org.apache.tuscany.sca.assembly.CompositeService;
import org.apache.tuscany.sca.assembly.SCABinding;
import org.apache.tuscany.sca.assembly.SCABindingFactory;
import org.apache.tuscany.sca.assembly.xml.Constants;
import org.apache.tuscany.sca.contribution.Contribution;
import org.apache.tuscany.sca.contribution.DeployedArtifact;
import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint;
import org.apache.tuscany.sca.contribution.service.ContributionService;
import org.apache.tuscany.sca.core.assembly.ActivationException;
import org.apache.tuscany.sca.core.context.ServiceReferenceImpl;
import org.apache.tuscany.sca.domain.DomainException;
import org.apache.tuscany.sca.domain.DomainManagerNodeEventService;
import org.apache.tuscany.sca.domain.model.Domain;
import org.apache.tuscany.sca.host.embedded.impl.EmbeddedSCADomain;
import org.apache.tuscany.sca.interfacedef.InterfaceContract;
import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory;
import org.apache.tuscany.sca.node.NodeManagerInitService;
import org.apache.tuscany.sca.node.SCADomainProxySPI;
import org.apache.tuscany.sca.node.SCANode;
import org.apache.tuscany.sca.runtime.RuntimeComponent;
import org.apache.tuscany.sca.runtime.RuntimeComponentContext;
import org.apache.tuscany.sca.runtime.RuntimeComponentReference;
import org.osoa.sca.CallableReference;
import org.osoa.sca.ServiceReference;
import org.osoa.sca.ServiceRuntimeException;

/**
 * A local representation of the sca domain running on a single node
 * 
 * @version $Rev: 552343 $ $Date: 2007-09-09 23:54:46 +0100 (Sun, 09 Sep 2007) $
 */
public class SCADomainImpl implements SCADomainProxySPI {
	
    private final static Logger logger = Logger.getLogger(SCADomainImpl.class.getName());
	    
    // the domain used to talk to the rest of the domain
    private EmbeddedSCADomain domainManagementRuntime;
    
    // class loader used to get the runtime going
    private ClassLoader domainClassLoader;  
    
    // representation of the private state of the node that the domain is running on
    private String domainURI; 
    private URL domainURL; 
    
    // proxy to the domain
    private DomainManagerNodeEventService domainManager;
    
    // proxy to the node manager
    private NodeManagerInitService nodeManagerInit;
    private String nodeManagerUrl;
    
    // the local node implementation
    private SCANodeImpl nodeImpl;
       
    // methods defined on the implementation only
          
    /** 
     * Creates a domain proxy connected to a wider domain.  
     * 
     * @param domainUri - identifies what host and port the domain service is running on, e.g. http://localhost:8081
     * @throws ActivationException
     */
    public SCADomainImpl(String domainURI) throws DomainException {
        this.domainURI = domainURI;
        this.domainClassLoader = SCADomainImpl.class.getClassLoader(); 
        init();
    }    
    
    /**
     * Start the composite that connects to the domain manager
     */
    private void init()
      throws DomainException {
        try {
            // check where domain and node uris are urls, they will be used to configure various
            // endpoints if they are
            URI tmpURI;
            try {
                tmpURI = new URI(domainURI); 
                if (tmpURI.isAbsolute() == true){
                    domainURL = tmpURI.toURL();
                }
            } catch(Exception ex) {
                domainURL = null;
            }            
          
            // Check if node has been given a valid domain name to connect to
            if (domainURL == null) {
            	logger.log(Level.INFO, "Domain will be started stand-alone as domain URL is not provided");
            } else {
                // load the composite that allows this domain representation to 
                // connect to the rest of the domain
                String domainCompositeName = "node.composite";
                URL contributionURL = SCANodeUtil.findContributionURLFromCompositeNameOrPath(domainClassLoader, null, new String[]{domainCompositeName} );
                
                if ( contributionURL != null ){ 
                    logger.log(Level.INFO, "Domain management configured from " + contributionURL);
                    
                    // start a local domain in order to talk to the logical domain
                    domainManagementRuntime = new EmbeddedSCADomain(domainClassLoader, domainURI);   
                    domainManagementRuntime.start();
                
                    // add node composite to the management domain
                    ContributionService contributionService = domainManagementRuntime.getContributionService();
                    Contribution contribution = null;

                    contribution = contributionService.contribute(domainURI, 
                                                                  contributionURL, 
                                                                  false);
                    
                    Composite composite = null;
                    for (DeployedArtifact artifact: contribution.getArtifacts()) {
                        if (domainCompositeName.equals(artifact.getURI())) {
                            composite = (Composite)artifact.getModel();
                        }
                    }
                    
                    if (composite != null) {
                    
                        domainManagementRuntime.getDomainComposite().getIncludes().add(composite);
                        domainManagementRuntime.getCompositeBuilder().build(composite);
                        
                        // deal with the special case of registering the node manager service 
                        // in service discovery. It's not on an SCA binding. 
                        // TODO - really want to be able to hand out service references but they
                        //        don't serialize out over web services yet. 
                        SCANodeUtil.fixUpNodeServiceUrls(domainManagementRuntime.getDomainComposite().getIncludes().get(0).getComponents(), null); 
                        SCANodeUtil.fixUpNodeReferenceUrls(domainManagementRuntime.getDomainComposite().getIncludes().get(0).getComponents(), domainURL);  
                      
                        domainManagementRuntime.getCompositeActivator().activate(composite); 
                        domainManagementRuntime.getCompositeActivator().start(composite);
                    
                        // get the management components out of the domain so that they 
                        // can be configured/used. 
                        domainManager = domainManagementRuntime.getService(DomainManagerNodeEventService.class, "DomainManagerComponent");
                        nodeManagerInit = domainManagementRuntime.getService(NodeManagerInitService.class, "NodeManagerComponent/NodeManagerInitService");
                        
                        // Now get the uri back out of the component now it has been built and started
                        // TODO - this doesn't pick up the url from external hosting environments
                        nodeManagerUrl = SCANodeUtil.getNodeManagerServiceUrl(domainManagementRuntime.getDomainComposite().getIncludes().get(0).getComponents());                       
                             
                    } else {
                        throw new ActivationException("Domain management contribution " + 
                                                      contributionURL + 
                                                      " found but could not be loaded");
                    }
                } else {
                    throw new ActivationException("Doamin management contribution " + 
                                                  domainCompositeName + 
                                                  " not found on the classpath");
                }
            }                   
        } catch(Exception ex) {
            throw new DomainException(ex);
        }
    }
    
   // SPI methods 
    
    public String addNode(String nodeURI, String nodeURL){
        // Does nothing in the proxy
        return null;
    }
    
    public String removeNode(String nodeURI){
        // Does nothing in the proxy
        return null;
    }  
    
    public void addNode(SCANode nodeImpl) throws DomainException {
        this.nodeImpl = (SCANodeImpl)nodeImpl;
        
        // register node with wider domain
        if (domainURL != null){
            // pass this object into the node manager service
            nodeManagerInit.setNode(nodeImpl);
            
            try {
                // go out and add this node to the wider domain
                domainManager.registerNode(nodeImpl.getURI(),nodeManagerUrl);

            } catch(Exception ex) {
                logger.log(Level.SEVERE,  
                           "Can't connect to domain manager at: " + 
                           domainURL);
                throw new DomainException(ex);
            }              
        }
    }
    
    public void removeNode(SCANode nodeImpl) throws DomainException {
        
        if (domainURL != null){
            // remove the node from node manager service
            //nodeManagerInit.removeNode(nodeImpl);
            
            try {
                // go out and add this node to the wider domain
                domainManager.removeNode(nodeImpl.getURI());
            } catch(Exception ex) {
                logger.log(Level.SEVERE,  
                           "Can't connect to domain manager at: " + 
                           domainURL);
                throw new DomainException(ex);
            }
        }  
        
        this.nodeImpl = null;
    }  
    
    public void registerContribution(String nodeURI, String contributionURI, String contributionURL){
        domainManager.registerContribution(nodeURI, contributionURI, contributionURL);
    }
    
    public void unregisterContribution(String contributionURI){
        domainManager.unregisterContribution(contributionURI);
    }    
     

    public String registerServiceEndpoint(String domainUri, String nodeUri, String serviceName, String bindingName, String URL){
        return domainManager.registerServiceEndpoint(domainUri, nodeUri, serviceName, bindingName, URL);
    }
   
    public String  removeServiceEndpoint(String domainUri, String nodeUri, String serviceName, String bindingName){
        return domainManager.removeServiceEndpoint(domainUri, nodeUri, serviceName, bindingName);
    }
     
    public String findServiceEndpoint(String domainUri, String serviceName, String bindingName){
        return domainManager.findServiceEndpoint(domainUri, serviceName, bindingName);
    }
    
    public Domain getDomainModel(){        
        return null;
    }     
       
      
    // API methods 
    
    public void start() throws DomainException {
        // TODO - what happens here?
    }
    
    public void stop() throws DomainException {
        try {
          // Stop the domain
          domainManagementRuntime.stop();
        } catch (Exception ex) {
            throw new DomainException(ex);
        }

    }    
 
    public String getURI(){
        return domainURI;
    }

    public void addContribution(String contributionURI, URL contributionURL) throws DomainException {
        try {
            nodeImpl.addContribution(contributionURI, contributionURL);
        } catch(Exception ex) {
            new DomainException(ex);
        }
    }

    public void removeContribution(String uri) throws DomainException {
        try {
            //nodeImpl.removeContributions();
        } catch(Exception ex) {
            new DomainException(ex);
        }
    }
    
    public void addDeploymentComposite(String contributionURI, String compositeXML) throws DomainException {
        // TODO 
    }
    
    public void addToDomainLevelComposite(QName qname) throws DomainException {
        try {
            nodeImpl.deployComposite(qname);
        } catch(Exception ex) {
            new DomainException(ex);
        }
    }
      
    public void removeFromDomainLevelComposite(QName qname) throws DomainException {
        try {
            //nodeImpl.stopComposite();
        } catch(Exception ex) {
            new DomainException(ex);
        }        
    }     
      
    public void startComposite(QName qname) throws DomainException {
        try {
            nodeImpl.deployComposite(qname);
        } catch(Exception ex) {
            new DomainException(ex);
        }        
    }
      
    public void stopComposite(QName qname) throws DomainException {
        try {
            //nodeImpl.stopComposite();
        } catch(Exception ex) {
            new DomainException(ex);
        }         
    }    
           
    public <B, R extends CallableReference<B>> R cast(B target) throws IllegalArgumentException {
        return (R)nodeImpl.getNodeRuntime().getProxyFactory().cast(target);
    }

    public <B> B getService(Class<B> businessInterface, String serviceName) {
        ServiceReference<B> serviceReference = getServiceReference(businessInterface, serviceName);
        if (serviceReference == null) {
            throw new ServiceRuntimeException("Service not found: " + serviceName);
        }
        return serviceReference.getService();
    }

    private <B> ServiceReference<B> createServiceReference(Class<B> businessInterface, String targetURI) {
        try {
            AssemblyFactory assemblyFactory = nodeImpl.getNodeRuntime().getAssemblyFactory();
            Composite composite = assemblyFactory.createComposite();
            composite.setName(new QName(Constants.SCA10_TUSCANY_NS, "default"));
            RuntimeComponent component = (RuntimeComponent)assemblyFactory.createComponent();
            component.setName("default");
            component.setURI("default");
            nodeImpl.getNodeRuntime().getCompositeActivator().configureComponentContext(component);
            composite.getComponents().add(component);
            RuntimeComponentReference reference = (RuntimeComponentReference)assemblyFactory.createComponentReference();
            reference.setName("default");
            ModelFactoryExtensionPoint factories =
                nodeImpl.getNodeRuntime().getExtensionPointRegistry().getExtensionPoint(ModelFactoryExtensionPoint.class);
            JavaInterfaceFactory javaInterfaceFactory = factories.getFactory(JavaInterfaceFactory.class);
            InterfaceContract interfaceContract = javaInterfaceFactory.createJavaInterfaceContract();
            interfaceContract.setInterface(javaInterfaceFactory.createJavaInterface(businessInterface));
            reference.setInterfaceContract(interfaceContract);
            component.getReferences().add(reference);
            reference.setComponent(component);
            SCABindingFactory scaBindingFactory = factories.getFactory(SCABindingFactory.class);
            SCABinding binding = scaBindingFactory.createSCABinding();
            binding.setURI(targetURI);
            reference.getBindings().add(binding);       
            return new ServiceReferenceImpl<B>(businessInterface, component, reference, binding, nodeImpl.getNodeRuntime()
                .getProxyFactory(), nodeImpl.getNodeRuntime().getCompositeActivator());
        } catch (Exception e) {
            throw new ServiceRuntimeException(e);
        }
    }

    public <B> ServiceReference<B> getServiceReference(Class<B> businessInterface, String name) {

        // Extract the component name
        String componentName;
        String serviceName;
        int i = name.indexOf('/');
        if (i != -1) {
            componentName = name.substring(0, i);
            serviceName = name.substring(i + 1);

        } else {
            componentName = name;
            serviceName = null;
        }

        // Lookup the component in the domain
        Component component = null;
        
        if (nodeImpl != null) {
            component = nodeImpl.getComponent(componentName);
       
            if (component == null) {
                // The component is not local in the partition, try to create a remote service ref
                return createServiceReference(businessInterface, name);
            }
        }
        
        RuntimeComponentContext componentContext = null;

        // If the component is a composite, then we need to find the
        // non-composite component that provides the requested service
        if (component.getImplementation() instanceof Composite) {
            for (ComponentService componentService : component.getServices()) {
                if (serviceName == null || serviceName.equals(componentService.getName())) {
                    CompositeService compositeService = (CompositeService)componentService.getService();
                    if (compositeService != null) {
                        if (serviceName != null) {
                            serviceName = "$promoted$." + serviceName;
                        }
                        componentContext =
                            ((RuntimeComponent)compositeService.getPromotedComponent()).getComponentContext();
                        return componentContext.createSelfReference(businessInterface, compositeService
                            .getPromotedService());
                    }
                    break;
                }
            }
            // No matching service is found
            throw new ServiceRuntimeException("Composite service not found: " + name);
        } else {
            componentContext = ((RuntimeComponent)component).getComponentContext();
            if (serviceName != null) {
                return componentContext.createSelfReference(businessInterface, serviceName);
            } else {
                return componentContext.createSelfReference(businessInterface);
            }
        }
    }

}