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, e.g. a local stateful view
044: */
045: public class StatefulLocalHomeView extends StatefulHomeView {
046: private static final L10N L = new L10N(StatefulLocalHomeView.class);
047:
048: public StatefulLocalHomeView(StatefulGenerator bean, ApiClass api) {
049: super (bean, api);
050: }
051:
052: protected String getViewClassName() {
053: return getApi().getSimpleName() + "__EJBLocalHome";
054: }
055:
056: /**
057: * Generates prologue for the context.
058: */
059: public void generateContextPrologue(JavaWriter out)
060: throws IOException {
061: out.println();
062: out.println("private " + getViewClassName() + " _localHome;");
063:
064: if (EJBLocalHome.class
065: .isAssignableFrom(getApi().getJavaClass())) {
066: out.println();
067: out.println("@Override");
068: out.println("public EJBLocalHome getEJBLocalHome()");
069: out.println("{");
070: out.println(" return _localHome;");
071: out.println("}");
072: }
073: }
074:
075: /**
076: * Generates context home's constructor
077: */
078: @Override
079: public void generateContextHomeConstructor(JavaWriter out)
080: throws IOException {
081: out.println("_localHome = new " + getViewClassName()
082: + "(this);");
083: }
084:
085: /**
086: * Generates code to create the provider
087: */
088: @Override
089: public void generateCreateProvider(JavaWriter out, String var)
090: throws IOException {
091: out.println();
092: out.println("if (" + var + " == " + getApi().getName()
093: + ".class)");
094: out.println(" return _localHome;");
095: }
096:
097: /**
098: * Generates context home's constructor
099: */
100: @Override
101: public void generateContextObjectConstructor(JavaWriter out)
102: throws IOException {
103: out.println("_localHome = context._localHome;");
104: }
105:
106: @Override
107: protected StatefulMethod createMethod(ApiMethod apiMethod, int index) {
108: if (apiMethod.getName().equals("create")) {
109: ApiMethod implMethod = getEjbClass().getMethod("ejbCreate",
110: apiMethod.getParameterTypes());
111:
112: if (implMethod == null)
113: throw ConfigException.create(apiMethod.getMethod(), L
114: .l("can't find ejbCreate"));
115:
116: View localView = getSessionBean().getView(
117: apiMethod.getReturnType());
118:
119: if (localView == null)
120: throw ConfigException.create(apiMethod.getMethod(), L
121: .l("'{0}' is an unknown object interface",
122: apiMethod.getReturnType()));
123:
124: return new StatefulCreateMethod(getSessionBean(), this ,
125: localView, apiMethod, implMethod.getMethod(), index);
126: } else {
127: return super .createMethod(apiMethod, index);
128: }
129: }
130:
131: protected ApiMethod findImplMethod(ApiMethod apiMethod) {
132: if (apiMethod.getName().equals("create")) {
133: return getEjbClass().getMethod("ejbCreate",
134: apiMethod.getParameterTypes());
135: } else
136: return super .findImplMethod(apiMethod);
137: }
138:
139: protected void generateSuper(JavaWriter out, String serverVar)
140: throws IOException {
141: out.println("super(" + serverVar + ");");
142: }
143:
144: @Override
145: protected void generateExtends(JavaWriter out) throws IOException {
146: out.println(" extends StatefulHome");
147: }
148: }
|