001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.ejb.gen;
031:
032: import com.caucho.config.*;
033: import com.caucho.config.types.InjectionTarget;
034: import com.caucho.ejb.cfg.*;
035: import com.caucho.java.JavaWriter;
036: import com.caucho.util.L10N;
037:
038: import javax.ejb.*;
039: import java.io.IOException;
040: import java.util.*;
041:
042: /**
043: * Generates the skeleton for a session bean.
044: */
045: abstract public class SessionGenerator extends BeanGenerator {
046: private static final L10N L = new L10N(SessionGenerator.class);
047:
048: private ApiClass _localHome;
049: private ApiClass _remoteHome;
050:
051: private ApiClass _localObject;
052: private ApiClass _remoteObject;
053:
054: private ArrayList<ApiClass> _localApi = new ArrayList<ApiClass>();
055:
056: private ArrayList<ApiClass> _remoteApi = new ArrayList<ApiClass>();
057:
058: private ArrayList<View> _views = new ArrayList<View>();
059:
060: protected String _contextClassName = "dummy";
061:
062: public SessionGenerator(String ejbName, ApiClass ejbClass) {
063: super (toFullClassName(ejbName, ejbClass.getSimpleName()),
064: ejbClass);
065:
066: _contextClassName = "dummy";
067: }
068:
069: private static String toFullClassName(String ejbName,
070: String className) {
071: StringBuilder sb = new StringBuilder();
072:
073: sb.append("_ejb.");
074:
075: if (!Character.isJavaIdentifierStart(ejbName.charAt(0)))
076: sb.append('_');
077:
078: for (int i = 0; i < ejbName.length(); i++) {
079: char ch = ejbName.charAt(i);
080:
081: if (ch == '/')
082: sb.append('.');
083: else if (Character.isJavaIdentifierPart(ch))
084: sb.append(ch);
085: else
086: sb.append('_');
087: }
088:
089: sb.append(".");
090: sb.append(className);
091: sb.append("__EJB");
092:
093: return sb.toString();
094: }
095:
096: public boolean isStateless() {
097: return false;
098: }
099:
100: /**
101: * Sets the local home
102: */
103: public void setLocalHome(ApiClass homeApi) {
104: _localHome = homeApi;
105: }
106:
107: /**
108: * Sets the remote home
109: */
110: public void setRemoteHome(ApiClass homeApi) {
111: _remoteHome = homeApi;
112: }
113:
114: /**
115: * Sets the local object
116: */
117: public void setLocalObject(ApiClass objectApi) {
118: _localObject = objectApi;
119: }
120:
121: /**
122: * the local object
123: */
124: public ApiClass getLocalObject() {
125: return _localObject;
126: }
127:
128: /**
129: * Sets the remote object
130: */
131: public void setRemoteObject(ApiClass objectApi) {
132: _remoteObject = objectApi;
133: }
134:
135: /**
136: * Gets the remote object
137: */
138: public ApiClass getRemoteObject() {
139: return _remoteObject;
140: }
141:
142: /**
143: * Adds a local
144: */
145: public void addLocal(ApiClass localApi) {
146: _localApi.add(localApi);
147: }
148:
149: /**
150: * Returns the local API list.
151: */
152: public ArrayList<ApiClass> getLocalApi() {
153: return _localApi;
154: }
155:
156: /**
157: * Adds a remote
158: */
159: @Override
160: public void addRemote(ApiClass remoteApi) {
161: _remoteApi.add(remoteApi);
162: }
163:
164: /**
165: * Returns the remote API list.
166: */
167: public ArrayList<ApiClass> getRemoteApi() {
168: return _remoteApi;
169: }
170:
171: /**
172: * Returns the views
173: */
174: public ArrayList<View> getViews() {
175: return _views;
176: }
177:
178: /**
179: * Returns the view matching the given class
180: */
181: public View getView(Class api) {
182: for (View view : _views) {
183: if (view.getApi().getName().equals(api.getName()))
184: return view;
185: }
186:
187: return null;
188: }
189:
190: /**
191: * Introspects the bean.
192: */
193: @Override
194: public void introspect() {
195: super .introspect();
196:
197: if (_localHome == null && _localApi.size() == 0)
198: _localApi = introspectLocalApi();
199:
200: if (_remoteHome == null && _remoteApi.size() == 0)
201: _remoteApi = introspectRemoteApi();
202: }
203:
204: /**
205: * Generates the views for the bean
206: */
207: @Override
208: public void createViews() {
209: if (_localHome != null) {
210: View view = createLocalHomeView(_localHome);
211:
212: _views.add(view);
213: }
214:
215: for (ApiClass api : _localApi) {
216: View view = createLocalView(api);
217:
218: _views.add(view);
219: }
220:
221: if (_remoteHome != null) {
222: View view = createRemoteHomeView(_remoteHome);
223:
224: _views.add(view);
225: }
226:
227: for (ApiClass api : _remoteApi) {
228: View view = createRemoteView(api);
229:
230: _views.add(view);
231: }
232:
233: for (View view : _views)
234: view.introspect();
235: }
236:
237: /**
238: * Generates the local home view for the given class
239: */
240: protected View createLocalHomeView(ApiClass api) {
241: throw new UnsupportedOperationException(getClass().getName());
242: }
243:
244: /**
245: * Generates the remote home view for the given class
246: */
247: protected View createRemoteHomeView(ApiClass api) {
248: throw new UnsupportedOperationException(getClass().getName());
249: }
250:
251: /**
252: * Generates the local view for the given class
253: */
254: protected View createLocalView(ApiClass api) {
255: throw new UnsupportedOperationException(getClass().getName());
256: }
257:
258: /**
259: * Generates the remote view for the given class
260: */
261: protected View createRemoteView(ApiClass api) {
262: throw new UnsupportedOperationException(getClass().getName());
263: }
264:
265: /**
266: * Scans for the @Local interfaces
267: */
268: private ArrayList<ApiClass> introspectLocalApi() {
269: ArrayList<ApiClass> apiList = new ArrayList<ApiClass>();
270:
271: Local local = (Local) getEjbClass().getAnnotation(Local.class);
272: Remote remote = (Remote) getEjbClass().getAnnotation(
273: Remote.class);
274:
275: if (local != null) {
276: for (Class api : local.value()) {
277: apiList.add(new ApiClass(api));
278: }
279:
280: return apiList;
281: }
282:
283: boolean hasRemote = remote != null;
284:
285: Class[] apiClasses = getEjbClass().getInterfaces();
286: for (Class api : apiClasses) {
287: if (api.isAnnotationPresent(Local.class))
288: apiList.add(new ApiClass(api));
289: if (api.isAnnotationPresent(Remote.class))
290: hasRemote = true;
291: }
292:
293: if (apiList.size() > 0 || hasRemote)
294: return apiList;
295:
296: Class singleApi = null;
297: for (Class api : apiClasses) {
298: if (api.equals(java.io.Serializable.class))
299: continue;
300: if (api.equals(java.io.Externalizable.class))
301: continue;
302: if (api.equals(javax.ejb.SessionBean.class))
303: continue;
304: if (api.getName().startsWith("javax.ejb."))
305: continue;
306: if (api.isAnnotationPresent(Remote.class)) {
307: continue;
308: }
309:
310: if (singleApi != null) {
311: throw new ConfigException(
312: L
313: .l(
314: "{0}: does not have a unique local API. Both '{1}' and '{2}' are local.",
315: getEjbClass().getName(),
316: singleApi.getName(), api
317: .getName()));
318: }
319:
320: singleApi = api;
321: }
322:
323: if (singleApi != null) {
324: apiList.add(new ApiClass(singleApi));
325:
326: return apiList;
327: }
328:
329: // XXX: only for stateful?
330: // apiList.add(getEjbClass());
331:
332: return apiList;
333: }
334:
335: /**
336: * Scans for the @Remote interfaces
337: */
338: private ArrayList<ApiClass> introspectRemoteApi() {
339: ArrayList<ApiClass> apiList = new ArrayList<ApiClass>();
340:
341: Remote remote = (Remote) getEjbClass().getAnnotation(
342: Remote.class);
343:
344: if (remote != null) {
345: for (Class api : remote.value()) {
346: apiList.add(new ApiClass(api));
347: }
348:
349: return apiList;
350: }
351:
352: Class[] apiClasses = getEjbClass().getInterfaces();
353: for (Class api : apiClasses) {
354: if (api.isAnnotationPresent(Remote.class))
355: apiList.add(new ApiClass(api));
356: }
357:
358: if (apiList.size() > 0)
359: return apiList;
360:
361: return apiList;
362: }
363:
364: abstract protected void generateContext(JavaWriter out)
365: throws IOException;
366:
367: protected void generateNewInstance(JavaWriter out, String suffix)
368: throws IOException {
369: }
370:
371: protected void generateNewRemoteInstance(JavaWriter out,
372: String suffix) throws IOException {
373: // ejb/0g27
374: /*
375: if (_bean.getRemoteHome() == null && _bean.getRemoteList().size() == 0)
376: return;
377: */
378: out.println();
379: out.println("protected Object _caucho_newRemoteInstance"
380: + suffix + "()");
381: out.println("{");
382: out.pushDepth();
383:
384: out.println(_contextClassName + " cxt = new "
385: + _contextClassName + "(_server);");
386:
387: if (isStateless())
388: out.println("Bean bean = new Bean(cxt);");
389: else
390: out.println("Bean bean = new Bean(cxt, null);");
391:
392: out.println("cxt._ejb_free(bean);");
393:
394: out.println();
395:
396: /*
397: Class retType = getReturnType();
398: if ("RemoteHome".equals(_prefix))
399: out.println("return (" + retType.getName() + ") cxt.getRemoteView();");
400: else if ("LocalHome".equals(_prefix))
401: out.println("return (" + retType.getName() + ") cxt.getLocalView();");
402: else
403: throw new IOException(L.l("trying to create unknown type {0}",
404: _prefix));
405: */
406:
407: out.println("return cxt.createRemoteView" + suffix + "();");
408:
409: out.popDepth();
410: out.println("}");
411: }
412:
413: private void generateBean(JavaWriter out) throws IOException {
414: out.println();
415: out.println("public static class Bean extends "
416: + _ejbClass.getName() + " {");
417: out.pushDepth();
418:
419: out.println();
420: out
421: .println("protected final static java.util.logging.Logger __caucho_log");
422: out.println(" = java.util.logging.Logger.getLogger(\""
423: + _ejbClass.getName() + "\");");
424: out.println("private static int __caucho_dbg_id;");
425: out.println("private String __caucho_id;");
426:
427: out.println(_contextClassName + " _ejb_context;");
428: out.println("boolean _ejb_isActive;");
429:
430: int i = 0;
431:
432: /*
433: for (Interceptor interceptor : _bean.getInterceptors()) {
434: out.println("Object _interceptor" + i++ + ";");
435: }
436: * */
437:
438: out.println();
439: if (isStateless())
440: out.println("Bean(" + _contextClassName + " context)");
441: else
442: out.println("Bean(" + _contextClassName
443: + " context, com.caucho.config.ConfigContext env)");
444: out.println("{");
445: out.pushDepth();
446:
447: out.println("if (__caucho_isFiner) {");
448: out.pushDepth();
449:
450: out.println("synchronized (" + _ejbClass.getName()
451: + ".class) {");
452: out.println(" __caucho_id = \"" + _ejbClass.getName()
453: + "[\" + __caucho_dbg_id++ + \"]\";");
454: out.println("}");
455: out.println("__caucho_log.fine(__caucho_id + \":new()\");");
456: out.popDepth();
457: out.println("}");
458:
459: out.println("try {");
460: out.pushDepth();
461:
462: out.println("_ejb_context = context;");
463:
464: if (hasMethod("setSessionContext",
465: new Class[] { SessionContext.class })) {
466: // TCK: ejb30/bb/session/stateless/annotation/resource/dataSourceTest
467: // ejb/0f55 setSessionContext() can be private, out.println("setSessionContext(context);");
468: out
469: .println("invokeMethod(this, \"setSessionContext\", new Class[] { javax.ejb.SessionContext.class }, new Object[] { context });");
470: }
471:
472: /*
473: // ejb/0fd0
474: out.println();
475: out.println("__caucho_initInjection();");
476: */
477:
478: out.println();
479: if (isStateless())
480: out
481: .println("context.getServer().initInstance(this, null);");
482: else
483: out.println("context.getServer().initInstance(this, env);");
484:
485: out.println();
486: out.println("__caucho_callInterceptorsPostConstruct();");
487:
488: out.popDepth();
489: out.println("} catch (RuntimeException e) {");
490: out.println(" throw e;");
491: out.println("} catch (Exception e) {");
492: out
493: .println(" __caucho_log.log(java.util.logging.Level.FINE, e.toString(), e);");
494: out
495: .println(" throw com.caucho.ejb.EJBExceptionWrapper.create(e);");
496: out.println("}");
497:
498: out.popDepth();
499: out.println("}");
500:
501: out.println();
502: out
503: .println("public Object __caucho_callInterceptors(Object target, Object []args, String methodName, Class paramTypes[])");
504: out
505: .println(" throws java.lang.reflect.InvocationTargetException");
506: out.println("{");
507: out.pushDepth();
508:
509: out.println("try {");
510: out.pushDepth();
511:
512: out.println("Class cl;");
513: out
514: .println("Class methodParamTypes[] = new Class[] { javax.interceptor.InvocationContext.class };");
515: out.println();
516:
517: StringBuilder interceptors = new StringBuilder();
518: StringBuilder interceptorMethods = new StringBuilder();
519:
520: boolean hasAroundInvoke = false;
521:
522: i = 0;
523:
524: /*
525: for (Interceptor interceptor : _bean.getInterceptors()) {
526: int j = i++;
527:
528: String aroundInvokeMethodName = interceptor.getAroundInvokeMethodName();
529:
530: // ejb/0fbi
531: if (aroundInvokeMethodName == null)
532: continue;
533:
534: if (! hasAroundInvoke) {
535: hasAroundInvoke = true;
536: } else {
537: interceptors.append(", ");
538: interceptorMethods.append(", ");
539: }
540:
541: interceptors.append("_interceptor");
542: interceptors.append(j);
543: interceptorMethods.append("method");
544: interceptorMethods.append(j);
545:
546: String clName = interceptor.getInterceptorClass();
547:
548: out.println("Class cl" + j + " = Class.forName(\"" + clName + "\");");
549:
550: out.println("if (_interceptor" + j + " == null) {");
551: out.println(" _interceptor" + j + " = cl" + j + ".newInstance();");
552:
553: out.println("}");
554:
555: out.println();
556:
557: generateCallReflectionGetMethod(out, "method" + j, aroundInvokeMethodName, "methodParamTypes", "cl" + j);
558:
559: out.println();
560: }
561:
562:
563: out.println("javax.interceptor.InvocationContext invocationContext;");
564:
565: String aroundInvokeMethodName = _bean.getAroundInvokeMethodName();
566:
567: // ejb/0fb8
568: if (aroundInvokeMethodName != null) {
569: if (i > 0) {
570: interceptors.append(", ");
571: interceptorMethods.append(", ");
572: }
573:
574: interceptors.append("this");
575: interceptorMethods.append(aroundInvokeMethodName);
576:
577: out.println();
578:
579: generateCallReflectionGetMethod(out, aroundInvokeMethodName, aroundInvokeMethodName, "methodParamTypes", "getClass()");
580: }
581:
582: // XXX: invocation context pool ???
583: out.println();
584: out.println("invocationContext = new com.caucho.ejb.interceptor.InvocationContextImpl(this,");
585: out.println(" target, methodName, paramTypes, new Object[] { " + interceptors + " }, new java.lang.reflect.Method[] { " + interceptorMethods + " });");
586: out.println("invocationContext.setParameters(args);");
587:
588: out.println("return invocationContext.proceed();");
589:
590: out.popDepth();
591: */
592: /* ejb/0fba
593:
594: // ejb/0f66
595: if (_bean.getInterceptors().size() > 0) {
596: out.println("} catch (java.lang.reflect.InvocationTargetException e) {");
597: out.println(" throw e;");
598: }
599: */
600:
601: out
602: .println("} catch (java.lang.reflect.InvocationTargetException e) {");
603: out.println(" throw e;");
604: out.println("} catch (RuntimeException e) {");
605: out.println(" throw e;");
606: out.println("} catch (Throwable e) {");
607: out
608: .println(" __caucho_log.log(java.util.logging.Level.FINE, e.toString(), e);");
609: out
610: .println(" throw com.caucho.ejb.EJBExceptionWrapper.create(e);");
611: out.println("}");
612:
613: // out.println();
614: // out.println("return null;");
615: //out.println("return invocationContext;");
616:
617: out.popDepth();
618: out.println("}");
619:
620: // ejb/0fbh
621: out.println();
622: out
623: .println("private void __caucho_callInterceptorsPostConstruct()");
624: out.println("{");
625: out.pushDepth();
626:
627: out.println("try {");
628: out.pushDepth();
629:
630: out
631: .println("Class methodParamTypes[] = new Class[] { javax.interceptor.InvocationContext.class };");
632:
633: i = 0;
634: /*
635: for (Interceptor interceptor : _bean.getInterceptors()) {
636: int j = i++;
637:
638: String postConstructMethodName = interceptor.getPostConstructMethodName();
639:
640: if (postConstructMethodName == null)
641: continue;
642:
643: String clName = interceptor.getInterceptorClass();
644:
645: out.println("Class cl" + j + " = Class.forName(\"" + clName + "\");");
646:
647: out.println("if (_interceptor" + j + " == null) {");
648: out.println(" _interceptor" + j + " = cl" + j + ".newInstance();");
649:
650: out.println("}");
651:
652: out.println();
653:
654: generateCallReflectionGetMethod(out, "method" + j, postConstructMethodName, "methodParamTypes", "cl" + j);
655:
656: out.println();
657: }
658:
659: interceptors.setLength(0);
660: interceptorMethods.setLength(0);
661:
662: boolean isFirst = true;
663:
664: i = 0;
665:
666: for (Interceptor interceptor : _bean.getInterceptors()) {
667: int j = i++;
668:
669: if (interceptor.getPostConstructMethodName() == null)
670: continue;
671:
672: if (isFirst) {
673: isFirst = false;
674: } else {
675: interceptors.append(", ");
676: interceptorMethods.append(", ");
677: }
678:
679: interceptors.append("_interceptor");
680: interceptors.append(j);
681: interceptorMethods.append("method");
682: interceptorMethods.append(j);
683: }
684:
685: out.println("javax.interceptor.InvocationContext invocationContext;");
686:
687: // XXX: invocation context pool ???
688: out.println();
689: out.println("invocationContext = new com.caucho.ejb.interceptor.InvocationContextImpl(this,");
690: out.println(" this, null, null, new Object[] { " + interceptors + " }, new java.lang.reflect.Method[] { " + interceptorMethods + " });");
691:
692: out.println("invocationContext.proceed();");
693:
694: out.popDepth();
695: out.println("} catch (Exception e) {");
696: out.println(" throw com.caucho.ejb.EJBExceptionWrapper.create(e);");
697: out.println("}");
698:
699: out.popDepth();
700: out.println("}");
701:
702: generateInitInjection(out);
703: */
704: generateReflectionGetMethod(out);
705:
706: out.popDepth();
707: out.println("}");
708: }
709:
710: /**
711: * Generates injection initialization.
712: */
713: protected void generateInitInjection(JavaWriter out)
714: throws IOException {
715: /*
716: // ejb/0fd0
717: out.println();
718: out.println("private void __caucho_initInjection()");
719: out.println("{");
720: out.pushDepth();
721:
722: out.println("try {");
723: out.pushDepth();
724:
725: out.println("java.lang.reflect.Field field;");
726: out.println();
727:
728: for (EnvEntry envEntry : _bean.getEnvEntries()) {
729: InjectionTarget injectionTarget = envEntry.getInjectionTarget();
730:
731: if (injectionTarget == null)
732: continue;
733:
734: String value = envEntry.getEnvEntryValue();
735:
736: // ejb/0fd4
737: if (value == null)
738: continue;
739:
740: Class cl = envEntry.getEnvEntryType();
741:
742: generateInjection(out, injectionTarget, value, cl, true);
743: }
744:
745: // ejb/0f54
746: for (ResourceRef resourceRef : _bean.getResourceRefs()) {
747: InjectionTarget injectionTarget = resourceRef.getInjectionTarget();
748:
749: if (injectionTarget == null)
750: continue;
751:
752: String value = "com.caucho.naming.Jndi.lookup(\"java:comp/env/" + resourceRef.getResRefName() + "\")";
753:
754: if (value == null)
755: continue;
756:
757: Class cl = resourceRef.getResType();
758:
759: generateInjection(out, injectionTarget, value, cl, false);
760: }
761:
762: out.popDepth();
763: out.println("} catch (Throwable e) {");
764: out.println(" __caucho_log.log(java.util.logging.Level.FINE, e.toString(), e);");
765: out.println(" throw com.caucho.ejb.EJBExceptionWrapper.create(e);");
766: out.println("}");
767:
768: out.popDepth();
769: out.println("}");
770: */
771: }
772:
773: /**
774: * Generates an individual injection.
775: */
776: protected void generateInjection(JavaWriter out,
777: InjectionTarget injectionTarget, String value, Class cl,
778: boolean isEscapeString) throws IOException {
779: // ejb/0fd1, ejb/0fd3
780: value = generateTypeCasting(value, cl, isEscapeString);
781:
782: String s = injectionTarget.getInjectionTargetName();
783:
784: out.println("try {");
785: out.pushDepth();
786:
787: String methodName = "set" + Character.toUpperCase(s.charAt(0));
788:
789: if (s.length() > 1)
790: methodName += s.substring(1);
791:
792: generateCallReflectionGetMethod(out, "method", methodName,
793: "new Class[] { " + cl.getName() + ".class }",
794: "getClass().getSuperclass()");
795:
796: out.println("method.setAccessible(true);");
797:
798: out.print("method.invoke(this, ");
799: out.print(value);
800: out.println(");");
801:
802: out.popDepth();
803: out.println("} catch (NoSuchMethodException e1) {");
804: out.pushDepth();
805:
806: java.lang.reflect.Field field = null;
807:
808: try {
809: field = cl.getDeclaredField("TYPE");
810: } catch (NoSuchFieldException e) {
811: }
812:
813: boolean isPrimitiveWrapper = false;
814:
815: if (field != null
816: && Class.class.isAssignableFrom(field.getType())) { //if (cl.isPrimitive())
817: isPrimitiveWrapper = true;
818: }
819:
820: // ejb/0fd2
821: if (isPrimitiveWrapper) {
822: out.println("try {");
823: out.pushDepth();
824:
825: // ejb/0fd2 vs ejb/0fd3
826: generateCallReflectionGetMethod(out, "method", methodName,
827: "new Class[] { " + cl.getName() + ".TYPE }",
828: "getClass().getSuperclass()");
829:
830: out.println("method.setAccessible(true);");
831:
832: out.print("method.invoke(this, ");
833: out.print(value);
834: out.println(");");
835:
836: out.popDepth();
837: out.println("} catch (NoSuchMethodException e2) {");
838: out.pushDepth();
839: }
840:
841: out
842: .print("field = getClass().getSuperclass().getDeclaredField(\"");
843: out.print(s);
844: out.println("\");");
845:
846: out.println("field.setAccessible(true);");
847:
848: out.print("field.set(this, ");
849: out.print(value);
850: out.println(");");
851:
852: // ejb/0fd2 vs ejb/0fd3
853: if (isPrimitiveWrapper) { // if (! cl.equals(String.class)) {
854: out.popDepth();
855: out.println("}");
856: }
857:
858: out.popDepth();
859: out.println("}");
860: }
861:
862: /**
863: * Generates a call to get a class method.
864: */
865: protected void generateCallReflectionGetMethod(JavaWriter out,
866: String methodVar, String methodName, String paramVar,
867: String classVar) throws IOException {
868: out.print("java.lang.reflect.Method ");
869: out.print(methodVar);
870: out.print(" = com.caucho.ejb.util.EjbUtil.getMethod(");
871: out.print(classVar);
872: out.print(", \"");
873: out.print(methodName);
874: out.print("\", ");
875: out.print(paramVar);
876: out.println(");");
877: }
878:
879: /**
880: * Generates reflection to access a class method.
881: */
882: protected void generateReflectionGetMethod(JavaWriter out)
883: throws IOException {
884: // moved to EjbUtil
885: }
886:
887: /**
888: * Makes private methods accessible before invoking them.
889: */
890: protected void generateInvokeMethod(JavaWriter out)
891: throws IOException {
892: out.println();
893: out
894: .println("private static void invokeMethod(Bean bean, String methodName, Class paramTypes[], Object paramValues[])");
895: out.println("{");
896: out.pushDepth();
897:
898: out.println("try {");
899: out.pushDepth();
900:
901: out
902: .println("java.lang.reflect.Method m = com.caucho.ejb.util.EjbUtil.getMethod(bean.getClass(), methodName, paramTypes);");
903: out.println("m.setAccessible(true);");
904: out.println("m.invoke(bean, paramValues);");
905:
906: out.popDepth();
907: out.println("} catch (Exception e) {");
908: out.pushDepth();
909:
910: out
911: .println("__caucho_log.log(java.util.logging.Level.FINE, e.toString(), e);");
912: out
913: .println("throw com.caucho.ejb.EJBExceptionWrapper.create(e);");
914:
915: out.popDepth();
916: out.println("}");
917:
918: out.popDepth();
919: out.println("}");
920: }
921:
922: /**
923: * Returns true if the method is implemented.
924: */
925: protected boolean hasMethod(String methodName, Class[] paramTypes) {
926: return _ejbClass.hasMethod(methodName, paramTypes);
927: }
928:
929: private String generateTypeCasting(String value, Class cl,
930: boolean isEscapeString) {
931: if (cl.equals(String.class)) {
932: if (isEscapeString)
933: value = "\"" + value + "\"";
934: } else if (cl.equals(Character.class))
935: value = "'" + value + "'";
936: else if (cl.equals(Byte.class))
937: value = "(byte) " + value;
938: else if (cl.equals(Short.class))
939: value = "(short) " + value;
940: else if (cl.equals(Integer.class))
941: value = "(int) " + value;
942: else if (cl.equals(Long.class))
943: value = "(long) " + value;
944: else if (cl.equals(Float.class))
945: value = "(float) " + value;
946: else if (cl.equals(Double.class))
947: value = "(double) " + value;
948:
949: return value;
950: }
951: }
|