001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.hivemind.lib.impl;
016
017 import java.rmi.RemoteException;
018
019 import org.apache.hivemind.lib.NameLookup;
020 import org.apache.hivemind.lib.RemoteExceptionCoordinator;
021
022 /**
023 * Generic EJB proxy for stateless session beans. Acts as an InvocationHandler
024 * for a dynamic proxy.
025 *
026 * @author Howard Lewis Ship
027 */
028 public abstract class AbstractEJBProxy
029 {
030 private NameLookup _nameLookup;
031 private RemoteExceptionCoordinator _coordinator;
032
033 protected AbstractEJBProxy(NameLookup nameLookup, RemoteExceptionCoordinator coordinator)
034 {
035 _nameLookup = nameLookup;
036 _coordinator = coordinator;
037 }
038
039 protected Object _lookup(String name)
040 {
041 return _nameLookup.lookup(name, Object.class);
042 }
043
044 /**
045 * Clears the home and remote objects after any remote exception.
046 */
047
048 protected abstract void _clearCachedReferences();
049
050 /**
051 * Invoked by the fabricated subclass when a remote exception occurs.
052 * This notifies the {@link RemoteExceptionCoordinator} (which, indirectly,
053 * allows the {@link NameLookup} service to release its JNDI context).
054 * In addition, {@link #_clearCachedReferences()} is invoked.
055 */
056 protected void _handleRemoteException(RemoteException ex)
057 {
058 _clearCachedReferences();
059 _coordinator.fireRemoteExceptionDidOccur(this, ex);
060 }
061 }