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

    Default Help me to fix this code

    Hi,
    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;
    
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    
    public class cryptoSha {
    
        public cryptoSha() {
        }   
        
            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();
               }
        
        
    }
    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 {
            reg.unregister();
            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.*;
    ........
    
    
    @Named("loginController")
    @SessionScoped
    public class userCheck implements Serializable {
        private String user = null;
        private String password = null;  
        
           public userCheck(){
           }
    
           @Resource
           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(BeanBuilder.java:193)
    	at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:102)
    	at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
    	at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
    	at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)
    	at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)
    	at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    	at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    	at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:71)
    	at org.apache.el.parser.AstValue.getValue(AstValue.java:147)
    	at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
    	at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    	at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    	at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    	at javax.faces.component.UIOutput.getValue(UIOutput.java:169)
    	at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
    	at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355)
    	at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)
    	at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    	at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1763)
    	at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    	at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    	at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1756)
    	at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1759)
    	at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
    	at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    	at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    	at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    	at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    	at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
    	at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:151)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362)
    	at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:897)
    	at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:626)
    	at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:2033)
    	at java.lang.Thread.run(Thread.java:722)
    Caused by: java.lang.ClassCastException: org.jboss.modules.ModuleClassLoader cannot be cast to org.osgi.framework.BundleReference
    	at com.SR_57.userCheck.<init>(userCheck.java:57)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    	at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    	at java.lang.Class.newInstance0(Class.java:372)
    	at java.lang.Class.newInstance(Class.java:325)
    	at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:188)
    	... 42 more
    I will be glad if someone assist me for the OSGI code.
    Write me if you have any questions.

    best wishes
    Peter

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Help me to fix this code

    Recommended reading: Use meaningful, specific subject headers.

    db
    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
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    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 03:49 PM.

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

    Default Re: Help me to fix this code

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

    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, 11:50 AM
  4. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  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
  •