D
Digby
Hi,
I'm trying to create a custom Object factory that I can refer to with a
Tomcat global resource, but I'm getting a "javax.servlet.ServletException:
Cannot create resource instance" exception.
It's basically going to be an object pool object I want returned, and uses
Jakarta Commons Pool. I've got 4 classes: MyBean, MyBeanFactory, MyBeanPool
and MyBeanPoolFactory (see below). They're all fairly straightforward at the
moment, and I believe I've copied the code perfectly from the Tomcat JNDI
how-to. I've also modified the server.xml and web.xml files as required,
also below.
The resource should return a MyBeanPool using the MyBeanPoolFactory.
MyBeanPool will then be able to lend from a pool of MyBeans, using the
MyBeanFactory to cteate them (this already works, and just uses the Commons
Pool GenericOnjectPool).
It's all so simple, I can't see why I'm getting the error. Any thoughts?
TIA
Digby
// MyBean class
package my.package;
public class MyBean {
private String name;
public MyBean() {
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
// MyBeanFactory class
package my.package;
import org.apache.commons.pool.*;
public class MyBeanFactory extends BasePoolableObjectFactory {
public MyBeanFactory() {
}
public Object makeObject() throws java.lang.Exception {
MyBean bean = new MyBean();
bean.setName("Digby");
return bean;
}
}
// MyBeanPool class
package my.package;
import org.apache.commons.pool.*;
import org.apache.commons.pool.impl.GenericObjectPool;
public class MyBeanPool extends GenericObjectPool {
public MyBeanPool() {
super(new MyBeanFactory());
}
}
package my.package;
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 MyBeanPoolFactory implements ObjectFactory {
public MyBeanPoolFactory() {
}
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
Hashtable environment) throws Exception {
MyBeanPool pool = new MyBeanPool();
return pool;
}
}
// web.xml
<resource-env-ref>
<description>
Object factory for MyBean instances.
</description>
<resource-env-ref-name>
bean/MyBeanPoolFactory
</resource-env-ref-name>
<resource-env-ref-type>
my.package.MyBeanPool
</resource-env-ref-type>
</resource-env-ref>
// server.xml
<Resource auth="Container" description="A pool"
name="bean/MyBeanPoolFactory" type="my.package.MyBeanPool"/>
<ResourceParams name="bean/MyBeanPoolFactory">
<parameter>
<name>factory</name>
<value>my.package.MyBeanPoolFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>7</value>
</parameter>
</ResourceParams>
I'm trying to create a custom Object factory that I can refer to with a
Tomcat global resource, but I'm getting a "javax.servlet.ServletException:
Cannot create resource instance" exception.
It's basically going to be an object pool object I want returned, and uses
Jakarta Commons Pool. I've got 4 classes: MyBean, MyBeanFactory, MyBeanPool
and MyBeanPoolFactory (see below). They're all fairly straightforward at the
moment, and I believe I've copied the code perfectly from the Tomcat JNDI
how-to. I've also modified the server.xml and web.xml files as required,
also below.
The resource should return a MyBeanPool using the MyBeanPoolFactory.
MyBeanPool will then be able to lend from a pool of MyBeans, using the
MyBeanFactory to cteate them (this already works, and just uses the Commons
Pool GenericOnjectPool).
It's all so simple, I can't see why I'm getting the error. Any thoughts?
TIA
Digby
// MyBean class
package my.package;
public class MyBean {
private String name;
public MyBean() {
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
// MyBeanFactory class
package my.package;
import org.apache.commons.pool.*;
public class MyBeanFactory extends BasePoolableObjectFactory {
public MyBeanFactory() {
}
public Object makeObject() throws java.lang.Exception {
MyBean bean = new MyBean();
bean.setName("Digby");
return bean;
}
}
// MyBeanPool class
package my.package;
import org.apache.commons.pool.*;
import org.apache.commons.pool.impl.GenericObjectPool;
public class MyBeanPool extends GenericObjectPool {
public MyBeanPool() {
super(new MyBeanFactory());
}
}
package my.package;
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 MyBeanPoolFactory implements ObjectFactory {
public MyBeanPoolFactory() {
}
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
Hashtable environment) throws Exception {
MyBeanPool pool = new MyBeanPool();
return pool;
}
}
// web.xml
<resource-env-ref>
<description>
Object factory for MyBean instances.
</description>
<resource-env-ref-name>
bean/MyBeanPoolFactory
</resource-env-ref-name>
<resource-env-ref-type>
my.package.MyBeanPool
</resource-env-ref-type>
</resource-env-ref>
// server.xml
<Resource auth="Container" description="A pool"
name="bean/MyBeanPoolFactory" type="my.package.MyBeanPool"/>
<ResourceParams name="bean/MyBeanPoolFactory">
<parameter>
<name>factory</name>
<value>my.package.MyBeanPoolFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>7</value>
</parameter>
</ResourceParams>