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: *
017: * $Header:$
018: */
019: package org.apache.beehive.controls.system.ejb;
020:
021: import java.lang.reflect.InvocationTargetException;
022: import java.lang.reflect.Method;
023: import javax.ejb.EJBObject;
024: import javax.ejb.EJBLocalObject;
025:
026: import org.apache.beehive.controls.api.ControlException;
027: import org.apache.beehive.controls.api.bean.ControlImplementation;
028:
029: /**
030: * The SessionEJBControlImpl class is the control implementation class for
031: * Stateless/Stateful Session EJBs.
032: * <p/>
033: * Currently, this is a noop since no session-bean specific control APIs
034: * are defined, but having a unique control interface/impl class still allows
035: * JellyBeans to create two different control types.
036: */
037: @ControlImplementation(assembler=EJBControlAssembler.class)
038: public class SessionEJBControlImpl extends EJBControlImpl implements
039: SessionEJBControl, java.io.Serializable {
040:
041: static final long serialVersionUID = 1L;
042:
043: /**
044: * Override ejbControl.onCreate to perform additional processing
045: */
046: public void onCreate() {
047:
048: super .onCreate();
049: if (_beanType != EJBControlImpl.SESSION_BEAN)
050: throw new ControlException(
051: "Attempting to use a session bean control with a bean that is not a session bean");
052: }
053:
054: /**
055: * Implements auto-create semantics for Session beans.
056: */
057: protected Object resolveBeanInstance() {
058:
059: // First, try to resolve from a cached EJB handle (if any)
060: Object fromHandle = resolveBeanInstanceFromHandle();
061: if (fromHandle != null)
062: return fromHandle;
063:
064: // Find null arg create() on the home interface, and use it to get an instance
065: try {
066: /* todo: for efficiency, this could be done once per home interface and cached */
067: Method createMethod = _homeInterface.getMethod("create",
068: new Class[] {});
069: Object beanInstance = createMethod.invoke(_homeInstance,
070: (Object[]) null);
071: _autoCreated = true;
072: return beanInstance;
073: } catch (NoSuchMethodException e) {
074: throw new ControlException(
075: "Cannot auto-create session bean instance because no null argument create() method exists. To use this bean, you will need to call create() directly with the appropriate parameters");
076: } catch (InvocationTargetException e) {
077: _lastException = e.getTargetException();
078:
079: throw new ControlException(
080: "Unable to create session bean instance",
081: _lastException);
082: } catch (Exception e) {
083: throw new ControlException(
084: "Unable to invoke home interface create method", e);
085: }
086: }
087:
088: protected void releaseBeanInstance(boolean alreadyRemoved) {
089: //
090: // For session EJBs, releasing the instance implies the physical
091: // removal of the bean instance. If the bean was auto-created by
092: // the control, it has responsibility for deleting.
093: //
094: if (_beanInstance != null && _autoCreated && !alreadyRemoved) {
095: try {
096: if (EJBObject.class.isAssignableFrom(_beanInterface))
097: ((EJBObject) _beanInstance).remove();
098: else
099: ((EJBLocalObject) _beanInstance).remove();
100: }
101: /* RemoteException or RemoveException */
102: catch (Exception e) {
103: _lastException = e;
104: }
105: }
106:
107: super .releaseBeanInstance(alreadyRemoved);
108: }
109:
110: /**
111: * Set to true if the EJB instance was autocreated as a result of a bean method
112: * call (as opposed to direct invocation of the home create() method).
113: */
114: private boolean _autoCreated = false;
115: }
|