I'm running Tomcat 5.5.7 in embedded mode, i.e., starting Tomcat using
the org.apache.catalina.startup.Embedded class. For some reason, the
JNDI resource I configured in
$tomcat_home/webapps/my_webapp/META-INF/context.xml is not created
when Tomcat starts. Your help will be greatly appreciated!!

The following is the code that I use for starting Tomcat:

      Embedded tomcat = new Embedded();

      // create an Engine
      Engine catalina = tomcat.createEngine();
      catalina.setName("Catalina");
      catalina.setDefaultHost("localhost");

      // create Host
      StandardHost localhost = (StandardHost)
tomcat.createHost("localhost", "webapps");

      // add host to Engine
      catalina.addChild(localhost);

      // create application Context
      StandardContext appCtx = (StandardContext)
tomcat.createContext("/gc", "gc");

      // make the web app's class loader use the normal delegation
model to search for classes
      appCtx.setDelegate(true);

      // add context to host
      localhost.addChild(appCtx);

      // add new Engine to set of Engine for embedded server
      tomcat.addEngine(catalina);

      // create HTTP Connector
      Connector httpConnector =
tomcat.createConnector((java.net.InetAddress)null, 8080, false);
      IntrospectionUtils.setProperty(httpConnector, "redirectPort", "8443");

      // add new Connector to set of Connectors for embedded server,
associated with Engine
      tomcat.addConnector(httpConnector);

      // create HTTPS Connector
      Connector httpsConnector =
tomcat.createConnector((java.net.InetAddress)null, 8443, true);
      httpsConnector.setScheme("https");
      IntrospectionUtils.setProperty(httpsConnector, "sslProtocol", "TLS");
      IntrospectionUtils.setProperty(httpsConnector, "keypass", "changeit");
      IntrospectionUtils.setProperty(httpsConnector, "keystore",
"emf.keystore");

      // add new Connector to set of Connectors for embedded server,
associated with Engine
      tomcat.addConnector(httpsConnector);

      // start operation
      try {
          tomcat.start();
      } catch (org.apache.catalina.LifecycleException ex) {
          ex.printStackTrace();
      }

As you can see from the code, we have one web app called "gc" running
inside Tomcat. Under the $tomcat_home/webapps/gc directory, we have a
META-INF/context.xml file that defines one JNDI resource (a
javax.sql.DataSource object):

<Context docBase="gc"
       path="/gc"
       crossContext="true" reloadable="true" debug="1"
       workDir="work/Catalina/localhost/gc">
<Resource
              name="jdbc/gc"
              auth="Container"
          description="GC DataSource Reference"
          type="javax.sql.DataSource"
          factory="com.good.gc.common.util.GCTomcatDataSourceFactory"
          driverClassName="org.postgresql.Driver"
          url="jdbc:postgresql://localhost:5432/gc"
          username="postgres"
          maxIdle="2"
          maxWait="-1"
          maxActive="4"
          validationQuery="select now()"
          />
</Context>

We use the Spring Framework (version 1.2.3 I believe) for the gc
webapp, and inside the spring context config file, we try to grab the
DataSource from the java:comp/env context this way:

  <bean id="myDataSource"
              class="org.springframework.jndi.JndiObjectFactoryBean">
              <property name="jndiName">
                    <value>java:/comp/env/jdbc/gc</value>
              </property>
  </bean>

Everything used to work just fine when we used Tomcat in standalone
mode. But now that we switched to embedded mode, we got the following
error when starting Tomcat up:

2006-06-01 15:02:15,448 ERROR main org.apache.catalina.core.ContainerBase.[Catal
ina].[localhost].[/gc] - Exception sending context initialized event to listener
instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean wi
th name 'myDataSource' defined in ServletContext resource [/WEB-INF/springContex
tConfig.xml]: Initialization of bean failed; nested exception is javax.naming.Na
mingException: Cannot create resource instance
javax.naming.NamingException: Cannot create resource instance
      at org.apache.naming.factory.ResourceEnvFactory.getObjectInstance(Resour
ceEnvFactory.java:113)
      at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:3
04)
      at org.apache.naming.NamingContext.lookup(NamingContext.java:792)
      at org.apache.naming.NamingContext.lookup(NamingContext.java:139)
      at org.apache.naming.NamingContext.lookup(NamingContext.java:780)
      at org.apache.naming.NamingContext.lookup(NamingContext.java:139)
      at org.apache.naming.NamingContext.lookup(NamingContext.java:780)
      at org.apache.naming.NamingContext.lookup(NamingContext.java:139)
      at org.apache.naming.NamingContext.lookup(NamingContext.java:780)
      at org.apache.naming.NamingContext.lookup(NamingContext.java:152)
      at org.apache.naming.SelectorContext.lookup(SelectorContext.java:136)
      at javax.naming.InitialContext.lookup(InitialContext.java:351)
      at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java
:123)
      at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:85)
      at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:121)
      at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport
.java:71)
      at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.j
ava:85)
      at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(Jnd
iObjectFactoryBean.java:124)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:937)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.createBean(AbstractAutowireCapableBeanFactory.java:334)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean
(AbstractBeanFactory.java:222)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean
(AbstractBeanFactory.java:146)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.
preInstantiateSingletons(DefaultListableBeanFactory.java:271)
      at org.springframework.context.support.AbstractApplicationContext.refres
h(AbstractApplicationContext.java:310)
      at org.springframework.web.context.support.AbstractRefreshableWebApplica
tionContext.refresh(AbstractRefreshableWebApplicationContext.java:133)
      at org.springframework.web.context.ContextLoader.createWebApplicationCon
text(ContextLoader.java:230)
      at org.springframework.web.context.ContextLoader.initWebApplicationConte
xt(ContextLoader.java:156)
      at org.springframework.web.context.ContextLoaderListener.contextInitiali
zed(ContextLoaderListener.java:48)
      at org.apache.catalina.core.StandardContext.listenerStart(StandardContex
t.java:3729)
      at org.apache.catalina.core.StandardContext.start(StandardContext.java:4
187)
      at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1013)

      at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
      at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1013)

      at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442
)
      at org.apache.catalina.startup.Embedded.start(Embedded.java:821)
      at com.good.emf.TomcatStarter.main(TomcatStarter.java:160)

Looks like Spring cannot obtain the DataSource object from
java:comp/env/jdbc/gc, so I'm suspecting the embedded Tomcat does not
read META-INF/context.xml at all. How can I get this to work?

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to