001: package org.objectweb.jonas_ejb.container;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005: import java.lang.reflect.Method;
006: import java.rmi.RemoteException;
007:
008: import javax.ejb.EJBException;
009:
010: import org.objectweb.util.monolog.api.BasicLevel;
011:
012: public class JRepStatefulInputStream extends JStatefulInputStream {
013:
014: /**
015: * Constructor.
016: * @throws IOException
017: */
018: public JRepStatefulInputStream(InputStream in, JStatefulSwitch jss)
019: throws IOException {
020: super (in, jss);
021: }
022:
023: /**
024: * We must care about some object types.
025: * @param obj serialized field
026: * @return resolved object
027: */
028: protected Object resolveObject(Object obj) throws IOException {
029: Object ret;
030: if (obj instanceof JWrapper) {
031: int type = ((JWrapper) obj).getType();
032: switch (type) {
033: /*case JWrapper.DATASOURCE:
034: String dsname = (String) ((JWrapper) obj).getObject();
035: ret = ConnectionManager.getConnectionManager(dsname);
036: break;*/
037: case JWrapper.LOCAL_ENTITY:
038: TraceEjb.ssfpool.log(BasicLevel.DEBUG, "JEntityLocal");
039: String jndiname = (String) ((JWrapper) obj).getObject();
040: Object pk = ((JWrapper) obj).getPK();
041: Object o = JLocalHome.getLocalHome(jndiname);
042: ret = null;
043: try {
044: ret = getFindByPKMethod(o.getClass(), pk.getClass())
045: .invoke(o, (new Object[] { pk }));
046: } catch (Exception e) {
047: e.printStackTrace();
048: }
049: break;
050: default:
051: ret = super .resolveObject(obj);
052: break;
053: }
054: } else if (obj instanceof JRepStatefulObjectId) {
055: // Wrapper -> Local stateful
056: // Recreate the bean in local
057: JRepStatefulObjectId jrsoi = (JRepStatefulObjectId) obj;
058: JRepStatefulLocalHome jrslh = (JRepStatefulLocalHome) JLocalHome
059: .getLocalHome(jrsoi.getJndi());
060: JRepStatefulLocal jrsl = null;
061:
062: JStatefulSwitch bs = null;
063: JSessionFactory sf = (JSessionFactory) jrslh
064: .getBeanFactory();
065:
066: try {
067: bs = (JStatefulSwitch) sf.createEJB();
068: JSessionContext bctx = sf.getJContext(bs);
069: bs.bindICtx(null, (JStatefulContext) bctx);
070: bctx.setState(2);
071: } catch (javax.ejb.AccessLocalException e) {
072: throw new EJBException(
073: "Security Exception thrown by an enterprise Bean",
074: e);
075: } catch (EJBException e) {
076: throw e;
077: } catch (RuntimeException e) {
078: throw new EJBException(
079: "RuntimeException thrown by an enterprise Bean",
080: e);
081: } catch (Error e) {
082: throw new EJBException(
083: "Error thrown by an enterprise Bean" + e);
084: } catch (RemoteException e) {
085: throw new EJBException("Remote Exception raised:", e);
086: }
087:
088: jrsl = (JRepStatefulLocal) bs.getLocal();
089: jrsl.setClusterOId(jrsoi.getClusterOId());
090: ret = jrsl;
091: } else {
092: ret = super .resolveObject(obj);
093: }
094: return ret;
095: }
096:
097: private Method getFindByPKMethod(Class c, Class pk) {
098:
099: Method method = null;
100: try {
101: Method[] methods = c.getMethods();
102: // Find first findByPrimaryKey method
103: for (int i = 0; i < methods.length; i++) {
104: if (methods[i].getName().equals("findByPrimaryKey")) {
105: method = methods[i];
106: break;
107: }
108: }
109: } catch (Exception e) {
110: e.printStackTrace();
111: }
112:
113: return method;
114: }
115: }
|