KickJava   Java API By Example, From Geeks To Geeks.

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


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.java.gen.MethodCallChain;
36 import com.caucho.util.L10N;
37
38 import java.io.IOException JavaDoc;
39 import java.util.Collection JavaDoc;
40 import java.util.Enumeration JavaDoc;
41 import java.util.Iterator JavaDoc;
42
43 /**
44  * Generates the skeleton for the find method.
45  */

46 public class EntityFindCollectionMethod extends BaseMethod {
47   private static L10N L = new L10N(EntityFindCollectionMethod.class);
48
49   private JMethod _apiMethod;
50   private String JavaDoc _contextClassName;
51   private String JavaDoc _prefix;
52   
53   public EntityFindCollectionMethod(JMethod apiMethod,
54                     JMethod implMethod,
55                     String JavaDoc contextClassName,
56                     String JavaDoc prefix)
57   {
58     super(apiMethod,
59       implMethod != null ? new MethodCallChain(implMethod) : null);
60
61     _apiMethod = apiMethod;
62     _contextClassName = contextClassName;
63     _prefix = prefix;
64   }
65
66   /**
67    * Gets the parameter types
68    */

69   public JClass []getParameterTypes()
70   {
71     return _apiMethod.getParameterTypes();
72   }
73
74   /**
75    * Gets the return type.
76    */

77   public JClass getReturnType()
78   {
79     return _apiMethod.getReturnType();
80   }
81
82   /**
83    * Prints the create method
84    *
85    * @param method the create method
86    */

87   public void generateCall(JavaWriter out, String JavaDoc []args)
88     throws IOException JavaDoc
89   {
90     out.println(getReturnType().getName() + " keys;");
91
92     getCall().generateCall(out, "keys", "bean", args);
93     
94     out.println();
95
96     out.println("java.util.ArrayList values = new java.util.ArrayList();");
97
98     JClass retType = getReturnType();
99     if (retType.isAssignableTo(Collection JavaDoc.class)) {
100       out.println("java.util.Iterator iter = keys.iterator();");
101       out.println("while (iter.hasNext()) {");
102       out.pushDepth();
103       out.println("Object key = iter.next();");
104     } else if (retType.isAssignableTo(Iterator JavaDoc.class)) {
105       out.println("while (keys.hasNext()) {");
106       out.pushDepth();
107       out.println("Object key = keys.next();");
108     } else if (retType.isAssignableTo(Enumeration JavaDoc.class)) {
109       out.println("while (keys.hasMoreElements()) {");
110       out.pushDepth();
111       out.println("Object key = keys.nextElement();");
112     }
113
114     out.print("values.add(_server.getContext(key, false)");
115
116     if ("RemoteHome".equals(_prefix))
117       out.print(".getEJBObject());");
118     else if ("LocalHome".equals(_prefix))
119       out.print(".getEJBLocalObject());");
120     else
121       throw new IOException JavaDoc(L.l("trying to create unknown type {0}", _prefix));
122
123     out.popDepth();
124     out.println("}");
125
126     if (retType.isAssignableTo(Collection JavaDoc.class)) {
127       out.println("return values;");
128     } else if (retType.isAssignableTo(Iterator JavaDoc.class)) {
129       out.println("return values.iterator();");
130     } else if (retType.isAssignableTo(Enumeration JavaDoc.class)) {
131       out.println("return java.util.Collections.enumeration(values);");
132     }
133   }
134 }
135
Popular Tags