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 StatelessHomeView extends StatelessView {
047: private static final L10N L = new L10N(StatelessHomeView.class);
048:
049: public StatelessHomeView(StatelessGenerator 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: public void generateCreateProvider(JavaWriter out, String var)
059: throws IOException {
060: out.println();
061: out.println("if (" + var + " == " + getApi().getName()
062: + ".class)");
063: out.println(" return _localHome;");
064: }
065:
066: /**
067: * Generates the view code.
068: */
069: public void generate(JavaWriter out) throws IOException {
070: out.println();
071: out.println("public static class " + getViewClassName());
072: out.println(" extends StatelessHome");
073: out.println(" implements StatelessProvider, "
074: + getApi().getName());
075: out.println("{");
076: out.pushDepth();
077:
078: generateClassContent(out);
079:
080: out.popDepth();
081: out.println("}");
082: }
083:
084: protected void generateClassContent(JavaWriter out)
085: throws IOException {
086: out.println("private " + getContextClassName() + " _context;");
087: out.println("private StatelessServer _server;");
088:
089: out.println();
090: out.println(getViewClassName() + "(" + getContextClassName()
091: + " context)");
092: out.println("{");
093: out.pushDepth();
094:
095: generateSuper(out, "context.getStatelessServer()");
096:
097: out.println("_context = context;");
098: out.println("_server = context.getStatelessServer();");
099:
100: generateBusinessConstructor(out);
101:
102: out.popDepth();
103: out.println("}");
104:
105: out.println();
106: out
107: .println("public Object __caucho_createNew(ConfigContext env)");
108: out.println("{");
109: out.println(" return this;");
110: out.println("}");
111:
112: out.println();
113: out.println("public StatelessServer getStatelessServer()");
114: out.println("{");
115: out.println(" return _server;");
116: out.println("}");
117: out.println();
118:
119: out.println();
120: out.println("public Object __caucho_get()");
121: out.println("{");
122: out.println(" return this;");
123: out.println("}");
124: out.println();
125:
126: generateBusinessMethods(out);
127: }
128:
129: @Override
130: protected BusinessMethodGenerator createMethod(ApiMethod apiMethod,
131: int index) {
132: if (apiMethod.getName().equals("create")) {
133: ApiMethod implMethod = getEjbClass().getMethod("ejbCreate",
134: apiMethod.getParameterTypes());
135:
136: // ejbCreate optional
137: /*
138: throw ConfigException.create(apiMethod.getMethod(),
139: L.l("can't find ejbCreate"));
140: */
141:
142: View localView = getStatelessBean().getView(
143: apiMethod.getReturnType());
144:
145: if (localView == null)
146: throw ConfigException.create(apiMethod.getMethod(), L
147: .l("'{0}' is an unknown object interface",
148: apiMethod.getReturnType()));
149:
150: return new StatelessCreateMethod(getStatelessBean(), this ,
151: localView, apiMethod, implMethod, index);
152: } else {
153: return super .createMethod(apiMethod, index);
154: }
155: }
156:
157: protected ApiMethod findImplMethod(ApiMethod apiMethod) {
158: if (apiMethod.getName().equals("create"))
159: return getEjbClass().getMethod("ejbCreate",
160: apiMethod.getParameterTypes());
161: else
162: return super .findImplMethod(apiMethod);
163: }
164:
165: protected void generateSuper(JavaWriter out, String serverVar)
166: throws IOException {
167: out.println("super(" + serverVar + ");");
168: }
169:
170: @Override
171: protected void generateExtends(JavaWriter out) throws IOException {
172: out.println(" extends StatelessHome");
173: }
174: }
|