db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas Fischer <fisc...@seitenbau.net>
Subject Re: Torque and Struts
Date Tue, 31 May 2005 07:00:48 GMT





Hi,

you can also extend the Struts Action Servlet and override the init()
method (do not forget to call super.init() somewhere in there)
This also works if you do not want to use load-on-startup

      Thomas

a.winter@telenet-ag.de schrieb am 31.05.2005 07:07:16:

> Czesz Jakub,
>
> we have actally two projekts using torque and struts.
>
> If you use struts and torque with Apache Tomcat, you have to put the
> initialisation into
> a class, which will be executet as a servlet during start of tomcat. The
> configuration is
> in the web.xml file of your application:
>
>   <!-- Initialisierung Servlet Configuration -->
>   <servlet>
>     <servlet-name>initSystem</servlet-name>
>     <servlet-class>YourInitClass</servlet-class>
>     <init-param>
>       <!-- siehe resource ref unten -->
>       <param-name>res-ref-name</param-name>
>       <param-value>jdbc/YOUR_DB</param-value>
>     </init-param>
>     <load-on-startup>2</load-on-startup>
>   </servlet>
> ...
>   <resource-ref>
>     <description>
>       Resource reference to a factory for java.sql.Connection
>       instances that may be used for talking to a particular
>       database that is configured in the server.xml file.
>     </description>
>     <res-ref-name>
>       jdbc/YOUR_DB
>     </res-ref-name>
>     <res-type>
>       javax.sql.DataSource
>     </res-type>
>     <res-auth>
>       Container
>     </res-auth>
>   </resource-ref>
>
> The java class YourInitClass looks like this:
>
> public class YourInitClass extends HttpServlet
> {
>
>     /** Ressource Reference */
>     private static final String CONFIG_RESREFKEY = "res-ref-name";
>
>         private static InitTorque m_TorqueDbcpInitialisation = null;
>
>          /**
>           *  initialisieren des Automatisierungstools.
>           */
>          public void init(ServletConfig config) throws ServletException
>          {
>                  super.init(config);
>
>                  String jndiRes =
> config.getInitParameter(CONFIG_RESREFKEY);
>                  try
>                  {
>                          new InitTorque().initJNDI(jndiRes);
>                  }
>                  catch (TorqueException dbe)
>                  {
>                          throw new ServletException(dbe);
>                  }
>          }
> }
>
> And finally the InitTorque class:
>
> public class InitTorque
> {
>         private static final String TORQUE_PROPERTIES =
> "torque.properties";
>
>         private static final String TORQUE_KEY = "torque.";
>
>         private static final String TORQUE_DS = "torque.dsfactory.";
>
>         private static final String J2EE_JNDI_REALM = "java:comp/env/";
>
>         public PropertiesConfiguration m_torqueProps = null;
>
>         public String m_dbName = null;
>
>         /**
>          * initialisiere ServletContainer Context für JNDI.
>          *
>          * @param javaNamingComponent
>          * @throws TorqueException
>          */
>         public void initJNDI(String javaNamingComponent) throws
> TorqueException
>         {
>                 initResource();
>                 initTorque();
>                 log.debug("end");
>         }
>
>         /**
>          * initialisiere Ressource für DB Access.
>          *
>          * @return PropertiesConfiguration die Standardparameter.
>          * @throws IOException
>          */
>         private void initResource() throws TorqueException {
>
>                 InputStream configStream =
> this.getClass().getClassLoader().getResourceAsStream(TORQUE_PROPERTIES);
>                 if (configStream == null)
>                 {
>                         throw new TorqueException("DB Initialisierung " +

> TORQUE_PROPERTIES + " nicht gefunden.");
>                 }
>
>                 m_torqueProps = new PropertiesConfiguration();
>                 try
>                 {
>                         m_torqueProps.load(configStream);
>
>                         Iterator it = m_torqueProps.getKeys();
>
>                         while (it.hasNext())
>                         {
>                                 String key = (String)it.next();
>                                 log.info("|- " + key + " = " +
> m_torqueProps.getProperty(key).toString());
>                         } // while ends
>
>                         m_dbName = m_torqueProps.getString(TORQUE_KEY +
> "database.default");
>                         if (m_dbName == null)
>                         {
>                                 throw new TorqueException("DB
> Initialisierung Default DB Name nicht gefunden.");
>                         }
>                 }
>                 catch (ConfigurationException e)
>                 {
>                         throw new TorqueException(e);
>                 }
>         }
>
>         public void shutdown()
>         {
>                 log.debug("start");
>                 Torque.shutdown();
>                 log.debug("end");
>         }
>
>         /**
>          * Initialisierung von Torque.
>          *
>          * @throws TorqueException
>          */
>         private void initTorque() throws TorqueException {
>
>                 if (false == Torque.isInit())
>                 {
>                         Torque.init(m_torqueProps);
>                 }
>
>                 log.debug("end");
>         }
> } // InitTorque
>
> Hope that helps,
> Arndt
>
>
>
>
>
>
> "Jakub Piechnik" <piechutm@wp.pl>
> 30.05.2005 22:13
> Please respond to "Apache Torque Users List"
>
>
>         To:     "Apache Torque Users List" <torque-user@db.apache.org>
>         cc:
>         Subject:        Torque and Struts
>
>
> Hi
>
> I was wondering if I could use classes generated by Torque in project
that
> uses Struts? If so where should I put all torque inicjalization? Can
> anyone gives me excample of how to use torque in struts?
>
> Greetings
>
> Jakub Piechnik
>
> ----------------------------------------------------
> Viva! - nowy serwis dla kobiet. Wywiady z gwiazdami, najnowsze
>  plotki i relacje z imprez towarzyskich. Wejdź koniecznie!
>  http://klik.wp.pl/?adr=http%3A%2F%2Fviva.wp.pl&sid=390
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
> For additional commands, e-mail: torque-user-help@db.apache.org
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
> For additional commands, e-mail: torque-user-help@db.apache.org
>


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


Mime
View raw message