Getting the Java webstart kit from
http://java.sun.com/products/javawebstart/download-jnlp.html... Look at the only current muffin example...
http://java.sun.com/products/javawebstart/docs/developersguide.html "Using a PersistenceService Service"
You notice that the example will not immediately compile... Fx. type error in "byte [] buf = new byte[fc.getLength()];" where .getLength() returns a long... (not a primitive int as expected by java compiler). Why catch on "Exception" and not the actually thrown exceptions... Well, make a person wonder if the writer ever actually produce a working example for himself?
Fixing and cleaning... (I can post the whole code if necessary - but let's just take a gentle approach first). I'm trying to read any existing muffins - and force creation of one anyway...
...
String urlName = "http://localhost:8888/servlets-examples/servlet/CookieServlet";
URL url;
try {
url = new URL(urlName);
}
catch(MalformedURLException mfe) {
mfe.printStackTrace();
url = null;
return;
}
PersistenceService persistanceService = null;
BasicService basicService = null;
try {
persistanceService = (PersistenceService)ServiceManager.lookup("javax.jnlp.PersistenceService");
basicService = (BasicService)ServiceManager.lookup("javax.jnlp.BasicService");
} catch (UnavailableServiceException e) {
}
if (persistanceService != null && basicService != null) {
try {
// find all the muffins for our URL
URL codebase = basicService.getCodeBase();
//code works for me this far - but fails in the following...
String [] muffins = persistanceService.getNames(url);
int numberOfMuffins = ((muffins==null)?0:muffins.length);
if(numberOfMuffins>0) {
// get the attributes (tags) for each of these muffins.
// update the server's copy of the data if any muffins
// are dirty
int [] tags = new int[muffins.length];
URL [] muffinURLs = new URL[muffins.length];
for (int i = 0; i < muffins.length; i++) {
muffinURLs[i] = new URL(codebase.toString() + muffins[i]);
tags[i] = persistanceService.getTag(muffinURLs[i]);
// update the server if anything is tagged DIRTY
if (tags[i] == PersistenceService.DIRTY) {
//doUpdateServer(muffinURLs[i]);
}
}
// read in the contents of a muffin and then delete it
FileContents fc = persistanceService.get(muffinURLs[0]);
long maxsize = fc.getMaxLength();
byte [] buf = new byte[(int)fc.getLength()];
InputStream is = fc.getInputStream();
long pos = 0;
while((pos = is.read(buf, (int)pos, (int)(buf.length - pos))) > 0) {
// just loop
}
is.close();
persistanceService.delete(muffinURLs[0]);
// re-create the muffin and repopulate its data
persistanceService.create(muffinURLs[0], maxsize);
fc = persistanceService.get(muffinURLs[0]);
// don't append
OutputStream os = fc.getOutputStream(false);
os.write(buf);
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
// well, just try and create the muffin and repopulate its data
persistanceService.create(url, 50);
FileContents fc = persistanceService.get(url);
// don't append
OutputStream os = fc.getOutputStream(false);
os.write("1234567890test1234567890test00".getBytes());
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Trying this code produces (why can't you cutn'paste from the webstart 1.2 console.. only a "Clear" button and hotkeys doesn't work..):
java.net.MalformedURLException: Access to persistant storage denied for URL localhost:8888/servlets-examples/servlet/CookieServlet/
at com.sun.jnlp.PersistanceServiceImpl.throwAccessDenied(Unknown Source)
at com.sun.jnlp.PersistanceServiceImpl.checkAccessDenied(Unknown Source)
at com.sun.jnlp.PersistanceServiceImpl.getNames(Unknown Source)
at CookieClient.main(CookieClient.java:91)
...
java.net.MalformedURLException: Access to persistant storage denied for URL localhost:8888/servlets-examples/servlet/CookieServlet/
at com.sun.jnlp.PersistanceServiceImpl.throwAccessDenied(Unknown Source)
at com.sun.jnlp.PersistanceServiceImpl.checkAccessDenied(Unknown Source)
at com.sun.jnlp.PersistanceServiceImpl.getNames(Unknown Source)
at CookieClient.main(CookieClient.java:144)
...
91: the place where I do the "persistanceService.getNames(url);"
144: the place with persistanceService.create(url, 50)
Obviously it appears to be some missing security permission...
OK, I did! sign the .jar file with a self-generated signature.
OK, my servlet "CookieServlet" actually exists and works. The url works fine from the browser url (.get(..) method).
OK, I did a
Policy.setPolicy( new Policy() {
public PermissionCollection
getPermissions(CodeSource codesource) {
Permissions perms = new Permissions();
perms.add(new AllPermission());
return(perms);
}
public void refresh() {}
});
just before the denied actions.... Hmm... I'll probably figure it out eventually - but a hint would surely save me for a lot of time
StigV