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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.ejb.gen21;
030:
031: import com.caucho.ejb.cfg.*;
032: import com.caucho.java.JavaWriter;
033: import com.caucho.java.gen.BaseMethod;
034: import com.caucho.java.gen.MethodCallChain;
035: import com.caucho.util.L10N;
036:
037: import java.io.IOException;
038: import java.util.Collection;
039: import java.util.Enumeration;
040: import java.util.Iterator;
041:
042: /**
043: * Generates the skeleton for the find method.
044: */
045: public class EntityFindCollectionMethod extends BaseMethod {
046: private static L10N L = new L10N(EntityFindCollectionMethod.class);
047:
048: private ApiMethod _apiMethod;
049: private String _contextClassName;
050: private String _prefix;
051:
052: public EntityFindCollectionMethod(ApiMethod apiMethod,
053: ApiMethod implMethod, String contextClassName, String prefix) {
054: super (apiMethod.getMethod(),
055: implMethod != null ? new MethodCallChain(implMethod
056: .getMethod()) : null);
057:
058: _apiMethod = apiMethod;
059: _contextClassName = contextClassName;
060: _prefix = prefix;
061: }
062:
063: /**
064: * Gets the parameter types
065: */
066: public Class[] getParameterTypes() {
067: return _apiMethod.getParameterTypes();
068: }
069:
070: /**
071: * Gets the return type.
072: */
073: public Class getReturnType() {
074: return _apiMethod.getReturnType();
075: }
076:
077: /**
078: * Prints the create method
079: *
080: * @param method the create method
081: */
082: public void generateCall(JavaWriter out, String[] args)
083: throws IOException {
084: out.println(getReturnType().getName() + " keys;");
085:
086: getCall().generateCall(out, "keys", "bean", args);
087:
088: out.println();
089:
090: out
091: .println("java.util.ArrayList values = new java.util.ArrayList();");
092:
093: Class retType = getReturnType();
094: if (Collection.class.isAssignableFrom(retType)) {
095: out.println("java.util.Iterator iter = keys.iterator();");
096: out.println("while (iter.hasNext()) {");
097: out.pushDepth();
098: out.println("Object key = iter.next();");
099: } else if (Iterator.class.isAssignableFrom(retType)) {
100: out.println("while (keys.hasNext()) {");
101: out.pushDepth();
102: out.println("Object key = keys.next();");
103: } else if (Enumeration.class.isAssignableFrom(retType)) {
104: out.println("while (keys.hasMoreElements()) {");
105: out.pushDepth();
106: out.println("Object key = keys.nextElement();");
107: }
108:
109: out.print("values.add(_server.getContext(key, false)");
110:
111: if ("RemoteHome".equals(_prefix))
112: out.print(".getEJBObject());");
113: else if ("LocalHome".equals(_prefix))
114: out.print(".getEJBLocalObject());");
115: else
116: throw new IOException(L.l(
117: "trying to create unknown type {0}", _prefix));
118:
119: out.popDepth();
120: out.println("}");
121:
122: if (Collection.class.isAssignableFrom(retType)) {
123: out.println("return values;");
124: } else if (Iterator.class.isAssignableFrom(retType)) {
125: out.println("return values.iterator();");
126: } else if (Enumeration.class.isAssignableFrom(retType)) {
127: out
128: .println("return java.util.Collections.enumeration(values);");
129: }
130: }
131: }
|