Results 1 to 4 of 4
  1. #1
    rcbandit is offline Member
    Join Date
    Aug 2011
    Rep Power

    Default Help me to fix this code

    I'm new to java programming. I need a help about implementing OSGI module and EJB. I wrote this simple OSGI bundle:

    Java Code:
    package com.cryptoLib;
    public class cryptoSha {
        public cryptoSha() {
            public String stringHash(String hash) throws NoSuchAlgorithmException{
                    MessageDigest md = MessageDigest.getInstance("SHA-256");
                    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();
    This is the Activator class:

    Java Code:
    package com.CL_24;
    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;
    import com.cryptoLib.cryptoSha;
    import org.osgi.framework.ServiceRegistration;
    public class Activator implements BundleActivator {
        private ServiceRegistration reg;
        public void start(BundleContext context) throws Exception {
            cryptoSha ois = new cryptoSha();
            reg = context.registerService(cryptoSha.class.getName(), ois, null);
            System.out.println("Module CL_24 is Loaded");       
        public void stop(BundleContext context) throws Exception {
            System.out.println("Module CL_24 is Unloaded");

    This is the code which calls the OSGI bundle:

    Java Code:
    package com.SR_57;
    import com.CL_24.*;
    import com.cryptoLib.*;
    public class userCheck implements Serializable {
        private String user = null;
        private String password = null;  
           public userCheck(){
           BundleContext context;
           ClassLoader classLoader = cryptoSha.class.getClassLoader();
           Bundle bundle = ((BundleReference) classLoader).getBundle();
           BundleContext load = bundle.getBundleContext();
    The idea is to create a login form which calls OSGI bundle and plain password string is passed to the bundle. The bundle takes the string and converts it to SHA-256 encrypted string. Then the encrypted string is returned to the login bean. I know that it's not a good idea to work on something that you don't know but this is a part of a open source project and this part nobody wants to do it. I donate my free time for this project.

    I use JBoss 7.1.0 for application server and Netbeans 7.1. This is the error stack:

    Java Code:
    com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: com.SR_57.userCheck.
    	at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(
    	at com.sun.faces.mgbean.BeanManager.createAndPush(
    	at com.sun.faces.mgbean.BeanManager.create(
    	at com.sun.faces.el.ManagedBeanELResolver.resolveBean(
    	at com.sun.faces.el.ManagedBeanELResolver.getValue(
    	at com.sun.faces.el.DemuxCompositeELResolver._getValue(
    	at com.sun.faces.el.DemuxCompositeELResolver.getValue(
    	at org.apache.el.parser.AstIdentifier.getValue(
    	at org.apache.el.parser.AstValue.getValue(
    	at org.apache.el.ValueExpressionImpl.getValue(
    	at com.sun.faces.facelets.el.TagValueExpression.getValue(
    	at javax.faces.component.ComponentStateHelper.eval(
    	at javax.faces.component.ComponentStateHelper.eval(
    	at javax.faces.component.UIOutput.getValue(
    	at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(
    	at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(
    	at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(
    	at javax.faces.component.UIComponentBase.encodeEnd(
    	at javax.faces.component.UIComponent.encodeAll(
    	at javax.faces.render.Renderer.encodeChildren(
    	at javax.faces.component.UIComponentBase.encodeChildren(
    	at javax.faces.component.UIComponent.encodeAll(
    	at javax.faces.component.UIComponent.encodeAll(
    	at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(
    	at com.sun.faces.application.view.MultiViewHandler.renderView(
    	at com.sun.faces.lifecycle.RenderResponsePhase.execute(
    	at com.sun.faces.lifecycle.Phase.doPhase(
    	at com.sun.faces.lifecycle.LifecycleImpl.render(
    	at javax.faces.webapp.FacesServlet.service(
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(
    	at org.apache.catalina.core.StandardWrapperValve.invoke(
    	at org.apache.catalina.core.StandardContextValve.invoke(
    	at org.apache.catalina.core.StandardHostValve.invoke(
    	at org.apache.catalina.valves.ErrorReportValve.invoke(
    	at org.apache.catalina.core.StandardEngineValve.invoke(
    	at org.apache.catalina.connector.CoyoteAdapter.service(
    	at org.apache.coyote.http11.Http11AprProcessor.process(
    	at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(
    Caused by: java.lang.ClassCastException: org.jboss.modules.ModuleClassLoader cannot be cast to org.osgi.framework.BundleReference
    	at com.SR_57.userCheck.<init>(
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance(
    	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(
    	at java.lang.reflect.Constructor.newInstance(
    	at java.lang.Class.newInstance0(
    	at java.lang.Class.newInstance(
    	at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(
    	... 42 more
    I will be glad if someone assist me for the OSGI code.
    Write me if you have any questions.

    best wishes

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: Help me to fix this code

    Recommended reading: Use meaningful, specific subject headers.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    London, UK
    Blog Entries
    Rep Power

    Default Re: Help me to fix this code

    Look at the stack trace. Caused by class cast exception.

    Its pointing to line 57 in userCheck class, which you havent included here.

    You should cast like this:
    Java Code:
    MyClass myObj;
    if (obj instanceof MyClass) {
        //safely cast
        myObj = (MyClass) obj;
    } else {
        //handle/log the problem
        myObj = new MyClass();
        System.out.println("Could not cast x to y");
    Last edited by ozzyman; 01-01-2012 at 04:49 PM.

  4. #4
    rcbandit is offline Member
    Join Date
    Aug 2011
    Rep Power

    Default Re: Help me to fix this code

    I have made some corrections to the code. See the attached file: - download page for

    I'm still learning java. I found several tutorials how to declare activation classes and bundles bun I can't find tutorials which explain how to reference a methods insight a bundle. Would you help me with the code. The bean insight the webapp package must reference and pass a string for encryption to the bundle. Then the OSGI bundle must return the encrypted string back to the bean.

    Kind regards Peter

Similar Threads

  1. Replies: 3
    Last Post: 08-10-2011, 09:17 AM
  2. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  3. Replies: 0
    Last Post: 02-21-2011, 12:50 PM
  4. Replies: 1
    Last Post: 03-04-2010, 12:19 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts