KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > storage > gammaStore > ObjectStreamClasses


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Core License version 1 published by ozone-db.org.
3
//
4
// Copyright (C) 2003-@year@, Leo Mekenkamp. All rights reserved.
5
//
6
// $Id: ObjectStreamClasses.java,v 1.1.2.1 2004/04/10 10:06:51 per_nyfelt Exp $
7

8 package org.ozoneDB.core.storage.gammaStore;
9
10 import java.io.ByteArrayOutputStream JavaDoc;
11 import java.io.File JavaDoc;
12 import java.io.FileInputStream JavaDoc;
13 import java.io.FileOutputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.ObjectInputStream JavaDoc;
16 import java.io.ObjectOutputStream JavaDoc;
17 import java.io.ObjectStreamClass JavaDoc;
18 import java.io.ObjectStreamField JavaDoc;
19 import java.security.Key JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Properties JavaDoc;
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 /**
33  * Takes care of storing ObjectStreamClass instances into another medium, while
34  * providing for a replacement in the form of an int.
35  *
36  * @author Leo
37  */

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 JavaDoc[] {
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 JavaDoc handleToOsc = new HashMap JavaDoc();
55     
56     private Map JavaDoc keyToHandle = new HashMap JavaDoc();
57     
58     private String JavaDoc prefix;
59     
60     private File JavaDoc directory;
61     
62     /**
63      *
64      * @throws ConfigurationException if error in properties
65      * @throws IOException if directory could not be created (initialize == true)
66      * @throws IOException if file in directory could not be read (initialize == false)
67      * @throws ClassNotFoundException invalid file in directory (initialize == false)
68      * @throws NumberFormatException invalid file in directory (initialize == false)
69      */

70     public ObjectStreamClasses(Properties JavaDoc properties, String JavaDoc prefix, boolean initialize) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
71         this.prefix = prefix;
72         String JavaDoc 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 JavaDoc(dirStr);
77         if (!directory.isAbsolute()) {
78             directory = new File JavaDoc(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 JavaDoc("could not create " + directory);
86             }
87         } else {
88             File JavaDoc[] files = directory.listFiles();
89             for (int i = 0; i < files.length; i++) {
90                 ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(new FileInputStream JavaDoc(files[i]));
91                 Integer JavaDoc handle = Integer.valueOf(files[i].getName());
92                 ObjectStreamClass JavaDoc objectStreamClass = (ObjectStreamClass JavaDoc) in.readObject();
93                 handleToOsc.put(handle, objectStreamClass);
94                 keyToHandle.put(toImage(objectStreamClass), handle);
95             }
96         }
97     }
98
99     public String JavaDoc getPrefix() {
100         return prefix;
101     }
102
103     /**
104      * Converts this instance to a byte[] that can be directly saved to disk or
105      * used to find the handle of an ObjectStreamClass.
106      */

107     public static byte[] toImage(ObjectStreamClass JavaDoc objectStreamClass) {
108         try {
109             ByteArrayOutputStream JavaDoc buf = new ByteArrayOutputStream JavaDoc();
110             ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buf);
111             out.writeObject(objectStreamClass);
112             out.close();
113             return buf.toByteArray();
114         } catch (IOException JavaDoc e) {
115             throw new RuntimeException JavaDoc(e);
116         }
117     }
118     
119     /**
120      * Retrieves the ObjectStreamClass given its handle.
121      */

122     public ObjectStreamClass JavaDoc getObjectStreamClass(int handle) {
123         return (ObjectStreamClass JavaDoc) handleToOsc.get(new Integer JavaDoc(handle));
124     }
125     
126     /**
127      * Gives the handle for a particular ObjectStreamClass. This handle can be
128      * used as a unique identifier. If the given <code>ObjectStreamClass</code>
129      * is unknown a new handle is allocated and returned. When a new handle is
130      * returned the same handle will always be returned for that same instance.
131      */

132     public int getHandle(ObjectStreamClass JavaDoc objectStreamClass) {
133         byte[] image = toImage(objectStreamClass);
134         Integer JavaDoc handle = (Integer JavaDoc) handleToOsc.get(image);
135         if (handle != null) {
136             return handle.intValue();
137         }
138         int result = handleToOsc.size();
139         handleToOsc.put(image, new Integer JavaDoc(result));
140         try {
141             FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(new File JavaDoc(directory, Integer.toString(result)));
142             out.write(image);
143             out.close();
144         } catch (IOException JavaDoc e) {
145             throw new RuntimeException JavaDoc(e);
146         }
147         return result;
148     }
149     
150     public Collection JavaDoc getPropertyInfos() {
151         Collection JavaDoc result = new LinkedList JavaDoc();
152         result.add(DIRECTORY);
153         return result;
154     }
155     
156 }
157
Popular Tags