| 1 |
/*- |
| 2 |
* See the file LICENSE for redistribution information. |
| 3 |
* |
| 4 |
* Copyright (c) 2002, 2013 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
package com.sleepycat.persist.model; |
| 9 |
|
| 10 |
import java.util.ArrayList; |
| 11 |
import java.util.Collections; |
| 12 |
import java.util.List; |
| 13 |
import java.util.Set; |
| 14 |
|
| 15 |
import com.sleepycat.compat.DbCompat; |
| 16 |
import com.sleepycat.persist.EntityStore; |
| 17 |
import com.sleepycat.persist.PrimaryIndex; |
| 18 |
import com.sleepycat.persist.SecondaryIndex; |
| 19 |
import com.sleepycat.persist.impl.Format; |
| 20 |
import com.sleepycat.persist.impl.PersistCatalog; |
| 21 |
import com.sleepycat.persist.impl.RefreshException; |
| 22 |
import com.sleepycat.persist.raw.RawObject; |
| 23 |
import com.sleepycat.persist.raw.RawType; |
| 24 |
import com.sleepycat.util.ClassResolver; |
| 25 |
|
| 26 |
/** |
| 27 |
* The base class for classes that provide entity model metadata. An {@link |
| 28 |
* EntityModel} defines entity classes, primary keys, secondary keys, and |
| 29 |
* relationships between entities. For each entity class that is part of the |
| 30 |
* model, a single {@link PrimaryIndex} object and zero or more {@link |
| 31 |
* SecondaryIndex} objects may be accessed via an {@link EntityStore}. |
| 32 |
* |
| 33 |
* <p>The built-in entity model, the {@link AnnotationModel}, is based on |
| 34 |
* annotations that are added to entity classes and their key fields. |
| 35 |
* Annotations are used in the examples in this package, and it is expected |
| 36 |
* that annotations will normally be used; most readers should therefore skip |
| 37 |
* to the {@link AnnotationModel} class. However, a custom entity model class |
| 38 |
* may define its own metadata. This can be used to define entity classes and |
| 39 |
* keys using mechanisms other than annotations.</p> |
| 40 |
* |
| 41 |
* <p>A concrete entity model class should extend this class and implement the |
| 42 |
* {@link #getClassMetadata}, {@link #getEntityMetadata} and {@link |
| 43 |
* #getKnownClasses} methods.</p> |
| 44 |
* |
| 45 |
* <p>This is an abstract class rather than an interface to allow adding |
| 46 |
* capabilities to the model at a future date without causing |
| 47 |
* incompatibilities. For example, a method may be added in the future for |
| 48 |
* returning new information about the model and subclasses may override this |
| 49 |
* method to return the new information. Any new methods will have default |
| 50 |
* implementations that return default values, and the use of the new |
| 51 |
* information will be optional.</p> |
| 52 |
* |
| 53 |
* @author Mark Hayes |
| 54 |
*/ |
| 55 |
public abstract class EntityModel { |
| 56 |
|
| 57 |
private volatile PersistCatalog catalog; |
| 58 |
private ClassLoader classLoader; |
| 59 |
|
| 60 |
/** |
| 61 |
* The default constructor for use by subclasses. |
| 62 |
*/ |
| 63 |
protected EntityModel() { |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Returns whether the model is associated with an open store. |
| 68 |
* |
| 69 |
* <p>The {@link #registerClass} method may only be called when the model |
| 70 |
* is not yet open. Certain other methods may only be called when the |
| 71 |
* model is open:</p> |
| 72 |
* <ul> |
| 73 |
* <li>{@link #convertRawObject}</li> |
| 74 |
* <li>{@link #getAllRawTypeVersions}</li> |
| 75 |
* <li>{@link #getRawType}</li> |
| 76 |
* <li>{@link #getRawTypeVersion}</li> |
| 77 |
* </ul> |
| 78 |
*/ |
| 79 |
public final boolean isOpen() { |
| 80 |
return catalog != null; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Registers a persistent class, most importantly, a {@link |
| 85 |
* PersistentProxy} class or entity subclass. Also registers an enum or |
| 86 |
* array class. |
| 87 |
* |
| 88 |
* <p>Any persistent class , enum class or array may be registered in |
| 89 |
* advance of using it, to avoid the overhead of updating the catalog |
| 90 |
* database when an instance of the class is first stored. This method |
| 91 |
* <em>must</em> be called in three cases:</p> |
| 92 |
* <ol> |
| 93 |
* <li>to register all {@link PersistentProxy} classes, and</li> |
| 94 |
* <li>to register an entity subclass defining a secondary key, if {@link |
| 95 |
* EntityStore#getSubclassIndex getSubclassIndex} is not called for the |
| 96 |
* subclass, and</li> |
| 97 |
* <li>to register all new enum or array classes, if the these enum or |
| 98 |
* array classes are unknown for DPL but will be used in a Converter |
| 99 |
* mutation. |
| 100 |
* </li> |
| 101 |
* </ol> |
| 102 |
* |
| 103 |
* <p>For example:</p> |
| 104 |
* |
| 105 |
* <pre class="code"> |
| 106 |
* EntityModel model = new AnnotationModel(); |
| 107 |
* model.registerClass(MyProxy.class); |
| 108 |
* model.registerClass(MyEntitySubclass.class); |
| 109 |
* model.registerClass(MyEnum.class); |
| 110 |
* model.registerClass(MyArray[].class); |
| 111 |
* |
| 112 |
* StoreConfig config = new StoreConfig(); |
| 113 |
* ... |
| 114 |
* config.setModel(model); |
| 115 |
* |
| 116 |
* EntityStore store = new EntityStore(..., config);</pre> |
| 117 |
* |
| 118 |
* <p>This method must be called before opening a store based on this |
| 119 |
* model.</p> |
| 120 |
* |
| 121 |
* @throws IllegalStateException if this method is called for a model that |
| 122 |
* is associated with an open store. |
| 123 |
* |
| 124 |
* @throws IllegalArgumentException if the given class is not persistent |
| 125 |
* or has a different class loader than previously registered classes. |
| 126 |
*/ |
| 127 |
public final void registerClass(Class persistentClass) { |
| 128 |
if (catalog != null) { |
| 129 |
throw new IllegalStateException("Store is already open"); |
| 130 |
} else { |
| 131 |
String className = persistentClass.getName(); |
| 132 |
ClassMetadata meta = getClassMetadata(className); |
| 133 |
if (meta == null && |
| 134 |
!persistentClass.isEnum() && |
| 135 |
!persistentClass.isArray()) { |
| 136 |
throw new IllegalArgumentException |
| 137 |
("Class is not persistent, or is not an enum or array: " + |
| 138 |
className); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Internal access method that should not be used by applications. |
| 145 |
* |
| 146 |
* This method is used to initialize the model when catalog creation is |
| 147 |
* complete, and reinitialize it when a Replica refresh occurs. See |
| 148 |
* Store.refresh. |
| 149 |
*/ |
| 150 |
protected void setCatalog(final PersistCatalog newCatalog) { |
| 151 |
this.catalog = newCatalog; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Internal access method that should not be used by applications. |
| 156 |
* |
| 157 |
* This method is called during EntityStore construction, before using the |
| 158 |
* model. |
| 159 |
*/ |
| 160 |
void setClassLoader(final ClassLoader loader) { |
| 161 |
this.classLoader = loader; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Internal access method that should not be used by applications. |
| 166 |
*/ |
| 167 |
ClassLoader getClassLoader() { |
| 168 |
return classLoader; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Returns the metadata for a given persistent class name, including proxy |
| 173 |
* classes and entity classes. |
| 174 |
* |
| 175 |
* @return the metadata or null if the class is not persistent or does not |
| 176 |
* exist. |
| 177 |
*/ |
| 178 |
public abstract ClassMetadata getClassMetadata(String className); |
| 179 |
|
| 180 |
/** |
| 181 |
* Returns the metadata for a given entity class name. |
| 182 |
* |
| 183 |
* @return the metadata or null if the class is not an entity class or does |
| 184 |
* not exist. |
| 185 |
*/ |
| 186 |
public abstract EntityMetadata getEntityMetadata(String className); |
| 187 |
|
| 188 |
/** |
| 189 |
* Returns the names of all known persistent classes. A type becomes known |
| 190 |
* when an instance of the type is stored for the first time or metadata or |
| 191 |
* type information is queried for a specific class name. |
| 192 |
* |
| 193 |
* @return an unmodifiable set of class names. |
| 194 |
* |
| 195 |
* @throws IllegalStateException if this method is called for a model that |
| 196 |
* is not associated with an open store. |
| 197 |
*/ |
| 198 |
public abstract Set<String> getKnownClasses(); |
| 199 |
|
| 200 |
/** |
| 201 |
* Returns the names of all known persistent enum and array classes that |
| 202 |
* may be used to store persistent data. This differs from |
| 203 |
* {@link #getKnownClasses}, which does not return enum and array classes |
| 204 |
* because they have no metadata. |
| 205 |
* |
| 206 |
* @return an unmodifiable set of enum and array class names. |
| 207 |
* |
| 208 |
* @throws IllegalStateException if this method is called for a model that |
| 209 |
* is not associated with an open store. |
| 210 |
*/ |
| 211 |
public Set<String> getKnownSpecialClasses() { |
| 212 |
return Collections.emptySet(); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Returns the type information for the current version of a given class, |
| 217 |
* or null if the class is not currently persistent. |
| 218 |
* |
| 219 |
* @param className the name of the current version of the class. |
| 220 |
* |
| 221 |
* @throws IllegalStateException if this method is called for a model that |
| 222 |
* is not associated with an open store. |
| 223 |
*/ |
| 224 |
public final RawType getRawType(String className) { |
| 225 |
if (catalog != null) { |
| 226 |
return catalog.getFormat(className); |
| 227 |
} else { |
| 228 |
throw new IllegalStateException("Store is not open"); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Returns the type information for a given version of a given class, |
| 234 |
* or null if the given version of the class is unknown. |
| 235 |
* |
| 236 |
* @param className the name of the latest version of the class. |
| 237 |
* |
| 238 |
* @param version the desired version of the class. |
| 239 |
* |
| 240 |
* @throws IllegalStateException if this method is called for a model that |
| 241 |
* is not associated with an open store. |
| 242 |
*/ |
| 243 |
public final RawType getRawTypeVersion(String className, int version) { |
| 244 |
if (catalog != null) { |
| 245 |
Format format = catalog.getLatestVersion(className); |
| 246 |
while (format != null) { |
| 247 |
if (version == format.getVersion()) { |
| 248 |
return format; |
| 249 |
} |
| 250 |
} |
| 251 |
return null; |
| 252 |
} else { |
| 253 |
throw new IllegalStateException("Store is not open"); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Returns all known versions of type information for a given class name, |
| 259 |
* or null if no persistent version of the class is known. |
| 260 |
* |
| 261 |
* @param className the name of the latest version of the class. |
| 262 |
* |
| 263 |
* @return an unmodifiable list of types for the given class name in order |
| 264 |
* from most recent to least recent. |
| 265 |
* |
| 266 |
* @throws IllegalStateException if this method is called for a model that |
| 267 |
* is not associated with an open store. |
| 268 |
*/ |
| 269 |
public final List<RawType> getAllRawTypeVersions(String className) { |
| 270 |
if (catalog != null) { |
| 271 |
Format format = catalog.getLatestVersion(className); |
| 272 |
if (format != null) { |
| 273 |
List<RawType> list = new ArrayList<RawType>(); |
| 274 |
while (format != null) { |
| 275 |
list.add(format); |
| 276 |
format = format.getPreviousVersion(); |
| 277 |
} |
| 278 |
return Collections.unmodifiableList(list); |
| 279 |
} else { |
| 280 |
return null; |
| 281 |
} |
| 282 |
} else { |
| 283 |
throw new IllegalStateException("Store is not open"); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Returns all versions of all known types. |
| 289 |
* |
| 290 |
* @return an unmodifiable list of types. |
| 291 |
* |
| 292 |
* @throws IllegalStateException if this method is called for a model that |
| 293 |
* is not associated with an open store. |
| 294 |
*/ |
| 295 |
public final List<RawType> getAllRawTypes() { |
| 296 |
if (catalog != null) { |
| 297 |
return catalog.getAllRawTypes(); |
| 298 |
} else { |
| 299 |
throw new IllegalStateException("Store is not open"); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Converts a given raw object to a live object according to the current |
| 305 |
* class definitions. |
| 306 |
* |
| 307 |
* <p>The given raw object must conform to the current class definitions. |
| 308 |
* However, the raw type ({@link RawObject#getType}) is allowed to be from |
| 309 |
* a different store, as long as the class names and the value types match. |
| 310 |
* This allows converting raw objects that are read from one store to live |
| 311 |
* objects in another store, for example, in a conversion program.</p> |
| 312 |
*/ |
| 313 |
public final Object convertRawObject(RawObject raw) { |
| 314 |
try { |
| 315 |
return catalog.convertRawObject(raw, null); |
| 316 |
} catch (RefreshException e) { |
| 317 |
e.refresh(); |
| 318 |
try { |
| 319 |
return catalog.convertRawObject(raw, null); |
| 320 |
} catch (RefreshException e2) { |
| 321 |
throw DbCompat.unexpectedException(e2); |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Should be called by entity model implementations instead of calling |
| 328 |
* Class.forName whenever loading an application class. This method honors |
| 329 |
* the BDB JE environment's ClassLoader property and uses {@link |
| 330 |
* ClassResolver} to implement the class loading policy. |
| 331 |
*/ |
| 332 |
public Class resolveClass(String className) |
| 333 |
throws ClassNotFoundException { |
| 334 |
|
| 335 |
return ClassResolver.resolveClass(className, classLoader); |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* @deprecated use {@link #resolveClass} instead. This method does not |
| 340 |
* use the environment's ClassLoader property. |
| 341 |
*/ |
| 342 |
public static Class classForName(String className) |
| 343 |
throws ClassNotFoundException { |
| 344 |
|
| 345 |
try { |
| 346 |
return Class.forName |
| 347 |
(className, true /*initialize*/, |
| 348 |
Thread.currentThread().getContextClassLoader()); |
| 349 |
} catch (ClassNotFoundException e) { |
| 350 |
return Class.forName(className); |
| 351 |
} |
| 352 |
} |
| 353 |
} |