有谁知道如何以编程方式从同一 EAR 中的 Java 代码获取部署在 JBoss 中的 EAR 文件系统中的绝对路径?

我需要这个,因为我想在部署时将 EAR 内的一些文件复制到文件系统的另一部分。

谢谢大家!

有帮助吗?

解决方案

我做的这样。结果 EAR具有服务为MyService,其中,I与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");
    }
}

其他提示

你可以做你“System.getProperty()”这里是其他的链接属性可以使用

例如:

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

结果

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

在你只需要添加"/deployments/YOUR_PROJECT_EAR/..."

要从煤层得到ServletContext,你可以这样做:

ServletLifecycle.getCurrentServletContext()

其中,一旦煤层已创建的applicationContext是可用的。然后getRealPath("/")工作正常根上下文的部署文件夹。上下文根内的任何文件夹结构可以到达。

这是相当繁琐的,但你可以通过查询JBoss的MainDeployer的MBean做到这一点。该MBean在jboss.system:service=MainDeployer发现,并具有JMX操作listDeployments。这将返回DeploymentInfo对象,其中之一将是你的EAR部署的集合。这DeploymentInfo有url属性,该属性是描述您的部署目录file:// URL。

尼斯,是吗?您可以使用原始JMX API来做到这一点,但是Spring提供了一个更加美好的机制,使用MBeanProxyFactoryBean暴露MainDeployerMBean的一个实例。

我想找到一个更简单的方法,但这是我到目前为止发现的最好的。

这些资源是否在 Web 路径下(在 WAR 内)映射或可用?

如果是这样,您可以尝试使用 ServletContext.getRealPath() 将虚拟路径转换为真实/文件系统路径。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top