You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by sv...@apache.org on 2007/10/07 15:48:07 UTC

svn commit: r582632 - in /incubator/tuscany/java/sca/modules: assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/ implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/ implementation-java-runt...

Author: svkrish
Date: Sun Oct  7 06:48:05 2007
New Revision: 582632

URL: http://svn.apache.org/viewvc?rev=582632&view=rev
Log:
extending java implementation type to recognize and apply policies, adding util support for this in Policy module

Added:
    incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/PoliciedJavaImplementationInvoker.java
    incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/policy/
    incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/policy/LoggingPolicyHandler.java
    incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.policy.PolicySetHandlers
    incubator/tuscany/java/sca/modules/policy/src/main/java/org/apache/tuscany/sca/policy/util/
    incubator/tuscany/java/sca/modules/policy/src/main/java/org/apache/tuscany/sca/policy/util/PolicyHandler.java
    incubator/tuscany/java/sca/modules/policy/src/main/java/org/apache/tuscany/sca/policy/util/PolicySetHandlerUtil.java
Modified:
    incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeWireBuilderImpl.java
    incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaComponentContextProvider.java
    incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaImplementationInvoker.java
    incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaImplementationProvider.java
    incubator/tuscany/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PolicyProcessor.java

Modified: incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeWireBuilderImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeWireBuilderImpl.java?rev=582632&r1=582631&r2=582632&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeWireBuilderImpl.java (original)
+++ incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeWireBuilderImpl.java Sun Oct  7 06:48:05 2007
@@ -1218,12 +1218,12 @@
     
     private void determineApplicableDomainPolicySets(Component component, PolicySetAttachPoint policiedImplementation) {
         if ( domainPolicySets != null && policiedImplementation.getRequiredIntents().size() > 0 ) {
-            IntentAttachPointType bindingType = policiedImplementation.getType();
+            IntentAttachPointType implType = policiedImplementation.getType();
             for ( PolicySet policySet : domainPolicySets ) {
-                if ( isPolicySetApplicable(component, policySet.getAppliesTo(), bindingType) ) {
+                if ( isPolicySetApplicable(component, policySet.getAppliesTo(), implType) ) {
                     int prevSize = policiedImplementation.getRequiredIntents().size();
                     trimProvidedIntents(policiedImplementation.getRequiredIntents(), policySet);
-                    //if any intent was trimmed off, then this policyset must be attached to the binding
+                    //if any intent was trimmed off, then this policyset must be attached to the implementation
                     if ( prevSize != policiedImplementation.getRequiredIntents().size() ) {
                         policiedImplementation.getPolicySets().add(policySet);
                     }   
@@ -1231,9 +1231,7 @@
             }
             
             if ( policiedImplementation.getRequiredIntents().size() > 0 ) {
-                if ( component instanceof Service ) {
-                    warning("There are unfulfilled intents for component implementation - " + component.getName(), component);
-                } 
+                warning("There are unfulfilled intents for component implementation - " + component.getName(), component);
             }
         }
     }

Modified: incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaComponentContextProvider.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaComponentContextProvider.java?rev=582632&r1=582631&r2=582632&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaComponentContextProvider.java (original)
+++ incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaComponentContextProvider.java Sun Oct  7 06:48:05 2007
@@ -54,6 +54,9 @@
 import org.apache.tuscany.sca.interfacedef.Operation;
 import org.apache.tuscany.sca.interfacedef.java.impl.JavaInterfaceUtil;
 import org.apache.tuscany.sca.invocation.Invoker;
+import org.apache.tuscany.sca.policy.PolicySet;
+import org.apache.tuscany.sca.policy.PolicySetAttachPoint;
+import org.apache.tuscany.sca.policy.util.PolicyHandler;
 import org.apache.tuscany.sca.runtime.RuntimeComponent;
 import org.apache.tuscany.sca.runtime.RuntimeComponentReference;
 import org.apache.tuscany.sca.runtime.RuntimeWire;
@@ -71,13 +74,15 @@
     private RuntimeComponent component;
     private JavaInstanceFactoryProvider<?> instanceFactoryProvider;
     private ProxyFactory proxyFactory;
+    private Map<PolicySet, PolicyHandler> policyHandlers = null;
 
     public JavaComponentContextProvider(RuntimeComponent component,
                                         JavaInstanceFactoryProvider configuration,
                                         DataBindingExtensionPoint dataBindingExtensionPoint,
                                         JavaPropertyValueObjectFactory propertyValueObjectFactory,
                                         ComponentContextFactory componentContextFactory,
-                                        RequestContextFactory requestContextFactory) {
+                                        RequestContextFactory requestContextFactory,
+                                        Map<PolicySet, PolicyHandler> policyHandlers) {
         super();
         this.instanceFactoryProvider = configuration;
         this.proxyFactory = configuration.getProxyFactory();
@@ -89,6 +94,7 @@
         this.component = component;
         this.dataBindingRegistry = dataBindingExtensionPoint;
         this.propertyValueFactory = propertyValueObjectFactory;
+        this.policyHandlers = policyHandlers;
     }
 
     InstanceWrapper<?> createInstanceWrapper() throws ObjectCreationException {
@@ -215,6 +221,7 @@
                 }
             }
         }
+        setUpPolicyHandlers();
     }
 
     void addResourceFactory(String name, ObjectFactory<?> factory) {
@@ -254,13 +261,20 @@
     }
 
     void stop() {
+        cleanUpPolicyHandlers();
     }
 
     Invoker createInvoker(Operation operation) throws NoSuchMethodException {
         Class<?> implClass = instanceFactoryProvider.getImplementationClass();
 
         Method method = JavaInterfaceUtil.findMethod(implClass, operation);
-        return new JavaImplementationInvoker(operation, method, component);
+        
+        if ( component.getImplementation() instanceof PolicySetAttachPoint  &&
+              !((PolicySetAttachPoint)component.getImplementation()).getPolicySets().isEmpty() ) {
+            return new PoliciedJavaImplementationInvoker(operation, method, component, policyHandlers);
+        } else {
+            return new JavaImplementationInvoker(operation, method, component);
+        }
     }
 
     private static class OptimizedObjectFactory<T> implements ObjectFactory<T> {
@@ -328,6 +342,18 @@
      */
     RuntimeComponent getComponent() {
         return component;
+    }
+    
+    private void setUpPolicyHandlers() {
+        for ( PolicyHandler policyHandler : policyHandlers.values() ) {
+            policyHandler.setUp(component.getImplementation());
+        }
+    }
+    
+    private void cleanUpPolicyHandlers() {
+        for ( PolicyHandler policyHandler : policyHandlers.values() ) {
+            policyHandler.cleanUp(this);
+        }
     }
 
 }

Modified: incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaImplementationInvoker.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaImplementationInvoker.java?rev=582632&r1=582631&r2=582632&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaImplementationInvoker.java (original)
+++ incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaImplementationInvoker.java Sun Oct  7 06:48:05 2007
@@ -40,11 +40,11 @@
  * @version $Rev$ $Date$
  */
 public class JavaImplementationInvoker implements Invoker {
-    private Operation operation;
-    private Method method;
+    protected Operation operation;
+    protected  Method method;
 
     @SuppressWarnings("unchecked")
-    private final ScopeContainer scopeContainer;
+    protected final ScopeContainer scopeContainer;
 
     public JavaImplementationInvoker(Operation operation, Method method, RuntimeComponent component) {
         assert method != null : "Operation method cannot be null";

Modified: incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaImplementationProvider.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaImplementationProvider.java?rev=582632&r1=582631&r2=582632&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaImplementationProvider.java (original)
+++ incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaImplementationProvider.java Sun Oct  7 06:48:05 2007
@@ -19,6 +19,11 @@
 
 package org.apache.tuscany.sca.implementation.java.invocation;
 
+import java.util.Hashtable;
+import java.util.Map;
+
+import javax.xml.namespace.QName;
+
 import org.apache.tuscany.sca.assembly.Service;
 import org.apache.tuscany.sca.context.ComponentContextFactory;
 import org.apache.tuscany.sca.context.RequestContextFactory;
@@ -36,6 +41,10 @@
 import org.apache.tuscany.sca.implementation.java.injection.ResourceObjectFactory;
 import org.apache.tuscany.sca.interfacedef.Operation;
 import org.apache.tuscany.sca.invocation.Invoker;
+import org.apache.tuscany.sca.policy.PolicySet;
+import org.apache.tuscany.sca.policy.PolicySetAttachPoint;
+import org.apache.tuscany.sca.policy.util.PolicyHandler;
+import org.apache.tuscany.sca.policy.util.PolicySetHandlerUtil;
 import org.apache.tuscany.sca.runtime.RuntimeComponent;
 import org.apache.tuscany.sca.runtime.RuntimeComponentService;
 import org.osoa.sca.ComponentContext;
@@ -48,6 +57,7 @@
     private JavaImplementation implementation;
     private JavaComponentContextProvider componentContextProvider;
     private RequestContextFactory requestContextFactory;
+    private Map<PolicySet, PolicyHandler> policyHandlers = new Hashtable<PolicySet, PolicyHandler>();
     
     public JavaImplementationProvider(RuntimeComponent component,
                                       JavaImplementation implementation,
@@ -64,7 +74,7 @@
             configuration.setProxyFactory(proxyService);
             componentContextProvider =
                 new JavaComponentContextProvider(component, configuration, dataBindingRegistry, propertyValueObjectFactory,
-                                      componentContextFactory, requestContextFactory);
+                                      componentContextFactory, requestContextFactory, policyHandlers);
 
             Scope scope = getScope();
 
@@ -93,10 +103,29 @@
 
             componentContextProvider.configureProperties(component.getProperties());
             handleResources(implementation, proxyService);
+            loadPolicyHandlers();
         } catch (Exception e) {
             throw new IllegalStateException(e);
         }
-
+        
+    }
+    
+    private void loadPolicyHandlers() throws Exception {
+        Map<QName, PolicyHandler> availablePolicyHandlers = 
+            PolicySetHandlerUtil.getPolicyHandlers(Thread.currentThread().getContextClassLoader(), 
+                                                    "org.apache.tuscany.sca.policy.PolicySetHandlers");
+        if ( implementation instanceof PolicySetAttachPoint ) {
+            PolicyHandler aHandler = null;
+            PolicySetAttachPoint policiedImpl = (PolicySetAttachPoint)implementation;
+            for ( PolicySet policySet : policiedImpl.getPolicySets() ) {
+                if ( ( aHandler = availablePolicyHandlers.get(policySet.getName()) ) != null ) {
+                    aHandler.setHandledPolicySet(policySet);
+                    policyHandlers.put(policySet, aHandler);
+                } else {
+                    //FIXME : maybe there must be a warning thrown here
+                }
+            }
+        }
     }
 
     private void handleResources(JavaImplementation componentType, ProxyFactory proxyService) {
@@ -169,5 +198,4 @@
     public long getMaxIdleTime() {
         return implementation.getMaxIdleTime();
     }
-
 }

Added: incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/PoliciedJavaImplementationInvoker.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/PoliciedJavaImplementationInvoker.java?rev=582632&view=auto
==============================================================================
--- incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/PoliciedJavaImplementationInvoker.java (added)
+++ incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/PoliciedJavaImplementationInvoker.java Sun Oct  7 06:48:05 2007
@@ -0,0 +1,65 @@
+/*
+ * 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.implementation.java.invocation;
+
+import java.lang.reflect.Method;
+import java.util.Map;
+
+import org.apache.tuscany.sca.interfacedef.Operation;
+import org.apache.tuscany.sca.invocation.Message;
+import org.apache.tuscany.sca.policy.PolicySet;
+import org.apache.tuscany.sca.policy.util.PolicyHandler;
+import org.apache.tuscany.sca.runtime.RuntimeComponent;
+
+/**
+ * Responsible for synchronously dispatching an invocation to a Java component
+ * implementation instance effecting applicable policies before and after invocation 
+ */
+public class PoliciedJavaImplementationInvoker extends JavaImplementationInvoker {
+    private Map<PolicySet, PolicyHandler> policyHandlers = null;
+    
+    public PoliciedJavaImplementationInvoker(Operation operation, 
+                                             Method method, 
+                                             RuntimeComponent component,
+                                             Map<PolicySet, PolicyHandler> policyHandlers) {  
+        super(operation, method, component);
+        this.policyHandlers = policyHandlers;
+    }
+    
+    public Message invoke(Message msg) { 
+        applyPreInvocationPolicies(operation, msg);
+        msg = super.invoke(msg);
+        applyPostInvocationPolices(operation, msg);
+        return msg;
+    }
+    
+    private void applyPreInvocationPolicies(Object... context) {
+        for ( PolicyHandler policyHandler : policyHandlers.values() ) {
+            policyHandler.beforeInvoke(context);
+        }
+    }
+    
+    private void applyPostInvocationPolices(Object...  context) {
+        for ( PolicyHandler policyHandler : policyHandlers.values() ) {
+            policyHandler.afterInvoke(context);
+        }
+    }
+        
+}

Added: incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/policy/LoggingPolicyHandler.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/policy/LoggingPolicyHandler.java?rev=582632&view=auto
==============================================================================
--- incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/policy/LoggingPolicyHandler.java (added)
+++ incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/policy/LoggingPolicyHandler.java Sun Oct  7 06:48:05 2007
@@ -0,0 +1,115 @@
+package org.apache.tuscany.sca.implementation.java.policy;
+import java.util.logging.ConsoleHandler;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.xml.namespace.QName;
+
+import org.apache.tuscany.sca.contribution.util.ServiceConfigurationUtil;
+import org.apache.tuscany.sca.implementation.java.JavaImplementation;
+import org.apache.tuscany.sca.interfacedef.Operation;
+import org.apache.tuscany.sca.invocation.Message;
+import org.apache.tuscany.sca.policy.PolicySet;
+import org.apache.tuscany.sca.policy.util.PolicyHandler;
+
+/*
+ * 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.    
+ */
+
+/**
+ * Policy handler to handle PolicySet related to Logging with the QName
+ * {http://tuscany.apache.org/xmlns/sca/1.0/impl/java}LoggingPolicy
+ */
+public class LoggingPolicyHandler implements PolicyHandler {
+    public static final String javaImplPolicyNamespace = "http://tuscany.apache.org/xmlns/sca/1.0/impl/java/policy";
+    public static final String logginPolicy = "LoggingPolicy";
+    public static final QName policySetQName = new QName(javaImplPolicyNamespace,
+                                                         logginPolicy); 
+    private Logger logger = null;
+    
+    private PolicySet handledPolicySet = null;
+
+    public PolicySet getHandledPolicySet() {
+        return handledPolicySet;
+    }
+
+    public void setHandledPolicySet(PolicySet handledPolicySet) {
+        this.handledPolicySet = handledPolicySet;
+    }
+
+    public void afterInvoke(Object... context) {
+        Operation operation = null;
+        Message msg = null; 
+        
+        for ( Object contextParam : context ) {
+            if (contextParam instanceof Operation ) {
+                operation = (Operation)contextParam;
+            } else if ( contextParam instanceof Message ) {
+                msg = (Message)contextParam;
+            }
+        }
+        
+        if ( operation != null && msg != null ) {
+            System.out.println(" Returning after invoking operation " + operation.getName() + " with result " +
+                               msg.getBody());
+            
+        }
+    }
+
+    public void beforeInvoke(Object... context) { 
+        Operation operation = null; 
+        Message msg = null; 
+        
+        for ( Object contextParam : context ) {
+            if (contextParam instanceof Operation ) {
+                operation = (Operation)contextParam;
+            } else if ( contextParam instanceof Message ) {
+                msg = (Message)contextParam;
+            }
+        }
+        
+        if ( operation != null && msg != null ) {
+            Object msgBody = msg.getBody();
+            if ( msgBody instanceof Object[] ) { 
+                System.out.println(" About to invoke operations " + operation.getName() + " with arguments... ");
+                for ( Object o : ((Object[])msg.getBody())) {
+                    System.out.println(o);
+                }
+            } else {
+                System.out.println(" About to invoke operation " + operation.getName() + " with arguments " +
+                                   msg.getBody());
+            }
+        }
+    }
+
+    public void cleanUp(Object... context) {
+        logger = null;
+    }
+
+    public void setUp(Object... context) {
+        for ( Object contextParam : context ) {
+            if ( contextParam instanceof JavaImplementation ) {
+                JavaImplementation impl = (JavaImplementation)contextParam;
+                logger = Logger.getLogger(impl.getJavaClass().getName()); 
+                logger.addHandler(new ConsoleHandler());
+                logger.setLevel(Level.ALL);
+            }
+        }
+    }
+}

Added: incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.policy.PolicySetHandlers
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.policy.PolicySetHandlers?rev=582632&view=auto
==============================================================================
--- incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.policy.PolicySetHandlers (added)
+++ incubator/tuscany/java/sca/modules/implementation-java-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.policy.PolicySetHandlers Sun Oct  7 06:48:05 2007
@@ -0,0 +1,18 @@
+# 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.
+# Implementation class for the ModuleActivator
+{http\://tuscany.apache.org/xmlns/sca/1.0/impl/java/policy}LoggingPolicy=org.apache.tuscany.sca.implementation.java.policy.LoggingPolicyHandler

Modified: incubator/tuscany/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PolicyProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PolicyProcessor.java?rev=582632&r1=582631&r2=582632&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PolicyProcessor.java (original)
+++ incubator/tuscany/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PolicyProcessor.java Sun Oct  7 06:48:05 2007
@@ -34,7 +34,9 @@
 import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceContract;
 import org.apache.tuscany.sca.policy.Intent;
 import org.apache.tuscany.sca.policy.PolicyFactory;
+import org.apache.tuscany.sca.policy.PolicySet;
 import org.apache.tuscany.sca.policy.PolicySetAttachPoint;
+import org.osoa.sca.annotations.PolicySets;
 import org.osoa.sca.annotations.Requires;
 
 /**
@@ -71,13 +73,15 @@
      * @param clazz
      * @param requiredIntents
      */
-    private void readIntents(Class<?> clazz, List<Intent> requiredIntents) {
+    private void readIntentsAndPolicySets(Class<?> clazz, 
+                                          List<Intent> requiredIntents, 
+                                          List<PolicySet> policySets) {
         Requires intentAnnotation = clazz.getAnnotation(Requires.class);
         if (intentAnnotation != null) {
             String[] intentNames = intentAnnotation.value();
             if (intentNames.length != 0) {
                 for (String intentName : intentNames) {
-                    
+
                     // Add each intent to the list
                     Intent intent = policyFactory.createIntent();
                     intent.setName(getQName(intentName));
@@ -85,6 +89,20 @@
                 }
             }
         }
+        
+        PolicySets policySetAnnotation = clazz.getAnnotation(PolicySets.class);
+        if (policySetAnnotation != null) {
+            String[] policySetNames = policySetAnnotation.value();
+            if (policySetNames.length != 0) {
+                for (String policySetName : policySetNames) {
+
+                    // Add each intent to the list
+                    PolicySet policySet = policyFactory.createPolicySet();
+                    policySet.setName(getQName(policySetName));
+                    policySets.add(policySet);
+                }
+            }
+        }
     }
     
     private void readIntents(Method method, List<Intent> requiredIntents) {
@@ -113,7 +131,9 @@
         
         // Read intents on the Java implementation class
         if ( type instanceof PolicySetAttachPoint ) {
-            readIntents(clazz, ((PolicySetAttachPoint)type).getRequiredIntents());
+            readIntentsAndPolicySets(clazz, 
+                                     ((PolicySetAttachPoint)type).getRequiredIntents(),
+                                     ((PolicySetAttachPoint)type).getPolicySets());
         }
         
         // Process annotations on the service interfaces
@@ -127,7 +147,9 @@
                 if (javaInterfaceContract.getInterface() != null) {
                     JavaInterface javaInterface = (JavaInterface)javaInterfaceContract.getInterface();
                     if (javaInterface.getJavaClass() != null) {
-                        readIntents(javaInterface.getJavaClass(), service.getRequiredIntents());
+                        readIntentsAndPolicySets(javaInterface.getJavaClass(), 
+                                                 service.getRequiredIntents(),
+                                                 service.getPolicySets());
 
                         // Read intents on the service interface methods 
                         Method[] methods = javaInterface.getJavaClass().getMethods();
@@ -147,7 +169,9 @@
                             callback = assemblyFactory.createCallback();
                             service.setCallback(callback);
                         }
-                        readIntents(javaCallbackInterface.getJavaClass(), callback.getRequiredIntents());
+                        readIntentsAndPolicySets(javaCallbackInterface.getJavaClass(), 
+                                                 callback.getRequiredIntents(),
+                                                 callback.getPolicySets());
 
                         // Read intents on the callback interface methods 
                         Method[] methods = javaCallbackInterface.getJavaClass().getMethods();

Added: incubator/tuscany/java/sca/modules/policy/src/main/java/org/apache/tuscany/sca/policy/util/PolicyHandler.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/policy/src/main/java/org/apache/tuscany/sca/policy/util/PolicyHandler.java?rev=582632&view=auto
==============================================================================
--- incubator/tuscany/java/sca/modules/policy/src/main/java/org/apache/tuscany/sca/policy/util/PolicyHandler.java (added)
+++ incubator/tuscany/java/sca/modules/policy/src/main/java/org/apache/tuscany/sca/policy/util/PolicyHandler.java Sun Oct  7 06:48:05 2007
@@ -0,0 +1,34 @@
+/*
+ * 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.policy.util;
+
+import org.apache.tuscany.sca.policy.PolicySet;
+
+/**
+ * Handler interface for handling policies defined in policysets
+ */
+public interface PolicyHandler {
+    PolicySet getHandledPolicySet();
+    void setHandledPolicySet(PolicySet policySet);
+    void setUp(Object... context);
+    void cleanUp(Object... context);
+    void beforeInvoke(Object... context);
+    void afterInvoke(Object... context);
+}

Added: incubator/tuscany/java/sca/modules/policy/src/main/java/org/apache/tuscany/sca/policy/util/PolicySetHandlerUtil.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/policy/src/main/java/org/apache/tuscany/sca/policy/util/PolicySetHandlerUtil.java?rev=582632&view=auto
==============================================================================
--- incubator/tuscany/java/sca/modules/policy/src/main/java/org/apache/tuscany/sca/policy/util/PolicySetHandlerUtil.java (added)
+++ incubator/tuscany/java/sca/modules/policy/src/main/java/org/apache/tuscany/sca/policy/util/PolicySetHandlerUtil.java Sun Oct  7 06:48:05 2007
@@ -0,0 +1,113 @@
+/*
+ * 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.policy.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.Properties;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.xml.namespace.QName;
+
+public class PolicySetHandlerUtil {
+    private final static Logger logger = Logger.getLogger(PolicySetHandlerUtil.class.getName());
+
+    /**
+     * Read the service name from a configuration file
+     * 
+     * @param classLoader
+     * @param name The name of the service class
+     * @return A class name which extends/implements the service class
+     * @throws IOException
+     */
+    public static Map<QName, PolicyHandler> getPolicyHandlers(ClassLoader classLoader, 
+                                                             String name) throws IllegalAccessException, 
+                                                                                 InstantiationException,
+                                                                                 ClassNotFoundException,
+                                                                                 IOException {
+        boolean debug = logger.isLoggable(Level.FINE);
+        if (debug) {
+            logger.fine("Discovering service providers using class loader " + classLoader);
+        }
+        
+        Map<QName, PolicyHandler> policyHandlersMap = new Hashtable<QName, PolicyHandler>();
+        for (URL url : Collections.list(classLoader.getResources("META-INF/services/" + name))) {
+            if (debug) {
+                logger.fine("Reading service provider file: " + url.toExternalForm());
+            }
+            
+            InputStream is = url.openStream();
+            
+            try {
+                Properties policyHanlderInfo = new Properties();
+                policyHanlderInfo.load(is);
+                
+                Enumeration keys = policyHanlderInfo.keys();
+                String policySetName;
+                String policyHandlerClassName;
+                PolicyHandler policyHandler;
+                
+                while ( keys.hasMoreElements() ) {
+                    policySetName = (String)keys.nextElement();
+                    policyHandlerClassName = policyHanlderInfo.getProperty(policySetName);
+                    
+                    policyHandler = 
+                        (PolicyHandler)Class.forName(policyHandlerClassName, true, classLoader).newInstance();
+                    
+                    if (debug) {
+                        logger.fine("Registering policyset handler : " + policySetName + " = " + policyHandlerClassName);;
+                    }
+                
+                    policyHandlersMap.put(getQName(policySetName), policyHandler);
+                }
+            } finally {
+                if (is != null) {
+                    try {
+                        is.close();
+                    } catch (IOException ioe) {
+                    }
+                }
+            }
+        }
+        return policyHandlersMap;
+    }
+
+    private static QName getQName(String policySetName) {
+        QName qname;
+        if (policySetName.startsWith("{")) {
+            int i = policySetName.indexOf('}');
+            if (i != -1) {
+                qname = new QName(policySetName.substring(1, i), policySetName.substring(i + 1));
+            } else {
+                qname = new QName("", policySetName);
+            }
+        } else {
+            qname = new QName("", policySetName);
+        }
+        return qname;
+    }
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org