1 22 23 24 package com.mchange.v2.c3p0.codegen; 25 26 import java.io.*; 27 import java.util.*; 28 import javax.xml.parsers.*; 29 import org.w3c.dom.*; 30 import com.mchange.v2.codegen.*; 31 import com.mchange.v2.codegen.bean.*; 32 import com.mchange.v2.c3p0.impl.*; 33 34 import java.lang.reflect.Modifier ; 35 import com.mchange.v1.xml.DomParseUtils; 36 37 public class BeangenDataSourceGenerator 38 { 39 public static void main( String [] argv ) 40 { 41 try 42 { 43 if (argv.length != 2) 44 { 45 System.err.println("java " + BeangenDataSourceGenerator.class.getName() + 46 " <infile.xml> <OutputFile.java>"); 47 return; 48 } 49 50 51 File outFile = new File( argv[1] ); 52 File parentDir = outFile.getParentFile(); 53 if (! parentDir.exists()) 54 { 55 System.err.println("Warning: making parent directory: " + parentDir); 56 parentDir.mkdirs(); 57 } 58 59 DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance(); 60 DocumentBuilder db = fact.newDocumentBuilder(); 61 Document doc = db.parse( new File( argv[0] ) ); 62 ParsedPropertyBeanDocument parsed = new ParsedPropertyBeanDocument( doc ); 63 Writer w = new BufferedWriter( new FileWriter( outFile ) ); 64 65 SimplePropertyBeanGenerator gen = new SimplePropertyBeanGenerator(); 66 gen.setGeneratorName( BeangenDataSourceGenerator.class.getName() ); 67 68 IndirectingSerializableExtension idse = new IndirectingSerializableExtension("com.mchange.v2.naming.ReferenceIndirector") 70 { 71 protected void generateExtraSerInitializers(ClassInfo info, Class superclassType, Property[] props, Class [] propTypes, IndentedWriter iw) 72 throws IOException 73 { 74 if (BeangenUtils.hasBoundProperties( props )) 75 iw.println("this.pcs = new PropertyChangeSupport( this );"); 76 if (BeangenUtils.hasConstrainedProperties( props )) 77 iw.println("this.vcs = new VetoableChangeSupport( this );"); 78 } 79 }; 80 gen.addExtension( idse ); 81 82 PropsToStringGeneratorExtension tsge = new PropsToStringGeneratorExtension(); 83 tsge.setExcludePropertyNames( Arrays.asList( new String [] {"userOverridesAsString","overrideDefaultUser","overrideDefaultPassword"} ) ); 84 gen.addExtension( tsge ); 85 86 PropertyReferenceableExtension prex = new PropertyReferenceableExtension(); 87 prex.setUseExplicitReferenceProperties( true ); 88 prex.setFactoryClassName( "com.mchange.v2.c3p0.impl.C3P0JavaBeanObjectFactory" ); 91 gen.addExtension( prex ); 92 93 BooleanInitIdentityTokenConstructortorGeneratorExtension biitcge = new BooleanInitIdentityTokenConstructortorGeneratorExtension(); 94 gen.addExtension( biitcge ); 95 96 if ( parsed.getClassInfo().getClassName().equals("WrapperConnectionPoolDataSourceBase") ) 97 gen.addExtension( new WcpdsExtrasGeneratorExtension() ); 98 99 if (unmodifiableShadow( doc ) ) 100 gen.addExtension( new UnmodifiableShadowGeneratorExtension() ); 101 102 103 gen.generate( parsed.getClassInfo(), parsed.getProperties(), w ); 104 105 w.flush(); 106 w.close(); 107 108 System.err.println("Processed: " + argv[0] ); } 110 catch ( Exception e ) 111 { e.printStackTrace(); } 112 } 113 114 private static boolean unmodifiableShadow( Document doc ) 115 { 116 Element docElem = doc.getDocumentElement(); 117 return DomParseUtils.uniqueChild(docElem, "unmodifiable-shadow") != null; 118 } 119 120 static class BooleanInitIdentityTokenConstructortorGeneratorExtension implements GeneratorExtension 121 { 122 public Collection extraGeneralImports() {return Collections.EMPTY_SET;} 123 124 public Collection extraSpecificImports() 125 { 126 Set out = new HashSet(); 127 out.add( "com.mchange.v2.c3p0.C3P0Registry" ); 128 return out; 129 } 130 131 public Collection extraInterfaceNames() {return Collections.EMPTY_SET;} 132 133 public void generate(ClassInfo info, Class superclassType, Property[] props, Class [] propTypes, IndentedWriter iw) 134 throws IOException 135 { 136 BeangenUtils.writeExplicitDefaultConstructor( Modifier.PRIVATE, info, iw); 137 iw.println(); 138 iw.println("public " + info.getClassName() + "( boolean autoregister )"); 139 iw.println("{"); 140 iw.upIndent(); 141 iw.println( "if (autoregister)"); 142 iw.println("{"); 143 iw.upIndent(); 144 iw.println("this.identityToken = C3P0ImplUtils.allocateIdentityToken( this );"); 145 iw.println("C3P0Registry.reregister( this );"); 146 iw.downIndent(); 147 iw.println("}"); 148 149 iw.downIndent(); 150 iw.println("}"); 151 } 152 } 153 154 static class WcpdsExtrasGeneratorExtension implements GeneratorExtension 155 { 156 public Collection extraGeneralImports() {return Collections.EMPTY_SET;} 157 158 public Collection extraSpecificImports() 159 { 160 Set out = new HashSet(); 161 out.add( "com.mchange.v2.c3p0.ConnectionCustomizer" ); 162 out.add( "javax.sql.PooledConnection" ); 163 out.add( "java.sql.SQLException" ); 164 return out; 165 } 166 167 public Collection extraInterfaceNames() {return Collections.EMPTY_SET;} 168 169 public void generate(ClassInfo info, Class superclassType, Property[] props, Class [] propTypes, IndentedWriter iw) 170 throws IOException 171 { 172 iw.println("protected abstract PooledConnection getPooledConnection( ConnectionCustomizer cc, String idt)" + 173 " throws SQLException;"); 174 iw.println("protected abstract PooledConnection getPooledConnection(String user, String password, ConnectionCustomizer cc, String idt)" + 175 " throws SQLException;"); 176 } 177 } 178 179 180 static class UnmodifiableShadowGeneratorExtension implements GeneratorExtension 181 { 182 BeanExtractingGeneratorExtension bege; 183 CompleteConstructorGeneratorExtension ccge; 184 185 { 186 bege = new BeanExtractingGeneratorExtension(); 187 bege.setExtractMethodModifiers( Modifier.PRIVATE ); 188 bege.setConstructorModifiers( Modifier.PUBLIC ); 189 190 ccge = new CompleteConstructorGeneratorExtension(); 191 } 192 193 public Collection extraGeneralImports() 194 { 195 Set out = new HashSet(); 196 out.addAll( bege.extraGeneralImports() ); 197 out.addAll( ccge.extraGeneralImports() ); 198 return out; 199 } 200 201 public Collection extraSpecificImports() 202 { 203 Set out = new HashSet(); 204 out.addAll( bege.extraSpecificImports() ); 205 out.addAll( ccge.extraSpecificImports() ); 206 return out; 207 } 208 209 public Collection extraInterfaceNames() {return Collections.EMPTY_SET;} 210 211 public void generate(ClassInfo info, Class superclassType, Property[] props, Class [] propTypes, IndentedWriter iw) 212 throws IOException 213 { 214 ClassInfo innerInfo = new SimpleClassInfo( info.getPackageName(), 215 Modifier.PUBLIC | Modifier.STATIC, 216 "UnmodifiableShadow", 217 info.getSuperclassName(), 218 info.getInterfaceNames(), 219 info.getGeneralImports(), 220 info.getSpecificImports() ); 221 222 SimplePropertyBeanGenerator innerGen = new SimplePropertyBeanGenerator(); 223 innerGen.setInner( true ); 224 innerGen.setForceUnmodifiable( true ); 225 innerGen.addExtension( bege ); 226 innerGen.addExtension( ccge ); 227 innerGen.generate( innerInfo, props, iw ); 228 } 229 } 230 } 231 | Popular Tags |