You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by sc...@apache.org on 2006/11/09 20:40:17 UTC

svn commit: r473044 - in /webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws: i18n/resource.properties marshaller/ClassUtils.java message/databinding/JAXBUtils.java

Author: scheu
Date: Thu Nov  9 11:40:16 2006
New Revision: 473044

URL: http://svn.apache.org/viewvc?view=rev&rev=473044
Log:
AXIS2-1659
Contributor: Nikhil Thaker & Rich Scheuerle
Improvements to JAXBContext intialization

Modified:
    webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/i18n/resource.properties
    webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/marshaller/ClassUtils.java
    webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/JAXBUtils.java

Modified: webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/i18n/resource.properties
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/i18n/resource.properties?view=diff&rev=473044&r1=473043&r2=473044
==============================================================================
--- webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/i18n/resource.properties (original)
+++ webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/i18n/resource.properties Thu Nov  9 11:40:16 2006
@@ -131,3 +131,6 @@
 ClassUtilsErr1=Unable to get class loader.
 ClassUtilsErr2={0} + " does not appear to be a valid package (Unsupported encoding)"
 ClassUtilsErr3="IOException was thrown when trying to get all resources for {0}"
+ClassUtilsErr1=Unable to get class loader.
+ClassUtilsErr2={0} + " does not appear to be a valid package (Unsupported encoding)"
+ClassUtilsErr3="IOException was thrown when trying to get all resources for {0}"

Modified: webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/marshaller/ClassUtils.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/marshaller/ClassUtils.java?view=diff&rev=473044&r1=473043&r2=473044
==============================================================================
--- webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/marshaller/ClassUtils.java (original)
+++ webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/marshaller/ClassUtils.java Thu Nov  9 11:40:16 2006
@@ -16,15 +16,26 @@
  */
 package org.apache.axis2.jaxws.marshaller;
 
+import java.io.File;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.net.URLDecoder;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+import java.util.ArrayList;
+import java.util.Enumeration;
 import java.util.HashMap;
+import java.util.List;
 
 import javax.management.openmbean.SimpleType;
 import javax.xml.bind.annotation.XmlRootElement;
 
 
+import org.apache.axis2.jaxws.i18n.Messages;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -239,4 +250,78 @@
         return null;
     }
     
+    /**
+	 * This method will return all the Class names excluding the interfaces from a given package. 
+	 * @param pckgname
+	 * @return
+	 * @throws ClassNotFoundException
+	 */
+	public static List<Class> getAllClassesFromPackage(String pckgname) throws ClassNotFoundException {
+	        // This will hold a list of directories matching the pckgname. There may be more than one if a package is split over multiple jars/paths
+	        ArrayList<File> directories = new ArrayList<File>();
+	        try {
+	            ClassLoader cld = Thread.currentThread().getContextClassLoader();
+	            if (cld == null) {
+	            	if(log.isDebugEnabled()){
+	            		log.debug("Unable to get class loader");
+	            	}
+	                throw new ClassNotFoundException(Messages.getMessage("ClassUtilsErr1"));
+	            }
+	            String path = pckgname.replace('.', '/');
+	            // Ask for all resources for the path
+	            Enumeration<URL> resources = cld.getResources(path);
+	            while (resources.hasMoreElements()) {
+	                directories.add(new File(URLDecoder.decode(resources.nextElement().getPath(), "UTF-8")));
+	            }
+	        } catch (UnsupportedEncodingException e) {
+	        	if(log.isDebugEnabled()){
+            		log.debug(pckgname + " does not appear to be a valid package (Unsupported encoding)");
+            	}
+	            throw new ClassNotFoundException(Messages.getMessage("ClassUtilsErr2", pckgname));
+	        } catch (IOException e) {
+	        	if(log.isDebugEnabled()){
+            		log.debug("IOException was thrown when trying to get all resources for "+ pckgname);
+            	}
+	            throw new ClassNotFoundException(Messages.getMessage("ClassUtilsErr3", pckgname));
+	        }
+	 
+	        ArrayList<Class> classes = new ArrayList<Class>();
+	        // For every directory identified capture all the .class files
+	        for (File directory : directories) {
+	            if (directory.exists()) {
+	                // Get the list of the files contained in the package
+	                String[] files = directory.list();
+	                for (String file : files) {
+	                    // we are only interested in .class files
+	                    if (file.endsWith(".class")) {
+	                        // removes the .class extension
+	                    	// TODO Java2 Sec
+	                    	try {
+	                    		Class clazz = Class.forName(pckgname + '.' + file.substring(0, file.length() - 6));
+	                    		// dont add any interfaces only classes
+	                    		if(!clazz.isInterface() && getDefaultPublicConstructor(clazz) != null){
+	                    			classes.add(clazz);
+	                    		}
+	                    	} catch (Exception e) {}
+	                       
+	                    }
+	                }
+	            }
+	        }
+	        return classes;
+	    }
+	
+	private static final Class[] noClass=new Class[] {};
+	/**
+	 * Get the default public constructor
+	 * @param clazz
+	 * @return Constructor or null
+	 */
+	public static Constructor getDefaultPublicConstructor(Class clazz) {
+		try {
+			return clazz.getConstructor(noClass);
+		} catch (Exception e) {
+			return null;
+		}
+	}
 }

Modified: webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/JAXBUtils.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/JAXBUtils.java?view=diff&rev=473044&r1=473043&r2=473044
==============================================================================
--- webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/JAXBUtils.java (original)
+++ webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/JAXBUtils.java Thu Nov  9 11:40:16 2006
@@ -17,6 +17,7 @@
 package org.apache.axis2.jaxws.message.databinding;
 
 import java.util.Collections;
+import java.util.List;
 import java.util.Map;
 import java.util.WeakHashMap;
 
@@ -26,6 +27,7 @@
 import javax.xml.bind.Marshaller;
 import javax.xml.bind.Unmarshaller;
 
+import org.apache.axis2.jaxws.marshaller.ClassUtils;
 import org.apache.axis2.jaxws.message.databinding.impl.JAXBBlockImpl;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -91,11 +93,30 @@
 		JAXBContext context = map.get(cls.getName());
 		if (context == null) {
             synchronized(map) {
+                try{
+                	Package pkg = cls.getPackage();
+                	if (log.isDebugEnabled()) {
+                        log.debug("Package for " + cls.getName() + " "+pkg.getName());
+                    }
+                	if (log.isDebugEnabled()) {
+                        log.debug("Attempting to read all classes from package " + pkg.getName());
+                    }
+                	List<Class> classList = ClassUtils.getAllClassesFromPackage(pkg.getName());
+                	Class[] classes = classList.toArray(new Class[0]);
+                	if (log.isDebugEnabled()) {
+                        log.debug("All classes from package " + pkg.getName() + "[read].");
+                    }
+                	if (log.isDebugEnabled()) {
+                        log.debug("Attempting to create JAXBContext for " + cls.getName());
+                    }
+                	context = JAXBContext.newInstance(classes);
+                    map.put(cls.getName(), context);	
+                }catch(ClassNotFoundException e){
+                	throw new JAXBException(e);
+                }
                 if (log.isDebugEnabled()) {
                     log.debug("JAXBContext [created] for" + cls.getName());
                 }
-                context = JAXBContext.newInstance(cls);
-                map.put(cls.getName(), context);
             }
 		} else {
             if (log.isDebugEnabled()) {



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