How to get current EAR location programmatically using JBoss

Does anyone know how to programmatically get the absolute path in the file system for an EAR deployed in JBoss from Java code inside the same EAR?

I need this because I want to copy some files inside the EAR to another part of the file system during deployment.

Thanks everyone!

+7
java jboss seam ear
source share
5 answers

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

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"); } } 
+3
source share

To get a ServletContext from Seam, you can do:

 ServletLifecycle.getCurrentServletContext() 

which is available once Seam has created the applicationContext . And then getRealPath("/") works fine for the root context deployment folder. Any folder structure in the context root can be achieved.

+3
source share

you can do "System.getProperty ()" here is a link for other properties that you can use

Example:

 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/..."

+3
source share

This is pretty complicated, but you can do it by MainDeployer JBoss MainDeployer MBean. The MBean is located in jboss.system:service=MainDeployer and has a JMX listDeployments operation. This returns a collection of DeploymentInfo objects, one of which will be your EAR deployment. This DeploymentInfo property has a url property, which is a file:// URL that describes your deployment directory.

Nice huh? You can use the raw JMX API for this, but Spring provides a much better mechanism by using MBeanProxyFactoryBean to expose an instance of MainDeployerMBean .

I would like to find an easier way, but this is the best I have found so far.

+2
source share

Are these resources mapped or accessible on a web path (under a WAR)?

If so, you can try using ServletContext.getRealPath() to translate the virtual path into the real / file system path.

+1
source share

All Articles