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 a public interface to a bean's home interface, i.e. the
044: * EJB 2.1-style factory
045: */
046: abstract public class StatefulHomeView extends StatefulView {
047: private static final L10N L = new L10N(StatefulHomeView.class);
048:
049: public StatefulHomeView(StatefulGenerator bean, ApiClass api) {
050: super (bean, api);
051: }
052:
053: abstract protected String getViewClassName();
054:
055: /**
056: * Generates code to create the provider
057: */
058: @Override
059: public void generateCreateProvider(JavaWriter out, String var)
060: throws IOException {
061: out.println();
062: out.println("if (" + var + " == " + getApi().getName()
063: + ".class)");
064: out.println(" return _localHome;");
065: }
066:
067: protected void generateClassContent(JavaWriter out)
068: throws IOException {
069: out.println("private " + getContextClassName() + " _context;");
070: out.println("private StatefulServer _server;");
071:
072: generateBusinessPrologue(out);
073:
074: out.println();
075: out.println(getViewClassName() + "(" + getContextClassName()
076: + " context)");
077: out.println("{");
078: out.pushDepth();
079:
080: generateSuper(out, "context.getStatefulServer()");
081:
082: out.println("_context = context;");
083: out.println("_server = context.getStatefulServer();");
084:
085: out.popDepth();
086: out.println("}");
087:
088: out.println();
089: out
090: .println("public Object __caucho_createNew(ConfigContext env)");
091: out.println("{");
092: out.println(" return this;");
093: out.println("}");
094:
095: out.println();
096: out.println("public StatefulServer getStatefulServer()");
097: out.println("{");
098: out.println(" return _server;");
099: out.println("}");
100: out.println();
101:
102: generateCreate(out);
103:
104: generateBusinessMethods(out);
105: }
106:
107: /**
108: * Generates any create() method
109: */
110: protected void generateCreate(JavaWriter out) throws IOException {
111: }
112:
113: @Override
114: protected StatefulMethod createMethod(ApiMethod apiMethod, int index) {
115: if (apiMethod.getName().startsWith("create")) {
116: String implName = "ejbC" + apiMethod.getName().substring(1);
117:
118: ApiMethod implMethod = getEjbClass().getMethod(implName,
119: apiMethod.getParameterTypes());
120:
121: if (implMethod == null)
122: throw ConfigException.create(apiMethod.getMethod(), L
123: .l("api has no matching '{0}' method in '{1}'",
124: implName, getEjbClass().getName()));
125:
126: View localView = getSessionBean().getView(
127: apiMethod.getReturnType());
128:
129: if (localView == null)
130: throw ConfigException.create(apiMethod.getMethod(), L
131: .l("'{0}' is an unknown object interface",
132: apiMethod.getReturnType()));
133:
134: StatefulMethod method = new StatefulCreateMethod(
135: getSessionBean(), this , localView, apiMethod,
136: implMethod.getMethod(), index);
137:
138: method.getXa().setContainerManaged(false);
139:
140: return method;
141: } else {
142: return super .createMethod(apiMethod, index);
143: }
144: }
145:
146: protected ApiMethod findImplMethod(ApiMethod apiMethod) {
147: if (apiMethod.getName().equals("create")) {
148: return getEjbClass().getMethod("ejbCreate",
149: apiMethod.getParameterTypes());
150: } else if (apiMethod.getName().equals("remove")
151: && apiMethod.getDeclaringClass().getName().startsWith(
152: "javax.ejb")) {
153: return null;
154: } else
155: return super .findImplMethod(apiMethod);
156: }
157:
158: protected void generateSuper(JavaWriter out, String serverVar)
159: throws IOException {
160: out.println("super(" + serverVar + ");");
161: }
162:
163: @Override
164: protected void generateExtends(JavaWriter out) throws IOException {
165: out.println(" extends StatefulHome");
166: }
167: }
|