1 Reply Latest reply on Aug 27, 2007 8:40 AM by Usha ShashiKumar

    javax.naming.NamingException: Cannot create resource instanc

    Usha ShashiKumar Newbie

      javax.naming.NamingException: Cannot create resource instance
      at org.apache.naming.factory.ResourceEnvFactory.getObjectInstance(ResourceEnvFactory.java:99)
      at javax.naming.spi.NamingManager.getObjectInstance(Unknown Source)
      at org.apache.naming.NamingContext.lookup(NamingContext.java:791)
      at org.apache.naming.NamingContext.lookup(NamingContext.java:151)
      at ShowContext.doGet(ShowContext.java:44)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
      at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
      at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
      at java.lang.Thread.run(Unknown Source)

        • 1. Re: javax.naming.NamingException: Cannot create resource ins
          Usha ShashiKumar Newbie


          Hi,

          Have Some issue while refering resource parameter in Tomcat any help and suggesion will be appreciated.
          Details for the samle code is given following. I had created jarfile which contains my bean class and factory class and i placed that in server\lib as well as common\lib

          Server.xml file Contains

          <?xml version='1.0' encoding='utf-8'?>









          factory
          com.ibm.resource.ConfigFactory


          bar
          23




          factory
          org.apache.catalina.users.MemoryUserDatabaseFactory


          pathname
          conf/tomcat-users.xml


















          web.xml file Contains

          <?xml version="1.0" encoding="UTF-8"?>
          <web-app>
          <display-name>Resource Refrence Sample</display-name>

          <display-name>ShowContext</display-name>
          <servlet-name>ShowContext</servlet-name>
          <servlet-class>ShowContext</servlet-class>

          <servlet-mapping>
          <servlet-name>ShowContext</servlet-name>
          <url-pattern>/ShowContext</url-pattern>
          </servlet-mapping>

          <resource-env-ref>
          <resource-env-ref-name>MyResourceLink</resource-env-ref-name>
          <resource-env-ref-type>com.ibm.resource.Config</resource-env-ref-type>
          </resource-env-ref>

          <welcome-file-list>
          <welcome-file>Test.jsp</welcome-file>
          </welcome-file-list>
          </web-app>

          Servlet Contains

          protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
          // TODO Auto-generated method stub
          try {
          int i=1;
          System.out.println("Initializing Initial Context");
          Context initCtx = new InitialContext();

          System.out.println("Initial Context Lookup");
          Context envCtx = (Context) initCtx.lookup("java:comp/env");

          System.out.println("Environment Context Lookup");
          //Object object = envCtx.lookup("bar");
          Object object = envCtx.lookup("MyResourceLink");

          System.out.println("Casting Context Object To Context");
          Config config = (Config) object;

          System.out.println("Getting Map");
          Map mp=(Map) config.getAttributes();

          System.out.println("Getting Map Key Set");
          Set mapkeyval=mp.keySet();
          Iterator itr=mapkeyval.iterator();
          while(itr.hasNext())
          {
          String keyVal=(String)itr.next();
          String myVariable = (String) config.getAttribute(keyVal);
          System.out.println("Variable No: "+i);
          System.out.println("Variable Name: "+keyVal);
          System.out.println("Variable Value: "+myVariable);
          System.out.println("---------------------------------------------");
          i=i+1;
          }
          } catch (Exception e) {System.out.println("Unable To Get Initial Context");e.printStackTrace(); }
          }

          Bean Class

          package com.ibm.resource;
          import java.util.HashMap;
          import java.util.Map;
          public class Config
          {
          private Map attributes = null;
          public Config()
          {
          attributes = new HashMap();
          }
          protected void setAttribute(String attributeName, String attributeValue)
          {
          attributes.put(attributeName, attributeValue);
          }
          public Object getAttribute(String attributeName)
          {
          return attributes.get(attributeName);
          }
          public Map getAttributes()
          {
          return attributes;
          }
          public void setAttributes(Map attributes)
          {
          this.attributes=attributes;
          }
          }

          Factory Class

          package com.ibm.resource;
          import java.util.Enumeration;
          import java.util.Hashtable;
          import javax.naming.Context;
          import javax.naming.Name;
          import javax.naming.NamingException;
          import javax.naming.RefAddr;
          import javax.naming.Reference;
          import javax.naming.spi.ObjectFactory;
          public class ConfigFactory implements ObjectFactory
          {
          private static Config config = null;

          public Object getObjectInstance(Object object,Name name,Context nameCtx,Hashtable environment) throws NamingException
          {
          if(config == null)
          {
          config = new Config();
          System.out.println("Converting Input Object Into Refrence");
          Reference ref = (Reference) object;
          System.out.println("Getting Address Enumeration From Refrence");
          Enumeration addrs = ref.getAll();
          System.out.println("Initializing Variables ");
          RefAddr addr = null;
          String entryName = null;
          String value = null;
          System.out.println("Going To Iterate Address Enumeration");
          while (addrs.hasMoreElements())
          {
          addr = (RefAddr) addrs.nextElement();
          System.out.println("Getting Type From RefAddr");
          entryName = addr.getType();
          System.out.println("Getting Contents");
          value = (String) addr.getContent();
          System.out.println("Setting Attribute");
          config.setAttribute(entryName, value);
          }
          }
          return config;
          }
          }

          Error I Am Getting (Log File Containts)
          Aug 27, 2007 5:14:46 PM org.apache.coyote.http11.Http11Protocol init
          INFO: Initializing Coyote HTTP/1.1 on http-8080
          Aug 27, 2007 5:14:46 PM org.apache.catalina.startup.Catalina load
          INFO: Initialization processed in 815 ms
          Converting Input Object Into Refrence
          Getting Address Enumeration From Refrence
          Initializing Variables
          Going To Iterate Address Enumeration
          Getting Type From RefAddr
          Getting Contents
          Setting Attribute
          Getting Type From RefAddr
          Getting Contents
          Setting Attribute
          Getting Type From RefAddr
          Getting Contents
          Setting Attribute
          Getting Type From RefAddr
          Getting Contents
          Setting Attribute
          Aug 27, 2007 5:14:46 PM org.apache.catalina.core.StandardService start
          INFO: Starting service Catalina
          Aug 27, 2007 5:14:46 PM org.apache.catalina.core.StandardEngine start
          INFO: Starting Servlet Engine: Apache Tomcat/5.0.28
          Aug 27, 2007 5:14:46 PM org.apache.catalina.core.StandardHost start
          INFO: XML validation disabled
          Aug 27, 2007 5:14:46 PM org.apache.catalina.core.StandardHost getDeployer
          INFO: Create Host deployer for direct deployment ( non-jmx )
          Aug 27, 2007 5:14:46 PM org.apache.catalina.core.StandardHostDeployer install
          INFO: Processing Context configuration file URL file:C:\ApacheTomcat5.0\conf\Catalina\localhost\admin.xml
          Aug 27, 2007 5:14:47 PM org.apache.struts.util.PropertyMessageResources
          INFO: Initializing, config='org.apache.struts.util.LocalStrings', returnNull=true
          Aug 27, 2007 5:14:47 PM org.apache.struts.util.PropertyMessageResources
          INFO: Initializing, config='org.apache.struts.action.ActionResources', returnNull=true
          Aug 27, 2007 5:14:47 PM org.apache.struts.util.PropertyMessageResources
          INFO: Initializing, config='org.apache.webapp.admin.ApplicationResources', returnNull=true
          Aug 27, 2007 5:14:47 PM org.apache.catalina.core.StandardHostDeployer install
          INFO: Processing Context configuration file URL file:C:\ApacheTomcat5.0\conf\Catalina\localhost\balancer.xml
          Aug 27, 2007 5:14:47 PM org.apache.catalina.core.StandardHostDeployer install
          INFO: Processing Context configuration file URL file:C:\ApacheTomcat5.0\conf\Catalina\localhost\jsp-examples.xml
          Aug 27, 2007 5:14:47 PM org.apache.catalina.core.StandardHostDeployer install
          INFO: Processing Context configuration file URL file:C:\ApacheTomcat5.0\conf\Catalina\localhost\manager.xml
          Aug 27, 2007 5:14:47 PM org.apache.catalina.core.StandardHostDeployer install
          INFO: Processing Context configuration file URL file:C:\ApacheTomcat5.0\conf\Catalina\localhost\ResRefSample.xml
          Aug 27, 2007 5:14:47 PM org.apache.catalina.core.StandardHostDeployer install
          INFO: Processing Context configuration file URL file:C:\ApacheTomcat5.0\conf\Catalina\localhost\ROOT.xml
          Aug 27, 2007 5:14:47 PM org.apache.catalina.core.StandardHostDeployer install
          INFO: Processing Context configuration file URL file:C:\ApacheTomcat5.0\conf\Catalina\localhost\servlets-examples.xml
          Aug 27, 2007 5:14:47 PM org.apache.catalina.core.StandardHostDeployer install
          INFO: Processing Context configuration file URL file:C:\ApacheTomcat5.0\conf\Catalina\localhost\tomcat-docs.xml
          Aug 27, 2007 5:14:48 PM org.apache.catalina.core.StandardHostDeployer install
          INFO: Processing Context configuration file URL file:C:\ApacheTomcat5.0\conf\Catalina\localhost\webdav.xml
          Aug 27, 2007 5:14:48 PM org.apache.coyote.http11.Http11Protocol start
          INFO: Starting Coyote HTTP/1.1 on http-8080
          Aug 27, 2007 5:14:48 PM org.apache.jk.common.ChannelSocket init
          INFO: JK2: ajp13 listening on /0.0.0.0:8009
          Aug 27, 2007 5:14:48 PM org.apache.jk.server.JkMain start
          INFO: Jk running ID=0 time=0/31 config=C:\ApacheTomcat5.0\conf\jk2.properties
          Aug 27, 2007 5:14:48 PM org.apache.catalina.startup.Catalina start
          INFO: Server startup in 1927 ms
          Initializing Initial Context
          Initial Context Lookup
          Environment Context Lookup
          Unable To Get Initial Context
          javax.naming.NamingException: Cannot create resource instance
          at org.apache.naming.factory.ResourceEnvFactory.getObjectInstance(ResourceEnvFactory.java:99)
          at javax.naming.spi.NamingManager.getObjectInstance(Unknown Source)
          at org.apache.naming.NamingContext.lookup(NamingContext.java:791)
          at org.apache.naming.NamingContext.lookup(NamingContext.java:151)
          at ShowContext.doGet(ShowContext.java:44)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
          at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
          at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
          at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
          at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
          at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
          at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
          at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
          at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
          at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
          at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
          at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
          at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
          at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
          at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
          at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
          at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
          at java.lang.Thread.run(Unknown Source)