View Javadoc
1 package xdoclet.sdk.xgg.betwixt; 2 3 import xdoclet.sdk.xgg.XGGPojo; 4 import xdoclet.util.ClasspathManager; 5 import xdoclet.util.betwixt.BetwixtConfigurer; 6 import xdoclet.plugins.BetwixtPlugin; 7 8 import java.util.List; 9 import java.util.Iterator; 10 import java.beans.IntrospectionException; 11 import java.io.IOException; 12 import java.io.File; 13 14 import org.apache.commons.betwixt.io.BeanReader; 15 import org.apache.commons.betwixt.XMLIntrospector; 16 import org.apache.commons.logging.LogFactory; 17 import org.apache.tools.ant.Task; 18 import org.apache.tools.ant.BuildException; 19 20 /*** 21 * This Ant task configures and serialises a {@link BeanReader} 22 * object to disk. It should be used as a post processor after compiling Java Beans 23 * creates by {@link xdoclet.sdk.xgg.XGGPlugin}. The idea is to have a preconfigured 24 * BeanReader for a set of Java Beans. Since constructing a BeanReader 25 * can be a time consuming process for complex sets of Java Beans, we're doing this 26 * when the plugin is packaged. The {@link xdoclet.plugins.BetwixtPlugin} will then 27 * try to deserialise the serialised BeanReader when it starts up, which 28 * is a lot faster. 29 * 30 * @bean.class name="betwixt-registryserializer" 31 * displayName="Betwixt BeanReaderSerializer" 32 * shortDescription="XDoclet SDK Ant task that serializes XMLBeanInfoRegistry" 33 * 34 * @bean.icon color16="betwixt-registryserializer.gif" 35 * 36 * @author <a href="mailto:aslak.hellesoy at bekk.no">Aslak Hellesøy</a> 37 * @version $Revision: 1.2 $ 38 */ 39 public class BeanReaderSerializer extends Task { 40 41 private File _root; 42 private String _package; 43 // private static final ElementDescriptorComparator _elementDescriptorComparator = new ElementDescriptorComparator(); 44 45 public void setRoot( File root ) { 46 _root = root; 47 } 48 49 /*** 50 * Set the package where the serialised file should be written. 51 * @param pakkage 52 */ 53 public void setPackage( String pakkage ) { 54 _package = pakkage; 55 } 56 57 public void execute() { 58 if( _root == null ) { 59 throw new BuildException("root must be set."); 60 } 61 if( !_root.exists() || !_root.isDirectory() ) { 62 throw new BuildException("root must be an existing directory containing Java Bean classes and a Meta-Inf/MANIFEST.MF declaring them."); 63 } 64 65 // pass by ref. 66 String[] pa = new String[] { null }; 67 BeanReader beanReader = createBeanReader( _root, pa ); 68 69 String pakkage = _package != null ? _package : pa[0]; 70 String path = pakkage.replace( '.', '/' ); 71 if( pakkage == null ) { 72 throw new BuildException( "There were no Java Beans in " + _root.getAbsolutePath() + "/" + path ); 73 } 74 75 try { 76 File registrySer = new File( _root, path + '/' + BetwixtPlugin.SERIALIZED_BEANREADER ); 77 java.io.FileOutputStream fos = new java.io.FileOutputStream( registrySer ); 78 java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(fos); 79 oos.writeObject(beanReader); 80 } catch( IOException e ) { 81 e.printStackTrace(); 82 throw new BuildException( "Couldn't serialise XMLBeanInfoRegistry" ); 83 } 84 } 85 86 public static BeanReader createBeanReader( File root, String[] pakkage ) { 87 XMLIntrospector introspector = new XMLIntrospector(); 88 BetwixtConfigurer.configureIntrospector( introspector, null ); 89 90 BeanReader beanReader = new BeanReader(); 91 beanReader.setXMLIntrospector( introspector ); 92 93 ClasspathManager classpathManager = new ClasspathManager( root.getAbsolutePath() ); 94 List beanClasses = classpathManager.findJavaBeans(); 95 for( Iterator i = beanClasses.iterator(); i.hasNext(); ) { 96 Class beanClass = ( Class ) i.next(); 97 // Let Betwixt introspect beans generated by the XGG SDK plugin. 98 if( XGGPojo.class.isAssignableFrom( beanClass ) ) { 99 try { 100 if( pakkage != null ) { 101 // package wasn't explicitly set. discover it from the beans. 102 String beanPackage = ClasspathManager.getPackage( beanClass ); 103 if( pakkage[0] != null && !pakkage[0].equals(beanPackage) ) { 104 throw new BuildException( "All beans should be in the same package. Found beans in " + pakkage[0] + " and " + beanPackage ); 105 } 106 pakkage[0] = beanPackage; 107 } 108 beanReader.registerBeanClass( beanClass ); 109 110 // Now, the elements are not properly ordered. Order them! 111 // XMLBeanInfo xmlInfo = introspector.introspect( beanClass ); 112 // ElementDescriptor elementDescriptor = xmlInfo.getElementDescriptor(); 113 // ElementDescriptor[] children = elementDescriptor.getElementDescriptors(); 114 // Arrays.sort( children, _elementDescriptorComparator); 115 116 } catch( IntrospectionException e ) { 117 LogFactory.getLog(BeanReaderSerializer.class).error("Couldn't introspect Java Bean class " + beanClass.getName(),e); 118 throw new IllegalStateException( "Couldn't introspect Java Bean class " + beanClass.getName() ); 119 } catch( IllegalStateException e ) { 120 LogFactory.getLog(BeanReaderSerializer.class).error("Couldn't introspect Java Bean class " + beanClass.getName(),e); 121 throw new IllegalStateException( "Couldn't introspect Java Bean class " + beanClass.getName() ); 122 } 123 } 124 } 125 return beanReader; 126 } 127 128 /* 129 private class ElementDescriptorComparator implements Comparator { 130 public int compare( Object o1, Object o2 ) { 131 ElementDescriptor e1 = (ElementDescriptor) o1; 132 ElementDescriptor e2 = (ElementDescriptor) o1; 133 e2. 134 } 135 136 private int getOrder( ) { 137 BeanInfo beanInfo1 = null; 138 String orderValue = (String) beanInfo1.getBeanDescriptor().getValue( "order" ); 139 int orderId = Integer.parseInt( orderValue ); 140 return orderId 141 } 142 } 143 */ 144 }

This page was automatically generated by Maven