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.core.stateful;
017:
018: import org.apache.openejb.InterfaceType;
019: import org.apache.openejb.DeploymentInfo;
020: import org.apache.openejb.Container;
021: import org.apache.openejb.core.ivm.EjbObjectProxyHandler;
022: import org.apache.openejb.util.proxy.ProxyManager;
023:
024: import java.lang.reflect.Method;
025: import java.rmi.RemoteException;
026: import java.util.List;
027: import java.io.Serializable;
028:
029: public class StatefulEjbObjectHandler extends EjbObjectProxyHandler {
030:
031: public StatefulEjbObjectHandler(DeploymentInfo deploymentInfo,
032: Object pk, InterfaceType interfaceType,
033: List<Class> interfaces) {
034: super (deploymentInfo, pk, interfaceType, interfaces);
035: }
036:
037: public Object getRegistryId() {
038: return new RegistryId(container, deploymentID, primaryKey);
039: }
040:
041: protected Object getPrimaryKey(Method method, Object[] args,
042: Object proxy) throws Throwable {
043: throw new RemoteException(
044: "Session objects are private resources and do not have primary keys");
045: }
046:
047: protected Object isIdentical(Method method, Object[] args,
048: Object proxy) throws Throwable {
049: checkAuthorization(method);
050:
051: if (args.length != 1) {
052: throw new IllegalArgumentException(
053: "Expected one argument to isIdentical, but received "
054: + args.length);
055: }
056:
057: Object that = args[0];
058: Object invocationHandler = ProxyManager
059: .getInvocationHandler(that);
060:
061: if (invocationHandler instanceof StatefulEjbObjectHandler) {
062: StatefulEjbObjectHandler handler = (StatefulEjbObjectHandler) invocationHandler;
063:
064: /*
065: * The registry id is a compound key composed of the bean's primary key, deployment id, and
066: * container id. It uniquely identifies the entity bean that is proxied by the EntityEjbObjectHandler
067: * within the IntraVM.
068: */
069: return this .getRegistryId().equals(handler.getRegistryId());
070: }
071:
072: return false;
073: }
074:
075: protected Object remove(Class interfce, Method method,
076: Object[] args, Object proxy) throws Throwable {
077: checkAuthorization(method);
078: Object value = container.invoke(deploymentID, interfce, method,
079: args, primaryKey);
080:
081: invalidateAllHandlers(getRegistryId());
082: return value;
083: }
084:
085: private static class RegistryId implements Serializable {
086: private static final long serialVersionUID = 5037368364299042022L;
087:
088: private final Object containerId;
089: private final Object deploymentId;
090: private final Object primaryKey;
091:
092: public RegistryId(Container container, Object deploymentId,
093: Object primaryKey) {
094: if (container == null)
095: throw new NullPointerException("container is null");
096: if (deploymentId == null)
097: throw new NullPointerException("deploymentId is null");
098:
099: this .containerId = container.getContainerID();
100: this .deploymentId = deploymentId;
101: this .primaryKey = primaryKey;
102: }
103:
104: public boolean equals(Object o) {
105: if (this == o)
106: return true;
107: if (o == null || getClass() != o.getClass())
108: return false;
109:
110: RegistryId that = (RegistryId) o;
111:
112: return containerId.equals(that.containerId)
113: && deploymentId.equals(that.deploymentId)
114: && !(primaryKey != null ? !primaryKey
115: .equals(that.primaryKey)
116: : that.primaryKey != null);
117: }
118:
119: public int hashCode() {
120: int result;
121: result = containerId.hashCode();
122: result = 31 * result + deploymentId.hashCode();
123: result = 31 * result
124: + (primaryKey != null ? primaryKey.hashCode() : 0);
125: return result;
126: }
127:
128: public String toString() {
129: return "[" + containerId + ", " + deploymentId + ", "
130: + primaryKey + "]";
131: }
132: }
133: }
|