1 2 5 14 package org.jacorb.trading.db.simple.types; 15 16 import java.io.*; 17 import java.util.*; 18 import org.omg.CORBA.*; 19 import org.omg.CosTradingRepos.ServiceTypeRepositoryPackage.*; 20 import org.jacorb.trading.db.TypeDatabase; 21 import org.jacorb.trading.util.RWLock; 22 23 24 public class TypeDatabaseImpl implements TypeDatabase 25 { 26 private Incarnation m_incarnation; 27 private Hashtable m_types; 28 private RWLock m_lock; 29 private File m_dbFile; 30 private boolean m_dirty = false; 31 32 private static final String DB_FILE = "types.dat"; 33 34 35 private TypeDatabaseImpl() 36 { 37 } 38 39 40 public TypeDatabaseImpl(String dirName) 41 { 42 m_dbFile = new File(dirName, DB_FILE); 43 44 if (m_dbFile.exists()) 45 readObjects(); 46 else { 47 m_incarnation = new Incarnation(); 48 m_types = new Hashtable(); 49 writeObjects(); 50 } 51 52 m_lock = new RWLock(); 53 } 54 55 56 public void begin(int mode) 57 { 58 if (mode == TypeDatabase.READ) 59 m_lock.acquireRead(); 60 else if (mode == TypeDatabase.WRITE) 61 m_lock.acquireWrite(); 62 else 63 throw new RuntimeException ("Invalid lock mode"); 64 } 65 66 67 public void end() 68 { 69 if (m_dirty) { 71 writeObjects(); 72 m_dirty = false; 73 } 74 75 m_lock.release(); 76 } 77 78 79 public TypeStruct describeType(String name) 80 { 81 TypeStruct result = null; 82 83 Type impl = (Type)m_types.get(name); 84 if (impl != null) 85 result = impl.describe(); 86 87 return result; 88 } 89 90 91 public boolean maskType(String name) 92 { 93 boolean result = false; 94 95 Type impl = (Type)m_types.get(name); 96 if (impl != null) { 97 impl.mask(); 98 m_dirty = true; 99 result = true; 100 } 101 102 return result; 103 } 104 105 106 public boolean unmaskType(String name) 107 { 108 boolean result = false; 109 110 Type impl = (Type)m_types.get(name); 111 if (impl != null) { 112 impl.unmask(); 113 m_dirty = true; 114 result = true; 115 } 116 117 return result; 118 } 119 120 121 public String [] getTypes() 122 { 123 String [] result = new String [m_types.size()]; 124 125 Enumeration e = m_types.keys(); 126 int count = 0; 127 while (e.hasMoreElements()) 128 result[count++] = (String )e.nextElement(); 129 130 return result; 131 } 132 133 134 public String [] getTypesSince(IncarnationNumber inc) 135 { 136 String [] result; 137 138 Vector types = new Vector(); 139 140 Incarnation i = new Incarnation(inc); 141 142 Enumeration e = m_types.elements(); 143 while (e.hasMoreElements()) { 144 Type type = (Type)e.nextElement(); 145 if (type.getIncarnation().compareTo(i) >= 0) 146 types.addElement(type.getName()); 147 } 148 149 result = new String [types.size()]; 150 types.copyInto((java.lang.Object [])result); 151 152 return result; 153 } 154 155 156 public IncarnationNumber getIncarnation() 157 { 158 return m_incarnation.getIncarnationNumber(); 159 } 160 161 162 public IncarnationNumber createType( 163 String name, 164 String interfaceName, 165 PropStruct[] props, 166 String [] superTypes) 167 { 168 IncarnationNumber result = null; 169 170 result = m_incarnation.getIncarnationNumber(); 172 Type type = new Type(name, interfaceName, props, superTypes, result); 173 m_types.put(name, type); 174 m_incarnation.increment(); 175 m_dirty = true; 176 177 return result; 178 } 179 180 181 public boolean removeType(String name) 182 { 183 boolean result = false; 184 185 if (m_types.containsKey(name)) { 186 m_types.remove(name); 187 m_dirty = true; 188 result = true; 189 } 190 191 return result; 192 } 193 194 195 public String findSubType(String name) 196 { 197 String result = null; 198 199 Enumeration e = m_types.elements(); 200 while (e.hasMoreElements() && result == null) { 201 Type type = (Type)e.nextElement(); 202 String [] superTypes = type.getSuperTypes(); 203 for (int t = 0; t < superTypes.length; t++) { 204 if (name.equals(superTypes[t])) { 205 result = type.getName(); 206 break; 207 } 208 } 209 } 210 211 return result; 212 } 213 214 215 public String [] getAllSuperTypes(String name) 216 { 217 String [] result = null; 218 219 Type type = (Type)m_types.get(name); 220 if (type != null) { 221 Vector vec = findAllSuperTypes(type); 222 223 result = new String [vec.size()]; 224 vec.copyInto((java.lang.Object [])result); 225 } 226 227 return result; 228 } 229 230 231 protected void readObjects() 232 { 233 try { 234 FileInputStream fileIn = new FileInputStream(m_dbFile); 235 ObjectInputStream objIn = new ObjectInputStream(fileIn); 236 m_incarnation = (Incarnation)objIn.readObject(); 237 m_types = (Hashtable)objIn.readObject(); 238 fileIn.close(); 239 } 240 catch (IOException e) { 241 throw new RuntimeException (e.getMessage()); 242 } 243 catch (ClassNotFoundException e) { 244 throw new RuntimeException (e.getMessage()); 245 } 246 } 247 248 249 protected void writeObjects() 250 { 251 try { 252 FileOutputStream fileOut = new FileOutputStream(m_dbFile); 253 ObjectOutputStream objOut = new ObjectOutputStream(fileOut); 254 objOut.writeObject(m_incarnation); 255 objOut.writeObject(m_types); 256 fileOut.close(); 257 } 258 catch (IOException e) { 259 throw new RuntimeException (e.getMessage()); 260 } 261 } 262 263 264 268 protected Vector findAllSuperTypes(Type type) 269 { 270 Vector result = new Vector(); 271 272 String [] supers = type.getSuperTypes(); 274 275 for (int i = 0; i < supers.length; i++) { 277 result.addElement(supers[i]); 278 Type impl = (Type)m_types.get(supers[i]); 279 Vector v = findAllSuperTypes(impl); 280 Enumeration e = v.elements(); 281 while (e.hasMoreElements()) { 282 String s = (String )e.nextElement(); 283 if (! result.contains(s)) 284 result.addElement(s); 285 } 286 } 287 288 return result; 289 } 290 } 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | Popular Tags |