package org.torquebox.base.deployers;

import org.jboss.beans.metadata.spi.builder.BeanMetaDataBuilder;
import org.jboss.deployers.spi.DeploymentException;
import org.jboss.deployers.spi.deployer.DeploymentStages;
import org.jboss.deployers.spi.deployer.helpers.AbstractDeployer;
import org.jboss.deployers.structure.spi.DeploymentUnit;
import org.torquebox.RubyApplication;
import org.torquebox.RubyApplicationMBean;
import org.torquebox.base.metadata.RubyApplicationMetaData;
import org.torquebox.mc.AttachmentUtils;
import org.torquebox.mc.jmx.JMXUtils;

public class RubyApplicationDeployer extends AbstractDeployer {
    
    public RubyApplicationDeployer() {
        setInput(RubyApplicationMetaData.class);
        setStage( DeploymentStages.REAL );
    }

    @Override
    public void deploy(DeploymentUnit unit) throws DeploymentException {
        System.err.println( "DEPLOY RUBY APP: " + unit );
        RubyApplicationMetaData rubyAppMetaData = unit.getAttachment( RubyApplicationMetaData.class );
        String beanName = AttachmentUtils.beanName( unit, RubyApplication.class );
        BeanMetaDataBuilder builder = BeanMetaDataBuilder.createBuilder( beanName, RubyApplication.class.getName() );
        builder.addPropertyMetaData( "name", rubyAppMetaData.getApplicationName() );
        builder.addPropertyMetaData( "environmentName", rubyAppMetaData.getEnvironmentName() );
        builder.addPropertyMetaData( "rootPath", rubyAppMetaData.getRootPath() );
        
        //String mbeanName = JMXUtils.jmxName( "torquebox.apps", rubyAppMetaData.getApplicationName() ).with("foo", "bar" ).name();
        String mbeanName = JMXUtils.jmxName( "torquebox.apps", rubyAppMetaData.getApplicationName() ).name();
        String jmxAnno = "@org.jboss.aop.microcontainer.aspects.jmx.JMX(name=\""+ mbeanName + "\", exposedInterface=" + RubyApplicationMBean.class.getName() + ".class)";
        builder.addAnnotation( jmxAnno );
        
        AttachmentUtils.attach( unit, builder.getBeanMetaData() );
    }
    
    

}

@@ -743,16 +743,20 @@
 tStages.
+PRE_
 REAL );%0A
@@ -1487,140 +1487,8 @@
    %0A
-        //String mbeanName = JMXUtils.jmxName( %22torquebox.apps%22, rubyAppMetaData.getApplicationName() ).with(%22foo%22, %22bar%22 ).name();%0A
     

Move RubyApplicationDeployer to PRE_REAL to see if that helps with torquebox.apps not always being available via jmx.