Howto call method from OSGI bundle?
rcbandit Dec 29, 2011 3:22 PMI 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.CL77;
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;
}
public void stop(BundleContext context) throws Exception {
Activator.context = null;
}
}
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(){
}
private BundleContext bundleContext;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
bundleContext = (BundleContext) config.getServletContext().getAttribute("osgi-bundlecontext");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
cryptoSha dc = new cryptoSha();
String nhash = dc.stringHash("test");
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
}
}
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