如何使用 JBoss 以编程方式获取当前 EAR 位置

发布于 2024-07-30 09:53:56 字数 132 浏览 6 评论 0原文

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

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

谢谢大家!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

荒岛晴空 2024-08-06 09:53:56

您可以执行“System.getProperty()”,这里是您可以获取的其他属性的链接使用

ex:

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

result

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

之后您只需添加 "/deployments/YOUR_PROJECT_EAR/..."

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

︶葆Ⅱㄣ 2024-08-06 09:53:56

我就是这样做的。
EAR 有一个服务 MyService,我可以在其中处理 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");
    }
}

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");
    }
}
行至春深 2024-08-06 09:53:56

要从 Seam 获取 ServletContext,您可以执行以下操作:

ServletLifecycle.getCurrentServletContext()

一旦 Seam 创建了 applicationContext,该操作就可用。 然后 getRealPath("/") 对于根上下文的部署文件夹来说效果很好。 可以访问上下文根中的任何文件夹结构。

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.

浅笑依然 2024-08-06 09:53:56

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

很好,嗯? 您可以使用原始 JMX API 来执行此操作,但 Spring 提供了更好的机制,使用 MBeanProxyFactoryBean 公开 MainDeployerMBean 的实例。

我想找到一种更简单的方法,但这是迄今为止我发现的最好的方法。

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.

花之痕靓丽 2024-08-06 09:53:56

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文