【问题标题】:how to load class from jar inside equinox server side application in jboss 7如何在 jboss 7 中从 Equinox 服务器端应用程序中的 jar 加载类
【发布时间】:2013-05-25 19:02:36
【问题描述】:

几天以来我一直面临一个问题,但我无法找到解决方案。以下是我的应用结构:

我在 MyearDeployedOnJboss7.ear 中有 ejbapp.jar 与 equinox-server-side-app.war 相同级别(使用 warproduct 构建),我想从 iModernizeWebClient_1.0.0.jar 中的 MyJarToLaoadForEjbapp.jar 加载类在 Equinox-server-side-app.war 的 plugins 文件夹中(我想显示应用结构的图像,但我无法发送图像,因为论坛规则需要 10 分才能做到这一点)

我的问题是如何允许 ejbapp.jar 从位于 equinox-server-side-app.war 中的 MyWebClient_1.0.0.jar 的插件文件夹中的“MyJarToLaoadForEjbapp.jar”加载类。

我想使用 servletbridge 类加载器,但不知道如何使用它。

在我的 launch.ini 中我有:

osgi.*=@null org.osgi.*=@null eclipse.*=@null osgi.parentClassloader=app osgi.contextClassLoaderParent=app

【问题讨论】:

    标签: servlets jboss classloader equinox bridge


    【解决方案1】:

    我使用 OSGI 规范中的 Servlet HttpServiceTracker 解决了我的问题。怎么做:写HttpServiceTracker liket:

    public class HttpServiceTracker extends ServiceTracker {
    
    private static final Logger logger = Logger
            .getLogger(HttpServiceTracker.class.getName());
    
    
    public HttpServiceTracker(BundleContext context) {
        super(context, HttpService.class.getName(), null);
    }
    
    public Object addingService(ServiceReference reference) {
        HttpService httpService = (HttpService) context.getService(reference);
        logger.info("default context path : "
                + org.eclipse.rap.ui.internal.servlet.HttpServiceTracker.ID_HTTP_CONTEXT);
        try {
            logger.info("will register servlet ");
            httpService.registerServlet("/programLauncherServlet",
                    new ProgramLauncherServlet(), null, null);
            logger.info("servlet has been registred with http context ");
            // httpService.registerResources( "/", "/html", null );
        } catch (Exception e) {
            //e.printStackTrace();
            logger.info("The alias '/programLauncherServlet' is already in use");
        }
    
        return httpService;
    }
    
    public void removedService(ServiceReference reference, Object service) {
        logger.info("will unregister servlet ");
        HttpService httpService = (HttpService) service;
        httpService.unregister("/programLauncher");
        super.removedService(reference, service);
        logger.info("servlet has been unregistred");
    }
    

    在方法开始处的插件激活器类中:

    @Override
    public void start(BundleContext context) throws Exception {
        super.start(context);
        Activator.plugin = this;
    
        BundleContext osgiContext = BundleReference.class
                .cast(AnyClassOfYourProject.class.getClassLoader()).getBundle()
                .getBundleContext();
        serviceTracker = new HttpServiceTracker(osgiContext);
        serviceTracker.open();
        LOGGER.info("servlet published !!");
        LOGGER.info("Bundle started.");
    }
    

    以及在 stop 方法中取消注册 servlet:

    public void stop(BundleContext context) throws Exception {
        Activator.plugin = null;
        serviceTracker.close();
        serviceTracker = null;
        LOGGER.info("servlet unregistered from context !!");
        super.stop(context);
    }
    

    就是这样。您的 servlet 可以在 eclipse 包之外访问,您可以在包中调用方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-24
      • 1970-01-01
      • 1970-01-01
      • 2018-03-23
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      • 2013-01-02
      相关资源
      最近更新 更多