0 Replies Latest reply on Dec 4, 2001 12:59 PM by Richard Rose

    An Example of CMP2 for Sybase

    Richard Rose Newbie

      After spending a day porting from Weblogic 6.1 to JBOSS3alpha, I though some of you may appreciate this example :-

      Source code for simple Sybase CMP2 example - Sample

      ============================================== Logger.java
      package cmptest;

      // shade of Logger, just for test

      public class Logger
      {
      public static Logger getLogger( String className )
      {
      // comes from cache in real implementation

      return new Logger();
      }

      public void debug( String method, String msg )
      {
      System.out.println( method + ":" + msg );
      }
      }

      ============================================== Sample.java
      package cmptest;

      import javax.ejb.EJBObject;
      import java.rmi.RemoteException;
      import javax.ejb.EJBException;

      import java.sql.Date;

      public interface Sample extends EJBObject
      {
      public Date getStmtDate() throws RemoteException, EJBException;
      public String getStmtNumber() throws RemoteException, EJBException;
      public String getPageState() throws RemoteException, EJBException;
      public int getNumPages() throws RemoteException, EJBException;
      public String getState() throws RemoteException, EJBException;
      }


      ============================================== SampleBean.java
      package cmptest;

      import javax.naming.NamingException;
      import javax.naming.Context;
      import javax.naming.InitialContext;

      import javax.ejb.EntityBean;
      import javax.ejb.EntityContext;
      import javax.ejb.EJBException;
      import javax.ejb.CreateException;
      import javax.ejb.FinderException;

      import javax.rmi.PortableRemoteObject;

      import java.rmi.RemoteException;

      import java.sql.Date;
      import java.sql.SQLException;

      abstract public class SampleBean implements EntityBean
      {
      private transient Logger m_logr;
      private transient EntityContext ctx;


      public void setEntityContext( EntityContext context ) throws RemoteException, EJBException
      {
      ctx = context;
      m_logr = Logger.getLogger( SampleBean.class.getName() );
      }

      public void unsetEntityContext() throws RemoteException, EJBException
      {
      ctx = null;
      m_logr = null;
      }

      public void ejbActivate() throws RemoteException, EJBException
      {
      m_logr.debug( "ejbActivate", "CALLED" );
      }

      public void ejbPassivate() throws RemoteException, EJBException
      {
      m_logr.debug( "ejbPassivate", "CALLED" );
      }

      public void ejbRemove() throws RemoteException, EJBException
      {
      m_logr.debug( "ejbRemove", "CALLED" );
      }

      public void ejbStore() throws RemoteException, EJBException
      {
      m_logr.debug( "ejbStore", "CALLED " + id() + " " + getPageState() );
      }

      public void ejbLoad() throws RemoteException, EJBException
      {
      m_logr.debug( "ejbLoad", "CALLED " + id() + " " + getPageState() );
      }

      public abstract String getClrAgentBIC();
      public abstract void setClrAgentBIC( String param );

      public abstract String getSafeAccount();
      public abstract void setSafeAccount( String param );

      public abstract Date getStmtDate();
      public abstract void setStmtDate( Date param );

      public abstract String getStmtNumber();
      public abstract void setStmtNumber( String param );

      public abstract String getPageState();
      public abstract void setPageState( String param );

      public abstract int getNumPages();
      public abstract void setNumPages( int param );

      public abstract String getState();
      public abstract void setState( String param );


      public SamplePK ejbCreate( String clrAgentBIC, String safeAccount )

      throws CreateException, EJBException, RemoteException, SQLException
      {
      m_logr.debug( "ejbCreate", "CALLED " + System.identityHashCode(this) +
      ", agentBIC=" + clrAgentBIC + ", safeAcc=" + safeAccount );

      setClrAgentBIC( clrAgentBIC );
      setSafeAccount( safeAccount );

      return null;
      }

      private String id()
      {
      return "" + System.identityHashCode(this) + ", PK = " +
      (String) ((ctx == null)
      ? "nullctx"
      : ((ctx.getPrimaryKey() == null
      ? "null"
      : ctx.getPrimaryKey().toString())));
      }

      public void ejbPostCreate( String clrAgentBIC, String safeAccount )

      throws CreateException, EJBException, RemoteException, SQLException
      {
      }
      }


      ============================================== SampleHome.java

      package cmptest;

      import javax.ejb.EJBHome;
      import javax.ejb.CreateException;
      import javax.ejb.EJBException;
      import java.rmi.RemoteException;
      import java.sql.SQLException;
      import javax.ejb.FinderException;

      public interface SampleHome extends EJBHome
      {
      Sample create( String clrAgentBIC, String safeAccount ) throws CreateException, EJBException,
      RemoteException, SQLException;

      Sample findByPrimaryKey(SamplePK param0) throws EJBException, FinderException, RemoteException ;
      }



      ============================================== SamplePK.java
      package cmptest;

      import java.io.Serializable;

      public class SamplePK implements Serializable
      {
      public SamplePK()
      {
      }

      public SamplePK( String clrAgentBIC, String safeAccount )
      {
      // remove padding from DB

      this.clrAgentBIC = clrAgentBIC;
      this.safeAccount = safeAccount;
      }

      public String toString()
      {
      return "SamplePK agent=" + clrAgentBIC + ", safeAcc=" + safeAccount;
      }

      public int hashCode()
      {
      return clrAgentBIC.hashCode() + safeAccount.hashCode();
      }

      public boolean equals(Object o)
      {
      if ( o instanceof SamplePK )
      {
      SamplePK otherKey = ( SamplePK )o;
      return ( clrAgentBIC.equals( otherKey.clrAgentBIC ) && safeAccount.equals( otherKey.safeAccount ) );
      }
      else
      return false;
      }

      public String clrAgentBIC;
      public String safeAccount;
      }


      ============================================== SampleTest.java
      package cmptest;

      import javax.naming.NamingException;
      import javax.naming.Context;
      import javax.naming.InitialContext;

      import javax.rmi.PortableRemoteObject;

      import junit.framework.Assert;
      import junit.framework.TestCase;


      public class SampleTest extends TestCase
      {
      protected static final Logger m_logr = Logger.getLogger( SampleTest.class.getName() );

      public SampleTest( String name )
      {
      super( name );
      }

      private void runTest( String key1, String key2 ) throws Exception
      {
      Context initCtx = new InitialContext();

      Object obj = ( SampleHome ) initCtx.lookup( "cmptest.SampleHome" );
      SampleHome home = ( SampleHome ) PortableRemoteObject.narrow( obj, SampleHome.class );

      Sample sample = home.create( key1, key2 );
      }

      public void test()
      {
      try
      {
      runTest( "ABC", "DEF" );
      }
      catch ( java.lang.Throwable t )
      {
      m_logr.debug( "test", t.getMessage() );

      t.printStackTrace();

      assertTrue( false );
      }
      }

      public static void main( String[] args )
      {
      junit.textui.TestRunner.run( SampleTest.class );
      }
      }


      ============================================== client.policy
      grant {
      // Allow everything for now
      permission java.security.AllPermission;
      };


      ============================================== jndi.properties
      java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
      java.naming.factory.url.pkgs=org.jnp.interfaces
      java.naming.provider.url=localhost:8112


      ============================================== META-INF/ejb-jar.xml
      <?xml version="1.0"?>

      <!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 'http://java.sun.com/j2ee/dtds/ejb-jar_2_0.dtd'>
      <ejb-jar>
      <enterprise-beans>

      <ejb-name>SampleBean</ejb-name>
      cmptest.SampleHome
      cmptest.Sample
      <ejb-class>cmptest.SampleBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>cmptest.SamplePK</prim-key-class>
      True
      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>SampleBean</abstract-schema-name>
      <cmp-field>
      <field-name>clrAgentBIC</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>safeAccount</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>stmtDate</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>stmtNumber</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>pageState</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>numPages</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>state</field-name>
      </cmp-field>

      </enterprise-beans>
      <assembly-descriptor>
      <container-transaction>

      <ejb-name>SampleBean</ejb-name>
      <method-name>*</method-name>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      </assembly-descriptor>
      </ejb-jar>


      ============================================== META-INF/jboss.xml
      <?xml version="1.0" encoding="Cp1252"?>



      false

      <enterprise-beans>

      <ejb-name>SampleBean</ejb-name>
      <jndi-name>cmptest.SampleHome</jndi-name>

      </enterprise-beans>

      <resource-managers>
      <resource-manager res-class="org.jboss.ejb.deployment.JDBCResource">
      <res-name>openItemDSRef</res-name>
      <res-jndi-name>java:/OpenItemDS</res-jndi-name>
      </resource-manager>
      </resource-managers>





      ============================================== META-INF/jbosscmp-jdbc.xml
      <?xml version="1.0" encoding="ISO-8859-1"?>

      <jbosscmp-jdbc>
      <enterprise-beans>

      <ejb-name>SampleBean</ejb-name>
      <table-name>Sample</table-name>
      <cmp-field>
      <field-name>clrAgentBIC</field-name>
      <column-name>clrAgentBIC</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>safeAccount</field-name>
      <column-name>safeAccount</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>stmtDate</field-name>
      <column-name>stmtDate</column-name>
      <jdbc-type>DATE</jdbc-type>
      <sql-type>DATETIME</sql-type>
      </cmp-field>
      <cmp-field>
      <field-name>stmtNumber</field-name>
      <column-name>stmtNumber</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>pageState</field-name>
      <column-name>pageState</column-name>
      <jdbc-type>LONGVARCHAR</jdbc-type>
      <sql-type>TEXT</sql-type>
      </cmp-field>
      <cmp-field>
      <field-name>numPages</field-name>
      <column-name>numPages</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>state</field-name>
      <column-name>state</column-name>
      </cmp-field>

      </enterprise-beans>
      </jbosscmp-jdbc>

      NOTE THE jdbc-type & sql-type for DATE & TEXT types


      ============================================== deploy/sybase-service.xml

      <?xml version="1.0" encoding="UTF-8"?>



      <!-- put jconn2.jar from the Sybase jConnect 5.x into the JBoss lib/ext
      directory !!! -->



      ConnectionURL=jdbc:sybase:Tds:bert:2211
      DriverClass=com.sybase.jdbc2.jdbc.SybDriver
      UserName=grr
      Password=ugg

      OpenItemDS
      java:/TransactionManager
      <mbean-ref name="ResourceAdapterName">JCA:service=RARDeployment,name=Minerva JDBC LocalTransaction ResourceAdapter</mbean-ref>
      <mbean-ref name="ConnectionManagerFactoryLoaderName">JCA:service=ConnectionManagerFactoryLoader,name=MinervaSharedLocalCMFactory</mbean-
      ref>
      #
      #Wed Aug 15 16:17:29 EDT 2001
      MinSize=0
      MaxSize=10
      BlockingTimeoutMillis=30000
      IdleTimeoutMinutes=30
      CleanupIntervalMinutes=10
      MaxIdleTimeoutPercent=1.0


      org.jboss.resource.security.ManyToOnePrincipalMapping






      ============================================== deploy/standardjbosscmp-jdbc.xml

      EDIT THIS FILE

      <jbosscmp-jdbc>

      java:/OpenItemDS
      <type-mapping>Sybase</type-mapping>

      ........... REST AS NORMAL ..................