Enhance the PolicyBuilder interface to provide ability to validate and build

git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@882246 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
rfeng 2009-11-19 18:57:02 +00:00
parent 140a3558b4
commit 8622a760ab
7 changed files with 226 additions and 15 deletions

View file

@ -31,6 +31,7 @@ import org.apache.tuscany.sca.assembly.Component;
import org.apache.tuscany.sca.assembly.Composite;
import org.apache.tuscany.sca.assembly.Contract;
import org.apache.tuscany.sca.assembly.Endpoint;
import org.apache.tuscany.sca.assembly.EndpointReference;
import org.apache.tuscany.sca.assembly.Implementation;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.core.FactoryExtensionPoint;
@ -355,8 +356,8 @@ public class DefaultBuilderExtensionPoint implements BuilderExtensionPoint, Life
this.qname = ServiceDeclarationParser.getQName(sd.getAttributes().get("qname"));
}
public void build(Component component, Implementation implementation, BuilderContext context) {
getBuilder().build(component, implementation, context);
public boolean build(Component component, Implementation implementation, BuilderContext context) {
return getBuilder().build(component, implementation, context);
}
public QName getPolicyType() {
@ -382,12 +383,16 @@ public class DefaultBuilderExtensionPoint implements BuilderExtensionPoint, Life
return builder;
}
public void build(Endpoint endpoint, BuilderContext context) {
getBuilder().build(endpoint, context);
public boolean build(Endpoint endpoint, BuilderContext context) {
return getBuilder().build(endpoint, context);
}
public void build(org.apache.tuscany.sca.assembly.EndpointReference endpointReference, BuilderContext context) {
getBuilder().build(endpointReference, context);
public boolean build(org.apache.tuscany.sca.assembly.EndpointReference endpointReference, BuilderContext context) {
return getBuilder().build(endpointReference, context);
}
public boolean build(EndpointReference endpointReference, Endpoint endpoint, BuilderContext context) {
return getBuilder().build(endpointReference, endpoint, context);
}
}

View file

@ -41,21 +41,33 @@ public interface PolicyBuilder<T> {
* Build (and validate) the policy settings on the endpoint
* @param endpoint
* @param monitor
* @return true if the policy setting is compatible
*/
void build(Endpoint endpoint, BuilderContext context);
boolean build(Endpoint endpoint, BuilderContext context);
/**
* Build (and validate) the policy settings on the endpoint reference
* @param endpointReference
* @param monitor
* @return true if the policy setting is compatible
*/
void build(EndpointReference endpointReference, BuilderContext context);
boolean build(EndpointReference endpointReference, BuilderContext context);
/**
* Build (and validate) the policy settings on the component implementation
* @param component
* @param implementation
* @param monitor
* @return true if the policy setting is compatible
*/
void build(Component component, Implementation implementation, BuilderContext context);
boolean build(Component component, Implementation implementation, BuilderContext context);
/**
* Build (and validate) the policy settings on the endpoint reference is compatible with the endpoint
* @param endpointReference
* @param endpoint
* @param context
* @return if the policy setting is compatible
*/
boolean build(EndpointReference endpointReference, Endpoint endpoint, BuilderContext context);
}

View file

@ -0,0 +1,38 @@
/*
* 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.assembly.builder;
import org.apache.tuscany.sca.assembly.Endpoint;
import org.apache.tuscany.sca.assembly.EndpointReference;
/**
* A builder that connects an endpoint reference to an endpoint
*/
public interface WireBuilder {
/**
* Build the endpoint reference against the endpoint. If the endpoint is a valid target for the
* endpoint reference, the wire buidler can populate some information from the endpoint into the
* endpoint reference (such as targetEndpoint, policySets or requiredIntents)
* @param endpointReference
* @param endpoint
* @return true if the endpoint is a valid target for the endpoint reference
*/
boolean build(EndpointReference endpointReference, Endpoint endpoint);
}

View file

@ -0,0 +1,148 @@
/*
* 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.builder.impl;
import java.util.List;
import javax.xml.namespace.QName;
import org.apache.tuscany.sca.assembly.Component;
import org.apache.tuscany.sca.assembly.Endpoint;
import org.apache.tuscany.sca.assembly.EndpointReference;
import org.apache.tuscany.sca.assembly.Implementation;
import org.apache.tuscany.sca.assembly.builder.BuilderContext;
import org.apache.tuscany.sca.assembly.builder.Messages;
import org.apache.tuscany.sca.assembly.builder.PolicyBuilder;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.monitor.Monitor;
import org.apache.tuscany.sca.policy.Intent;
import org.apache.tuscany.sca.policy.PolicySubject;
/**
*
*/
public class IntentValidator implements PolicyBuilder {
/**
* @param registry
*/
public IntentValidator(ExtensionPointRegistry registry) {
super();
}
/* (non-Javadoc)
* @see org.apache.tuscany.sca.assembly.builder.PolicyBuilder#build(org.apache.tuscany.sca.assembly.Endpoint, org.apache.tuscany.sca.assembly.builder.BuilderContext)
*/
public boolean build(Endpoint endpoint, BuilderContext context) {
return checkMutualExclusion(endpoint, context);
}
/* (non-Javadoc)
* @see org.apache.tuscany.sca.assembly.builder.PolicyBuilder#build(org.apache.tuscany.sca.assembly.EndpointReference, org.apache.tuscany.sca.assembly.builder.BuilderContext)
*/
public boolean build(EndpointReference endpointReference, BuilderContext context) {
boolean ok = checkMutualExclusion(endpointReference, context);
if(!ok) {
return false;
}
Endpoint endpoint = endpointReference.getTargetEndpoint();
if (endpoint == null) {
return true;
}
ok = checkMutualExclusion(endpointReference, endpoint, context);
List<Intent> intentList1 = endpointReference.getRequiredIntents();
List<Intent> intentList2 = endpoint.getRequiredIntents();
return ok;
}
/* (non-Javadoc)
* @see org.apache.tuscany.sca.assembly.builder.PolicyBuilder#build(org.apache.tuscany.sca.assembly.Component, org.apache.tuscany.sca.assembly.Implementation, org.apache.tuscany.sca.assembly.builder.BuilderContext)
*/
public boolean build(Component component, Implementation implementation, BuilderContext context) {
return true;
}
/* (non-Javadoc)
* @see org.apache.tuscany.sca.assembly.builder.PolicyBuilder#getPolicyType()
*/
public QName getPolicyType() {
// TODO Auto-generated method stub
return null;
}
/**
* Check if two policy subjects requires multually exclusive intents
* @param subject1
* @param subject2
* @param monitor
* @return
*/
private boolean checkMutualExclusion(PolicySubject subject1, PolicySubject subject2, BuilderContext context) {
if (subject1 == subject2 || subject1 == null || subject2 == null) {
return false;
}
for (Intent i1 : subject1.getRequiredIntents()) {
for (Intent i2 : subject2.getRequiredIntents()) {
if (i1.getExcludedIntents().contains(i2) || i2.getExcludedIntents().contains(i1)) {
Monitor.error(context.getMonitor(),
this,
Messages.ASSEMBLY_VALIDATION,
"MutuallyExclusiveIntents",
new Object[] {subject1, subject2},
i1,
i2);
return true;
}
}
}
return false;
}
private boolean checkMutualExclusion(PolicySubject subject, BuilderContext context) {
if (subject == null) {
return false;
}
List<Intent> intents = subject.getRequiredIntents();
int size = intents.size();
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
Intent i1 = intents.get(i);
Intent i2 = intents.get(j);
if (i1 != i2 && i1.getExcludedIntents().contains(i2) || i2.getExcludedIntents().contains(i1)) {
Monitor.error(context.getMonitor(),
this,
Messages.ASSEMBLY_VALIDATION,
"MutuallyExclusiveIntents",
subject,
i1,
i2);
return true;
}
}
}
return false;
}
public boolean build(EndpointReference endpointReference, Endpoint endpoint, BuilderContext context) {
return true;
}
}

View file

@ -454,6 +454,7 @@ public class RuntimeEndpointImpl extends EndpointImpl implements RuntimeEndpoint
}
public synchronized ServiceBindingProvider getBindingProvider() {
resolve();
if (bindingProvider == null) {
BindingProviderFactory factory =
(BindingProviderFactory)providerFactories.getProviderFactory(getBinding().getClass());
@ -467,6 +468,7 @@ public class RuntimeEndpointImpl extends EndpointImpl implements RuntimeEndpoint
}
public synchronized List<PolicyProvider> getPolicyProviders() {
resolve();
if (policyProviders == null) {
policyProviders = new ArrayList<PolicyProvider>();
for (PolicyProviderFactory factory : providerFactories.getPolicyProviderFactories()) {
@ -484,7 +486,7 @@ public class RuntimeEndpointImpl extends EndpointImpl implements RuntimeEndpoint
}
public Contract getContract() {
return service;
return getService();
}
public CompositeContext getCompositeContext() {

View file

@ -451,8 +451,7 @@ public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implemen
}
public Contract getContract() {
resolve();
return reference;
return getReference();
}
public CompositeContext getCompositeContext() {

View file

@ -39,19 +39,22 @@ import org.apache.tuscany.sca.policy.PolicySubject;
*/
public class JDKLoggingPolicyBuilder implements PolicyBuilder<JDKLoggingPolicy> {
public void build(Endpoint endpoint, BuilderContext context) {
public boolean build(Endpoint endpoint, BuilderContext context) {
List<JDKLoggingPolicy> polices = getPolicies(endpoint);
System.out.println(endpoint + ": " + polices);
return true;
}
public void build(EndpointReference endpointReference, BuilderContext context) {
public boolean build(EndpointReference endpointReference, BuilderContext context) {
List<JDKLoggingPolicy> polices = getPolicies(endpointReference);
System.out.println(endpointReference + ": " + polices);
return true;
}
public void build(Component component, Implementation implementation, BuilderContext context) {
public boolean build(Component component, Implementation implementation, BuilderContext context) {
List<JDKLoggingPolicy> polices = getPolicies(implementation);
System.out.println(implementation + ": " + polices);
return true;
}
public QName getPolicyType() {
@ -70,4 +73,8 @@ public class JDKLoggingPolicyBuilder implements PolicyBuilder<JDKLoggingPolicy>
return polices;
}
public boolean build(EndpointReference endpointReference, Endpoint endpoint, BuilderContext context) {
return true;
}
}