Peter Williams wrote:
> Are you absolutely sure that the static initializer in this case in
> not invoked before the Filter.init() method? Without checking the spec
> to be certain, I suspect that
Well, I am absolutely sure it _is_.
> InitialContext is not available until Filter.init() is fired and that
> in your case something else is loading this file prematurely. See SRV
> 2.3.2.2 for a (possibly) related comment on the use of static
> initializers.
>
> FWIW, In a small example, I successfully loaded the environment entry
> via a static initializer in a class referenced only via a
> Filter.init() method. It also worked well in an @Resource injection
> annotation in the filter class itself.
>
> -Peter
>
Here are fragments of a simple test case I just wrote:
public class NewSimpleFilter implements Filter
{
private TestJndi testJndi = new TestJndi(); // *
public void init(FilterConfig filterConfig)
{
System.out.println("Filter.init");
testJndi.doSomething(); // **
}
[...]
}
public class TestJndi {
static
{
doTest();
}
public void doSomething()
{
System.out.println("Doing something.");
doTest();
}
static public void doTest()
{
System.out.println("*TESTJNDI* - start");
try
{
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
String value = (String)envCtx.lookup("xxx/test");
System.out.println("value = \"" + value + "\"");
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("*TESTJNDI* - end");
}
}
As you see, doTest() gets called twice, first from line marked "*",
second from within Filter.init. On Tomcat both tests succeed. On
Glassfish first one fails, second succeeds.
SRV 2.3.2.2? You mean this: "SRV.2.3.2.2 Tool Considerations
The triggering of static initialization methods when a tool loads and
introspects a
web application is to be distinguished from the calling of the init
method. Developers
should not assume a servlet is in an active container runtime until the init
method of the Servlet interface is called. For example, a servlet should
not try to
establish connections to databases or Enterprise JavaBeans™ containers
when only
static (class) initialization methods have been invoked."?
Well, that would explain it. Swell.
Is there anything that can be done about it?
Greetings, Lilianne E. Blaze