cziegeler 02/02/12 04:55:24 Modified: src/java/org/apache/cocoon/components/store FilesystemStore.java Log: Fixed Filesystemstore: - keys can now be any values - the get() method now returns the object previously stored and not a File Revision Changes Path 1.7 +58 -4 xml-cocoon2/src/java/org/apache/cocoon/components/store/FilesystemStore.java Index: FilesystemStore.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/store/FilesystemStore.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- FilesystemStore.java 4 Feb 2002 12:31:09 -0000 1.6 +++ FilesystemStore.java 12 Feb 2002 12:55:24 -0000 1.7 @@ -75,12 +75,18 @@ * * @author ? * @author <a href="mailto:[EMAIL PROTECTED]">Vadim Gritsenko</a> - * @version CVS $Id: FilesystemStore.java,v 1.6 2002/02/04 12:31:09 cziegeler Exp $ + * @version CVS $Id: FilesystemStore.java,v 1.7 2002/02/12 12:55:24 cziegeler Exp $ */ public final class FilesystemStore extends AbstractLoggable implements Store, Contextualizable, Parameterizable, ThreadSafe { + protected static final int BYTE_MASK = 0x0f; + protected static final char[] HEX_DIGITS = + { + '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' + }; + protected File workDir; protected File cacheDir; @@ -182,7 +188,11 @@ if (this.getLogger().isDebugEnabled()) { getLogger().debug("Found file: " + key); } - return file; + try { + return IOUtils.deserializeObject(file); + } catch (Exception any) { + getLogger().error("Error during deseralization.", any); + } } else { if (this.getLogger().isDebugEnabled()) { getLogger().debug("NOT Found file: " + key); @@ -284,7 +294,7 @@ if (files[i].isDirectory()) { this.addKeys(enum, files[i]); } else { - enum.add(files[i].getAbsolutePath().substring(subStringBegin)); + enum.add(this.decode(files[i].getAbsolutePath().substring(subStringBegin))); } } } @@ -339,7 +349,7 @@ /* Utility Methods*/ protected File fileFromKey(final Object key) { - return IOUtils.createFile(this.directoryFile, key.toString()); + return IOUtils.createFile(this.directoryFile, this.encode(key.toString())); } public String getString(final Object key) @@ -364,4 +374,48 @@ return null; } + + /** + * Returns a String that uniquely identifies the object. + * <b>Note:</b> since this method uses the Object.toString() + * method, it's up to the caller to make sure that this method + * doesn't change between different JVM executions (like + * it may normally happen). For this reason, it's highly recommended + * (even if not mandated) that Strings be used as keys. + */ + protected String encode( final String key ) + { + final byte[] bytes = key.getBytes(); + final char[] buffer = new char[ bytes.length << 1 ]; + + for( int i = 0, j = 0; i < bytes.length; i++ ) + { + final int k = bytes[ i ]; + buffer[ j++ ] = HEX_DIGITS[ ( k >>> 4 ) & BYTE_MASK ]; + buffer[ j++ ] = HEX_DIGITS[ k & BYTE_MASK ]; + } + + return new String(buffer); + } + + /** + * Inverse of encode exept it do not use path. + * So decode(encode(s) - m_path) = s. + * In other words it returns a String that can be used as key to retive + * the record contained in the 'filename' file. + */ + protected String decode( String filename ) + { + final int size = filename.length(); + final byte[] bytes = new byte[ size >>> 1 ]; + + for( int i = 0, j = 0; i < size; j++ ) + { + bytes[ j ] = Byte.parseByte( filename.substring( i, i + 2 ), 16 ); + i +=2; + } + + return new String( bytes ); + } + }
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]