summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/contrib/modules/section10/src/main/java/org/apache/tuscany/sca/something/impl/DeployedComposite.java
diff options
context:
space:
mode:
Diffstat (limited to 'sca-java-2.x/contrib/modules/section10/src/main/java/org/apache/tuscany/sca/something/impl/DeployedComposite.java')
-rw-r--r--sca-java-2.x/contrib/modules/section10/src/main/java/org/apache/tuscany/sca/something/impl/DeployedComposite.java158
1 files changed, 158 insertions, 0 deletions
diff --git a/sca-java-2.x/contrib/modules/section10/src/main/java/org/apache/tuscany/sca/something/impl/DeployedComposite.java b/sca-java-2.x/contrib/modules/section10/src/main/java/org/apache/tuscany/sca/something/impl/DeployedComposite.java
new file mode 100644
index 0000000000..226d3c2b87
--- /dev/null
+++ b/sca-java-2.x/contrib/modules/section10/src/main/java/org/apache/tuscany/sca/something/impl/DeployedComposite.java
@@ -0,0 +1,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();
+ }
+ }
+}