KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > c3p0 > codegen > BeangenDataSourceGenerator


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

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 JavaDoc;
35 import com.mchange.v1.xml.DomParseUtils;
36
37 public class BeangenDataSourceGenerator
38 {
39     public static void main( String JavaDoc[] 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         // tightly coupled to the implementation of SimplePropertyBeanGenerator!
69
IndirectingSerializableExtension idse = new IndirectingSerializableExtension("com.mchange.v2.naming.ReferenceIndirector")
70             {
71             protected void generateExtraSerInitializers(ClassInfo info, Class JavaDoc superclassType, Property[] props, Class JavaDoc[] 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 JavaDoc[] {"userOverridesAsString","overrideDefaultUser","overrideDefaultPassword"} ) );
84         gen.addExtension( tsge );
85
86         PropertyReferenceableExtension prex = new PropertyReferenceableExtension();
87         prex.setUseExplicitReferenceProperties( true );
88         // we use the string version to creating dependencies between the bean generator and c3p0 classes
89
//prex.setFactoryClassName( C3P0JavaBeanObjectFactory.class.getName() );
90
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] ); //+ " -> " + argv[1]);
109
}
110     catch ( Exception JavaDoc 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 JavaDoc superclassType, Property[] props, Class JavaDoc[] 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 JavaDoc superclassType, Property[] props, Class JavaDoc[] 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 JavaDoc superclassType, Property[] props, Class JavaDoc[] 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