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.java.JavaWriter;
034: import com.caucho.util.L10N;
035:
036: import java.io.*;
037: import java.lang.reflect.*;
038: import java.util.*;
039: import javax.annotation.security.*;
040: import javax.ejb.*;
041: import javax.interceptor.*;
042:
043: /**
044: * Represents a stateful local business method
045: */
046: public class StatefulMethod extends BusinessMethodGenerator {
047: public StatefulMethod(StatefulView view, ApiMethod apiMethod,
048: Method implMethod, int index) {
049: super (view, apiMethod, implMethod, index);
050: }
051:
052: /**
053: * Session bean default is REQUIRED
054: */
055: @Override
056: public void introspect(Method apiMethod, Method implMethod) {
057: getXa().setTransactionType(getDefaultTransactionType());
058:
059: super .introspect(apiMethod, implMethod);
060: }
061:
062: protected TransactionAttributeType getDefaultTransactionType() {
063: return TransactionAttributeType.REQUIRED;
064: }
065:
066: /**
067: * Returns true if any interceptors enhance the business method
068: */
069: @Override
070: public boolean isEnhanced() {
071: return true;
072: }
073:
074: protected void generatePreCall(JavaWriter out) throws IOException {
075: // XXX: need correct exception
076: out.println("if (_bean == null) throw new EJBException();");
077: out.println();
078:
079: out.println("Thread thread = Thread.currentThread();");
080: out
081: .println("ClassLoader oldLoader = thread.getContextClassLoader();");
082: out.println("try {");
083: out.pushDepth();
084: out
085: .println("thread.setContextClassLoader(_server.getClassLoader());");
086: }
087:
088: /**
089: * Generates the underlying bean instance
090: */
091: protected void generateThis(JavaWriter out) throws IOException {
092: out.print("_bean");
093: }
094:
095: /**
096: * Generates the underlying bean instance
097: */
098: protected void generateSuper(JavaWriter out) throws IOException {
099: out.print("_bean");
100: }
101:
102: /**
103: * Generates the underlying bean instance
104: */
105: protected void generatePostCall(JavaWriter out) throws IOException {
106: out.popDepth();
107: out.println("} finally {");
108: out.println(" thread.setContextClassLoader(oldLoader);");
109: out.println("}");
110: }
111: }
|