You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-dev@ws.apache.org by dk...@apache.org on 2009/06/22 20:41:05 UTC

svn commit: r787347 - in /webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi: ./ util/

Author: dkulp
Date: Mon Jun 22 18:41:04 2009
New Revision: 787347

URL: http://svn.apache.org/viewvc?rev=787347&view=rev
Log:
Update to use java 5 generics.   Get rest of Eclipse warnings cleaned
up.   Nothing major at this point.

Modified:
    webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AbstractPolicyOperator.java
    webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/All.java
    webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AssertionBuilderFactory.java
    webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/Policy.java
    webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/PolicyOperator.java
    webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/PolicyRegistryImpl.java
    webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/util/PolicyComparator.java
    webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/util/Service.java

Modified: webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AbstractPolicyOperator.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AbstractPolicyOperator.java?rev=787347&r1=787346&r2=787347&view=diff
==============================================================================
--- webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AbstractPolicyOperator.java (original)
+++ webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AbstractPolicyOperator.java Mon Jun 22 18:41:04 2009
@@ -30,22 +30,22 @@
  * PolicyOperator interface that other PolicyOperators can use.
  */
 public abstract class AbstractPolicyOperator implements PolicyOperator {
-    protected ArrayList policyComponents = new ArrayList();
+    protected List<PolicyComponent> policyComponents = new ArrayList<PolicyComponent>();
 
     public void addPolicyComponent(PolicyComponent component) {
         policyComponents.add(component);
     }
 
-    public void addPolicyComponents(List components) {
+    public void addPolicyComponents(List<PolicyComponent> components) {
         policyComponents.addAll(components);
     }
 
-    public List getPolicyComponents() {
+    public List<PolicyComponent> getPolicyComponents() {
         return policyComponents;
     }
 
     public PolicyComponent getFirstPolicyComponent() {
-        return (PolicyComponent) policyComponents.get(0);
+        return policyComponents.get(0);
     }
 
     public boolean isEmpty() {
@@ -88,12 +88,8 @@
             return exactlyOne;
         }
                 
-        ArrayList childComponentsList = new ArrayList();
-        PolicyComponent policyComponent;
-        
-        for (Iterator iterator = operator.getPolicyComponents().iterator(); iterator.hasNext();) {
-            policyComponent = (PolicyComponent) iterator.next();
-            
+        List<PolicyComponent> childComponentsList = new ArrayList<PolicyComponent>();
+        for (PolicyComponent policyComponent : operator.getPolicyComponents()) {
             if (policyComponent.getType() == Constants.TYPE_ASSERTION) {
                 
                 if (deep) {
@@ -136,15 +132,14 @@
         return computeResultantComponent(childComponentsList, type);
     }
     
-    private static PolicyComponent computeResultantComponent(List normalizedInnerComponets, short componentType) {
+    private static PolicyComponent computeResultantComponent(List<PolicyComponent> normalizedInnerComponets, 
+                                                             short componentType) {
         
         ExactlyOne exactlyOne = new ExactlyOne();
         
         if (componentType == Constants.TYPE_EXACTLYONE) {            
-            ExactlyOne innerExactlyOne;
-            
-            for (Iterator iter = normalizedInnerComponets.iterator(); iter.hasNext();) {
-                innerExactlyOne = (ExactlyOne) iter.next();
+            for (PolicyComponent comp : normalizedInnerComponets) {
+                ExactlyOne innerExactlyOne = (ExactlyOne)comp;
                 exactlyOne.addPolicyComponents(innerExactlyOne.getPolicyComponents());
             }
             
@@ -152,7 +147,7 @@
             // if the parent type is All then we have to get the cross product
             if (normalizedInnerComponets.size() > 1) {
                 // then we have to get the cross product with each other to process all elements
-                Iterator iter = normalizedInnerComponets.iterator();
+                Iterator<PolicyComponent> iter = normalizedInnerComponets.iterator();
                 // first get the first element
                 exactlyOne = (ExactlyOne) iter.next();
                 // if this is empty, this is an not admissible policy and total result is equalent to that
@@ -175,7 +170,7 @@
             } else {
                 // i.e only one element exists in the list then we can safely
                 // return that element this is ok even if it is an empty element
-                exactlyOne = (ExactlyOne) normalizedInnerComponets.iterator().next();
+                exactlyOne = (ExactlyOne) normalizedInnerComponets.get(0);
             }
         }
         

Modified: webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/All.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/All.java?rev=787347&r1=787346&r2=787347&view=diff
==============================================================================
--- webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/All.java (original)
+++ webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/All.java Mon Jun 22 18:41:04 2009
@@ -44,7 +44,7 @@
      * 
      * @return a List of it's PolicyComponents
      */
-    public List getAssertions() {
+    public List<PolicyComponent> getAssertions() {
         return policyComponents;
     }
 
@@ -60,11 +60,7 @@
             writer.writeStartElement(Constants.URI_POLICY_NS, Constants.ELEM_ALL);
         }
 
-        PolicyComponent policyComponent;
-
-        for (Iterator iterator = getPolicyComponents().iterator(); iterator
-                .hasNext();) {
-            policyComponent = (PolicyComponent) iterator.next();
+        for (PolicyComponent policyComponent : getPolicyComponents()){
             policyComponent.serialize(writer);
         }
 

Modified: webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AssertionBuilderFactory.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AssertionBuilderFactory.java?rev=787347&r1=787346&r2=787347&view=diff
==============================================================================
--- webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AssertionBuilderFactory.java (original)
+++ webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AssertionBuilderFactory.java Mon Jun 22 18:41:04 2009
@@ -21,6 +21,8 @@
 
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import javax.xml.namespace.QName;
 
@@ -49,15 +51,11 @@
     private static final QName XML_ASSERTION_BUILDER = new QName(
             "http://test.org/test", "test");
 
-    private static HashMap registeredBuilders = new HashMap();
+    private static Map<QName, AssertionBuilder> registeredBuilders 
+        = new ConcurrentHashMap<QName, AssertionBuilder>();
 
     static {
-        AssertionBuilder builder;
-
-        for (Iterator providers = Service.providers(AssertionBuilder.class); providers
-                .hasNext();) {
-            builder = (AssertionBuilder) providers.next();
-
+        for (AssertionBuilder builder : Service.providers(AssertionBuilder.class)) {
             QName[] knownElements = builder.getKnownElements();
             for (int i = 0; i < knownElements.length; i++) {
                 registerBuilder(knownElements[i], builder);
@@ -93,7 +91,7 @@
         AssertionBuilder builder;
 
         QName qname = element.getQName();
-        builder = (AssertionBuilder) registeredBuilders.get(qname);
+        builder = registeredBuilders.get(qname);
 
         if (builder != null) {
             return builder.build(element, this);
@@ -103,8 +101,7 @@
          *  if we can't locate an appropriate AssertionBuilder, we always
          *  use the XMLPrimitiveAssertionBuilder 
          */ 
-        builder = (AssertionBuilder) registeredBuilders
-                .get(XML_ASSERTION_BUILDER);
+        builder = registeredBuilders.get(XML_ASSERTION_BUILDER);
         return builder.build(element, this);
     }
     
@@ -116,6 +113,6 @@
      * @return an AssertionBuilder that understands qname type
      */
     public AssertionBuilder getBuilder(QName qname) {
-        return (AssertionBuilder) registeredBuilders.get(qname);
+        return registeredBuilders.get(qname);
     }
 }

Modified: webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/Policy.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/Policy.java?rev=787347&r1=787346&r2=787347&view=diff
==============================================================================
--- webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/Policy.java (original)
+++ webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/Policy.java Mon Jun 22 18:41:04 2009
@@ -34,7 +34,7 @@
  */
 public class Policy extends All {
 
-    private HashMap attributes = new HashMap();
+    private Map<QName, String> attributes = new HashMap<QName, String>();
 
     /**
      * Returns a Normalized version of self. If <tt>deep</tt> is set
@@ -111,7 +111,7 @@
         String namespaceURI = null;
         String localName = null;
 
-        HashMap prefix2ns = new HashMap();
+        Map<String, String> prefix2ns = new HashMap<String, String>();
 
         for (Iterator iterator = getAttributes().keySet().iterator(); iterator
                 .hasNext();) {
@@ -238,7 +238,7 @@
      * @return the value of the attribute specified by the QName
      */
     public String getAttribute(QName name) {
-        return (String) attributes.get(name);
+        return attributes.get(name);
     }
 
     /**
@@ -246,7 +246,7 @@
      * 
      * @return a Map of all attributes of self
      */
-    public Map getAttributes() {
+    public Map<QName, String> getAttributes() {
         return attributes;
     }
 

Modified: webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/PolicyOperator.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/PolicyOperator.java?rev=787347&r1=787346&r2=787347&view=diff
==============================================================================
--- webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/PolicyOperator.java (original)
+++ webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/PolicyOperator.java Mon Jun 22 18:41:04 2009
@@ -41,7 +41,7 @@
      * 
      * @return the List of PolicyComponents that this PolicyOperator contains.
      */
-    public List getPolicyComponents();
+    public List<PolicyComponent> getPolicyComponents();
 
     /**
      * Returns true if the PolicyOperator doesn't contain any PolicyComponents.

Modified: webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/PolicyRegistryImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/PolicyRegistryImpl.java?rev=787347&r1=787346&r2=787347&view=diff
==============================================================================
--- webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/PolicyRegistryImpl.java (original)
+++ webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/PolicyRegistryImpl.java Mon Jun 22 18:41:04 2009
@@ -19,7 +19,8 @@
 
 package org.apache.neethi;
 
-import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * Provides a default implementation of PolicyRegistry interface.
@@ -28,7 +29,7 @@
     
     private PolicyRegistry parent = null;
     
-    private HashMap reg = new HashMap();
+    private Map<String, Policy> reg = new ConcurrentHashMap<String, Policy>();
     
     public PolicyRegistryImpl() {
     }
@@ -45,7 +46,7 @@
     }
     
     public Policy lookup(String key) {
-        Policy policy = (Policy) reg.get(key);
+        Policy policy = reg.get(key);
         
         if (policy == null && parent != null) {
             return parent.lookup(key);

Modified: webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/util/PolicyComparator.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/util/PolicyComparator.java?rev=787347&r1=787346&r2=787347&view=diff
==============================================================================
--- webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/util/PolicyComparator.java (original)
+++ webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/util/PolicyComparator.java Mon Jun 22 18:41:04 2009
@@ -133,23 +133,15 @@
         return true;
     }
 
-    private static boolean compare(List arg1, List arg2) {
+    private static boolean compare(List<PolicyComponent> arg1, List<PolicyComponent> arg2) {
         if (arg1.size() != arg2.size()) {
             return false;
         }
 
-        Iterator iterator = arg1.iterator();
-        PolicyComponent assertion1;
-
-        while (iterator.hasNext()) {
-            assertion1 = (PolicyComponent) iterator.next();
-
-            Iterator iterator2 = arg2.iterator();
+        
+        for (PolicyComponent assertion1 : arg1) {
             boolean match = false;
-            PolicyComponent assertion2;
-
-            while (iterator2.hasNext()) {
-                assertion2 = (PolicyComponent) iterator2.next();
+            for (PolicyComponent assertion2 : arg2) {
                 if (compare(assertion1, assertion2)) {
                     match = true;
                     break;

Modified: webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/util/Service.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/util/Service.java?rev=787347&r1=787346&r2=787347&view=diff
==============================================================================
--- webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/util/Service.java (original)
+++ webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/util/Service.java Mon Jun 22 18:41:04 2009
@@ -25,7 +25,9 @@
 import java.io.InputStreamReader;
 import java.io.Reader;
 import java.net.URL;
+import java.util.ArrayList;
 import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -45,8 +47,12 @@
 public class Service {
 
     // Remember providers we have looked up before.
-    static Map classMap = new java.util.HashMap();
-    static Map instanceMap = new java.util.HashMap();
+    static Map<String, List<?>> instanceMap = new HashMap<String, List<?>>();
+    
+    @SuppressWarnings("unchecked")
+    private static <T> List<T> cast(List<?> p) {
+        return (List<T>)p;
+    }
 
     /**
      * Returns an iterator where each element should implement the
@@ -59,33 +65,17 @@
      *
      * @param cls The class/interface to search for providers of.
      */
-    public static synchronized Iterator providers(Class cls) {
-        return providers(cls, true);
-    }
-    
-    /**
-     * Returns an iterator where each element should implement the
-     * interface (or subclass the baseclass) described by cls.  The
-     * Classes are found by searching the classpath for service files
-     * named: 'META-INF/services/&lt;fully qualified classname&gt; that list
-     * fully qualifted classnames of classes that implement the
-     * service files classes interface.  These classes must have
-     * default constructors if returnInstances is true.
-     *
-     * @param cls The class/interface to search for providers of.
-     * @param returnInstances true if the iterator should return instances rather than class names.
-     */
-    public static synchronized Iterator providers(Class cls, boolean returnInstances) {
+    public static synchronized <T> List<? extends T> providers(Class<T> cls) {
+
         String serviceFile = "META-INF/services/" + cls.getName();
-        Map cacheMap = (returnInstances ? instanceMap : classMap);
 
-        List l = (List)cacheMap.get(serviceFile);
+        List<T> l = cast(instanceMap.get(serviceFile));
         if (l != null) {
-            return l.iterator();
+            return l;
         }
 
-        l = new java.util.ArrayList();
-        cacheMap.put(serviceFile, l);
+        l = new ArrayList<T>();
+        instanceMap.put(serviceFile, l);
 
         ClassLoader cl = null;
         try {
@@ -98,20 +88,22 @@
         if (cl == null) cl = ClassLoader.getSystemClassLoader();
 
         // No class loader so we can't find 'serviceFile'.
-        if (cl == null) return l.iterator();
+        if (cl == null) return l;
 
-        Enumeration e;
+        Enumeration<URL> e;
         try {
             e = cl.getResources(serviceFile);
         } catch (IOException ioe) {
-            return l.iterator();
+            return l;
         }
 
         while (e.hasMoreElements()) {
+            InputStream is = null;
             try {
-                URL u = (URL)e.nextElement();
+                URL u = e.nextElement();
 
-                InputStream    is = u.openStream();
+                is = u.openStream();
+                
                 Reader         r  = new InputStreamReader(is, "UTF-8");
                 BufferedReader br = new BufferedReader(r);
 
@@ -132,14 +124,10 @@
                             continue;
                         }
 
-                        if (returnInstances) {
-                            // Try and load the class 
-                            Object obj = cl.loadClass(line).newInstance();
-                            // stick it into our vector...
-                            l.add(obj);
-                        } else {
-                            l.add(line);
-                        }
+                        // Try and load the class 
+                        Object obj = cl.loadClass(line).newInstance();
+                        // stick it into our vector...
+                        l.add(cls.cast(obj));
                     } catch (Exception ex) {
                         // Just try the next line
                     }
@@ -149,9 +137,17 @@
                 // Just try the next file...
             } catch (LinkageError le) {
                 // Just try the next file...
+            } finally {
+                try {
+                    if (is != null) {
+                        is.close();
+                    }
+                } catch (IOException ex) {
+                    //ignore
+                }
             }
         }
-        return l.iterator();
+        return l;
     }
     
 }
\ No newline at end of file