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.ejb.cfg.*;
034: import com.caucho.java.JavaWriter;
035: import com.caucho.util.L10N;
036:
037: import javax.ejb.*;
038: import java.io.IOException;
039: import java.lang.reflect.*;
040: import java.util.*;
041:
042: /**
043: * Represents any stateless view.
044: */
045: public class StatelessView extends View {
046: private static final L10N L = new L10N(StatelessView.class);
047:
048: private StatelessGenerator _statelessBean;
049:
050: private ArrayList<BusinessMethodGenerator> _businessMethods = new ArrayList<BusinessMethodGenerator>();
051:
052: public StatelessView(StatelessGenerator bean, ApiClass api) {
053: super (bean, api);
054:
055: _statelessBean = bean;
056: }
057:
058: public StatelessGenerator getStatelessBean() {
059: return _statelessBean;
060: }
061:
062: public String getContextClassName() {
063: return getStatelessBean().getClassName();
064: }
065:
066: protected String getViewClassName() {
067: return getApi().getSimpleName() + "__EJBLocal";
068: }
069:
070: protected String getBeanClassName() {
071: return getApi().getSimpleName() + "__Bean";
072: }
073:
074: /**
075: * Returns the introspected methods
076: */
077: public ArrayList<? extends BusinessMethodGenerator> getMethods() {
078: return _businessMethods;
079: }
080:
081: /**
082: * Introspects the APIs methods, producing a business method for
083: * each.
084: */
085: public void introspect() {
086: ApiClass implClass = getEjbClass();
087: ApiClass apiClass = getApi();
088:
089: for (ApiMethod apiMethod : apiClass.getMethods()) {
090: if (apiMethod.getDeclaringClass().equals(Object.class))
091: continue;
092: if (apiMethod.getDeclaringClass().getName().startsWith(
093: "javax.ejb."))
094: continue;
095:
096: if (apiMethod.getName().startsWith("ejb")) {
097: throw new ConfigException(
098: L
099: .l(
100: "{0}: '{1}' must not start with 'ejb'. The EJB spec reserves all methods starting with ejb.",
101: apiMethod.getDeclaringClass(),
102: apiMethod.getName()));
103: }
104:
105: int index = _businessMethods.size();
106:
107: BusinessMethodGenerator bizMethod = createMethod(apiMethod,
108: index);
109:
110: if (bizMethod != null) {
111: bizMethod.introspect(bizMethod.getApiMethod(),
112: bizMethod.getImplMethod());
113:
114: _businessMethods.add(bizMethod);
115: }
116: }
117: }
118:
119: /**
120: * Generates code to create the provider
121: */
122: public void generateCreateProvider(JavaWriter out, String var)
123: throws IOException {
124: out.println();
125: out.println("if (" + var + " == " + getApi().getName()
126: + ".class)");
127: out.println(" return new " + getViewClassName() + "(this);");
128: }
129:
130: protected void generateExtends(JavaWriter out) throws IOException {
131: out.println("extends StatelessObject");
132: }
133:
134: protected BusinessMethodGenerator createMethod(ApiMethod apiMethod,
135: int index) {
136: ApiMethod implMethod = findImplMethod(apiMethod);
137:
138: if (implMethod == null) {
139: throw new ConfigException(
140: L
141: .l(
142: "'{0}' method '{1}' has no corresponding implementation in '{2}'",
143: apiMethod.getMethod()
144: .getDeclaringClass()
145: .getSimpleName(),
146: getFullMethodName(apiMethod),
147: getEjbClass().getName()));
148: }
149:
150: StatelessLocalMethod bizMethod = new StatelessLocalMethod(
151: getEjbClass(), this , apiMethod, implMethod.getMethod(),
152: index);
153:
154: return bizMethod;
155: }
156:
157: /**
158: * Generates the view code.
159: */
160: public void generate(JavaWriter out) throws IOException {
161: }
162:
163: protected void generateSuper(JavaWriter out, String serverVar)
164: throws IOException {
165: out.println("super(" + serverVar + ");");
166: }
167:
168: protected ApiMethod findImplMethod(ApiMethod apiMethod) {
169: return getEjbClass().getMethod(apiMethod);
170: }
171: }
|