KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > gen > SessionCreateMethod


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb.gen;
30
31 import com.caucho.bytecode.JClass;
32 import com.caucho.bytecode.JMethod;
33 import com.caucho.java.JavaWriter;
34 import com.caucho.java.gen.BaseMethod;
35 import com.caucho.util.L10N;
36
37 import java.io.IOException JavaDoc;
38
39 /**
40  * Generates the skeleton for the create method.
41  */

42 public class SessionCreateMethod extends BaseMethod {
43   private static final L10N L = new L10N(StatelessCreateMethod.class);
44
45   private JMethod _method;
46   private String JavaDoc _contextClassName;
47   private String JavaDoc _prefix;
48   
49   public SessionCreateMethod(JMethod apiMethod,
50                  JMethod implMethod,
51                  String JavaDoc contextClassName,
52                  String JavaDoc prefix)
53   {
54     super(apiMethod, implMethod);
55
56     _method = apiMethod;
57     _contextClassName = contextClassName;
58     _prefix = prefix;
59   }
60
61   /**
62    * Gets the parameter types
63    */

64   public JClass []getParameterTypes()
65   {
66     return _method.getParameterTypes();
67   }
68
69   /**
70    * Gets the return type.
71    */

72   public JClass getReturnType()
73   {
74     return _method.getReturnType();
75   }
76
77   /**
78    * Prints the create method
79    *
80    * @param method the create method
81    */

82   public void generateCall(JavaWriter out, String JavaDoc []args)
83     throws IOException JavaDoc
84   {
85     out.println("Thread thread = Thread.currentThread();");
86     out.println("ClassLoader oldLoader = thread.getContextClassLoader();");
87     out.println();
88     out.println("try {");
89     out.pushDepth();
90     out.println("thread.setContextClassLoader(_server.getClassLoader());");
91     out.println();
92     
93     out.println(_contextClassName + " cxt = new " + _contextClassName + "(_server);");
94
95     out.println("Bean bean = new Bean(cxt);");
96
97     getCall().generateCall(out, null, "bean", args);
98     
99     out.println("cxt._ejb_free(bean);");
100     
101     out.println();
102     out.println("_server.createSessionKey(cxt);");
103
104     JClass retType = getReturnType();
105     if ("RemoteHome".equals(_prefix))
106       out.println("return (" + retType.getName() + ") cxt.getRemoteView();");
107     else if ("LocalHome".equals(_prefix))
108       out.println("return (" + retType.getName() + ") cxt.getEJBLocalObject();");
109     else
110       throw new IOException JavaDoc(L.l("trying to create unknown type {0}",
111                 _prefix));
112     
113     out.popDepth();
114     out.println("} finally {");
115     out.println(" thread.setContextClassLoader(oldLoader);");
116     out.println("}");
117   }
118 }
119
Popular Tags