KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > apollo > dev > DevPersistenceService


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.dev;
24
25 import java.io.*;
26 import java.net.*;
27 import java.util.*;
28 import apollo.*;
29 import houston.*;
30
31 public class DevPersistenceService implements PersistenceService
32 {
33    static Logger T = Logger.getLogger( DevPersistenceService.class );
34
35    File _muffinDir;
36
37    public DevPersistenceService()
38    {
39       File userDir = new File( System.getProperty( "user.home" ) );
40       File rootDir = new File( userDir, ".apollo" );
41       _muffinDir = new File( rootDir, "muffins" );
42
43       if( !_muffinDir.exists() )
44          _muffinDir.mkdirs();
45    }
46
47    public void setTag( URL url, int tag )
48           throws MalformedURLException
49    {
50       // fix: use a property file to set/get tag
51
}
52
53    public FileContents get( URL url )
54           throws MalformedURLException, IOException, FileNotFoundException
55    {
56       return new DevFileContents( mapUrlToFile( url ) );
57    }
58
59    public String JavaDoc[] getNames( URL url )
60           throws MalformedURLException, IOException
61    {
62       File file = mapUrlToFile( url );
63
64       // if url doesn't end with slash; strip file name and use parent directory for lookup
65
if( !url.toString().endsWith( "/" ) )
66          file = file.getParentFile();
67
68       File entries[] = file.listFiles();
69       ArrayList tempNames = new ArrayList();
70
71       if( entries != null )
72       {
73          for( int i = 0; i < entries.length; i++ )
74          {
75             File entry = entries[i];
76             if( entry.isDirectory() )
77                continue;
78             else
79                tempNames.add( entry.getName() );
80          }
81       }
82
83       return ( String JavaDoc[] ) tempNames.toArray( new String JavaDoc[0] );
84    }
85
86    public int getTag( URL url )
87           throws MalformedURLException
88    {
89       // fix: use a property file to set/get tag
90
return CACHED;
91    }
92
93    public long create( URL url, long maxSize )
94           throws MalformedURLException, IOException
95    {
96       File file = mapUrlToFile( url );
97       file.getParentFile().mkdirs();
98       file.createNewFile();
99       return maxSize;
100    }
101
102    public void delete( URL url )
103           throws MalformedURLException, IOException
104    {
105       File file = mapUrlToFile( url );
106       file.delete();
107    }
108
109    private static String JavaDoc escapeSpecialCharacters( String JavaDoc line )
110    {
111       // resolve entities (aka escape sequences)
112
// - known entities:
113
// : -> &c
114

115       // how about space?
116

117       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
118
119       for( int pos = 0; pos < line.length(); )
120       {
121          char c = line.charAt( pos );
122
123          switch ( c )
124          {
125             case ':':
126                buf.append( "&c" );
127                ++pos;
128                break;
129             default:
130                buf.append( c );
131                ++pos;
132                break;
133          }
134       }
135       return buf.toString();
136    }
137
138    private File mapUrlToFile( URL url )
139    {
140       String JavaDoc protocol = url.getProtocol();
141       String JavaDoc host = url.getHost();
142       int port = url.getPort();
143       String JavaDoc path = url.getPath();
144
145       // set default port to 80 if protocol is http
146
if( port == -1
147              && protocol.toLowerCase().equals( "http" ) )
148       {
149          port = 80;
150       }
151
152       if( host.equals( "" ) )
153          host = "localhost";
154
155       T.debug( "protcol=" + protocol );
156       T.debug( "host=" + host );
157       T.debug( "port=" + port );
158       T.debug( "path=" + path );
159
160       StringBuffer JavaDoc mangledPathBuf = new StringBuffer JavaDoc();
161       mangledPathBuf.append( protocol
162              + "/" + host
163              + "/" + port );
164
165       // remove leading slash e.g (/c:/test)
166
// /test/test.html
167

168       if( path.startsWith( "/" ) )
169          path = path.substring( 1 );
170
171       T.debug( "path=" + path );
172
173       StringTokenizer tok = new StringTokenizer( path, "/" );
174       while( tok.hasMoreTokens() )
175       {
176          String JavaDoc name = tok.nextToken();
177          name = escapeSpecialCharacters( name );
178
179          mangledPathBuf.append( "/" + name );
180       }
181
182       T.debug( "mangledPath=" + mangledPathBuf.toString() );
183
184       File mangledPathFile = new File( _muffinDir, mangledPathBuf.toString() );
185       return mangledPathFile;
186    }
187
188 }
189
Popular Tags