/* * 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 ..."); } }