001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.c3p0.codegen;
024:
025: import java.io.*;
026: import java.util.*;
027: import javax.xml.parsers.*;
028: import org.w3c.dom.*;
029: import com.mchange.v2.codegen.*;
030: import com.mchange.v2.codegen.bean.*;
031: import com.mchange.v2.c3p0.impl.*;
032:
033: import java.lang.reflect.Modifier;
034: import com.mchange.v1.xml.DomParseUtils;
035:
036: public class BeangenDataSourceGenerator {
037: public static void main(String[] argv) {
038: try {
039: if (argv.length != 2) {
040: System.err.println("java "
041: + BeangenDataSourceGenerator.class.getName()
042: + " <infile.xml> <OutputFile.java>");
043: return;
044: }
045:
046: File outFile = new File(argv[1]);
047: File parentDir = outFile.getParentFile();
048: if (!parentDir.exists()) {
049: System.err.println("Warning: making parent directory: "
050: + parentDir);
051: parentDir.mkdirs();
052: }
053:
054: DocumentBuilderFactory fact = DocumentBuilderFactory
055: .newInstance();
056: DocumentBuilder db = fact.newDocumentBuilder();
057: Document doc = db.parse(new File(argv[0]));
058: ParsedPropertyBeanDocument parsed = new ParsedPropertyBeanDocument(
059: doc);
060: Writer w = new BufferedWriter(new FileWriter(outFile));
061:
062: SimplePropertyBeanGenerator gen = new SimplePropertyBeanGenerator();
063: gen.setGeneratorName(BeangenDataSourceGenerator.class
064: .getName());
065:
066: // tightly coupled to the implementation of SimplePropertyBeanGenerator!
067: IndirectingSerializableExtension idse = new IndirectingSerializableExtension(
068: "com.mchange.v2.naming.ReferenceIndirector") {
069: protected void generateExtraSerInitializers(
070: ClassInfo info, Class super classType,
071: Property[] props, Class[] propTypes,
072: IndentedWriter iw) throws IOException {
073: if (BeangenUtils.hasBoundProperties(props))
074: iw
075: .println("this.pcs = new PropertyChangeSupport( this );");
076: if (BeangenUtils.hasConstrainedProperties(props))
077: iw
078: .println("this.vcs = new VetoableChangeSupport( this );");
079: }
080: };
081: gen.addExtension(idse);
082:
083: PropsToStringGeneratorExtension tsge = new PropsToStringGeneratorExtension();
084: tsge.setExcludePropertyNames(Arrays.asList(new String[] {
085: "userOverridesAsString", "overrideDefaultUser",
086: "overrideDefaultPassword" }));
087: gen.addExtension(tsge);
088:
089: PropertyReferenceableExtension prex = new PropertyReferenceableExtension();
090: prex.setUseExplicitReferenceProperties(true);
091: // we use the string version to creating dependencies between the bean generator and c3p0 classes
092: //prex.setFactoryClassName( C3P0JavaBeanObjectFactory.class.getName() );
093: prex
094: .setFactoryClassName("com.mchange.v2.c3p0.impl.C3P0JavaBeanObjectFactory");
095: gen.addExtension(prex);
096:
097: BooleanInitIdentityTokenConstructortorGeneratorExtension biitcge = new BooleanInitIdentityTokenConstructortorGeneratorExtension();
098: gen.addExtension(biitcge);
099:
100: if (parsed.getClassInfo().getClassName().equals(
101: "WrapperConnectionPoolDataSourceBase"))
102: gen.addExtension(new WcpdsExtrasGeneratorExtension());
103:
104: if (unmodifiableShadow(doc))
105: gen
106: .addExtension(new UnmodifiableShadowGeneratorExtension());
107:
108: gen.generate(parsed.getClassInfo(), parsed.getProperties(),
109: w);
110:
111: w.flush();
112: w.close();
113:
114: System.err.println("Processed: " + argv[0]); //+ " -> " + argv[1]);
115: } catch (Exception e) {
116: e.printStackTrace();
117: }
118: }
119:
120: private static boolean unmodifiableShadow(Document doc) {
121: Element docElem = doc.getDocumentElement();
122: return DomParseUtils
123: .uniqueChild(docElem, "unmodifiable-shadow") != null;
124: }
125:
126: static class BooleanInitIdentityTokenConstructortorGeneratorExtension
127: implements GeneratorExtension {
128: public Collection extraGeneralImports() {
129: return Collections.EMPTY_SET;
130: }
131:
132: public Collection extraSpecificImports() {
133: Set out = new HashSet();
134: out.add("com.mchange.v2.c3p0.C3P0Registry");
135: return out;
136: }
137:
138: public Collection extraInterfaceNames() {
139: return Collections.EMPTY_SET;
140: }
141:
142: public void generate(ClassInfo info, Class super classType,
143: Property[] props, Class[] propTypes, IndentedWriter iw)
144: throws IOException {
145: BeangenUtils.writeExplicitDefaultConstructor(
146: Modifier.PRIVATE, info, iw);
147: iw.println();
148: iw.println("public " + info.getClassName()
149: + "( boolean autoregister )");
150: iw.println("{");
151: iw.upIndent();
152: iw.println("if (autoregister)");
153: iw.println("{");
154: iw.upIndent();
155: iw
156: .println("this.identityToken = C3P0ImplUtils.allocateIdentityToken( this );");
157: iw.println("C3P0Registry.reregister( this );");
158: iw.downIndent();
159: iw.println("}");
160:
161: iw.downIndent();
162: iw.println("}");
163: }
164: }
165:
166: static class WcpdsExtrasGeneratorExtension implements
167: GeneratorExtension {
168: public Collection extraGeneralImports() {
169: return Collections.EMPTY_SET;
170: }
171:
172: public Collection extraSpecificImports() {
173: Set out = new HashSet();
174: out.add("com.mchange.v2.c3p0.ConnectionCustomizer");
175: out.add("javax.sql.PooledConnection");
176: out.add("java.sql.SQLException");
177: return out;
178: }
179:
180: public Collection extraInterfaceNames() {
181: return Collections.EMPTY_SET;
182: }
183:
184: public void generate(ClassInfo info, Class super classType,
185: Property[] props, Class[] propTypes, IndentedWriter iw)
186: throws IOException {
187: iw
188: .println("protected abstract PooledConnection getPooledConnection( ConnectionCustomizer cc, String idt)"
189: + " throws SQLException;");
190: iw
191: .println("protected abstract PooledConnection getPooledConnection(String user, String password, ConnectionCustomizer cc, String idt)"
192: + " throws SQLException;");
193: }
194: }
195:
196: static class UnmodifiableShadowGeneratorExtension implements
197: GeneratorExtension {
198: BeanExtractingGeneratorExtension bege;
199: CompleteConstructorGeneratorExtension ccge;
200:
201: {
202: bege = new BeanExtractingGeneratorExtension();
203: bege.setExtractMethodModifiers(Modifier.PRIVATE);
204: bege.setConstructorModifiers(Modifier.PUBLIC);
205:
206: ccge = new CompleteConstructorGeneratorExtension();
207: }
208:
209: public Collection extraGeneralImports() {
210: Set out = new HashSet();
211: out.addAll(bege.extraGeneralImports());
212: out.addAll(ccge.extraGeneralImports());
213: return out;
214: }
215:
216: public Collection extraSpecificImports() {
217: Set out = new HashSet();
218: out.addAll(bege.extraSpecificImports());
219: out.addAll(ccge.extraSpecificImports());
220: return out;
221: }
222:
223: public Collection extraInterfaceNames() {
224: return Collections.EMPTY_SET;
225: }
226:
227: public void generate(ClassInfo info, Class super classType,
228: Property[] props, Class[] propTypes, IndentedWriter iw)
229: throws IOException {
230: ClassInfo innerInfo = new SimpleClassInfo(info
231: .getPackageName(), Modifier.PUBLIC
232: | Modifier.STATIC, "UnmodifiableShadow", info
233: .getSuperclassName(), info.getInterfaceNames(),
234: info.getGeneralImports(), info.getSpecificImports());
235:
236: SimplePropertyBeanGenerator innerGen = new SimplePropertyBeanGenerator();
237: innerGen.setInner(true);
238: innerGen.setForceUnmodifiable(true);
239: innerGen.addExtension(bege);
240: innerGen.addExtension(ccge);
241: innerGen.generate(innerInfo, props, iw);
242: }
243: }
244: }
|