1 8 package org.ozoneDB.core.storage.gammaStore; 9 10 import java.io.ByteArrayOutputStream ; 11 import java.io.File ; 12 import java.io.FileInputStream ; 13 import java.io.FileOutputStream ; 14 import java.io.IOException ; 15 import java.io.ObjectInputStream ; 16 import java.io.ObjectOutputStream ; 17 import java.io.ObjectStreamClass ; 18 import java.io.ObjectStreamField ; 19 import java.security.Key ; 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 import java.util.LinkedList ; 23 import java.util.Map ; 24 import java.util.Properties ; 25 import org.ozoneDB.OzoneObject; 26 import org.ozoneDB.OzoneProxy; 27 import org.ozoneDB.core.ConfigurationException; 28 import org.ozoneDB.core.storage.PropertyConfigurable; 29 import org.ozoneDB.core.storage.PropertyInfo; 30 import org.ozoneDB.core.storage.wizardStore.Cluster; 31 32 38 public class ObjectStreamClasses implements PropertyConfigurable { 39 40 public static final PropertyInfo DIRECTORY = new PropertyInfo( 41 ".classDesc", 42 "string (path)", 43 null, 44 "directory to store class descriptions, either relative to the database directory or an absolute path", 45 new String [] { 46 "/var/ozone/classdesc (*nix absolute path)", 47 "c:\\ozoneFiles\\classdesc (windows absolute path)", 48 "classdesc (*nix or windows relative path in database dir)", 49 "./classdesc (*nix relative path in database dir)", 50 ".\\classdesc (windows relative path in database dir)", 51 } 52 ); 53 54 private Map handleToOsc = new HashMap (); 55 56 private Map keyToHandle = new HashMap (); 57 58 private String prefix; 59 60 private File directory; 61 62 70 public ObjectStreamClasses(Properties properties, String prefix, boolean initialize) throws IOException , ClassNotFoundException { 71 this.prefix = prefix; 72 String dirStr = properties.getProperty(getPrefix() + DIRECTORY.getKey(), DIRECTORY.getDefaultValue()); 73 if (dirStr == null) { 74 throw new ConfigurationException(getPrefix() + DIRECTORY.getKey() + " has not been specified"); 75 } 76 directory = new File (dirStr); 77 if (!directory.isAbsolute()) { 78 directory = new File (properties.getProperty(GammaStore.DIRECTORY.getKey()), dirStr); 79 } 80 if (initialize) { 81 if (directory.exists()) { 82 throw new ConfigurationException("directory " + directory + " already exists"); 83 } 84 if (!directory.mkdirs()) { 85 throw new IOException ("could not create " + directory); 86 } 87 } else { 88 File [] files = directory.listFiles(); 89 for (int i = 0; i < files.length; i++) { 90 ObjectInputStream in = new ObjectInputStream (new FileInputStream (files[i])); 91 Integer handle = Integer.valueOf(files[i].getName()); 92 ObjectStreamClass objectStreamClass = (ObjectStreamClass ) in.readObject(); 93 handleToOsc.put(handle, objectStreamClass); 94 keyToHandle.put(toImage(objectStreamClass), handle); 95 } 96 } 97 } 98 99 public String getPrefix() { 100 return prefix; 101 } 102 103 107 public static byte[] toImage(ObjectStreamClass objectStreamClass) { 108 try { 109 ByteArrayOutputStream buf = new ByteArrayOutputStream (); 110 ObjectOutputStream out = new ObjectOutputStream (buf); 111 out.writeObject(objectStreamClass); 112 out.close(); 113 return buf.toByteArray(); 114 } catch (IOException e) { 115 throw new RuntimeException (e); 116 } 117 } 118 119 122 public ObjectStreamClass getObjectStreamClass(int handle) { 123 return (ObjectStreamClass ) handleToOsc.get(new Integer (handle)); 124 } 125 126 132 public int getHandle(ObjectStreamClass objectStreamClass) { 133 byte[] image = toImage(objectStreamClass); 134 Integer handle = (Integer ) handleToOsc.get(image); 135 if (handle != null) { 136 return handle.intValue(); 137 } 138 int result = handleToOsc.size(); 139 handleToOsc.put(image, new Integer (result)); 140 try { 141 FileOutputStream out = new FileOutputStream (new File (directory, Integer.toString(result))); 142 out.write(image); 143 out.close(); 144 } catch (IOException e) { 145 throw new RuntimeException (e); 146 } 147 return result; 148 } 149 150 public Collection getPropertyInfos() { 151 Collection result = new LinkedList (); 152 result.add(DIRECTORY); 153 return result; 154 } 155 156 } 157 | Popular Tags |