summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/contrib/modules/section10/src/main/java/org/apache/tuscany/sca/something/impl/DeployedComposite.java
blob: 226d3c2b87417fe7c601a5c78d1f75303b8c010e (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
/*
 * 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.something.impl;

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

import javax.xml.namespace.QName;

import org.apache.tuscany.sca.assembly.Composite;
import org.apache.tuscany.sca.context.CompositeContext;
import org.apache.tuscany.sca.contribution.Artifact;
import org.apache.tuscany.sca.contribution.Contribution;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.deployment.Deployer;
import org.apache.tuscany.sca.monitor.Monitor;
import org.apache.tuscany.sca.monitor.MonitorFactory;
import org.apache.tuscany.sca.monitor.Problem;
import org.apache.tuscany.sca.monitor.Problem.Severity;
import org.apache.tuscany.sca.runtime.ActivationException;
import org.apache.tuscany.sca.runtime.CompositeActivator;
import org.apache.tuscany.sca.runtime.EndpointRegistry;
import org.oasisopen.sca.ServiceRuntimeException;

public class DeployedComposite {
    
    private Composite c; 
    private InstalledContribution ic; 
    private List<Contribution> dependentContributions;
    private String uri;

    private CompositeActivator compositeActivator;
    private CompositeContext compositeContext;
    private Composite domainComposite; // TODO: this is a misleadingly named
    private Deployer deployer;
    private MonitorFactory monitorFactory;
    private EndpointRegistry endpointRegistry;
    private ExtensionPointRegistry extensionPointRegistry;

    public DeployedComposite(Composite c,
                             InstalledContribution ic,
                             List<Contribution> dependentContributions,
                             Deployer deployer,
                             CompositeActivator compositeActivator,
                             MonitorFactory monitorFactory,
                             EndpointRegistry endpointRegistry,
                             ExtensionPointRegistry extensionPointRegistry) throws ActivationException {
        this.c = c;
        this.ic = ic;
        this.dependentContributions = dependentContributions;
        this.deployer = deployer;
        this.compositeActivator = compositeActivator;
        this.monitorFactory = monitorFactory;
        this.endpointRegistry = endpointRegistry;
        this.extensionPointRegistry = extensionPointRegistry;
        try {
            init();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    protected void init() throws Throwable {
        
        List<Contribution> contributions = new ArrayList<Contribution>();
        contributions.add(ic.getContribution());
        contributions.get(0).getDeployables().clear();
        contributions.get(0).getDeployables().add(c);
        
        Monitor monitor = monitorFactory.createMonitor();
        Monitor tcm = monitorFactory.setContextMonitor(monitor);
        try {

            domainComposite = deployer.build(contributions, dependentContributions, new HashMap<QName, List<String>>(), monitor);
            analyzeProblems(monitor);

        } finally {
            monitorFactory.setContextMonitor(tcm);
        }
        
        compositeContext = new CompositeContext(extensionPointRegistry, 
                                                endpointRegistry, 
                                                domainComposite, 
                                                null, // nothing appears to use the domain name in CompositeContext 
                                                null, // don't need node uri
                                                deployer.getSystemDefinitions());
                       
        CompositeContext.setThreadCompositeContext(compositeContext); // TODO: what is this doing?

        compositeActivator.activate(compositeContext, domainComposite);
        compositeActivator.start(compositeContext, domainComposite);

        this.uri = getCompositeURI(c, ic);
    }
    
    
    public void unDeploy() throws ActivationException {
        compositeActivator.stop(compositeContext, domainComposite);
        compositeActivator.deactivate(domainComposite);
    }
    
    public String getURI() {
        return uri;
    }

    /**
     * Deployable composites don't have the uri set so get it from the artifact in the contribution
     */
    protected String getCompositeURI(Composite c, InstalledContribution ic) {
        for (Artifact a : ic.getContribution().getArtifacts()) {
            if (a.getModel() != null) {
                if (a.getModel() instanceof Composite) {
                    Composite cm = a.getModel();
                    if (c.getName().equals(cm.getName())) {
                        return cm.getURI();
                    }
                }
            }
        }
        // shouldn't ever happen
        throw new IllegalStateException("can't determine composte uri");
    }

    protected void analyzeProblems(Monitor monitor) throws Throwable {
        try {
            for (Problem problem : monitor.getProblems()) {
                if ((problem.getSeverity() == Severity.ERROR)) {
                    if (problem.getCause() != null) {
                        throw problem.getCause();
                    } else {
                        throw new ServiceRuntimeException(problem.toString());
                    }
                }
            }
        } finally {
            // FIXME: Clear problems so that the monitor is clean again
            monitor.reset();
        }
    }
}