001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.client;
017:
018: import java.lang.reflect.Method;
019: import java.rmi.RemoteException;
020:
021: import javax.ejb.EJBObject;
022: import javax.ejb.Handle;
023:
024: public class EntityEJBHomeHandler extends EJBHomeHandler {
025:
026: public EntityEJBHomeHandler() {
027: }
028:
029: public EntityEJBHomeHandler(EJBMetaDataImpl ejb,
030: ServerMetaData server, ClientMetaData client) {
031: super (ejb, server, client);
032: }
033:
034: protected Object findX(Method method, Object[] args, Object proxy)
035: throws Throwable {
036: EJBRequest req = new EJBRequest(
037: RequestMethodConstants.EJB_HOME_FIND, ejb, method,
038: args, null);
039:
040: EJBResponse res = request(req);
041:
042: Object primKey = null;
043: EJBObjectHandler handler = null;
044: Object[] primaryKeys = null;
045:
046: switch (res.getResponseCode()) {
047: case ResponseCodes.EJB_ERROR:
048: throw new SystemError((ThrowableArtifact) res.getResult());
049: case ResponseCodes.EJB_SYS_EXCEPTION:
050: throw new SystemException((ThrowableArtifact) res
051: .getResult());
052: case ResponseCodes.EJB_APP_EXCEPTION:
053: throw new ApplicationException((ThrowableArtifact) res
054: .getResult());
055:
056: case ResponseCodes.EJB_OK_FOUND:
057: primKey = res.getResult();
058: if (primKey == null) {
059: return null;
060: } else {
061: handler = EJBObjectHandler.createEJBObjectHandler(ejb,
062: server, client, primKey);
063: handler.setEJBHomeProxy((EJBHomeProxy) proxy);
064: registerHandler(ejb.deploymentID + ":" + primKey,
065: handler);
066: return handler.createEJBObjectProxy();
067: }
068:
069: case ResponseCodes.EJB_OK_FOUND_COLLECTION:
070:
071: primaryKeys = (Object[]) res.getResult();
072:
073: for (int i = 0; i < primaryKeys.length; i++) {
074: primKey = primaryKeys[i];
075: if (primKey != null) {
076: handler = EJBObjectHandler.createEJBObjectHandler(
077: ejb, server, client, primKey);
078: handler.setEJBHomeProxy((EJBHomeProxy) proxy);
079: registerHandler(ejb.deploymentID + ":" + primKey,
080: handler);
081: primaryKeys[i] = handler.createEJBObjectProxy();
082: }
083: }
084: return java.util.Arrays.asList(primaryKeys);
085: case ResponseCodes.EJB_OK_FOUND_ENUMERATION:
086:
087: primaryKeys = (Object[]) res.getResult();
088:
089: for (int i = 0; i < primaryKeys.length; i++) {
090: primKey = primaryKeys[i];
091: if (primKey != null) {
092: handler = EJBObjectHandler.createEJBObjectHandler(
093: ejb, server, client, primKey);
094: handler.setEJBHomeProxy((EJBHomeProxy) proxy);
095: registerHandler(ejb.deploymentID + ":" + primKey,
096: handler);
097: primaryKeys[i] = handler.createEJBObjectProxy();
098: }
099: }
100:
101: return new ArrayEnumeration(java.util.Arrays
102: .asList(primaryKeys));
103: default:
104: throw new RemoteException(
105: "Received invalid response code from server: "
106: + res.getResponseCode());
107: }
108: }
109:
110: protected Object removeByPrimaryKey(Method method, Object[] args,
111: Object proxy) throws Throwable {
112: Object primKey = args[0];
113:
114: if (primKey == null)
115: throw new NullPointerException("The primary key is null.");
116:
117: EJBRequest req = new EJBRequest(
118: RequestMethodConstants.EJB_HOME_REMOVE_BY_PKEY, ejb,
119: method, args, primKey);
120:
121: EJBResponse res = request(req);
122:
123: switch (res.getResponseCode()) {
124: case ResponseCodes.EJB_ERROR:
125: throw new SystemError((ThrowableArtifact) res.getResult());
126: case ResponseCodes.EJB_SYS_EXCEPTION:
127: throw new SystemException((ThrowableArtifact) res
128: .getResult());
129: case ResponseCodes.EJB_APP_EXCEPTION:
130: throw new ApplicationException((ThrowableArtifact) res
131: .getResult());
132: case ResponseCodes.EJB_OK:
133: invalidateAllHandlers(ejb.deploymentID + ":" + primKey);
134: return null;
135: default:
136: throw new RemoteException(
137: "Received invalid response code from server: "
138: + res.getResponseCode());
139: }
140: }
141:
142: protected Object removeWithHandle(Method method, Object[] args,
143: Object proxy) throws Throwable {
144: if (args[0] == null)
145: throw new RemoteException("Handler is null");
146:
147: Handle handle = (Handle) args[0];
148:
149: EJBObject ejbObject = handle.getEJBObject();
150: if (ejbObject == null)
151: throw new NullPointerException(
152: "The handle.getEJBObject() is null.");
153:
154: Object primKey = ejbObject.getPrimaryKey();
155: if (primKey == null)
156: throw new NullPointerException(
157: "The handle.getEJBObject().getPrimaryKey() is null.");
158:
159: EJBRequest req = new EJBRequest(
160: RequestMethodConstants.EJB_HOME_REMOVE_BY_HANDLE, ejb,
161: method, args, primKey);
162:
163: EJBResponse res = request(req);
164:
165: switch (res.getResponseCode()) {
166: case ResponseCodes.EJB_ERROR:
167: throw new SystemError((ThrowableArtifact) res.getResult());
168: case ResponseCodes.EJB_SYS_EXCEPTION:
169: throw new SystemException((ThrowableArtifact) res
170: .getResult());
171: case ResponseCodes.EJB_APP_EXCEPTION:
172: throw new ApplicationException((ThrowableArtifact) res
173: .getResult());
174: case ResponseCodes.EJB_OK:
175: invalidateAllHandlers(ejb.deploymentID + ":" + primKey);
176: return null;
177: default:
178: throw new RemoteException(
179: "Received invalid response code from server: "
180: + res.getResponseCode());
181: }
182: }
183: }
|