You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Rafael Cunha de Almeida <al...@gmail.com> on 2008/11/12 08:40:12 UTC

Using JAXB with tomcat

I have been trying to use JAXB with tomcat, but I have been have some
problems. I began trying to use the following class loader:

	this.getClass().getClassLoader()

That didn't work. I don't actually understand much about those class
loader things. So I browsed the Internet for a solution and I found out
the following line:

	Thread.currentThread().getContextClassLoader()

Then I plugged it into my JAXB initialization (br/sids/brh/jaxb is in
WEB-INF/class):

        JAXBContext jc = JAXBContext.newInstance("br.sids.brh.jaxb",
                        Thread.currentThread().getContextClassLoader());
        unmarshaller = jc.createUnmarshaller();

After some tomcat permissions tweaking I finally get that line working.
I was happy for a while, that is until I tried to actually get some
information from the XML. I used the following, very simple, just to
test it:

	BoOcorrenciaPolicial bo = (BoOcorrenciaPolicial)
		unmarshaller.unmarshal(new File(XML_DIR + id + ".xml"));
	String hist = bo.getHIST();

Things didn't go as smoothly as I'd hope. This is the error I got:

java.lang.IllegalAccessError: Class com.sun.xml.internal.bind.v2.runtime.reflect.Accessor$FieldReflection can not access a member of class br.sids.brh.jaxb.BoOcorrenciaPolicial with modifiers "protected" com.sun.xml.internal.bind.v2.runtime.reflect.Accessor$FieldReflection.set(Accessor.java:204)

I noticed that "protected" modifiers seemed to be bothering tomcat. So
I thought I'd edit br/sids/brh/jaxb/BoOcorrenciaPolicial.java and
change all the occurences to "public". That seems to have done it. Even
<%= hist %> worked as expected. I was pleased, but I didn't like the
solution.

That file is automatically generated, it seems rather bad to me to edit
a automatically generated file like that. I'd have to notice any future
user that he has to do that (or call some script I could make) and
overall it doesn't seems really elegant to simple change protected to
public, whoever made the program probably had a reason to keep those
methods protected. Does anyone have a better solution? Maybe some
further tomcat configuration, the user of a different loader, perhaps.

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


Re: Using JAXB with tomcat

Posted by Rafael Cunha de Almeida <al...@gmail.com>.
On Wed, 12 Nov 2008 09:09:01 +0100
<To...@teliasonera.com> wrote:

> I am not sure if this works and it is probably not a nice solution, but maybe you can do something like
> 
> BoOcorrenciaPolicial bo = (BoOcorrenciaPolicial)
> 		unmarshaller.unmarshal(new File(XML_DIR + id + ".xml"));
> 
> Class clazz = bo.getClass();
> Field field = clazz.getDeclaredField("hist");
> field.setAccessible(true);
> 
> String hist = bo.getHist();
> 
> Like that you would at least not have to change the fields from protected to public.

Hm, I'm not sure I liked that solution so much. I was having some other
trouble with loading of classes and then I decided to add the following
to my catalina.policy:

grant {
    permission java.security.AllPermission;
    ...
};

After that I didn't even have to rename the fields to public or
anything. Though I think I have just given permission to my webapp to
do whatever it pleases. That doesn't seem very safe, so I would
appreciate if someone knows a more specific permission for my case.

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


RE: Using JAXB with tomcat

Posted by To...@teliasonera.com.
I am not sure if this works and it is probably not a nice solution, but maybe you can do something like

BoOcorrenciaPolicial bo = (BoOcorrenciaPolicial)
		unmarshaller.unmarshal(new File(XML_DIR + id + ".xml"));

Class clazz = bo.getClass();
Field field = clazz.getDeclaredField("hist");
field.setAccessible(true);

String hist = bo.getHist();

Like that you would at least not have to change the fields from protected to public.

Torsten

-----Original Message-----
From: Rafael Cunha de Almeida [mailto:almeidaraf@gmail.com] 
Sent: 12. november 2008 08:40
To: users@tomcat.apache.org
Subject: Using JAXB with tomcat


I have been trying to use JAXB with tomcat, but I have been have some
problems. I began trying to use the following class loader:

	this.getClass().getClassLoader()

That didn't work. I don't actually understand much about those class
loader things. So I browsed the Internet for a solution and I found out
the following line:

	Thread.currentThread().getContextClassLoader()

Then I plugged it into my JAXB initialization (br/sids/brh/jaxb is in
WEB-INF/class):

        JAXBContext jc = JAXBContext.newInstance("br.sids.brh.jaxb",
                        Thread.currentThread().getContextClassLoader());
        unmarshaller = jc.createUnmarshaller();

After some tomcat permissions tweaking I finally get that line working.
I was happy for a while, that is until I tried to actually get some
information from the XML. I used the following, very simple, just to
test it:

	BoOcorrenciaPolicial bo = (BoOcorrenciaPolicial)
		unmarshaller.unmarshal(new File(XML_DIR + id + ".xml"));
	String hist = bo.getHIST();

Things didn't go as smoothly as I'd hope. This is the error I got:

java.lang.IllegalAccessError: Class com.sun.xml.internal.bind.v2.runtime.reflect.Accessor$FieldReflection can not access a member of class br.sids.brh.jaxb.BoOcorrenciaPolicial with modifiers "protected" com.sun.xml.internal.bind.v2.runtime.reflect.Accessor$FieldReflection.set(Accessor.java:204)

I noticed that "protected" modifiers seemed to be bothering tomcat. So
I thought I'd edit br/sids/brh/jaxb/BoOcorrenciaPolicial.java and
change all the occurences to "public". That seems to have done it. Even
<%= hist %> worked as expected. I was pleased, but I didn't like the
solution.

That file is automatically generated, it seems rather bad to me to edit
a automatically generated file like that. I'd have to notice any future
user that he has to do that (or call some script I could make) and
overall it doesn't seems really elegant to simple change protected to
public, whoever made the program probably had a reason to keep those
methods protected. Does anyone have a better solution? Maybe some
further tomcat configuration, the user of a different loader, perhaps.

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


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