db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Robert Sfeir <rob...@codepuccino.com>
Subject Re: Tutorial - How to connect OJB and Struts
Date Tue, 11 May 2004 14:32:42 GMT
Just spoke with Brian, you have my permission to remove the license  
info... as long as *I* don't end up getting stuck not being able to use  
it in my own code :-)

R
On May 11, 2004, at 10:03 AM, Robert Sfeir wrote:

> Could someone post that somewhere, check it in, or what ever?
>
> How to connect OJB and Struts.
>
> The easiest way to go about connecting Struts and OJB is to use  
> Strut's built in PlugIn API.  By doing so, you can write code which  
> will allow you to connect to Struts and minimize the juggling of the  
> various APIs.
>
> The following are quick steps to getting a connection.
>
> First things first - The BaseOJBConnector class.
> The following class gives you methods to initiate and destroy a  
> PersistenceBroker.  These methods will be called by your framework, in  
> this case Struts - but can also be used with Spring, to initialize the  
> parameters OJB requires when making its connection, and getting  
> PersistenceBroker instances.
>
> BaseOJBConnector.java
>
> /**
>  * CODEPUCCINO Development Suite IS COPYRIGHTED (c) 2002 - 2004 BY  
> CODEPUCCINO.
>  * This includes, Tracker, Live, Security, Knowledge, Support,
>  * Blog, Forum, Project and any future product(s) created by  
> CODEPUCCINO under this
>  * GPL license.
>  *
>  * This is FREE SOFTWARE.
>  * It is provided under the GNU General Public License (GPL) wherein  
> "program"
>  * shall refer to this digital map. You may use, distribute, or modify  
> this software
>  * only if you agree to all of the terms and conditions of the GPL,
>  * and if this licensing statement accompanies the data.
>  * Read the full text of the GPL from the following sources:
>  *
>  * http://www.gnu.org/licenses/gpl.txt
>  *
>  * Free Software Foundation, Inc.,
>  * 59 Temple Place - Suite 330,
>  * Boston, MA 02111-1307
>  *
>  * Informal discussion: See the License for governing terms and  
> conditions.
>  * Informally, this is a "copyleft" item that is free to use however  
> you wish
>  * (including incorporation into commercial products or derivative  
> works for sale)
>  * as long as you pass it on in a way that perpetuates GNU freedom of  
> usage,
>  * which means all derived works also must be made available under the  
> GPL.
>  *
>  * This program is distributed in the hope that it will be useful,
>  * but WITHOUT ANY WARRANTY; without even the implied warranty of  
> MERCHANTABILITY
>  * or FITNESS FOR A PARTICULAR PURPOSE.
>  *
>  * See the GNU General Public License for more details.
>  *
>  * Contact Information:
>  *
>  * Robert S. Sfeir
>  * codepuccino.com
>  * robert@codepuccino.com
>  */
>
> package com.codepuccino.utilities;
>
> import org.apache.ojb.broker.PBKey;
> import org.apache.ojb.broker.PersistenceBroker;
> import org.apache.ojb.broker.PersistenceBrokerException;
> import org.apache.ojb.broker.PersistenceBrokerFactory;
>
> import java.io.Serializable;
> import java.util.logging.Level;
> import java.util.logging.Logger;
>
>
> /**
>  * Base OJB Connector to startup and open OJB Connections.  The  
> connector opens up a PersistenceBroker connection.  The
>  * ODMG connection is no longer supported due to performance issues  
> with ODMG in OJB.
>  * <p/>
>  * There are a couple of ways to open the connection, the first is to  
> use the initConnection() method, the other is to
>  * extend this class using which ever framework you're using and  
> calling the initConnection() method where ever it's
>  * appropriate in your code.  As an example with Struts, extending  
> this class and implementing the Struts PlugIn
>  * Interface, you can start the PersistenceBroker on Struts startup by  
> simply adding the initConnection() to the init()
>  * method of the PlugIn impl, and passing it the value of the plugin  
> parameter.
>  * <p/>
>  * To destroy a PersistenceBroker connection, call the  
> destroyConnection() method, which first closes the
>  * PersistenceBroker, then cleans up all data in the various methods  
> in the class.  Use this method also with Struts in
>  * the destro() method of the PlugIn Interface so the connection will  
> properly close on Struts app shutdown or reload.
>  *
>  * @author <a href='mailto:robert@codepuccino.com'>Robert S. Sfeir</a>
>  * @version $Revision: 1.18 $
>  * @since 1.0
>  */
> public class BaseOJBConnector
>     implements Serializable
> {
>
>   private static PBKey brokerKey = null;
>   private static String ojbDBAlias = null;
>   protected static final Logger LOGGER = Logger.getLogger(  
> BaseOJBConnector.class.getName() );
>
>   public BaseOJBConnector()
>   {
>   }
>
>   /**
>    * gets the OjbDBAlias, this is needed in order to know which  
> database connection you want to open.  Be sure to
>    * setOjbDBAlias first, it is private and is called by the openDB().
>    *
>    * @return OJB DB Alias String
>    */
>   private static String getOjbDBAlias()
>   {
>     return ojbDBAlias;
>   }
>
>   /**
>    * Set the OjbDBAlias to let the openDB() method know what to open.   
> This is used by initConnection().
>    *
>    * @param ojbDBAlias
>    */
>   private static void setOjbDBAlias( final String ojbDBAlias )
>   {
>     LOGGER.log( Level.INFO, "OJB DB ALIAS set to: " + ojbDBAlias );
>     BaseOJBConnector.ojbDBAlias = ojbDBAlias;
>   }
>
>   /**
>    * Whenever you're persisting or un-persisting an object you will  
> need to get a PersistenceBroker.  Call this method
>    * to get the broker instance.  Be sure not to close the broker  
> after this call, rather either commit or abort the
>    * trasaction to return the connection to the broker pool.
>    *
>    * @return PersistenceBroker instance
>    */
>   public static PersistenceBroker getBroker()
>   {
>     return PersistenceBrokerFactory.createPersistenceBroker(  
> getBrokerKey() );
>   }
>
>
>   /**
>    * Gets the broker key we asked for based on the OJB Alias.  Private  
> method used by openDB().
>    *
>    * @return PBKey brokerKey
>    */
>   private static PBKey getBrokerKey()
>   {
>     return brokerKey;
>   }
>
>   /**
>    * Set the PBKey we want to work with and instantiate.  Private  
> method used by openDB().
>    *
>    * @param theBrokerKey
>    */
>   private static void setBrokerKey( final PBKey theBrokerKey )
>   {
>     brokerKey = theBrokerKey;
>   }
>
>
>   /**
>    * Convenience method to open a PersistenceBroker connection.  Call  
> this method to start things up.
>    *
>    * @param databaseAliasName
>    */
>   public final static void initConnection( final String  
> databaseAliasName )
>   {
>     LOGGER.log( Level.INFO, "Setting OJB Alias to: " +  
> databaseAliasName );
>     setOjbDBAlias( databaseAliasName );
>     if( getOjbDBAlias() == null || getOjbDBAlias().length() == 0 )
>     {
>       LOGGER.log( Level.SEVERE, "databaseAliasName was empty" );
>       throw new PersistenceBrokerException( "OJB Alias Value Cannot be  
> null!  Please set it properly before proceeding." );
>     }
>     LOGGER.log( Level.INFO, "Setting Broker Key for Persistence  
> Broker." );
>     setBrokerKey( new PBKey( getOjbDBAlias() ) );
>   }
>
>   /**
>    * Convenience method to close the PersistenceBroker and clean up  
> method info on shutdwon.
>    */
>   public final static void destroyConnection()
>   {
>     try
>     {
>       setBrokerKey( null );
>       setOjbDBAlias( null );
>     }
>     catch( Exception e )
>     {
>       LOGGER.log( Level.SEVERE, e.getMessage() );
>     }
>   }
> }
>
> The second piece to this puzzle involves extending this class with a  
> Struts specific class, StrutsOJBConnector, which implements the Struts  
> PlugIn API.
> This class will be loaded on startup by Struts, using the information  
> provided in the struts-config.xml (below), and call the init of the  
> BaseOJBConnector.  The StrutsOJBConnector will loop through all the  
> available Struts Plugins, find the StrutsOJBConnector class, and will  
> look for the value called database.  That value is the value of the  
> OJB repository.xml's jcd-alias.  That's it!
>
> /**
>  * CODEPUCCINO Development Suite IS COPYRIGHTED (c) 2002 - 2004 BY  
> CODEPUCCINO.
>  * This includes, Tracker, Live, Security, Knowledge, Support,
>  * Blog, Forum, Project and any future product(s) created by  
> CODEPUCCINO under this
>  * GPL license.
>  *
>  * This is FREE SOFTWARE.
>  * It is provided under the GNU General Public License (GPL) wherein  
> "program"
>  * shall refer to this digital map. You may use, distribute, or modify  
> this software
>  * only if you agree to all of the terms and conditions of the GPL,
>  * and if this licensing statement accompanies the data.
>  * Read the full text of the GPL from the following sources:
>  *
>  * http://www.gnu.org/licenses/gpl.txt
>  *
>  * Free Software Foundation, Inc.,
>  * 59 Temple Place - Suite 330,
>  * Boston, MA 02111-1307
>  *
>  * Informal discussion: See the License for governing terms and  
> conditions.
>  * Informally, this is a "copyleft" item that is free to use however  
> you wish
>  * (including incorporation into commercial products or derivative  
> works for sale)
>  * as long as you pass it on in a way that perpetuates GNU freedom of  
> usage,
>  * which means all derived works also must be made available under the  
> GPL.
>  *
>  * This program is distributed in the hope that it will be useful,
>  * but WITHOUT ANY WARRANTY; without even the implied warranty of  
> MERCHANTABILITY
>  * or FITNESS FOR A PARTICULAR PURPOSE.
>  *
>  * See the GNU General Public License for more details.
>  *
>  * Contact Information:
>  *
>  * DevTeam
>  * codepuccino.com
>  * opensource@codepuccino.com
>  */
> package com.codepuccino.framework;
>
> import com.codepuccino.utilities.BaseOJBConnector;
> import org.apache.struts.action.ActionServlet;
> import org.apache.struts.action.PlugIn;
> import org.apache.struts.config.ModuleConfig;
> import org.apache.struts.config.PlugInConfig;
>
> import java.io.Serializable;
> import java.util.logging.Level;
> import java.util.logging.Logger;
>
> /**
>  * Struts OJB Connector exists to load the database connection using  
> the Struts PlugIn API.  I makes use of the init()
>  * and destroy() method to bring up and take down the database.
>  *
>  * @author Robert S. Sfeir
>  * @version $Id: StrutsOJBConnector.java,v 1.8 2004/02/19 03:11:29  
> sfeir Exp $
>  * @since 1.0
>  */
> public class StrutsOJBConnector
>     extends BaseOJBConnector
>     implements PlugIn, Serializable
> {
>   protected static final Logger LOGGER = Logger.getLogger(  
> BaseOJBConnector.class.getName() );
>
>   /**
>    * Processed on context destruction, this includes reload of app.   
> Calls close() on the DB Interface, and closes the
>    * OJB Connection to the DB.
>    */
>   public final void destroy()
>   {
>     LOGGER.log( Level.FINE, "Closing database connection." );
>     destroyConnection();
>     LOGGER.log( Level.INFO, "Database connection closed." );
>   }
>
>   /**
>    * Processed on startup and Struts Controller Servlet load.  Sets  
> the DB alias from the plugin parameter called
>    * <code>database</code>, sets the value then calls initConnection()  
> to create the connection to the DB.
>    *
>    * @param actionServlet
>    * @param moduleConfig
>    */
>   public final void init( final ActionServlet actionServlet, final  
> ModuleConfig moduleConfig )
>   {
>     final PlugInConfig[] plugins = moduleConfig.findPlugInConfigs();
>     boolean found = false;
>     int i = 0;
>     while( i < plugins.length && !found )
>     {
>       if( this.getClass().getName().equals( plugins[i].getClassName()  
> ) )
>       {
>         found = true;
>       }
>       else
>       {
>         i++;
>       }
>     }
>     if( found )
>     {
>       final String alias = plugins[i].getProperties().get( "database"  
> ).toString();
>       initConnection( alias.toString() );
>     }
>   }
>
> }
>
> The struts-config.xml PlugIn setting:
>  <!-- Struts OJB Connector Plugin. -->
>   <plug-in className="com.codepuccino.framework.StrutsOJBConnector">
>     <set-property property="database"  
> value="yourDBConnectionNameHere"/>
>   </plug-in>
>
> The sample repository.xml setting:
> <jdbc-connection-descriptor jcd-alias="yourDBConnectionNameHere"  
> default-connection="false" platform="Hsqldb" jdbc-level="3.0"  
> driver="org.hsqldb.jdbcDriver" protocol="jdbc" subprotocol="hsqldb"  
> dbalias="/mesquite-data/mesquite" username="sa" password=""  
> eager-release="true" batch-mode="false" useAutoCommit="0"  
> ignoreAutoCommitExceptions="true">
>   <connection-pool maxActive="1" maxIdle="2" maxWait="3"  
> validationQuery="" logAbandoned="true" removeAbandoned="true"  
> removeAbandonedTimeout="8"/>
>   <sequence-manager  
> className="org.apache.ojb.broker.util.sequence.SequenceManagerHighLowIm 
> pl">
>     <attribute attribute-name="grabSize" attribute-value="100"/>
>     <attribute attribute-name="autoNaming" attribute-value="true"/>
>     <attribute attribute-name="globalSequenceId"  
> attribute-value="false"/>
>     <attribute attribute-name="globalSequenceStart"  
> attribute-value="10000"/>
>   </sequence-manager>
> </jdbc-connection-descriptor>
>
> Robert S. Sfeir
> Technical Lead
> HHS Portal
> robert_sfeir@sra.com
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-dev-help@db.apache.org
>
Robert S. Sfeir
Technical Lead
HHS Portal
robert_sfeir@sra.com


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message