簡體   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