From a206102be754cca89c3aa8b386f635521aa56bde Mon Sep 17 00:00:00 2001 From: slaws Date: Mon, 17 Oct 2011 12:51:22 +0000 Subject: TUSCANY-3958 - serialize endpoint intents/policy sets across the registry for matching purposes. In this change they are stashed in the component element on write and then retrieved back into the endpoint model on read. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1185135 13f79535-47bb-0310-9956-ffa450edef68 --- .../core/assembly/impl/EndpointSerializerImpl.java | 94 +++++++++++++++++++++- .../core/assembly/impl/RuntimeEndpointImpl.java | 8 +- 2 files changed, 100 insertions(+), 2 deletions(-) (limited to 'sca-java-2.x/trunk/modules/core/src/main/java') diff --git a/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/EndpointSerializerImpl.java b/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/EndpointSerializerImpl.java index 4e5275058e..8cdb0363e9 100644 --- a/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/EndpointSerializerImpl.java +++ b/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/EndpointSerializerImpl.java @@ -21,6 +21,8 @@ package org.apache.tuscany.sca.core.assembly.impl; import java.io.StringReader; import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLOutputFactory; @@ -29,12 +31,21 @@ import javax.xml.stream.XMLStreamWriter; import org.apache.tuscany.sca.assembly.Endpoint; import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.context.CompositeContext; +import org.apache.tuscany.sca.contribution.Contribution; import org.apache.tuscany.sca.contribution.processor.ProcessorContext; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.definitions.Definitions; +import org.apache.tuscany.sca.policy.BindingType; +import org.apache.tuscany.sca.policy.Intent; +import org.apache.tuscany.sca.policy.PolicySet; +import org.apache.tuscany.sca.policy.util.PolicyHelper; import org.apache.tuscany.sca.runtime.EndpointSerializer; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; import org.oasisopen.sca.ServiceRuntimeException; public class EndpointSerializerImpl implements EndpointSerializer { @@ -57,6 +68,7 @@ public class EndpointSerializerImpl implements EndpointSerializer { public Endpoint readEndpoint(String xml) { try { + //System.out.println("Read Endpoint string >> " + xml); XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(xml)); Endpoint result = processor.read(reader, new ProcessorContext(registry)); result.setRemote(true); @@ -66,6 +78,84 @@ public class EndpointSerializerImpl implements EndpointSerializer { throw new ServiceRuntimeException(e); } } + + public void resolveEndpoint(Endpoint endpoint) { + CompositeContext compositeContext = ((RuntimeEndpoint)endpoint).getCompositeContext(); + + if (compositeContext == null){ + // will be null if this is the SCAClient + return; + } + + Definitions systemDefinitions = compositeContext.getSystemDefinitions(); + if (systemDefinitions != null){ + // Find pre-resolved intents from the system definition + List intents = new ArrayList(); + + for (Intent intent : endpoint.getRequiredIntents()){ + Intent resolvedIntent = PolicyHelper.getIntent(systemDefinitions, intent.getName()); + + if (resolvedIntent != null){ + intents.add(resolvedIntent); + } else { + // look to see if this intent is provided by the binding + BindingType bindingType = systemDefinitions.getBindingType(endpoint.getBinding().getType()); + + if (bindingType != null){ + for (Intent apIntent : bindingType.getAlwaysProvidedIntents()){ + if (apIntent.getName().equals(intent.getName())){ + resolvedIntent = apIntent; + break; + } + } + + if (resolvedIntent == null){ + for (Intent mpIntent : bindingType.getMayProvidedIntents()){ + if (mpIntent.getName().equals(intent.getName())){ + resolvedIntent = mpIntent; + break; + } + } + } + } + + if (resolvedIntent != null){ + intents.add(resolvedIntent); + } else { + throw new ServiceRuntimeException("Remote endpoint " + + endpoint + + " has intent " + + intent + + " that can't be found in the local system definitions in node " + + compositeContext.getNodeURI()); + } + } + } + + endpoint.getRequiredIntents().clear(); + endpoint.getRequiredIntents().addAll(intents); + + // Find pre-resolved policy sets from the system definition + List policySets = new ArrayList(); + + for (PolicySet policySet : endpoint.getPolicySets()){ + PolicySet resolvedPolicySet = PolicyHelper.getPolicySet(systemDefinitions, policySet.getName()); + if (resolvedPolicySet != null){ + policySets.add(resolvedPolicySet); + } else { + throw new ServiceRuntimeException("Remote endpoint " + + endpoint + + " has policy set " + + policySet + + " that can't be found in the local system definitions in node " + + compositeContext.getNodeURI()); + } + } + + endpoint.getPolicySets().clear(); + endpoint.getPolicySets().addAll(policySets); + } + } public String write(Endpoint endpoint) { StringWriter sw = new StringWriter(); @@ -74,7 +164,9 @@ public class EndpointSerializerImpl implements EndpointSerializer { processor.write(endpoint, writer, new ProcessorContext(registry)); writer.flush(); writer.close(); - return sw.toString(); + String endpointString = sw.toString(); + //System.out.println("Write Endpoint string >> " + endpointString); + return endpointString; } catch (Exception e) { throw new ServiceRuntimeException(e); } diff --git a/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeEndpointImpl.java b/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeEndpointImpl.java index db8911d466..09cbab76d1 100644 --- a/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeEndpointImpl.java +++ b/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeEndpointImpl.java @@ -925,7 +925,12 @@ public class RuntimeEndpointImpl extends EndpointImpl implements RuntimeEndpoint @Override protected synchronized void resolve() { if (xml != null && component == null) { - if (compositeContext == null) { + // TUSCANY-3958 - when an endpoint arrives at the remote side of the + // domain registry it's composite context is set, but to + // a default that's not that useful. We can tell because it + // doesn't set the system definitions + if (compositeContext == null || + compositeContext.getSystemDefinitions() == null) { compositeContext = CompositeContext.getCurrentCompositeContext(); if (compositeContext != null) { bind(compositeContext); @@ -934,6 +939,7 @@ public class RuntimeEndpointImpl extends EndpointImpl implements RuntimeEndpoint if (serializer != null) { RuntimeEndpointImpl ep = (RuntimeEndpointImpl)serializer.readEndpoint(xml); copyFrom(ep); + serializer.resolveEndpoint(this); } else { // In this case, we assume that we're running on a detached (non Tuscany) thread and // as a result we need to connect back to the Tuscany environment... -- cgit v1.2.3