/* Generated by Together */
package multimonster.mediaproxy;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import multimonster.common.Action;
import multimonster.common.ConnectionAddress;
import multimonster.common.FormatId;
import multimonster.common.ProtocolId;
import multimonster.common.Session;
import multimonster.common.media.MOIdentifier;
import multimonster.common.pipe.Pipe;
import multimonster.common.resource.ResourceRequestIdentifier;
import multimonster.common.util.EjbCreator;
import multimonster.common.util.EjbHomeGetter;
import multimonster.controller.exceptions.ControllerException;
import multimonster.controller.interfaces.ControllerImpl;
import multimonster.controller.interfaces.ControllerImplHome;
import multimonster.mediaproxy.exceptions.MediaProxyException;
import multimonster.transporter.exceptions.TransporterException;
import multimonster.transporter.interfaces.TransporterImpl;
import multimonster.transporter.interfaces.TransporterImplHome;
import org.apache.log4j.Logger;
/**
*
* @ejb.bean name = "MediaProxyImpl"
* display-name = "MediaProxyFacade SessionBean"
* description = "The Facade of the MediaProxy-Package of MultiMonster"
* view-type = "remote"
* jndi-name = "multimonster/mediaproxy/MediaProxyFacade"
*/
public class MediaProxyImplBean implements MediaProxyFacade, SessionBean {
private SessionContext ctx;
private Context context;
private Logger log;
ControllerImplHome controllerHome;
TransporterImplHome transporterHome;
public void setSessionContext(SessionContext context) throws RemoteException, EJBException {
ctx = context;
}
public void ejbActivate() throws EJBException {
}
public void ejbPassivate() throws EJBException {
}
public void ejbRemove() throws EJBException {
}
/**
* @ejb.create-method
*
* @throws CreateException
* @throws EJBException
*/
public void ejbCreate() throws CreateException, EJBException {
log = Logger.getLogger(this.getClass());
log.debug("EJB created.");
try {
context = new InitialContext();
controllerHome = EjbHomeGetter.getControllerHome(context);
transporterHome = EjbHomeGetter.getTransporterHome(context);
} catch (NamingException e) {
log.error("Couldn't get referenced EJB-Home-Interfaces.");
}
}
/* (non-Javadoc)
* @see multimonster.mediaproxy.MediaProxyFacade#getOutputProxy(multimonster.common.Protocol)
*/
/**
* @ejb.interface-method view-type = "remote"
*/
public ConnectionAddress getOutputProxy(Session session, MOIdentifier mOId, FormatId fId, ProtocolId protocolId) throws MediaProxyException {
log.debug("getOutputProxy() called.");
ConnectionAddress addr = null;
MediaProxyPluginFactory mppFactory = null;
MediaProxyPlugin mpPlugin = null;
String errorText = "";
// check parameters
if (session == null) {
errorText = "session is null - aborting.";
log.error(errorText);
throw new MediaProxyException(errorText);
} else if (mOId == null) {
errorText = "mOId is null - aborting.";
log.error(errorText);
throw new MediaProxyException(errorText);
} else if (fId == null) {
errorText = "fId is null - aborting.";
log.error(errorText);
throw new MediaProxyException(errorText);
} else if (protocolId == null) {
errorText = "protocol is null - aborting.";
log.error(errorText);
throw new MediaProxyException(errorText);
} else {
log.debug("Parameter ok.");
}
// get factory and produce plugin
mppFactory = MediaProxyPluginFactory.getInstance();
mpPlugin = mppFactory.getOutputProxy(session, mOId, protocolId, fId);
addr = mpPlugin.getConnectionAddress();
if (addr == null) {
errorText = "Didn't get ConnectionAddress of produced proxy.";
log.error(errorText);
throw new MediaProxyException(errorText);
}
return addr;
}
/* (non-Javadoc)
* @see multimonster.mediaproxy.MediaProxyFacade#getInputProxy(multimonster.common.Protocol)
*/
/**
* @ejb.interface-method view-type = "remote"
*/
public ConnectionAddress getInputProxy(Session session, MOIdentifier mOId, ProtocolId protocolId) throws MediaProxyException {
log.debug("getInputProxy() called.");
ConnectionAddress addr = null;
MediaProxyPluginFactory mppFactory = null;
MediaProxyPlugin mpPlugin = null;
String errorText = "";
// check parameter
if (session == null) {
errorText = "Parameter session is null - aborting.";
log.error(errorText);
throw new MediaProxyException(errorText);
} else if (mOId == null) {
errorText = "Parameter mOId is null - aborting.";
log.error(errorText);
throw new MediaProxyException(errorText);
} else if (protocolId == null) {
errorText = "Parameter protocol is null - aborting.";
log.error(errorText);
throw new MediaProxyException(errorText);
} else {
log.debug("Parameter ok.");
}
mppFactory = MediaProxyPluginFactory.getInstance();
if (mppFactory == null){
errorText = "Couldn't get PlugInFactory - mppFactory null - aborting.";
log.error(errorText);
throw new MediaProxyException(errorText);
}
mpPlugin = mppFactory.getInputProxy(session, mOId, protocolId);
if (mpPlugin == null){
errorText = "Factory didn't produce Plugin for mOId (" +mOId +") and protocolId (" +protocolId +") - mpPlugin is null - aborting.";
log.error(errorText);
throw new MediaProxyException(errorText);
}
addr = mpPlugin.getConnectionAddress();
if (addr == null) {
errorText = "Didn't get ConnectionAddress of produced proxy.";
log.error(errorText);
throw new MediaProxyException(errorText);
}
return addr;
}
/* (non-Javadoc)
* @see multimonster.mediaproxy.MediaProxyFacade#initOutput(multimonster.common.Session, multimonster.common.MOIdentifier, multimonster.common.Format, multimonster.common.Protocol, multimonster.common.Action)
*/
/**
* @ejb.interface-method view-type = "remote"
*/
public ProxyInitObjects initWork(Session session, MOIdentifier mOId, FormatId fId, ProtocolId protocolId, Action action, boolean isInput) throws MediaProxyException {
log.debug("initWork() called.");
ProxyInitObjects pio = null;
ControllerImpl controller;
TransporterImpl transporter;
boolean authAnswer = false;
ResourceRequestIdentifier rrId = null;
Pipe pipeFromTransporter = null;
String errorText = "";
// parameter check
if (session == null) {
errorText = "session is null - aborting.";
log.error(errorText);
throw new MediaProxyException(errorText);
} else if (mOId == null) {
errorText = "mOId is null - aborting.";
log.error(errorText);
throw new MediaProxyException(errorText);
} else if (fId == null) {
log.debug("FormatId is null - can only do input, otherwise transporter will crash.");
} else if (protocolId == null) {
errorText = "protocol is null - aborting.";
log.error(errorText);
throw new MediaProxyException(errorText);
} else if (action == null) {
errorText = "action is null - aborting.";
log.error(errorText);
throw new MediaProxyException(errorText);
} else {
log.debug("parameter ok.");
}
// trying to get ControllerEJB
try {
controller = EjbCreator.createController(controllerHome, context);
} catch (Exception e) {
errorText = "Couldn't get Controller: " +e.getMessage();
log.error(errorText);
throw new MediaProxyException(errorText);
}
try {
/* authorize request */
authAnswer = controller.authorize(session, mOId, action);
if (authAnswer == false) {
errorText = "Request is not allowed!";
log.warn(errorText);
throw new MediaProxyException(errorText);
}
/* requestRessource */
rrId = controller.requestResources(session, mOId, fId, protocolId, action);
if (rrId == null) {
errorText = "Didn't get a ResourceRequestIdentifier.!";
log.error(errorText);
throw new MediaProxyException(errorText);
}
controller.remove();
} catch (RemoteException e) {
errorText = "Error calling remote object: " +e.getMessage();
log.error(errorText);
throw new MediaProxyException(errorText);
} catch (RemoveException e) {
errorText = "Couldn't remove Controller: " +e.getMessage();
log.error(errorText);
throw new MediaProxyException(errorText);
} catch (ControllerException e) {
errorText = "Error in Controller: " +e.getMessage();
log.error(errorText);
throw new MediaProxyException(errorText);
}
// trying to get TransporterEJB
try {
transporter = EjbCreator.createTransporter(transporterHome, context);
} catch (Exception e) {
errorText = "Couldn't create Transporter: " +e.getMessage();
log.error(errorText);
requestFinished(rrId);
throw new MediaProxyException(errorText);
}
try {
/* get pipe from transporter */
if (isInput) {
pipeFromTransporter = transporter.input(mOId, protocolId);
} else {
pipeFromTransporter = transporter.output(mOId, fId, protocolId);
}
if (pipeFromTransporter == null){
errorText = "Didn't get a pipe from transporter.";
log.error(errorText);
requestFinished(rrId);
throw new MediaProxyException(errorText);
}
transporter.remove();
} catch (RemoteException e) {
errorText = "";
log.error(errorText);
requestFinished(rrId);
throw new MediaProxyException(errorText);
} catch (RemoveException e) {
errorText = "";
log.error(errorText);
requestFinished(rrId);
throw new MediaProxyException(errorText);
} catch (TransporterException e1) {
errorText = "Didn't get Pipe from Transporter: " + e1.getMessage()
+" (rrId: " +rrId.getId() +")";
log.error(errorText);
requestFinished(rrId);
throw new MediaProxyException(errorText);
}
// packing everything together:
pio = new ProxyInitObjects(pipeFromTransporter, rrId);
// pipeSetup finished
pipeFromTransporter.setupFinished();
return pio;
}
/* (non-Javadoc)
* @see multimonster.mediaproxy.MediaProxyFacade#requestFinished(multimonster.common.ResourceRequestIdentifier)
*/
/**
* @ejb.interface-method view-type = "remote"
*/
public void requestFinished(ResourceRequestIdentifier rrId) throws MediaProxyException {
log.debug("requestFinished called.");
ControllerImpl controller = null;
String errorText = "";
// paramter check
if (rrId == null){
errorText = "rrId is null - aborting.";
log.error(errorText);
throw new MediaProxyException(errorText);
}
// trying to get ControllerEJB
try {
controller = EjbCreator.createController(controllerHome, context);
} catch (Exception e) {
errorText = "Couldn't get Controller: " +e.getMessage();
log.error(errorText);
throw new MediaProxyException(errorText);
}
try {
/* release Resources */
controller.releaseResource(rrId);
controller.remove();
} catch (RemoteException e1) {
errorText = "Error calling remote Object: " +e1.getMessage();
log.error(errorText);
throw new MediaProxyException(errorText);
} catch (RemoveException e) {
errorText = "Couldn't remove Controller: " +e.getMessage();
log.error(errorText);
throw new MediaProxyException(errorText);
} catch (ControllerException e) {
errorText = "Couldn't remove Controller: " +e.getMessage();
log.error(errorText);
throw new MediaProxyException(errorText);
}
return;
}
/**
* @ejb.interface-method view-type = "remote"
*/
public void directInput(byte[] file, String link) {
log.warn("This method isn't implemented anymore! Use real InputProxy.");
}
}