001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.geronimo.openejb.cluster.stateful.container;
021:
022: import java.lang.reflect.Method;
023: import java.net.URI;
024: import java.util.HashMap;
025: import java.util.Map;
026: import java.util.Set;
027:
028: import javax.transaction.TransactionManager;
029:
030: import org.apache.geronimo.clustering.SessionManager;
031: import org.apache.geronimo.clustering.wadi.WADISessionManager;
032: import org.apache.geronimo.openejb.cluster.infra.NetworkConnectorTracker;
033: import org.apache.geronimo.openejb.cluster.infra.NetworkConnectorTrackerException;
034: import org.apache.geronimo.openejb.cluster.infra.SessionManagerTracker;
035: import org.apache.openejb.ClusteredRPCContainer;
036: import org.apache.openejb.DeploymentInfo;
037: import org.apache.openejb.OpenEJBException;
038: import org.apache.openejb.core.CoreDeploymentInfo;
039: import org.apache.openejb.core.stateful.StatefulContainer;
040: import org.apache.openejb.spi.SecurityService;
041: import org.codehaus.wadi.core.contextualiser.BasicInvocation;
042: import org.codehaus.wadi.core.contextualiser.InvocationContext;
043: import org.codehaus.wadi.core.contextualiser.InvocationException;
044: import org.codehaus.wadi.core.manager.Manager;
045: import org.codehaus.wadi.servicespace.ServiceRegistry;
046: import org.codehaus.wadi.servicespace.ServiceSpace;
047:
048: /**
049: *
050: * @version $Rev:$ $Date:$
051: */
052: public class ClusteredStatefulContainer extends StatefulContainer
053: implements SessionManagerTracker, ClusteredRPCContainer {
054: private final Map<Object, Manager> deploymentIdToManager;
055: private final Map<Object, NetworkConnectorTracker> deploymentIdToNetworkConnectorTracker;
056:
057: public ClusteredStatefulContainer(Object id,
058: TransactionManager transactionManager,
059: SecurityService securityService, Class passivator,
060: int timeOut, int poolSize, int bulkPassivate)
061: throws OpenEJBException {
062: super (id, transactionManager, securityService, passivator,
063: timeOut, poolSize, bulkPassivate);
064:
065: deploymentIdToManager = new HashMap<Object, Manager>();
066: deploymentIdToNetworkConnectorTracker = new HashMap<Object, NetworkConnectorTracker>();
067: }
068:
069: @Override
070: protected ClusteredStatefulInstanceManager newStatefulInstanceManager(
071: TransactionManager transactionManager,
072: SecurityService securityService, Class passivator,
073: int timeOut, int poolSize, int bulkPassivate)
074: throws OpenEJBException {
075: return new ClusteredStatefulInstanceManager(transactionManager,
076: securityService, entityManagerRegistry, passivator,
077: timeOut, poolSize, bulkPassivate);
078: }
079:
080: public void addSessionManager(Object deploymentId,
081: SessionManager sessionManager) {
082: ((ClusteredStatefulInstanceManager) instanceManager)
083: .addSessionManager(deploymentId, sessionManager);
084:
085: WADISessionManager wadiSessionManager = (WADISessionManager) sessionManager;
086:
087: Manager manager = wadiSessionManager.getManager();
088: synchronized (deploymentIdToManager) {
089: deploymentIdToManager.put(deploymentId, manager);
090: }
091:
092: ServiceSpace serviceSpace = wadiSessionManager
093: .getServiceSpace();
094: ServiceRegistry serviceRegistry = serviceSpace
095: .getServiceRegistry();
096: NetworkConnectorTracker networkConnectorTracker;
097: try {
098: networkConnectorTracker = (NetworkConnectorTracker) serviceRegistry
099: .getStartedService(NetworkConnectorTracker.NAME);
100: } catch (Exception e) {
101: throw new IllegalStateException("Should never occur", e);
102: }
103: synchronized (deploymentIdToNetworkConnectorTracker) {
104: deploymentIdToNetworkConnectorTracker.put(deploymentId,
105: networkConnectorTracker);
106: }
107: }
108:
109: public void removeSessionManager(Object deploymentId,
110: SessionManager sessionManager) {
111: ((ClusteredStatefulInstanceManager) instanceManager)
112: .removeSessionManager(deploymentId, sessionManager);
113:
114: synchronized (deploymentIdToManager) {
115: deploymentIdToManager.remove(deploymentId);
116: }
117: synchronized (deploymentIdToNetworkConnectorTracker) {
118: deploymentIdToNetworkConnectorTracker.remove(deploymentId);
119: }
120: }
121:
122: public URI[] getLocations(DeploymentInfo deploymentInfo) {
123: Object deploymentID = deploymentInfo.getDeploymentID();
124: NetworkConnectorTracker networkConnectorTracker;
125: synchronized (deploymentIdToNetworkConnectorTracker) {
126: networkConnectorTracker = deploymentIdToNetworkConnectorTracker
127: .get(deploymentID);
128: }
129: if (null == networkConnectorTracker) {
130: return null;
131: }
132:
133: Set<URI> connectorURIs;
134: try {
135: connectorURIs = networkConnectorTracker
136: .getConnectorURIs(deploymentID);
137: } catch (NetworkConnectorTrackerException e) {
138: return null;
139: }
140: return connectorURIs.toArray(new URI[0]);
141: }
142:
143: @Override
144: protected Object businessMethod(CoreDeploymentInfo deploymentInfo,
145: Object primKey, Class callInterface, Method callMethod,
146: Object[] args) throws OpenEJBException {
147: AbstractEJBInvocation invocation = new BusinessMethodInvocation(
148: primKey.toString(), 5000, deploymentInfo, primKey,
149: callInterface, callMethod, args);
150: return invoke(deploymentInfo, invocation);
151: }
152:
153: protected Object super BusinessMethod(
154: CoreDeploymentInfo deploymentInfo, Object primKey,
155: Class callInterface, Method callMethod, Object[] args)
156: throws OpenEJBException {
157: return super .businessMethod(deploymentInfo, primKey,
158: callInterface, callMethod, args);
159: }
160:
161: @Override
162: protected Object removeEJBObject(CoreDeploymentInfo deploymentInfo,
163: Object primKey, Class callInterface, Method callMethod,
164: Object[] args) throws OpenEJBException {
165: AbstractEJBInvocation invocation = new RemoveEJBObjectInvocation(
166: primKey.toString(), 5000, deploymentInfo, primKey,
167: callInterface, callMethod, args);
168: return invoke(deploymentInfo, invocation);
169: }
170:
171: protected Object super RemoveEJBObject(
172: CoreDeploymentInfo deploymentInfo, Object primKey,
173: Class callInterface, Method callMethod, Object[] args)
174: throws OpenEJBException {
175: return super .removeEJBObject(deploymentInfo, primKey,
176: callInterface, callMethod, args);
177: }
178:
179: protected Object invoke(CoreDeploymentInfo deploymentInfo,
180: AbstractEJBInvocation invocation) throws OpenEJBException {
181: Manager manager;
182: synchronized (deploymentIdToManager) {
183: manager = deploymentIdToManager.get(deploymentInfo
184: .getDeploymentID());
185: }
186: if (null == manager) {
187: throw new OpenEJBException("No manager registered for ["
188: + deploymentInfo + "]");
189: }
190: try {
191: manager.contextualise(invocation);
192: } catch (InvocationException e) {
193: Throwable throwable = e.getCause();
194: if (throwable instanceof OpenEJBException) {
195: throw (OpenEJBException) throwable;
196: } else {
197: throw new OpenEJBException(e);
198: }
199: }
200: return invocation.getResult();
201: }
202:
203: protected abstract class AbstractEJBInvocation extends
204: BasicInvocation {
205: protected final CoreDeploymentInfo deploymentInfo;
206: protected final Object primKey;
207: protected final Class callInterface;
208: protected final Method callMethod;
209: protected final Object[] args;
210: protected Object result;
211:
212: protected AbstractEJBInvocation(String sessionKey,
213: long exclusiveSessionLockWaitTime,
214: CoreDeploymentInfo deploymentInfo, Object primKey,
215: Class callInterface, Method callMethod, Object[] args) {
216: super (sessionKey, exclusiveSessionLockWaitTime);
217: this .deploymentInfo = deploymentInfo;
218: this .primKey = primKey;
219: this .callMethod = callMethod;
220: this .callInterface = callInterface;
221: this .args = args;
222: }
223:
224: public Object getResult() {
225: return result;
226: }
227:
228: @Override
229: protected void doInvoke() throws InvocationException {
230: invokeEJBMethod();
231: }
232:
233: @Override
234: protected void doInvoke(InvocationContext context)
235: throws InvocationException {
236: invokeEJBMethod();
237: }
238:
239: protected abstract void invokeEJBMethod()
240: throws InvocationException;
241: }
242:
243: protected class BusinessMethodInvocation extends
244: AbstractEJBInvocation {
245:
246: protected BusinessMethodInvocation(String sessionKey,
247: long exclusiveSessionLockWaitTime,
248: CoreDeploymentInfo deploymentInfo, Object primKey,
249: Class callInterface, Method callMethod, Object[] args) {
250: super (sessionKey, exclusiveSessionLockWaitTime,
251: deploymentInfo, primKey, callInterface, callMethod,
252: args);
253: }
254:
255: protected void invokeEJBMethod() throws InvocationException {
256: try {
257: result = super BusinessMethod(deploymentInfo, primKey,
258: callInterface, callMethod, args);
259: } catch (OpenEJBException e) {
260: throw new InvocationException(e);
261: }
262: }
263: }
264:
265: protected class RemoveEJBObjectInvocation extends
266: AbstractEJBInvocation {
267:
268: protected RemoveEJBObjectInvocation(String sessionKey,
269: long exclusiveSessionLockWaitTime,
270: CoreDeploymentInfo deploymentInfo, Object primKey,
271: Class callInterface, Method callMethod, Object[] args) {
272: super (sessionKey, exclusiveSessionLockWaitTime,
273: deploymentInfo, primKey, callInterface, callMethod,
274: args);
275: }
276:
277: protected void invokeEJBMethod() throws InvocationException {
278: try {
279: result = super RemoveEJBObject(deploymentInfo, primKey,
280: callInterface, callMethod, args);
281: } catch (OpenEJBException e) {
282: throw new InvocationException(e);
283: }
284: }
285:
286: }
287:
288: }
|