package org.objectweb.jonas_ejb.container;

import java.io.IOException;
import java.io.OutputStream;

import org.objectweb.carol.cmi.ObjectId;
import org.objectweb.util.monolog.api.BasicLevel;

public class JRepStatefulOutputStream extends JStatefulOutputStream {

    public JRepStatefulOutputStream(OutputStream out) throws IOException {
        super(out);
    }
    
    protected Object replaceObject(Object obj) throws IOException {
        Object ret;
        /*if(obj instanceof DataSource) {
        ConnectionManager cm = (ConnectionManager) obj;
        String jndiname = cm.getDSName();
        ret = new JWrapper(JWrapper.DATASOURCE, jndiname);
        } else */
        if (obj instanceof JRepStatefulLocal) {
            // Local stateful -> a wrapper
            TraceEjb.ssfpool.log(BasicLevel.DEBUG, "JRepStatefulLocal");
            JRepStatefulLocal jrsl = (JRepStatefulLocal) obj;
            String jndi = ((JLocalHome) jrsl.getSessionSwitch().getLocal().getEJBLocalHome()).getJndiLocalName();
            ObjectId clusterOId = jrsl.getClusterOId();
            ret = new JRepStatefulObjectId(jndi, clusterOId);
        } else {
            ret = super.replaceObject(obj);
        }
        
        return ret;
    }    
}
