001 package com.croftsoft.core.jnlp; 002 003 import java.io.*; 004 import java.net.*; 005 006 import javax.jnlp.*; 007 008 import com.croftsoft.core.io.SerializableLib; 009 010 /********************************************************************* 011 * JNLP Services. 012 * 013 * @version 014 * $Date: 2008/04/19 21:27:13 $ 015 * @since 016 * 2002-12-21 017 * @author 018 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 019 *********************************************************************/ 020 021 public final class JnlpServicesImpl 022 implements JnlpServices 023 ////////////////////////////////////////////////////////////////////// 024 ////////////////////////////////////////////////////////////////////// 025 { 026 027 public URL createFileContentsURL ( String fileContentsSpec ) 028 throws MalformedURLException, UnsupportedOperationException 029 ////////////////////////////////////////////////////////////////////// 030 { 031 return new URL ( getCodeBase ( ), fileContentsSpec ); 032 } 033 034 public URL getCodeBase ( ) 035 throws UnsupportedOperationException 036 ////////////////////////////////////////////////////////////////////// 037 { 038 try 039 { 040 BasicService basicService = ( BasicService ) 041 ServiceManager.lookup ( "javax.jnlp.BasicService" ); 042 043 return basicService.getCodeBase ( ); 044 } 045 catch ( UnavailableServiceException ex ) 046 { 047 throw ( UnsupportedOperationException ) 048 new UnsupportedOperationException ( ).initCause ( ex ); 049 } 050 } 051 052 /********************************************************************* 053 * Loads a byte array. 054 *********************************************************************/ 055 public byte [ ] loadBytesUsingPersistenceService ( 056 String fileContentsSpec ) 057 throws IOException, UnsupportedOperationException 058 ////////////////////////////////////////////////////////////////////// 059 { 060 InputStream inputStream = null; 061 062 try 063 { 064 PersistenceService persistenceService = ( PersistenceService ) 065 ServiceManager.lookup ( "javax.jnlp.PersistenceService" ); 066 067 FileContents fileContents = persistenceService.get ( 068 createFileContentsURL ( fileContentsSpec ) ); 069 070 ByteArrayOutputStream byteArrayOutputStream 071 = new ByteArrayOutputStream ( ); 072 073 inputStream 074 = new BufferedInputStream ( fileContents.getInputStream ( ) ); 075 076 int i; 077 078 while ( ( i = inputStream.read ( ) ) > -1 ) 079 { 080 byteArrayOutputStream.write ( i ); 081 } 082 083 return byteArrayOutputStream.toByteArray ( ); 084 } 085 catch ( FileNotFoundException ex ) 086 { 087 return null; 088 } 089 catch ( UnavailableServiceException ex ) 090 { 091 throw ( UnsupportedOperationException ) 092 new UnsupportedOperationException ( ).initCause ( ex ); 093 } 094 finally 095 { 096 if ( inputStream != null ) 097 { 098 inputStream.close ( ); 099 } 100 } 101 } 102 103 /********************************************************************* 104 * Loads GZIP compressed data. 105 *********************************************************************/ 106 public Serializable loadSerializableUsingPersistenceService ( 107 String fileContentsSpec ) 108 throws ClassNotFoundException, IOException, 109 UnsupportedOperationException 110 ////////////////////////////////////////////////////////////////////// 111 { 112 try 113 { 114 PersistenceService persistenceService = ( PersistenceService ) 115 ServiceManager.lookup ( "javax.jnlp.PersistenceService" ); 116 117 FileContents fileContents = persistenceService.get ( 118 createFileContentsURL ( fileContentsSpec ) ); 119 120 return SerializableLib.load ( fileContents.getInputStream ( ) ); 121 } 122 catch ( FileNotFoundException ex ) 123 { 124 return null; 125 } 126 catch ( UnavailableServiceException ex ) 127 { 128 throw ( UnsupportedOperationException ) 129 new UnsupportedOperationException ( ).initCause ( ex ); 130 } 131 } 132 133 public void saveBytesUsingPersistenceService ( 134 String fileContentsSpec, 135 byte [ ] bytes ) 136 throws IOException, UnsupportedOperationException 137 ////////////////////////////////////////////////////////////////////// 138 { 139 BufferedOutputStream bufferedOutputStream = null; 140 141 try 142 { 143 PersistenceService persistenceService = ( PersistenceService ) 144 ServiceManager.lookup ( "javax.jnlp.PersistenceService" ); 145 146 URL fileContentsURL 147 = createFileContentsURL ( fileContentsSpec ); 148 149 try 150 { 151 persistenceService.delete ( fileContentsURL ); 152 } 153 catch ( FileNotFoundException ex ) 154 { 155 // ignore 156 } 157 158 persistenceService.create ( fileContentsURL, bytes.length ); 159 160 FileContents fileContents 161 = persistenceService.get ( fileContentsURL ); 162 163 bufferedOutputStream = new BufferedOutputStream ( 164 fileContents.getOutputStream ( true ) ); 165 166 bufferedOutputStream.write ( bytes ); 167 } 168 catch ( UnavailableServiceException ex ) 169 { 170 throw ( UnsupportedOperationException ) 171 new UnsupportedOperationException ( ).initCause ( ex ); 172 } 173 finally 174 { 175 if ( bufferedOutputStream != null ) 176 { 177 bufferedOutputStream.close ( ); 178 } 179 } 180 } 181 182 /********************************************************************* 183 * Saves data using GZIP compression. 184 *********************************************************************/ 185 public void saveSerializableUsingPersistenceService ( 186 String fileContentsSpec, 187 Serializable serializable ) 188 throws IOException, UnsupportedOperationException 189 ////////////////////////////////////////////////////////////////////// 190 { 191 saveBytesUsingPersistenceService ( 192 fileContentsSpec, 193 SerializableLib.compress ( serializable ) ); 194 } 195 196 public boolean showDocument ( URL url ) 197 throws UnsupportedOperationException 198 ////////////////////////////////////////////////////////////////////// 199 { 200 try 201 { 202 BasicService basicService = ( BasicService ) 203 ServiceManager.lookup ( "javax.jnlp.BasicService" ); 204 205 return basicService.showDocument ( url ); 206 } 207 catch ( UnavailableServiceException ex ) 208 { 209 throw ( UnsupportedOperationException ) 210 new UnsupportedOperationException ( ).initCause ( ex ); 211 } 212 } 213 214 ////////////////////////////////////////////////////////////////////// 215 ////////////////////////////////////////////////////////////////////// 216 }