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.gen21;
031:
032: import com.caucho.ejb.cfg21.EjbEntityBean;
033: import com.caucho.ejb.gen21.EntityCreateCall;
034: import com.caucho.ejb.cfg.*;
035: import com.caucho.java.JavaWriter;
036: import com.caucho.java.gen.BaseMethod;
037: import com.caucho.java.gen.CallChain;
038: import com.caucho.util.L10N;
039:
040: import javax.ejb.EJBLocalObject;
041: import javax.ejb.EJBObject;
042: import java.io.IOException;
043:
044: /**
045: * Generates the skeleton for the create method.
046: */
047: public class EntityCreateMethod extends BaseMethod {
048: private static L10N L = new L10N(EntityCreateMethod.class);
049:
050: private ApiMethod _apiMethod;
051: private String _contextClassName;
052:
053: protected EntityCreateMethod(ApiMethod apiMethod, CallChain call) {
054: super (apiMethod.getMethod(), call);
055: }
056:
057: public EntityCreateMethod(EjbEntityBean bean, ApiMethod apiMethod,
058: ApiMethod beanCreateMethod, ApiMethod beanPostCreateMethod,
059: String contextClassName) {
060: this (apiMethod, new EntityCreateCall(bean, beanCreateMethod,
061: beanPostCreateMethod, contextClassName));
062:
063: _apiMethod = apiMethod;
064:
065: _contextClassName = contextClassName;
066: }
067:
068: /**
069: * Prints the create method
070: *
071: * @param method the create method
072: */
073: public void generateCall(JavaWriter out, String[] args)
074: throws IOException {
075: /*
076: out.println("Thread thread = Thread.currentThread();");
077: out.println("ClassLoader oldLoader = thread.getContextClassLoader();");
078: out.println();
079: out.println("try {");
080: out.pushDepth();
081: out.println("thread.setContextClassLoader(_server.getClassLoader());");
082: out.println();
083: */
084:
085: out.println(_contextClassName + " cxt;");
086: out.println("cxt = new " + _contextClassName + "(_server);");
087:
088: getCall().generateCall(out, null, "cxt", args);
089:
090: out.println();
091:
092: Class retType = _apiMethod.getReturnType();
093: if (EJBObject.class.isAssignableFrom(retType))
094: out.println("return (" + retType.getName()
095: + ") cxt.getEJBObject();");
096: else if (EJBLocalObject.class.isAssignableFrom(retType))
097: out.println("return (" + retType.getName()
098: + ") cxt.getEJBLocalObject();");
099: else
100: throw new RuntimeException(L.l(
101: "trying to create unknown type {0}", retType
102: .getName()));
103:
104: /*
105: out.popDepth();
106: out.println("} finally {");
107: out.println(" thread.setContextClassLoader(oldLoader);");
108: out.println("}");
109: */
110: }
111: }
|