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.ejb.cfg.*;
033: import com.caucho.util.L10N;
034: import com.caucho.java.*;
035: import com.caucho.java.gen.*;
036:
037: import java.io.*;
038: import java.lang.reflect.*;
039: import java.util.ArrayList;
040: import javax.interceptor.*;
041: import javax.ejb.*;
042:
043: /**
044: * Generates the skeleton for a bean.
045: */
046: abstract public class BeanGenerator extends GenClass {
047: private static final L10N L = new L10N(BeanGenerator.class);
048:
049: protected final ApiClass _ejbClass;
050:
051: private Method _aroundInvokeMethod;
052:
053: private ArrayList<Class> _defaultInterceptors = new ArrayList<Class>();
054:
055: protected BeanGenerator(String fullClassName, ApiClass ejbClass) {
056: super (fullClassName);
057:
058: _ejbClass = ejbClass;
059: }
060:
061: protected ApiClass getEjbClass() {
062: return _ejbClass;
063: }
064:
065: /**
066: * Sets the remote name
067: */
068: public void setRemoteHome(ApiClass homeClass) {
069: throw new UnsupportedOperationException(getClass().getName());
070: }
071:
072: /**
073: * Sets the local name
074: */
075: public void setLocalHome(ApiClass homeClass) {
076: throw new UnsupportedOperationException(getClass().getName());
077: }
078:
079: /**
080: * Adds a remote
081: */
082: public void addRemote(ApiClass remoteApi) {
083: }
084:
085: /**
086: * Adds a local
087: */
088: public void addLocal(ApiClass localApi) {
089: }
090:
091: /**
092: * Introspects the bean.
093: */
094: public void introspect() {
095: _aroundInvokeMethod = findAroundInvokeMethod(_ejbClass
096: .getJavaClass());
097: }
098:
099: private static Method findAroundInvokeMethod(Class cl) {
100: if (cl == null)
101: return null;
102:
103: for (Method method : cl.getDeclaredMethods()) {
104: if (method.isAnnotationPresent(AroundInvoke.class)
105: && method.getParameterTypes().length == 1
106: && method.getParameterTypes()[0]
107: .equals(InvocationContext.class)) {
108: return method;
109: }
110: }
111:
112: return findAroundInvokeMethod(cl.getSuperclass());
113: }
114:
115: /**
116: * Returns the around-invoke method
117: */
118: public Method getAroundInvokeMethod() {
119: return _aroundInvokeMethod;
120: }
121:
122: /**
123: * Sets the around-invoke method
124: */
125: public void setAroundInvokeMethod(Method method) {
126: _aroundInvokeMethod = method;
127: }
128:
129: /**
130: * Adds a default interceptor
131: */
132: public void addInterceptor(Class cl) {
133: _defaultInterceptors.add(cl);
134: }
135:
136: /**
137: * Gets the default interceptor
138: */
139: public ArrayList<Class> getDefaultInterceptors() {
140: return _defaultInterceptors;
141: }
142:
143: /**
144: * Returns the views.
145: */
146: public ArrayList<View> getViews() {
147: throw new UnsupportedOperationException(getClass().getName());
148: }
149:
150: /**
151: * Generates the views for the bean
152: */
153: public void createViews() {
154: }
155:
156: /**
157: * Generates the view contents
158: */
159: public void generateViews(JavaWriter out) throws IOException {
160: for (View view : getViews()) {
161: out.println();
162:
163: view.generate(out);
164: }
165: }
166:
167: /**
168: * Generates the view contents
169: */
170: public void generateDestroyViews(JavaWriter out) throws IOException {
171: for (View view : getViews()) {
172: out.println();
173:
174: view.generateDestroy(out);
175: }
176: }
177:
178: /**
179: * Returns true if the method is implemented.
180: */
181: protected boolean hasMethod(String methodName, Class[] paramTypes) {
182: return _ejbClass.hasMethod(methodName, paramTypes);
183: }
184:
185: private String generateTypeCasting(String value, Class cl,
186: boolean isEscapeString) {
187: if (cl.equals(String.class)) {
188: if (isEscapeString)
189: value = "\"" + value + "\"";
190: } else if (cl.equals(Character.class))
191: value = "'" + value + "'";
192: else if (cl.equals(Byte.class))
193: value = "(byte) " + value;
194: else if (cl.equals(Short.class))
195: value = "(short) " + value;
196: else if (cl.equals(Integer.class))
197: value = "(int) " + value;
198: else if (cl.equals(Long.class))
199: value = "(long) " + value;
200: else if (cl.equals(Float.class))
201: value = "(float) " + value;
202: else if (cl.equals(Double.class))
203: value = "(double) " + value;
204:
205: return value;
206: }
207: }
|