KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > apollo > MuffinStoreHandler


1 /*
2 ** Apollo - Test Skeleton Toolkit for Web Start/JNLP
3 ** Copyright (c) 2001, 2002, 2003 by Gerald Bauer
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** General Public License as published by the Free Software Foundation.
9 ** Version 2 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

22
23 package apollo;
24
25 import java.io.*;
26 import java.net.*;
27 import java.util.*;
28
29 public class MuffinStoreHandler
30 {
31    private static MuffinStoreHandler _handler;
32
33    private BasicService _basicService;
34    private URL _codebase;
35    private PersistenceService _persistenceService;
36
37    public MuffinStoreHandler()
38    {
39       _basicService = ServiceManager.lookupBasicService();
40       _persistenceService = ServiceManager.lookupPersistenceService();
41
42       _codebase = _basicService.getCodeBase();
43    }
44
45    public void setTag( URL url, int tag ) throws MalformedURLException, IOException
46    {
47       _persistenceService.setTag( url, tag );
48    }
49
50    public static MuffinStoreHandler getHandler()
51    {
52       if( _handler == null )
53          _handler = new MuffinStoreHandler();
54       return _handler;
55    }
56
57    public FileContents get( URL url ) throws MalformedURLException, FileNotFoundException, IOException
58    {
59       return _persistenceService.get( url );
60    }
61
62    public FileContents get( String JavaDoc name ) throws MalformedURLException, FileNotFoundException, IOException
63    {
64       return _persistenceService.get( new URL( getCodeBase(), name ) );
65    }
66
67    public URL getCodeBase()
68    {
69       return _codebase;
70    }
71
72    public String JavaDoc[] getNames() throws MalformedURLException, IOException
73    {
74       return _persistenceService.getNames( getCodeBase() );
75    }
76
77    public String JavaDoc[] getNames( URL url ) throws MalformedURLException, IOException
78    {
79       return _persistenceService.getNames( url );
80    }
81
82    public String JavaDoc[] getNames( String JavaDoc name ) throws MalformedURLException, IOException
83    {
84       return _persistenceService.getNames( new URL( getCodeBase(), name ) );
85    }
86
87    public int getTag( URL url ) throws MalformedURLException, IOException
88    {
89       return _persistenceService.getTag( url );
90    }
91
92    /**
93     * return empty string for directory urls (that is, urls ending with a
94     * slash) e.g. http://www.host.org/path/path/ --> empty string return file
95     * name (that is, last path entry) for all other urls e.g.
96     * http://www.host.org/path/path/filename --> filename
97     */

98    private String JavaDoc getUrlFileName( URL url )
99    {
100       String JavaDoc path = url.getPath();
101
102       if( path.endsWith( "/" ) )
103          // it's a directory; no file name available
104
return "";
105
106       String JavaDoc name = "";
107       int index = path.lastIndexOf( '/' );
108       if( index != -1 )
109          name = path.substring( index + 1 );
110
111       return name;
112    }
113
114    public long create( URL url, long maxSize ) throws MalformedURLException, IOException
115    {
116       return _persistenceService.create( url, maxSize );
117    }
118
119    public long create( String JavaDoc name, long maxSize ) throws MalformedURLException, IOException
120    {
121       return _persistenceService.create( new URL( getCodeBase(), name ), maxSize );
122    }
123
124    public void delete( URL url ) throws MalformedURLException, IOException
125    {
126       _persistenceService.delete( url );
127    }
128
129    public void delete( String JavaDoc name ) throws MalformedURLException, IOException
130    {
131       _persistenceService.delete( new URL( getCodeBase(), name ) );
132    }
133
134    public boolean exists( URL url ) throws MalformedURLException, IOException
135    {
136       String JavaDoc names[] = getNames( url );
137
138       // todo: throw URLException for URLs ending with a slash
139

140       // get filename only (no directory path)
141
String JavaDoc key = getUrlFileName( url );
142
143       for( int i = 0; i < names.length; i++ )
144          if( names[i].equals( key ) )
145             return true;
146
147       return false;
148    }
149
150    public boolean exists( String JavaDoc name ) throws MalformedURLException, IOException
151    {
152       return exists( new URL( getCodeBase(), name ) );
153    }
154
155    public Properties loadProperties( String JavaDoc name ) throws MalformedURLException, FileNotFoundException, IOException
156    {
157       // todo: should I check if the muffin exists first and return an empty property table
158
// instead of throwing a FileNotFoundException?
159

160       FileContents contents = get( name );
161
162       Properties props = new Properties();
163       InputStream in = contents.getInputStream();
164       props.load( in );
165       in.close();
166
167       return props;
168    }
169
170    public String JavaDoc loadText( String JavaDoc name ) throws MalformedURLException, FileNotFoundException, IOException
171    {
172       // todo: should I check if the muffin exists first and return an empty or default value
173
// instead of throwing a FileNotFoundException?
174

175       FileContents contents = get( name );
176
177       StringBuffer JavaDoc text = new StringBuffer JavaDoc();
178
179       InputStreamReader in = new InputStreamReader( contents.getInputStream() );
180       char buffer[] = new char[4096];
181       int bytes_read;
182       while( ( bytes_read = in.read( buffer ) ) != -1 )
183          text.append( new String JavaDoc( buffer, 0, bytes_read ) );
184       in.close();
185
186       return text.toString();
187    }
188
189    public void saveProperties( String JavaDoc name, Properties props ) throws MalformedURLException, FileNotFoundException, IOException
190    {
191       saveProperties( name, props, 2056 );
192    }
193
194    public void saveProperties( String JavaDoc name, Properties props, long maxSize ) throws MalformedURLException, FileNotFoundException, IOException
195    {
196       URL muffinUrl = new URL( getCodeBase(), name );
197       if( !exists( muffinUrl ) )
198          create( muffinUrl, maxSize );
199
200       FileContents contents = get( muffinUrl );
201
202       OutputStream out = contents.getOutputStream( true );
203       /*
204        * overwrite:=true
205        */

206       props.store( out, new Date().toString() );
207       out.flush();
208       out.close();
209    }
210
211    public void saveText( String JavaDoc name, String JavaDoc text ) throws MalformedURLException, FileNotFoundException, IOException
212    {
213       saveText( name, text, 2056 );
214    }
215
216    public void saveText( String JavaDoc name, String JavaDoc text, long maxSize ) throws MalformedURLException, FileNotFoundException, IOException
217    {
218       URL muffinUrl = new URL( getCodeBase(), name );
219       if( !exists( muffinUrl ) )
220          create( muffinUrl, maxSize );
221
222       FileContents contents = get( muffinUrl );
223
224       OutputStreamWriter out = new OutputStreamWriter( contents.getOutputStream( true ) );
225       /*
226        * overwrite:=true
227        */

228       out.write( text );
229       out.flush();
230       out.close();
231    }
232 }
233
Popular Tags