Hi

I'm using JBoss to deploy some beans we've previously had deployed on
WebLogic server. These beans access a service which generates primary
keys in a particular way. I'm trying to implement this 'key manager'
as an MBean to be loaded by jboss' JMX architecture. I've written the
service and it seems load into jboss fine, connect to the datasource
fine and I can admin it via that groovy thing on port 8082. I've
written a test client and that doesn't work so well. The KeyManager
interface and client look like this:


KeyManager.java (a generic interface for key managers):
---
package com.lisasoft.core.sql.keygen;

public interface KeyManager extends java.rmi.Remote
{
    public String getOID() throws KeyManagerException, java.rmi.RemoteException;
}
---

KeyManagerTestClient.java (a client to test the key manager):
---
import javax.naming.InitialContext;
import com.lisasoft.core.sql.keygen.KeyManager;
import com.lisasoft.core.sql.keygen.PostgresKeyManagerImpl;

public class KeyManagerTestClient
{
        public static void main( String args[] )
        {
                try
                        {
                                PostgresKeyManagerImpl pkmi = new 
PostgresKeyManagerImpl();
                                pkmi = null;
                                InitialContext ctx = new InitialContext();
                                Object objRef = ctx.lookup( args[0] );
                                PostgresKeyManagerImpl keys = 
(PostgresKeyManagerImpl)objRef;
                                System.out.println( keys.getOID() );
                                System.exit( 0 );
                        }
                catch( Throwable t )
                        {
                                System.out.println( t.getClass().getName() );
                                System.out.println( t.getMessage() );
                                t.printStackTrace();
                        }
        }
}
---

The rest of the code is attached below. When I try to run the test
client with this command line:

java -Djava.naming.factory.initial=org.jnp.interfaces.NamingContext\
                 -Djava.naming.provider.url=192.168.83.54\
                 -Djava.naming.factory.url.pkgs=org.jboss.naming\
                 KeyManagerTestClient keymanager

('keymanager' being the JNDI name I've given it) I get this stack
trace:

javax.naming.NamingException: Could not dereference object.  Root exception is 
java.lang.ClassCastException: com.lisasoft.core.sql.keygen.PostgresKeyManagerImpl
        at 
javax.naming.spi.NamingManager.getObjectFactoryFromReference(NamingManager.java:152)
        at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:305)
        at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:368)
        at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:265)
        at javax.naming.InitialContext.lookup(InitialContext.java:350)
        at KeyManagerTestClient.main(KeyManagerTestClient.java:14)

And if I don't have those first two lines in the test client (which
just instantiate an object then make it ready for garbage collection)
I get almost identical errors but this time a
java.lang.InstantiationException.

Any ideas?

Tom

Other code:

PostgresKeyManagerImplMBean.java:
---
package com.lisasoft.core.sql.keygen;

public interface PostgresKeyManagerImplMBean
        extends org.jboss.util.ServiceMBean
{
        public static final String OBJECT_NAME = ":service=KeyManager";
}
---

PostgresKeyManagerImpl.java (an implementation which talks to
PostgreSQL and operates as an MBean):
---
package com.lisasoft.core.sql.keygen;

import javax.management.*;

import javax.naming.Reference;
import javax.naming.Referenceable;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.NameNotFoundException;
import javax.naming.Name;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;

import javax.sql.DataSource;

import org.jboss.util.ServiceMBeanSupport;

public class PostgresKeyManagerImpl
        extends ServiceMBeanSupport
        implements MBeanRegistration, PostgresKeyManagerImplMBean, Referenceable, 
KeyManager
{
        private String jndiName = null;
        private String datasource = null;

        private byte low_num = 0;

        private byte high_num;

        public PostgresKeyManagerImpl()
        {
        }

        /* Implementation */
        public PostgresKeyManagerImpl( String jndiName, String datasource )
        {
                this.jndiName = jndiName;
                this.datasource = datasource;
        }

        private void refreshHighNum()
        {
                log.log( "High byte of keys being generated from database." );
                try
                        {
                                Connection conn = getConnection();
                                Statement stmt = conn.createStatement();
                                ResultSet rs = stmt.executeQuery
                                        ( "SELECT NEXTVAL( 'KEYMANAGER_SEQUENCE' ) AS 
HIGH_BYTE" );
                                rs.next();
                                log.log( "Old high byte: " + high_num );
                                high_num = (byte)rs.getByte( 1 );
                                log.log( "New high byte: " + high_num );
                        }
                catch( Throwable e )
                        {
                                stopService();
                                log.log( e.getMessage() );
                                log.log( e.toString() );
                        }
        }

        private Connection getConnection()
                throws java.sql.SQLException
        {
                try
                        {
                                InitialContext context = new InitialContext();
                                Object objRef = context.lookup( datasource );
                                DataSource ds = (DataSource)objRef;
                                
                                return (java.sql.Connection)ds.getConnection();
                        }
                catch( java.sql.SQLException sqle )
                        {
                                stopService();
                                log.log( sqle.getMessage() );
                                log.log( sqle.toString() );
                                throw sqle;
                        }
                catch( Throwable e )
                        {
                                stopService();
                                log.log( e.getMessage() );
                                log.log( e.toString() );
                        }
                return null;
        }

  public String getOID()
                throws KeyManagerException, java.rmi.RemoteException
        {
                if( low_num > 127 )
                        {
                                refreshHighNum();
                                low_num = 0;
                        }

                Integer ioid = new Integer( (int)(low_num & (high_num << 4)) );
                return ioid.toString();
        }

        /* MBean junk */

        public ObjectName getObjectName( MBeanServer server, ObjectName name )
        {
                try
                        {
                                return new ObjectName(OBJECT_NAME+",name="+jndiName);
                        }
                catch( javax.management.MalformedObjectNameException mone )
                        {
                                log.log( mone.getMessage() );
                                log.log( mone.toString() );
                        }
                return null;
        }

        public String getName()
        {
                return "KeyManager";
        }

        public void initService()
                throws Exception
        {
                try
                        {
                                // Bind in JNDI
                                bind((Context)(new InitialContext()), jndiName, this);
                        }
                catch( NamingException ne )
                        {
                                log.log( ne.getMessage() );
                                log.log( ne.toString() );
                                return;
                        }

                log.log( "KeyManager " + jndiName + " started and bound." );

                refreshHighNum();
        }

        private void bind(Context ctx, String name, Object val)
                throws NamingException
        {
                // Bind val to name in ctx, and make sure that all intermediate 
contexts exist
      
                Name n = ctx.getNameParser("").parse(name);
                while (n.size() > 1)
      {
                                String ctxName = n.get(0);
                                try
                                        {
            ctx = (Context)ctx.lookup(ctxName);
                                        } catch (NameNotFoundException e)
                                                {
                                                        ctx = 
ctx.createSubcontext(ctxName);
                                                }
                                n = n.getSuffix(1);
      }
      
                ctx.bind(n.get(0), val);
        }

        public void stopService()
        {
                // Unbind from JNDI
                try
      {
                                new InitialContext().unbind(jndiName);
      }
                catch (NamingException e)
      {
                                ;
      }
        }

        /* eferenceable implementation */
        public Reference getReference()
        {
                return new Reference(getClass().getName(), getClass().getName(), null);
        }
}
---


--
--------------------------------------------------------------
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
Problems?:           [EMAIL PROTECTED]

Reply via email to