简体   繁体   中英

Howto call method from OSGI bundle?

I have written this OSGI bundle:

/*
 * 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:

        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:

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

  2. In your servlet, you're not actually using any OSGi facilities. Your init method tries to retrieve the bundleContext, but then you don't ever do anything with it. Typically you'd do something like this:

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

    and then invoke against that serviceRef .

  3. Your servlet doGet is just relying on standard Java object creation:

     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.

    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.

There is a knopflerfish tutorial that may help: 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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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