Skip to main content

JAXBContext in applet doesn't work

2 replies [Last post]
mallerborn
Offline
Joined: 2013-09-17
Points: 0

Hello,

my problem is to use JAXBContext.newInstance(myClassNyme.class) in my applet. My applet contains only one method. Input parameter is a string (with xml structure).
public String testApplet (final String data) throws JAXBException {
byte[] dataAsByte = data.getBytes();
GenericDataTransformer transformer = new GenericDataTransformer(); // my own class
Map dataMap = transformer.transform(dataAsByte);

ErrorMessage error = new ErrorMessage (); // Is only small class with @Xml annotations
error.setId("errorId:201");
System.out.println(error.getId);
JAXBContext jc = JAXBContext.newInstance(ErrorMessage.class);
System.out.println("context was created");
StringWriter writer = new StringWriter();
jc.createMarshaller().marshal(error, writer);
return writer.getBuffer().toString();
}
}
In java console I get "errorId:201" and that's it. No more messages, no more errors. Only network: cacheentry not found [URL: file:/C:/..../MyApplet.jar, Version: null].
In JUnit tests are all green
.
Html: APPLET CODE="applet.MyApplet.class" NAME="demoApplet"
archive="MyApplet.jar"
style="width: 1px; height: 1px; float: left;">
/APPLET

With ant task I create a jar
"jar destfile="${build.dir}/${generated.jar.file.name}" filesetmanifest="mergewithoutmain" duplicate="preserve">
manifest>
attribute name="Built-By" value="${user.name}"/>
attribute name="Main-Class" value="MyApplet"/>
attribute name="Class-Path" value="."/>
/manifest>
fileset dir="${basedir}/bin/classes" />
zipfileset excludes="META-INF/*.SF" src="lib/jaxb-api-2.2.5.jar"/>
zipfileset excludes="META-INF/*.SF" src="lib/jaxb-impl-2.2.5.jar"/>
zipfileset excludes="META-INF/*.SF" src="lib/generic_data-0.69.jar"/>
/jar>"
And sign it later:
target name="Sign jar files" description="Signs jars">
signjar alias="${keystore.alias}" keystore="${keystore.file}" storetype="${keystore.type}"
storepass="idexpert" sigalg="MD5withRSA" digestalg="SHA1" lazy="true" >
fileset dir="${build.dir}" >
include name="*.jar" />
/fileset>
/signjar>
ivy:publish settingsref="ref.settings.ivy" artifactspattern="${build.dir}/[artifact]-[revision]-[type].[ext]" resolver="shared" overwrite="true" />
/target

Has anyone any idea? Applet, html and ant tasks are new areas for me. But i have to do that.

Please help !!

I am very thankful for your ideas.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mallerborn
Offline
Joined: 2013-09-17
Points: 0

I have lost my problem. Solution was very simple.
JAXBContext.newInstance... has to access to file system. But java security doesn't allow to do that.
If you want to allow this access you have to add "privileged" block in your method and add your "access here JAXBContext.newInstance.." in this block. With Access controller it will be possible.
My solution :

public String testApplet (final String data) {
------ // the same code as above
AccessController.doPrivileged(new PrivilegedAction() {
@Override
public Object run() {
System.out.println(error.getId);
JAXBContext jc = JAXBContext.newInstance(ErrorMessage.class);
System.out.println("context was created");
try {
System.out.println("context was successful created"); ;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
});

More information you get in google. (search after Access controller)

yaroska
Offline
Joined: 2006-05-05
Points: 0

Yes, you are right. To work with JAXB in applets or Java Web Start you have to grant JAXB some privileges.