繁体   English   中英

如何从OSGI包中调用方法?

[英]Howto call method from OSGI bundle?

我已经写了这个OSGI包:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CryptoLib;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class cryptoSha {

    public cryptoSha() {
    }

    /** method for converting simple string into SHA-256 hash */
       public String stringHash(String hash) throws NoSuchAlgorithmException{

            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(hash.getBytes());

            byte byteData[] = md.digest();

            /** convert the byte to hex format */
            StringBuilder sb = new StringBuilder();
                for (int i = 0; i < byteData.length; i++) {
            sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
            }              
           return sb.toString();
       }


}

这是Acticator类:

        package com.CL_67;

import CryptoLib.cryptoSha;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {
    private static BundleContext context;   

    public void start(BundleContext context) throws Exception {
        Activator.context = context;
        context.registerService(cryptoSha.class.getName(), new cryptoSha(), null);
        System.out.println("Module CL-67 is Loaded ...");              
    }

    public void stop(BundleContext context) throws Exception {
        context.ungetService(context.getServiceReference(cryptoSha.class.getName()));
        Activator.context = null;
        System.out.println("Module CL-67 is Unloaded ...");      
    }

}

这是EAR包,它调用捆绑包:

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.osgi.framework.BundleContext;

@Named("loginController")
@SessionScoped
public class userCheck extends HttpServlet implements Serializable {

       public userCheck(){
       }

       @WebServlet(name = "CL_67", urlPatterns = {"/CL_67"})
    public class cryptoSha extends HttpServlet {

    @Inject
    cryptoSha stringHash;
}


   @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println(cryptoSha.stringHash("test"));
    }

   }
}       

我成功地编译并部署到了JBoss 7.1.0上,但是当我启动EAR包时什么也没发生。 您可以帮助我找到代码中的错误之处吗?

亲切的问候,彼得

编辑:不幸的是,我是Java编程的新手,并且示例中的某些代码不了解它们是如何工作的。 您愿意帮我做这个例子吗? 我需要看看如何以正确的方式编写此代码,以便将来使用它? 有人会修复代码吗?

先感谢您。 彼得

要点:

  1. 根据您提供的代码,您实际上并没有在捆绑软件中设置OSGi服务。

  2. 在您的servlet中,您实际上并没有使用任何OSGi工具。 您的init方法尝试检索bundleContext,但是您什么也没做。 通常,您会执行以下操作:

     ServiceReference serviceRef = bundleContext.getServiceReference("myService"); 

    然后针对该serviceRef调用。

  3. 您的servlet doGet仅依赖于标准Java对象的创建:

     try { cryptoSha dc = new cryptoSha(); String nhash = dc.stringHash("test"); } catch (NoSuchAlgorithmException ex) { ex.printStackTrace(); } 

    因此,除非cryptoSha以某种方式进入您的耳朵或以其他方式出现在应用程序类路径中,否则我怀疑您在此处遇到了NoClassDefFoundError

    但是,即使您创建cryptoSha ,您也只是试图为String nhash分配一个值,但是您却对此不做任何事情,因此您的servlet确实没有做任何事情。

有一个knopflerfish教程可能会有所帮助: http ://www.knopflerfish.org/osgi_service_tutorial.html

关于这个问题的一切都吓到了货运崇拜节目

如何在同一时间与一些潜水前简单到OSGi和EJB ...开始?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM