summaryrefslogtreecommitdiffstats
path: root/sandbox/slaws/modules/domain-rework/src/main/java/org/apache/tuscany/sca/domain/rework/Launcher.java
blob: 2ec8d2322200d41ebf27c5842ff9cb96a29a9f1f (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
/*
 * 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.domain.rework;

import java.io.ByteArrayOutputStream;
import java.io.Externalizable;
import java.io.File;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;

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.Service;
import org.apache.tuscany.sca.assembly.builder.DomainBuilder;
import org.apache.tuscany.sca.assembly.xml.Constants;
import org.apache.tuscany.sca.contribution.Artifact;
import org.apache.tuscany.sca.contribution.Contribution;
import org.apache.tuscany.sca.contribution.DeployedArtifact;
import org.apache.tuscany.sca.contribution.Export;
import org.apache.tuscany.sca.contribution.Import;
import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint;
import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor;
import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint;
import org.apache.tuscany.sca.contribution.service.ContributionService;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.core.assembly.ActivationException;
import org.apache.tuscany.sca.core.context.ServiceReferenceImpl;
import org.apache.tuscany.sca.databinding.impl.XSDDataTypeConverter.Base64Binary;
import org.apache.tuscany.sca.domain.DomainException;
import org.apache.tuscany.sca.domain.SCADomain;
import org.apache.tuscany.sca.domain.SCADomainEventService;
import org.apache.tuscany.sca.domain.SCADomainSPI;
import org.apache.tuscany.sca.domain.management.SCADomainManagerInitService;
import org.apache.tuscany.sca.domain.model.CompositeModel;
import org.apache.tuscany.sca.domain.model.ContributionModel;
import org.apache.tuscany.sca.domain.model.DomainModel;
import org.apache.tuscany.sca.domain.model.DomainModelFactory;
import org.apache.tuscany.sca.domain.model.NodeModel;
import org.apache.tuscany.sca.domain.model.ServiceModel;
import org.apache.tuscany.sca.domain.model.NodeModel.LifecyleState;
import org.apache.tuscany.sca.domain.model.impl.DomainModelFactoryImpl;
import org.apache.tuscany.sca.domain.model.impl.NodeModelImpl;
import org.apache.tuscany.sca.host.embedded.impl.ReallySmallRuntime;
import org.apache.tuscany.sca.host.http.ServletHost;
import org.apache.tuscany.sca.host.http.ServletHostExtensionPoint;
import org.apache.tuscany.sca.interfacedef.InterfaceContract;
import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory;
import org.apache.tuscany.sca.node.NodeException;
import org.apache.tuscany.sca.node.NodeFactoryImpl;
import org.apache.tuscany.sca.node.util.SCAContributionUtil;
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;

/**
 * The SCA domain repository that manages domain contributions
 * 
 * @version $Rev$ $Date$
 */
public class Launcher  {
	
    private final static Logger logger = Logger.getLogger(Launcher.class.getName());
    
       
    // class loader used to get the runtime going
    private ClassLoader domainClassLoader;
    
    // domain management application runtime
    private ReallySmallRuntime domainRepositoryRuntime;
    private ContributionService domainRepositoryContributionService;
    private Contribution domainRepositoryContribution;
    private Composite domainRepositoryComposite;
    
    // domain service
    private DomainServiceInit domainService; 
   
    // Implementation methods
     
    /** 
     * Create a domain giving the URI for the domain. 
     * 
     * @throws DomainException
     */
    public Launcher() throws DomainException {
        this.domainClassLoader = Launcher.class.getClassLoader();       
        init();
    }    
       
    /**
     * Create a runtime for the repository app
     * TODO: we need a better wrapper for this that isn't an SCADomain - too confusing!
     */
    protected void init() throws DomainException {
        try {
                
            // create a runtime for the domain repository services to run on
            domainRepositoryRuntime = new ReallySmallRuntime(domainClassLoader);
            domainRepositoryRuntime.start();
            
            // Create an in-memory domain level management composite
            AssemblyFactory assemblyFactory = domainRepositoryRuntime.getAssemblyFactory();
            domainRepositoryComposite = assemblyFactory.createComposite();
            domainRepositoryComposite.setName(new QName(Constants.SCA10_NS, "domainManagement"));
            domainRepositoryComposite.setURI("domainManagement");   
                        
            // Find the composite that will configure the domain
            String domainCompositeName = "domain/domain.composite";
            URL contributionURL = SCAContributionUtil.findContributionFromResource(domainClassLoader, domainCompositeName);
            
            
            if ( contributionURL != null ){ 
                logger.log(Level.INFO, "Domain configured from " + contributionURL);
                           
                // add domain.composite to the management domain
                domainRepositoryContributionService = domainRepositoryRuntime.getContributionService();
                Contribution contribution = null;
                contribution = domainRepositoryContributionService.contribute("domainRepository", 
                                                                              contributionURL, 
                                                                              false);
                
                // update the runtime for all SCA Definitions processed from the contribution.
                // so that the policyset determination done during 'build' has the all the defined
                // intents and policysets from the management contribution
                domainRepositoryRuntime.updateSCADefinitions(domainRepositoryContributionService.getContributionSCADefinitions());
                
                Composite composite = null;
                for (Artifact artifact: contribution.getArtifacts()) {
                    if (domainCompositeName.equals(artifact.getURI())) {
                        composite = (Composite)artifact.getModel();
                    }
                }
                
                if (composite != null) {
                
                    domainRepositoryRuntime.buildComposite(composite);
                    domainRepositoryRuntime.getCompositeActivator().activate(composite); 
                    domainRepositoryRuntime.getCompositeActivator().start(composite);
                    
                    // find the domain service
                    Component component = null;

                    for (Component compositeComponent: composite.getComponents()) {
                        if (compositeComponent.getName().equals("DomainService")) {
                            component = compositeComponent;
                        }
                    }
                    
                    RuntimeComponentContext componentContext = ((RuntimeComponent)component).getComponentContext();
                    domainService = componentContext.createSelfReference(DomainServiceInit.class, "DomainServiceInit").getService();

                    // set up the domain service. Could work out what the URI
                    // is by looking at the service URL or could put it in the 
                    // config file
                    domainService.setDomainURI("http://localhost:8080");
                    domainService.setRuntime(domainRepositoryRuntime);
                        
                } else {
                    throw new ActivationException("Domain repository contribution " + 
                                                  contributionURL + 
                                                  " found but could not be loaded");
                }
            } else {
                throw new ActivationException("Domain repository contribution " + 
                                              domainCompositeName + 
                                              " not found on the classpath");
            }  
            
            
        } catch(Exception ex) {
            throw new DomainException(ex);
        }
    }     

    public static void main(String[] args) throws Exception {
        System.out.println("Starting ...");
               
        Launcher domainLauncher = new Launcher();
        
        System.out.println("domain repository started");
        
        // start contribution processing
        domainLauncher.domainService.test();
        
        System.out.println("Running ...");
       // System.in.read();
        

        System.out.println("Stopping ...");                
    }
}