001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program 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 General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024: package org.myoodb.tools.generator;
025:
026: import java.io.*;
027: import java.util.*;
028: import java.lang.reflect.Method;
029: import java.lang.reflect.Modifier;
030:
031: import org.myoodb.*;
032: import org.myoodb.core.*;
033:
034: public class ProxyGenerator extends AbstractCodeGeneratorImpl implements
035: AbstractCodeGenerator {
036: public final static String IMPL_NAME_SUFFIX = "@implExtension@";
037: public final static String PROXY_NAME_SUFFIX = "@proxyExtension@";
038:
039: private String m_file;
040: private PrintWriter m_out;
041: private Class m_myoodbImplClass;
042: private Method[] m_sortedMethods;
043: private ArrayList m_remoteInterfaces;
044:
045: public ProxyGenerator() {
046: m_out = null;
047: m_file = null;
048: m_sortedMethods = null;
049: m_myoodbImplClass = null;
050: m_remoteInterfaces = null;
051: }
052:
053: private int getMethodIndex(String methodName,
054: AbstractCodeGenerator.Parameter parameters[])
055: throws GeneratorException {
056: try {
057: Method method = m_myoodbImplClass.getMethod(methodName,
058: parametersToClasses(parameters));
059: return MethodHelper.getMethodIndex(m_sortedMethods, method);
060: } catch (ClassNotFoundException e) {
061: throw new GeneratorException(e);
062: } catch (NoSuchMethodException e) {
063: throw new GeneratorException(e);
064: }
065: }
066:
067: private void makeConstructor(String proxyClassName) {
068: proxyClassName += PROXY_NAME_SUFFIX;
069:
070: m_out.print(" public "
071: + Helper.simpleClassName(proxyClassName) + "()\n");
072: m_out.print(" {\n");
073: m_out.print(" }\n");
074: m_out
075: .print(" public "
076: + Helper.simpleClassName(proxyClassName)
077: + "(org.myoodb.core.Identifier id, org.myoodb.core.AbstractDatabase db)\n");
078: m_out.print(" {\n");
079: m_out.print(" super(id, db);\n");
080: m_out.print(" }\n");
081: }
082:
083: private void makePackage(String implementationClass) {
084: m_out
085: .print("// Proxy class generated by MyOODB-@version@ (DO NOT EDIT!)\n");
086: m_out.print("\n");
087: if (Helper.packageName(implementationClass).equals("") == false) {
088: m_out.print("package "
089: + Helper.packageName(implementationClass) + ";\n");
090: m_out.print("\n");
091: }
092: }
093:
094: private void makeInhertance(String proxyClassName) {
095: proxyClassName += PROXY_NAME_SUFFIX;
096:
097: /*
098: String parentProxyClassName = m_myoodbImplClass.getSuperclass().getName() + PROXY_NAME_SUFFIX;
099: if (parentProxyClassName.equals("org.myoodb.MyOodbObject" + PROXY_NAME_SUFFIX) == false)
100: {
101: m_out.print("public class " + Helper.simpleClassName(proxyClassName) + " extends " + parentProxyClassName);
102: }
103: else
104: {
105: */
106: m_out.print("public class "
107: + Helper.simpleClassName(proxyClassName)
108: + " extends org.myoodb.MyOodbProxy");
109: /*
110: }
111: */
112:
113: boolean first = true;
114: Iterator iter = m_remoteInterfaces.iterator();
115: while (iter.hasNext()) {
116: Class classType = (Class) iter.next();
117: if (first) {
118: first = false;
119: m_out.print(" implements ");
120: } else {
121: m_out.print(", ");
122: }
123: m_out.print(classType.getName());
124: }
125: m_out.print("\n");
126: m_out.print("{\n");
127: }
128:
129: public void beginClass(String sourcePath, String className)
130: throws GeneratorException {
131: m_file = Helper.classToFileName(className) + PROXY_NAME_SUFFIX
132: + ".java";
133: System.out
134: .println(" BEGIN - Generating proxy for: " + m_file);
135:
136: try {
137: String fileName = sourcePath + "/" + m_file;
138: m_out = new PrintWriter(fileName);
139: } catch (IOException e) {
140: throw new GeneratorException(e);
141: }
142:
143: try {
144: Class interfaceType = Class.forName(className);
145: m_myoodbImplClass = Class.forName(className
146: + IMPL_NAME_SUFFIX);
147: m_sortedMethods = MethodHelper.getMethods(
148: m_myoodbImplClass, interfaceType);
149: m_remoteInterfaces = Helper.findInterfaces(
150: m_myoodbImplClass, MyOodbRemote.class);
151: } catch (ClassNotFoundException e) {
152: throw new GeneratorException(e);
153: }
154:
155: makePackage(className);
156: makeInhertance(className);
157: makeConstructor(className);
158: }
159:
160: public void makeMethod(String name, int accessLevel)
161: throws GeneratorException {
162: for (int methodIndex = 0; methodIndex < m_sortedMethods.length; methodIndex++) {
163: Method method = m_sortedMethods[methodIndex];
164:
165: if ((Modifier.isFinal(method.getModifiers()) == true)
166: || (Modifier.isStatic(method.getModifiers()) == true)
167: || (method.getName().equals(name) == false)) {
168: continue;
169: }
170:
171: AbstractCodeGenerator.Parameter parameters[] = getParameters(method
172: .getParameterTypes());
173: Class returnType = method.getReturnType();
174: Class exceptions[] = method.getExceptionTypes();
175:
176: StringBuilder signaturBuf = new StringBuilder();
177: signaturBuf.append(" public " + getTypecode(returnType)
178: + " " + name + "(");
179:
180: for (int i = 0; i < parameters.length; i++) {
181: signaturBuf.append(i > 0 ? ", " : "");
182: signaturBuf.append(parameters[i].getType() + " "
183: + parameters[i].getName());
184: }
185: signaturBuf.append(")");
186: String signaturStr = signaturBuf.toString();
187:
188: m_out.print(signaturStr);
189:
190: for (int i = 0; i < exceptions.length; i++) {
191: m_out.print(i == 0 ? " throws " : ", ");
192: m_out.print(exceptions[i].getName());
193: }
194: m_out.print("\n");
195: m_out.print(" {\n");
196:
197: String params = null;
198: if (accessLevel == org.myoodb.MyOodbAccess.WRITE) {
199: params = "m_writeMode, m_timeout);\n";
200: } else if (accessLevel == org.myoodb.MyOodbAccess.TUNNEL) {
201: params = "org.myoodb.MyOodbAccess.TUNNEL, m_timeout);\n";
202: } else if (accessLevel == org.myoodb.MyOodbAccess.REALTIME) {
203: params = "org.myoodb.MyOodbAccess.REALTIME, m_timeout);\n";
204: } else if (accessLevel == org.myoodb.MyOodbAccess.ABANDON) {
205: params = "org.myoodb.MyOodbAccess.ABANDON, m_timeout);\n";
206: } else {
207: params = "org.myoodb.MyOodbAccess.READ, m_timeout);\n";
208: }
209:
210: m_out.print(" try\n");
211: m_out.print(" {\n");
212: if (parameters.length != 0) {
213: m_out.print(" Object[] args = {");
214: for (int i = 0; i < parameters.length; i++) {
215: m_out.print(i > 0 ? ", " : "");
216: if (Helper.isPrimitive(parameters[i].getType())) {
217: m_out.print(Helper
218: .wrappercodeForPrimitive(parameters[i]
219: .getType())
220: + ".valueOf("
221: + parameters[i].getName()
222: + ")");
223: } else {
224: m_out.print("arg" + i);
225: }
226: }
227: m_out.print("};\n");
228:
229: if (returnType.getName().equals("void") == false) {
230: m_out.print(" Object result = ");
231: } else {
232: m_out.print(" ");
233: }
234:
235: m_out.print("m_database.invokeMethod(this, "
236: + getMethodIndex(name, parameters) + ", args, "
237: + params);
238: } else {
239: if (returnType.getName().equals("void") == false) {
240: m_out.print(" Object result = ");
241: } else {
242: m_out.print(" ");
243: }
244:
245: m_out.print("m_database.invokeMethod(this, "
246: + getMethodIndex(name, parameters) + ", null, "
247: + params);
248: }
249:
250: if (returnType.getName().equals("void") == false) {
251: if (Helper.isPrimitive(returnType.getName())) {
252: m_out.print(" return "
253: + Helper.returncodeForPrimitive(returnType
254: .getName(), "result") + ";\n");
255: } else {
256: m_out.print(" return ("
257: + getTypecode(returnType) + ") result;\n");
258: }
259: }
260:
261: m_out.print(" }\n");
262: m_out
263: .print(" catch (org.myoodb.exception.ObjectException oe)\n");
264: m_out.print(" {\n");
265: if (exceptions.length != 0) {
266: m_out
267: .print(" Throwable ee = oe.getCause();\n");
268: for (int i = 0; i < exceptions.length; i++) {
269: m_out.print(" if (ee instanceof "
270: + exceptions[i].getName() + ")\n");
271: m_out.print(" {\n");
272: m_out.print(" throw ("
273: + exceptions[i].getName() + ") ee;\n");
274: m_out.print(" }\n");
275: }
276: m_out
277: .print(" throw (RuntimeException) ee;\n");
278: } else {
279: m_out
280: .print(" throw (RuntimeException) oe.getCause();\n");
281: }
282:
283: m_out.print(" }\n");
284: m_out.print(" }\n");
285: }
286: }
287:
288: public void endClass() throws GeneratorException {
289: m_out.print("}\n");
290: m_out.close();
291:
292: System.out.println(" END - Generating proxy for: " + m_file);
293: }
294: }
|