/** * Get persistent value. * * @param uri Data URI * @return Data String or null if not present */ public static String getValue(String uri) { Logger logger = Core.getLogger(); javax.jnlp.PersistenceService ps = null; String codebase = null; try { ps = (javax.jnlp.PersistenceService) javax.jnlp.ServiceManager.lookup("javax.jnlp.PersistenceService"); javax.jnlp.BasicService bs = (javax.jnlp.BasicService) javax.jnlp.ServiceManager.lookup("javax.jnlp.BasicService"); codebase = bs.getCodeBase().toString(); } catch (Exception ex) { logger.warning("Unable to look up PersistenceService: " + ex); return null; } URL key = null; javax.jnlp.FileContents fc = null; try { key = new URL(codebase + uri); } catch (MalformedURLException ex) { Core.getLogger().warning("Unable to build persistence service uri: " + ex); return null; } try { fc = ps.get(key); byte[] b = new byte[(int) fc.getLength()]; fc.getInputStream().read(b); return new String(b); } catch (Exception ex) { return null; // Failed to put something } }
/** * Set persistent value. * * @param uri Data URI * @param data Data String * @return true on success, else false */ public static boolean setValue(String uri, String data) { Logger logger = Core.getLogger(); javax.jnlp.PersistenceService ps = null; String codebase = null; try { ps = (javax.jnlp.PersistenceService) javax.jnlp.ServiceManager.lookup("javax.jnlp.PersistenceService"); javax.jnlp.BasicService bs = (javax.jnlp.BasicService) javax.jnlp.ServiceManager.lookup("javax.jnlp.BasicService"); codebase = bs.getCodeBase().toString(); } catch (Exception ex) { logger.warning("Unable to look up PersistenceService: " + ex); return false; } URL key = null; javax.jnlp.FileContents fc = null; try { key = new URL(codebase + uri); } catch (MalformedURLException ex) { Core.getLogger().warning("Unable to build persistence service uri: " + ex); return false; } try { ps.create(key, data.getBytes().length); fc = ps.get(key); fc.getOutputStream(false).write(data.getBytes()); return true; } catch (Exception ex) { logger.warning("Unable to write to persistence service: " + ex); } return false; }