Mel Martinez wrote:

> Hi,
>
> I'm not sure what I'm missing here, but I'm running
> into a strange bit of behavior in Tomcat 3.2.1.
>
> I've got a jsp that uses classes in a jar file that is
> placed in the WEB-INF/lib folder like so:
>
> webapps/demo/simple.jsp
> webapps/demo/WEB-INF/lib/mylib.jar
>
> Early on one of the classes inside mylib.jar gets
> handed a reference to the ServletConfig object (as
> returned from
> javax.servlet.Servlet.getServletConfig()).  At some
> point further on (through a sequence too complex to
> relate here just yet) this class invokes the
> 'getServletName()' method on the ServletConfig object
> using reflection (i.e. Method.invoke()).
>
> This results in an IllegalAccessException.  Details
> are like so:
>
> com.g1440.naf.util.NFRuntimeException:
> com.g1440.naf.reflect.SimpleDecorator: illegal access
> attempt on :
>  object =
>    org.apache.tomcat.facade.ServletConfigImpl@5e83f9 :
>
>  object class =
>     org.apache.tomcat.facade.ServletConfigImpl :
>  method = getServletContext :
>  modifiers = public abstract :
>  object class loader =
>        sun.misc.Launcher$AppClassLoader@404536 :
>  handler class loader = AdaptiveClassLoader(  )
>
>  Nested Exception = java.lang.IllegalAccessException
>
> In the above diagnostics, 'handler' refers to the
> class that is simply calling (the equivalent of)
>
> ServletConfig cfg = getServletConfig();
> ...
> Method.invoke(cfg,"getServletName",null);
>
> The 'ServletConfig.getServletName()' method is
> declared public in the API.  Can anyone suggest to me
> why the above might be happening?  I note that the
> implementation class:
> org.apache.tomcat.facade.ServletConfigImpl is declared
> package-level, (why?) but I wouldn't think this should
> affect my access to a public method.  There seems to
> be some mumblings about access protection in the
> facade package but I'm not yet familiar with what is
> going on there yet.
>

This is exactly the cause of the problem.  When you use Java reflection
to return a Method object from the implementation class, you'll get the
access error exception if the class itself is not public (which is true
in this case).

The workaround is to use reflection to identify the interfaces that this
class implements, and get the Method object from the interface instead of
the implementation class.  We just went through this rigamarole on the
Struts utility module that calls property getters and setters via
reflection -- check out the org.apache.struts.util.PropertyUtils class in
Struts <http://jakarta.apache.org/struts> for how we dealt with it.

>
> Any help is appreciated.  As usual, I'm sure this is
> something boneheaded that I am doing wrong...
>
> Mel
>

Craig



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]

Reply via email to