KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > persist > impl > PersistEntityBinding


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: PersistEntityBinding.java,v 1.16 2006/10/30 21:14:32 bostic Exp $
7  */

8
9 package com.sleepycat.persist.impl;
10
11 import com.sleepycat.bind.EntityBinding;
12 import com.sleepycat.bind.tuple.TupleBase;
13 import com.sleepycat.je.DatabaseEntry;
14 import com.sleepycat.persist.raw.RawObject;
15
16 /**
17  * A persistence entity binding for a given entity class.
18  *
19  * @author Mark Hayes
20  */

21 public class PersistEntityBinding implements EntityBinding {
22
23     Catalog catalog;
24     Format entityFormat;
25     boolean rawAccess;
26     PersistKeyAssigner keyAssigner;
27
28     /**
29      * Creates a key binding for a given entity class.
30      */

31     public PersistEntityBinding(Catalog catalog,
32                                 String JavaDoc entityClassName,
33                                 boolean rawAccess) {
34         this.catalog = catalog;
35         if (rawAccess) {
36             entityFormat = catalog.getFormat(entityClassName);
37             if (entityFormat == null || !entityFormat.isEntity()) {
38                 throw new IllegalArgumentException JavaDoc
39                     ("Not an entity class: " + entityClassName);
40             }
41         } else {
42             Class JavaDoc entityCls;
43             try {
44                 entityCls = Class.forName(entityClassName);
45             } catch (ClassNotFoundException JavaDoc e) {
46                 throw new IllegalArgumentException JavaDoc(e);
47             }
48             entityFormat = catalog.getFormat(entityCls);
49         }
50         this.rawAccess = rawAccess;
51     }
52
53     public PersistKeyAssigner getKeyAssigner() {
54         return keyAssigner;
55     }
56
57     public Object JavaDoc entryToObject(DatabaseEntry key, DatabaseEntry data) {
58         return readEntity(catalog, key, data, rawAccess);
59     }
60
61     /**
62      * Creates the instance, reads the entity key first to track visited
63      * entities correctly, then reads the data and returns the entity.
64      *
65      * This is a special case of EntityInput.readObject for a top level entity.
66      * Special treatments are:
67      * - The formatId must be >= 0; since this is the top level instance, it
68      * cannot refer to a visited object nor be a null reference.
69      * - The resulting entity is not added to the visited object set; entities
70      * cannot be referenced by another (or the same) entity.
71      * - Reader.readPriKey must be called prior to calling Reader.readObject.
72      */

73     static Object JavaDoc readEntity(Catalog catalog,
74                              DatabaseEntry key,
75                              DatabaseEntry data,
76                              boolean rawAccess) {
77         RecordInput keyInput = new RecordInput
78             (catalog, rawAccess, null, 0,
79              key.getData(), key.getOffset(), key.getSize());
80         RecordInput dataInput = new RecordInput
81             (catalog, rawAccess, null, 0,
82              data.getData(), data.getOffset(), data.getSize());
83         int formatId = dataInput.readPackedInt();
84         Format format = catalog.getFormat(formatId);
85         Reader reader = format.getReader();
86         Object JavaDoc entity = reader.newInstance(dataInput, rawAccess);
87         reader.readPriKey(entity, keyInput, rawAccess);
88         return reader.readObject(entity, dataInput, rawAccess);
89     }
90
91     public void objectToData(Object JavaDoc entity, DatabaseEntry data) {
92         Format format = getValidFormat(entity);
93         writeEntity(format, catalog, entity, data, rawAccess);
94     }
95
96     /**
97      * Writes the formatId and object, and returns the bytes.
98      *
99      * This is a special case of EntityOutput.writeObject for a top level
100      * entity. Special treatments are:
101      * - The entity may not be null.
102      * - The entity is not added to the visited object set nor checked for
103      * existence in the visited object set; entities cannot be referenced by
104      * another (or the same) entity.
105      */

106     static void writeEntity(Format format,
107                             Catalog catalog,
108                             Object JavaDoc entity,
109                             DatabaseEntry data,
110                             boolean rawAccess) {
111         RecordOutput output = new RecordOutput(catalog, rawAccess);
112         output.writePackedInt(format.getId());
113         format.writeObject(entity, output, rawAccess);
114         TupleBase.outputToEntry(output, data);
115     }
116
117     public void objectToKey(Object JavaDoc entity, DatabaseEntry key) {
118
119         /*
120          * Write the primary key field as a special case since the output
121          * format is for a key binding, not entity data.
122          */

123         Format format = getValidFormat(entity);
124         RecordOutput output = new RecordOutput(catalog, rawAccess);
125
126         /* Write the primary key and return the bytes. */
127         format.writePriKey(entity, output, rawAccess);
128         TupleBase.outputToEntry(output, key);
129     }
130
131     /**
132      * Returns the format for the given entity and validates it, throwing an
133      * exception if it is invalid for this binding.
134      */

135     private Format getValidFormat(Object JavaDoc entity) {
136
137         /* A null entity is not allowed. */
138         if (entity == null) {
139             throw new IllegalArgumentException JavaDoc("An entity may not be null");
140         }
141
142         /*
143          * Get the format. getFormat throws IllegalArgumentException if the
144          * class is not persistent.
145          */

146         Format format;
147         if (rawAccess) {
148             if (!(entity instanceof RawObject)) {
149                 throw new IllegalArgumentException JavaDoc
150                     ("Entity must be a RawObject");
151             }
152             format = (Format) ((RawObject) entity).getType();
153         } else {
154             format = catalog.getFormat(entity.getClass());
155         }
156
157         /* Check that the entity class/subclass is valid for this binding. */
158         if (format.getEntityFormat() != entityFormat) {
159             throw new IllegalArgumentException JavaDoc
160                 ("The entity class (" + format.getClassName() +
161                  ") must be this entity class or a subclass of it: " +
162                  entityFormat.getClassName());
163         }
164
165         return format;
166     }
167 }
168
Popular Tags