Author: ffang
Date: Fri Sep  4 04:21:29 2009
New Revision: 811215

URL: http://svn.apache.org/viewvc?rev=811215&view=rev
Log:
[SMX4-352]Fix the ClassNotFoundException in the xml security bundle

Added:
    servicemix/smx4/bundles/trunk/xmlsec-1.4.3/src/
    servicemix/smx4/bundles/trunk/xmlsec-1.4.3/src/main/
    servicemix/smx4/bundles/trunk/xmlsec-1.4.3/src/main/java/
    servicemix/smx4/bundles/trunk/xmlsec-1.4.3/src/main/java/org/
    servicemix/smx4/bundles/trunk/xmlsec-1.4.3/src/main/java/org/apache/
    servicemix/smx4/bundles/trunk/xmlsec-1.4.3/src/main/java/org/apache/xml/
    
servicemix/smx4/bundles/trunk/xmlsec-1.4.3/src/main/java/org/apache/xml/security/
    
servicemix/smx4/bundles/trunk/xmlsec-1.4.3/src/main/java/org/apache/xml/security/transforms/
    
servicemix/smx4/bundles/trunk/xmlsec-1.4.3/src/main/java/org/apache/xml/security/transforms/Transform.java
    
servicemix/smx4/bundles/trunk/xmlsec-1.4.3/src/main/java/org/apache/xml/security/utils/
    
servicemix/smx4/bundles/trunk/xmlsec-1.4.3/src/main/java/org/apache/xml/security/utils/ClassLoaderUtils.java

Added: 
servicemix/smx4/bundles/trunk/xmlsec-1.4.3/src/main/java/org/apache/xml/security/transforms/Transform.java
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/bundles/trunk/xmlsec-1.4.3/src/main/java/org/apache/xml/security/transforms/Transform.java?rev=811215&view=auto
==============================================================================
--- 
servicemix/smx4/bundles/trunk/xmlsec-1.4.3/src/main/java/org/apache/xml/security/transforms/Transform.java
 (added)
+++ 
servicemix/smx4/bundles/trunk/xmlsec-1.4.3/src/main/java/org/apache/xml/security/transforms/Transform.java
 Fri Sep  4 04:21:29 2009
@@ -0,0 +1,360 @@
+/*
+ * Copyright 1999-2008 The Apache Software Foundation.
+ *
+ *  Licensed 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.xml.security.transforms;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.HashMap;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.apache.xml.security.c14n.CanonicalizationException;
+import org.apache.xml.security.c14n.InvalidCanonicalizerException;
+import org.apache.xml.security.exceptions.AlgorithmAlreadyRegisteredException;
+import org.apache.xml.security.exceptions.XMLSecurityException;
+import org.apache.xml.security.signature.XMLSignatureInput;
+import org.apache.xml.security.utils.ClassLoaderUtils;
+import org.apache.xml.security.utils.Constants;
+import org.apache.xml.security.utils.HelperNodeList;
+import org.apache.xml.security.utils.SignatureElementProxy;
+import org.apache.xml.security.utils.XMLUtils;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+
+/**
+ * Implements the behaviour of the <code>ds:Transform</code> element.
+ *
+ * This <code>Transform</code>(Factory) class acts as the Factory and Proxy of
+ * the implementing class that supports the functionality of <a
+ * href=http://www.w3.org/TR/xmldsig-core/#sec-TransformAlg>a Transform
+ * algorithm</a>.
+ * Implements the Factory and Proxy pattern for ds:Transform algorithms.
+ *
+ * @author Christian Geuer-Pollmann
+ * @see Transforms
+ * @see TransformSpi
+ */
+public final class Transform extends SignatureElementProxy {
+
+    /** {...@link org.apache.commons.logging} logging facility */
+    private static org.apache.commons.logging.Log log = 
+        
org.apache.commons.logging.LogFactory.getLog(Transform.class.getName());
+
+    /** Field _alreadyInitialized */
+    private static boolean alreadyInitialized = false;
+
+    /** All available Transform classes are registered here */
+    private static HashMap transformClassHash = null;
+   
+    private static HashMap transformSpiHash = new HashMap();         
+
+    private TransformSpi transformSpi = null;
+
+    /**
+     * Constructs {...@link Transform}
+     *
+     * @param doc the {...@link Document} in which <code>Transform</code> will 
be 
+     * placed
+     * @param algorithmURI URI representation of 
+     * <code>Transform algorithm</code> which will be specified as parameter 
of 
+     * {...@link #getInstance(Document, String)}, when generated. </br>
+     * @param contextNodes the child node list of <code>Transform</code> 
element
+     * @throws InvalidTransformException
+     */
+    public Transform(Document doc, String algorithmURI, NodeList contextNodes)
+        throws InvalidTransformException {
+
+        super(doc);
+
+        this._constructionElement.setAttributeNS
+            (null, Constants._ATT_ALGORITHM, algorithmURI);
+
+        transformSpi = getTransformSpi(algorithmURI);
+        if (transformSpi == null) {
+            Object exArgs[] = { algorithmURI };
+            throw new InvalidTransformException(
+                "signature.Transform.UnknownTransform", exArgs);
+        }
+
+        if (log.isDebugEnabled()) {
+                   log.debug("Create URI \"" + algorithmURI + "\" class \""
+                   + transformSpi.getClass() + "\"");
+                   log.debug("The NodeList is " + contextNodes);
+        }
+        
+        // give it to the current document
+        if (contextNodes != null) {
+            for (int i = 0; i < contextNodes.getLength(); i++) {
+                this._constructionElement.appendChild
+                    (contextNodes.item(i).cloneNode(true));
+            }
+         }
+    }
+
+    /**
+     * This constructor can only be called from the {...@link Transforms} 
object,
+     * so it's protected.
+     *
+     * @param element <code>ds:Transform</code> element
+     * @param BaseURI the URI of the resource where the XML instance was stored
+     * @throws InvalidTransformException
+     * @throws TransformationException
+     * @throws XMLSecurityException
+     */
+    public Transform(Element element, String BaseURI)
+        throws InvalidTransformException, TransformationException,
+               XMLSecurityException {
+
+        super(element, BaseURI);
+
+        // retrieve Algorithm Attribute from ds:Transform
+        String algorithmURI = element.getAttributeNS(null, 
Constants._ATT_ALGORITHM);
+
+        if (algorithmURI == null || algorithmURI.length() == 0) {
+            Object exArgs[] = { Constants._ATT_ALGORITHM,
+                                Constants._TAG_TRANSFORM };
+            throw new TransformationException("xml.WrongContent", exArgs);
+        }
+
+     
+        transformSpi = getTransformSpi(algorithmURI);
+        if (transformSpi == null) {
+            Object exArgs[] = { algorithmURI };
+            throw new InvalidTransformException(
+               "signature.Transform.UnknownTransform", exArgs);
+        }
+    }
+
+    /**
+     * Generates a Transform object that implements the specified 
+     * <code>Transform algorithm</code> URI.
+     *
+     * @param algorithmURI <code>Transform algorithm</code> URI 
representation, 
+     * such as specified in 
+     * <a href=http://www.w3.org/TR/xmldsig-core/#sec-TransformAlg>Transform 
algorithm </a>
+     * @param doc the proxy {...@link Document}
+     * @return <code>{...@link Transform}</code> object
+     * @throws InvalidTransformException
+     */
+    public static Transform getInstance(
+        Document doc, String algorithmURI) throws InvalidTransformException {
+        return getInstance(doc, algorithmURI, (NodeList) null);
+    }
+
+    /**
+     * Generates a Transform object that implements the specified 
+     * <code>Transform algorithm</code> URI.
+     *
+     * @param algorithmURI <code>Transform algorithm</code> URI 
representation, 
+     * such as specified in 
+     * <a href=http://www.w3.org/TR/xmldsig-core/#sec-TransformAlg>Transform 
algorithm </a>
+     * @param contextChild the child element of <code>Transform</code> element
+     * @param doc the proxy {...@link Document}
+     * @return <code>{...@link Transform}</code> object
+     * @throws InvalidTransformException
+     */
+    public static Transform getInstance(
+        Document doc, String algorithmURI, Element contextChild)
+        throws InvalidTransformException {
+
+        HelperNodeList contextNodes = new HelperNodeList();
+
+        XMLUtils.addReturnToElement(doc, contextNodes);
+        contextNodes.appendChild(contextChild);
+        XMLUtils.addReturnToElement(doc, contextNodes);
+
+        return getInstance(doc, algorithmURI, contextNodes);
+    }
+
+    /**
+     * Generates a Transform object that implements the specified 
+     * <code>Transform algorithm</code> URI.
+     *
+     * @param algorithmURI <code>Transform algorithm</code> URI form, such as 
+     * specified in <a 
href=http://www.w3.org/TR/xmldsig-core/#sec-TransformAlg>
+     * Transform algorithm </a>
+     * @param contextNodes the child node list of <code>Transform</code> 
element
+     * @param doc the proxy {...@link Document}
+     * @return <code>{...@link Transform}</code> object
+     * @throws InvalidTransformException
+     */
+    public static Transform getInstance(
+        Document doc, String algorithmURI, NodeList contextNodes)
+        throws InvalidTransformException {
+        return new Transform(doc, algorithmURI, contextNodes);
+    }
+
+    /**
+     * Initalizes for this {...@link Transform}.
+     */
+    public static void init() {
+        if (!alreadyInitialized) {
+            transformClassHash = new HashMap(10);
+            alreadyInitialized = true;
+        }
+    }
+
+    /**
+     * Registers implementing class of the Transform algorithm with 
algorithmURI
+     *
+     * @param algorithmURI algorithmURI URI representation of 
+     * <code>Transform algorithm</code> will be specified as parameter of 
+     * {...@link #getInstance(Document, String)}, when generate. </br>
+     * @param implementingClass <code>implementingClass</code> the 
implementing 
+     * class of {...@link TransformSpi}
+     * @throws AlgorithmAlreadyRegisteredException if specified algorithmURI 
+     * is already registered
+     */
+    public static void register(String algorithmURI, String implementingClass)
+        throws AlgorithmAlreadyRegisteredException {
+
+        // are we already registered?
+        Class registeredClass = getImplementingClass(algorithmURI);
+        if ((registeredClass != null) ) {
+            Object exArgs[] = { algorithmURI, registeredClass };
+            throw new AlgorithmAlreadyRegisteredException(
+               "algorithm.alreadyRegistered", exArgs);
+        }
+
+        try {
+           transformClassHash.put
+                (algorithmURI, ClassLoaderUtils.loadClass(implementingClass, 
Transform.class));
+       } catch (ClassNotFoundException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Returns the URI representation of Transformation algorithm
+     *
+     * @return the URI representation of Transformation algorithm
+     */
+    public String getURI() {
+        return this._constructionElement.getAttributeNS
+            (null, Constants._ATT_ALGORITHM);
+    }
+
+    /**
+     * Transforms the input, and generates {...@link XMLSignatureInput} as 
output.
+     *
+     * @param input input {...@link XMLSignatureInput} which can supplied 
Octet 
+     * Stream and NodeSet as Input of Transformation
+     * @return the {...@link XMLSignatureInput} class as the result of 
+     * transformation
+     * @throws CanonicalizationException
+     * @throws IOException
+     * @throws InvalidCanonicalizerException
+     * @throws TransformationException
+     */
+    public XMLSignatureInput performTransform(XMLSignatureInput input)
+        throws IOException, CanonicalizationException,
+               InvalidCanonicalizerException, TransformationException {
+
+        XMLSignatureInput result = null;
+
+        try {
+            result = transformSpi.enginePerformTransform(input, this);
+        } catch (ParserConfigurationException ex) {
+            Object exArgs[] = { this.getURI(), "ParserConfigurationException" 
};
+            throw new CanonicalizationException(
+                "signature.Transform.ErrorDuringTransform", exArgs, ex);
+        } catch (SAXException ex) {
+            Object exArgs[] = { this.getURI(), "SAXException" };
+            throw new CanonicalizationException(
+                "signature.Transform.ErrorDuringTransform", exArgs, ex);
+        }
+
+        return result;
+    }
+   
+    /**
+     * Transforms the input, and generates {...@link XMLSignatureInput} as 
output.
+     *
+     * @param input input {...@link XMLSignatureInput} which can supplied 
Octect 
+     * Stream and NodeSet as Input of Transformation
+     * @param os where to output the result of the last transformation
+     * @return the {...@link XMLSignatureInput} class as the result of 
+     * transformation
+     * @throws CanonicalizationException
+     * @throws IOException
+     * @throws InvalidCanonicalizerException
+     * @throws TransformationException
+     */
+    public XMLSignatureInput performTransform(XMLSignatureInput input, 
+        OutputStream os) throws IOException, CanonicalizationException,
+        InvalidCanonicalizerException, TransformationException {
+
+        XMLSignatureInput result = null;
+
+        try {
+                   result = transformSpi.enginePerformTransform(input, os, 
this);
+        } catch (ParserConfigurationException ex) {
+                   Object exArgs[] = { this.getURI(), 
"ParserConfigurationException" };
+           throw new CanonicalizationException(
+               "signature.Transform.ErrorDuringTransform", exArgs, ex);
+        } catch (SAXException ex) {
+                   Object exArgs[] = { this.getURI(), "SAXException" };
+           throw new CanonicalizationException(
+               "signature.Transform.ErrorDuringTransform", exArgs, ex);
+        }
+
+        return result;
+    }
+
+    /**
+     * Method getImplementingClass
+     *
+     * @param URI
+     * @return The name of the class implementing the URI.
+     */
+    private static Class getImplementingClass(String URI) {
+        return (Class) transformClassHash.get(URI);
+    }
+
+    private static TransformSpi getTransformSpi(String URI) 
+        throws InvalidTransformException {
+        try {
+           Object value = transformSpiHash.get(URI);
+           if (value != null) {
+               return (TransformSpi) value;
+           }
+           Class cl = (Class) transformClassHash.get(URI);
+           if (cl != null) {
+               TransformSpi tr = (TransformSpi) cl.newInstance();
+               transformSpiHash.put(URI, tr);
+               return tr;
+           } 
+       } catch (InstantiationException ex) {
+           Object exArgs[] = { URI };
+            throw new InvalidTransformException(
+                "signature.Transform.UnknownTransform", exArgs, ex);      
+       } catch (IllegalAccessException ex) {
+           Object exArgs[] = { URI };
+            throw new InvalidTransformException(
+                "signature.Transform.UnknownTransform", exArgs, ex);      
+       }
+       return null;    
+    }
+   
+    /** @inheritDoc */
+    public String getBaseLocalName() {
+        return Constants._TAG_TRANSFORM;
+    }
+}

Added: 
servicemix/smx4/bundles/trunk/xmlsec-1.4.3/src/main/java/org/apache/xml/security/utils/ClassLoaderUtils.java
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/bundles/trunk/xmlsec-1.4.3/src/main/java/org/apache/xml/security/utils/ClassLoaderUtils.java?rev=811215&view=auto
==============================================================================
--- 
servicemix/smx4/bundles/trunk/xmlsec-1.4.3/src/main/java/org/apache/xml/security/utils/ClassLoaderUtils.java
 (added)
+++ 
servicemix/smx4/bundles/trunk/xmlsec-1.4.3/src/main/java/org/apache/xml/security/utils/ClassLoaderUtils.java
 Fri Sep  4 04:21:29 2009
@@ -0,0 +1,242 @@
+/**
+ * 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.xml.security.utils;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+
+/**
+ * This class is extremely useful for loading resources and classes in a fault
+ * tolerant manner that works across different applications servers. Do not
+ * touch this unless you're a grizzled classloading guru veteran who is going 
to
+ * verify any change on 6 different application servers.
+ */
+public final class ClassLoaderUtils {
+    
+    private ClassLoaderUtils() {
+    }
+    
+    /**
+     * Load a given resource. <p/> This method will try to load the resource
+     * using the following methods (in order):
+     * <ul>
+     * <li>From Thread.currentThread().getContextClassLoader()
+     * <li>From ClassLoaderUtil.class.getClassLoader()
+     * <li>callingClass.getClassLoader()
+     * </ul>
+     * 
+     * @param resourceName The name of the resource to load
+     * @param callingClass The Class object of the calling object
+     */
+    public static URL getResource(String resourceName, Class callingClass) {
+        URL url = 
Thread.currentThread().getContextClassLoader().getResource(resourceName);
+        if (url == null && resourceName.startsWith("/")) {
+            //certain classloaders need it without the leading /
+            url = Thread.currentThread().getContextClassLoader()
+                .getResource(resourceName.substring(1));
+        }
+
+        ClassLoader cluClassloader = ClassLoaderUtils.class.getClassLoader();
+        if (cluClassloader == null) {
+            cluClassloader = ClassLoader.getSystemClassLoader();
+        }
+        if (url == null) {
+            url = cluClassloader.getResource(resourceName);
+        }
+        if (url == null && resourceName.startsWith("/")) {
+            //certain classloaders need it without the leading /
+            url = cluClassloader.getResource(resourceName.substring(1));
+        }
+
+        if (url == null) {
+            ClassLoader cl = callingClass.getClassLoader();
+
+            if (cl != null) {
+                url = cl.getResource(resourceName);
+            }
+        }
+
+        if (url == null) {
+            url = callingClass.getResource(resourceName);
+        }
+        
+        if ((url == null) && (resourceName != null) && (resourceName.charAt(0) 
!= '/')) {
+            return getResource('/' + resourceName, callingClass);
+        }
+
+        return url;
+    }
+    
+    /**
+     * Load a given resources. <p/> This method will try to load the resources
+     * using the following methods (in order):
+     * <ul>
+     * <li>From Thread.currentThread().getContextClassLoader()
+     * <li>From ClassLoaderUtil.class.getClassLoader()
+     * <li>callingClass.getClassLoader()
+     * </ul>
+     * 
+     * @param resourceName The name of the resource to load
+     * @param callingClass The Class object of the calling object
+     */
+    public static List getResources(String resourceName, Class callingClass) {
+        List ret = new ArrayList();
+        Enumeration urls = new Enumeration() {
+            public boolean hasMoreElements() {
+                return false;
+            }
+            public Object nextElement() {
+                return null;
+            }
+            
+        };
+        try {
+            urls = Thread.currentThread().getContextClassLoader()
+                .getResources(resourceName);
+        } catch (IOException e) {
+            //ignore
+        }
+        if (!urls.hasMoreElements() && resourceName.startsWith("/")) {
+            //certain classloaders need it without the leading /
+            try {
+                urls = Thread.currentThread().getContextClassLoader()
+                    .getResources(resourceName.substring(1));
+            } catch (IOException e) {
+                // ignore
+            }
+        }
+
+        ClassLoader cluClassloader = ClassLoaderUtils.class.getClassLoader();
+        if (cluClassloader == null) {
+            cluClassloader = ClassLoader.getSystemClassLoader();
+        }
+        if (!urls.hasMoreElements()) {
+            try {
+                urls = cluClassloader.getResources(resourceName);
+            } catch (IOException e) {
+                // ignore
+            }
+        }
+        if (!urls.hasMoreElements() && resourceName.startsWith("/")) {
+            //certain classloaders need it without the leading /
+            try {
+                urls = cluClassloader.getResources(resourceName.substring(1));
+            } catch (IOException e) {
+                // ignore
+            }
+        }
+
+        if (!urls.hasMoreElements()) {
+            ClassLoader cl = callingClass.getClassLoader();
+
+            if (cl != null) {
+                try {
+                    urls = cl.getResources(resourceName);
+                } catch (IOException e) {
+                    // ignore
+                }
+            }
+        }
+
+        if (!urls.hasMoreElements()) {
+            URL url = callingClass.getResource(resourceName);
+            if (url != null) {
+                ret.add(url);
+            }
+        }
+        while (urls.hasMoreElements()) {
+            ret.add(urls.nextElement());
+        }
+
+        
+        if (ret.isEmpty() && (resourceName != null) && (resourceName.charAt(0) 
!= '/')) {
+            return getResources('/' + resourceName, callingClass);
+        }
+        return ret;
+    }
+
+
+    /**
+     * This is a convenience method to load a resource as a stream. <p/> The
+     * algorithm used to find the resource is given in getResource()
+     * 
+     * @param resourceName The name of the resource to load
+     * @param callingClass The Class object of the calling object
+     */
+    public static InputStream getResourceAsStream(String resourceName, Class 
callingClass) {
+        URL url = getResource(resourceName, callingClass);
+
+        try {
+            return (url != null) ? url.openStream() : null;
+        } catch (IOException e) {
+            return null;
+        }
+    }
+
+    /**
+     * Load a class with a given name. <p/> It will try to load the class in 
the
+     * following order:
+     * <ul>
+     * <li>From Thread.currentThread().getContextClassLoader()
+     * <li>Using the basic Class.forName()
+     * <li>From ClassLoaderUtil.class.getClassLoader()
+     * <li>From the callingClass.getClassLoader()
+     * </ul>
+     * 
+     * @param className The name of the class to load
+     * @param callingClass The Class object of the calling object
+     * @throws ClassNotFoundException If the class cannot be found anywhere.
+     */
+    public static Class loadClass(String className, Class callingClass)
+        throws ClassNotFoundException {
+        try {
+            ClassLoader cl = Thread.currentThread().getContextClassLoader();
+
+            if (cl != null) {
+                return cl.loadClass(className);
+            }            
+        } catch (ClassNotFoundException e) {
+            //ignore
+        }
+        return loadClass2(className, callingClass);
+    }
+
+    private static Class loadClass2(String className, Class callingClass)
+        throws ClassNotFoundException {
+        try {
+            return Class.forName(className);
+        } catch (ClassNotFoundException ex) {
+            try {
+                if (ClassLoaderUtils.class.getClassLoader() != null) {
+                    return 
ClassLoaderUtils.class.getClassLoader().loadClass(className);
+                }
+            } catch (ClassNotFoundException exc) {
+                if (callingClass != null && callingClass.getClassLoader() != 
null) {
+                    return callingClass.getClassLoader().loadClass(className);
+                }
+            }
+            throw ex;
+        }
+    }
+}


Reply via email to