8

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!

ptdev
  • 453
  • 1
  • 3
  • 20

5 Answers5

4

you can do you "System.getProperty()" here is the link for other the properties you can used

ex:

String jBossPath = System.getProperty("jboss.server.base.dir")

result

"/Users/ALL_THE_PATH/JBoss_7-1/standelone"

After you just need to add "/deployments/YOUR_PROJECT_EAR/..."

douarbou
  • 2,283
  • 1
  • 21
  • 25
3

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
  • 395
  • 1
  • 3
  • 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 Aug 06 '09 at 14:12
3

To get the ServletContext from Seam, you can do:

ServletLifecycle.getCurrentServletContext()

which is available as soon as Seam has created the applicationContext. And then getRealPath("/") works fine for root context's deployment folder. Any folder structure within context root can be reached.

tugcem
  • 1,078
  • 3
  • 14
  • 25
sylvain
  • 31
  • 2
2

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
  • 398,947
  • 96
  • 818
  • 769
  • 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 Aug 06 '09 at 14:14
1

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
  • 138,234
  • 66
  • 282
  • 345
  • 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 Aug 06 '09 at 14:16