简体   繁体   English

如何从OSGI包中调用方法?

[英]Howto call method from OSGI bundle?

I have written this 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();
       }


}

And this is the Acticator class: 这是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 ...");      
    }

}

This is the EAR package which calls the bundle: 这是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"));
    }

   }
}       

I successfully compile and deploy then on JBoss 7.1.0 but when I start the EAR package nothing happens. 我成功地编译并部署到了JBoss 7.1.0上,但是当我启动EAR包时什么也没发生。 Can you help me to find where I'm wrong in the code? 您可以帮助我找到代码中的错误之处吗?

kind regards, Peter 亲切的问候,彼得

EDIT: Unfortunately I'm new to java programming and some of the code in the examples I don't understand how they work. 编辑:不幸的是,我是Java编程的新手,并且示例中的某些代码不了解它们是如何工作的。 Would you be so kind to help me with the example. 您愿意帮我做这个例子吗? I need to see how this code must be written in proper way in order to use it in future time? 我需要看看如何以正确的方式编写此代码,以便将来使用它? Would someone repair the code? 有人会修复代码吗?

Thank you in advance. 先感谢您。 Peter 彼得

A couple of points: 要点:

  1. From the code you've supplied, you haven't really set up an OSGi service in your bundle. 根据您提供的代码,您实际上并没有在捆绑软件中设置OSGi服务。

  2. In your servlet, you're not actually using any OSGi facilities. 在您的servlet中,您实际上并没有使用任何OSGi工具。 Your init method tries to retrieve the bundleContext, but then you don't ever do anything with it. 您的init方法尝试检索bundleContext,但是您什么也没做。 Typically you'd do something like this: 通常,您会执行以下操作:

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

    and then invoke against that serviceRef . 然后针对该serviceRef调用。

  3. Your servlet doGet is just relying on standard Java object creation: 您的servlet doGet仅依赖于标准Java对象的创建:

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

    So unless cryptoSha is somehow part of your ear or otherwise in the application classpath, I suspect you're getting a NoClassDefFoundError here. 因此,除非cryptoSha以某种方式进入您的耳朵或以其他方式出现在应用程序类路径中,否则我怀疑您在此处遇到了NoClassDefFoundError

    But even if you create cryptoSha , you are just trying to assign a value to String nhash , but then you don't do anything with it, so your servlet is indeed doing nothing. 但是,即使您创建cryptoSha ,您也只是试图为String nhash分配一个值,但是您却对此不做任何事情,因此您的servlet确实没有做任何事情。

There is a knopflerfish tutorial that may help: http://www.knopflerfish.org/osgi_service_tutorial.html 有一个knopflerfish教程可能会有所帮助: http ://www.knopflerfish.org/osgi_service_tutorial.html

Everything about this question screams cargo cult programming . 关于这个问题的一切都吓到了货运崇拜节目

How about starting with something simple before diving into OSGi and EJB... at the same time ? 如何在同一时间与一些潜水前简单到OSGi和EJB ...开始?

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

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