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:
From the code you’ve supplied, you haven’t really set up an OSGi service in your bundle.
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:
and then invoke against that
serviceRef.Your servlet
doGetis just relying on standard Java object creation:So unless
cryptoShais somehow part of your ear or otherwise in the application classpath, I suspect you’re getting aNoClassDefFoundErrorhere.But even if you create
cryptoSha, you are just trying to assign a value toString 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