Skip to content

Instantly share code, notes, and snippets.

@tfennelly
Created March 11, 2011 17:06
Show Gist options
  • Save tfennelly/866202 to your computer and use it in GitHub Desktop.
Save tfennelly/866202 to your computer and use it in GitHub Desktop.
public class CDIBeanManagerJNDIDeployer extends AbstractRealDeployer {
private static Logger _logger = Logger.getLogger(CDIBeanManagerJNDIDeployer.class);
private static final String SWITCHYARD_SUB_CONTEXT = "switchyard";
private NameSpaces _nameSpaces;
private Context _beanManagerContext;
/**
* Public default constructor.
*/
public CDIBeanManagerJNDIDeployer() {
setStage(DeploymentStages.PRE_REAL);
}
/**
* Set the Namespaces.
*
* @param nameSpaces Namespaces instance.
*/
public void setNameSpaces(NameSpaces nameSpaces) throws NamingException {
this._nameSpaces = nameSpaces;
this._beanManagerContext = _nameSpaces.getGlobalContext().createSubcontext(SWITCHYARD_SUB_CONTEXT);
}
@Override
protected void internalDeploy(DeploymentUnit unit) throws DeploymentException {
if (isSwitchYardDeployment(unit)) {
unit.visit(new BinderVisitor());
}
}
@Override
protected void internalUndeploy(DeploymentUnit unit) {
if (isSwitchYardDeployment(unit)) {
try {
unit.visit(new UnbinderVisitor());
} catch (DeploymentException e) {
_logger.debug("Deployment error undeploying " + unit.getSimpleName());
}
}
}
private boolean isSwitchYardDeployment(DeploymentUnit unit) {
return (unit.getClassLoader().getResource(AbstractDeployment.SWITCHYARD_XML) != null);
}
private class BinderVisitor implements DeploymentUnitVisitor {
public void visit(DeploymentUnit unit) throws DeploymentException {
try {
String path = unit.getSimpleName() + "/" + unit.getSimpleName();
Context subcontext = Util.createSubcontext(_beanManagerContext, path);
Reference reference = new Reference("javax.enterprise.inject.spi.BeanManager", "org.jboss.weld.integration.deployer.jndi.JBossBeanManagerObjectFactory", null);
reference.add(new StringRefAddr("id", IdFactory.getIdFromClassLoader(unit.getClassLoader())));
subcontext.bind("BeanManager", reference);
} catch (NamingException e) {
throw new DeploymentException("Error binding BeanManager.", e);
}
}
public void error(DeploymentUnit unit) {
// TODO: Any info on the error please?
_logger.debug("Deployment error deploying " + unit.getSimpleName());
}
}
private class UnbinderVisitor implements DeploymentUnitVisitor {
public void visit(DeploymentUnit unit) throws DeploymentException {
try {
String path = unit.getSimpleName() + "/" + unit.getSimpleName();
Context subcontext = (Context) _beanManagerContext.lookup(path);
subcontext.unbind("BeanManager");
_beanManagerContext.destroySubcontext(path);
} catch (NamingException e) {
throw new DeploymentException("Error unbinding BeanManager.", e);
}
}
public void error(DeploymentUnit unit) {
// TODO: Any info on the error please?
_logger.debug("Deployment error undeploying " + unit.getSimpleName());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment