1 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 } 52 53 public FileContents get( URL url ) 54 throws MalformedURLException, IOException, FileNotFoundException 55 { 56 return new DevFileContents( mapUrlToFile( url ) ); 57 } 58 59 public String [] getNames( URL url ) 60 throws MalformedURLException, IOException 61 { 62 File file = mapUrlToFile( url ); 63 64 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 [] ) tempNames.toArray( new String [0] ); 84 } 85 86 public int getTag( URL url ) 87 throws MalformedURLException 88 { 89 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 escapeSpecialCharacters( String line ) 110 { 111 115 117 StringBuffer buf = new StringBuffer (); 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 protocol = url.getProtocol(); 141 String host = url.getHost(); 142 int port = url.getPort(); 143 String path = url.getPath(); 144 145 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 mangledPathBuf = new StringBuffer (); 161 mangledPathBuf.append( protocol 162 + "/" + host 163 + "/" + port ); 164 165 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 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 |