tags:

views:

1056

answers:

3

Hi there!

Does anyone know how to get programmatically the absolute path in the filesystem for an EAR deployed in JBoss, from Java code within that same EAR?

I need this because I want to copy some files that are inside the EAR to another part of the filesystem, on deploy-time.

Thank you everyone!

+1  A: 

This is quite fiddly, but you can do this by querying the JBoss MainDeployer MBean. The MBean is found at jboss.system:service=MainDeployer, and has a JMX operation listDeployments. This returns a collection of DeploymentInfo objects, one of which will be your EAR deployment. That DeploymentInfo has a url property which is a file:// URL describing your deployment directory.

Nice, eh? You can use the raw JMX API to do this, but Spring provides a much nicer mechanism, using a MBeanProxyFactoryBean to expose an instance of MainDeployerMBean.

I'd like to find a simpler way, but that's the best I've found so far.

skaffman
Thanks for your help! It didn't work in my case however: I'm using SeamFramework together with JBoss, and I need this code to run from a method that is annotated as @Observer("org.jboss.seam.postInitialization"), which is called when a Seam application starts. At this point I would get a MainDeployerMBean that reported: 0 deployed EARs, 0 incomplete and 0 waiting for deploy...
ptdev
+1  A: 

Are these resources mapped or made available under a web path (within a WAR)?

If so, you could attempt to use ServletContext.getRealPath() to translate the virtual path to the real/filesystem path.

matt b
Thanks for your help! It didn't work in my case however: As I stated in another comment, I'm using this in Seam startup. I couldn't find a way to get a ServletContext at this point (it would return as null).
ptdev
+1  A: 

I do this way.
EAR has a service MyService, where I work with EAR contents:

import org.jboss.system.ServiceControllerMBean;
import org.jboss.system.ServiceMBeanSupport;

public class MyService extends ServiceMBeanSupport {

    public void workWithEar() 
    {
        ServiceControllerMBean serviceController = (ServiceControllerMBean) MBeanProxy.get(
                    ServiceControllerMBean.class,
                    ServiceControllerMBean.OBJECT_NAME, server);
        // server is ServiceMBeanSupport member

        ClassLoader cl = serviceController.getClass().getClassLoader();

        String path = cl.getResource("META-INF/jboss-service.xml").getPath()
        InputStream file = cl.getResourceAsStream("META-INF/jboss-service.xml");
    }
}
avro
I could use a simpler way, since the class where I need that path, I'm within the EAR that holds the WAR that contains the files I want to copy.So, I just needed 1 line of code:String path = this.getClass().getClassLoader().getResource("my_war_filename.war").getPath();Thanks!
ptdev