svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libTools / src / org / gvsig / tools / persistence / PersistenceManager.java @ 28468
History | View | Annotate | Download (7.48 KB)
1 |
package org.gvsig.tools.persistence; |
---|---|
2 |
|
3 |
import java.io.Reader; |
4 |
import java.util.Iterator; |
5 |
|
6 |
import org.gvsig.tools.dynobject.DynStruct; |
7 |
import org.gvsig.tools.persistence.validation.ValidationResult; |
8 |
|
9 |
/**
|
10 |
* <p>This interface contains the necessary methods to get a persistent representation
|
11 |
* of an object, suitable for storage or transmission, and to create a an object from
|
12 |
* its persistent representation.</p>
|
13 |
*
|
14 |
* @author The gvSIG project <http://www.gvsig.org>
|
15 |
* @author Cesar Martinez Izquierdo <cesar.martinez@iver.es>
|
16 |
* @author IVER T.I. <http://www.iver.es>
|
17 |
*/
|
18 |
public interface PersistenceManager { |
19 |
/**
|
20 |
* <p>Validation Mode -- MANDATORY: Validation is active, so
|
21 |
* {@link PersistenceManager#create(org.gvsig.tools.persistence.PersistentState)}
|
22 |
* and
|
23 |
* {@link PersistenceManager#getState(org.gvsig.tools.persistence.Persistent)}
|
24 |
* will throw validation exceptions if validation errors are found.</p>
|
25 |
* <p>If an undeclared attribute or class is found, this will be considered
|
26 |
* a validation error.</p>
|
27 |
*/
|
28 |
static public final int MANDATORY = 3; |
29 |
/**
|
30 |
* <p>Validation Mode -- MANDATORY_IF_DECLARED: Validation is active, but
|
31 |
* it will be only applied to Persistent objectswhich have been registered.
|
32 |
* {@link PersistenceManager#create(org.gvsig.tools.persistence.PersistentState)}
|
33 |
* and
|
34 |
* {@link PersistenceManager#getState(org.gvsig.tools.persistence.Persistent)}
|
35 |
* methods will throw validation exceptions if validation errors are found.</p>
|
36 |
*/
|
37 |
static public final int MANDATORY_IF_DECLARED = 2; |
38 |
/**
|
39 |
* <p>Validation Mode - DISABLED: No validation is performed at all.
|
40 |
* In this mode, {@link PersistenceManager#ge}</p>
|
41 |
*/
|
42 |
static public final int DISABLED = 0; |
43 |
|
44 |
/**
|
45 |
* <p>Creates a persistent state from an Persistent object.</p>
|
46 |
*
|
47 |
* @param obj The Persistent object to be persisted
|
48 |
*
|
49 |
* @return A PersistentState object, which stores the state of the
|
50 |
* provided Persistent Object.
|
51 |
* @throws PersistenceException
|
52 |
*/
|
53 |
public PersistentState getState(Persistent obj) throws PersistenceException; |
54 |
|
55 |
/**
|
56 |
* <p>Instantiates an object from a persistent state. The PersistentState object knows
|
57 |
* the class of the persisted object, and instantiates it by using introspection. The
|
58 |
* object must implement the Persistent interface so that it can understand the
|
59 |
* PersistentState.</p>
|
60 |
*
|
61 |
* @param state The state of the object to be instantiated
|
62 |
* @return A new object whose state is the same as the provided <code>state</code> object.
|
63 |
*
|
64 |
* @throws PersistenceException
|
65 |
*/
|
66 |
public Object create(PersistentState state) throws PersistenceException; |
67 |
|
68 |
/**
|
69 |
* <p>Associates an alias with a class. This is similar to a symbolic link, which allows
|
70 |
* to access the class by means of its alias.</p>
|
71 |
*
|
72 |
* <p>When an alias is defined, it replaces any
|
73 |
* class whose qualified name is equal to the alias. Therefore, this class will never
|
74 |
* be instantiated, and instead the class pointed by the the alias will be instantiated.</p>
|
75 |
*
|
76 |
* <p>For example, if the following alias is defined:</p>
|
77 |
*
|
78 |
* <pre>Class aClass = org.gvsig.fmap.mapcontext.rendering.symbols.SimpleMarkerSymbol.class;
|
79 |
* manager.addAlias("org.gvsig.fmap.mapcontext.rendering.symbols.ArrowMarkerSymbol", aClass);
|
80 |
* </pre>
|
81 |
* <p>then, SimpleMarkerSymbol will be instantiated instead of ArrowMarkerSymbol from any
|
82 |
* PersistentState which references ArrowMarkerSymbol.</p>
|
83 |
*
|
84 |
* <p>Aliases are useful to provided backward-compatibility paths (as old unexisting classes
|
85 |
* may be aliased to substitution classes), but are also useful to avoid limitations on
|
86 |
* ClassLoaders. As a Class object is provided, it will be possible to instantiate it even
|
87 |
* if the current ClassLoader has no direct visibility of the class to instantiate.</p>
|
88 |
*
|
89 |
* @param alias The alias to reference a class
|
90 |
* @param aClass The class to be referenced by the alias
|
91 |
*/
|
92 |
public void addAlias(String alias, Class aClass); |
93 |
|
94 |
/**
|
95 |
* <p>Registers persistentClass as Persistent, and associates an attribute definition to that
|
96 |
* class. During a persistence process, the PersistenceManager will validate that a class
|
97 |
* only persists attributes which has been defined in its DynStruct definition.</p>
|
98 |
*
|
99 |
* @param persistentClass The class to register
|
100 |
* @param definition The definition of the attributes allowed for the persistentClass
|
101 |
*/
|
102 |
public void addDefinition(Class persistentClass, DynStruct definition); |
103 |
|
104 |
/**
|
105 |
* <p>De-registers a class which has been previously registered using
|
106 |
* <code>{@link #addDefinition(Class, DynStruct)}</code></p>
|
107 |
*
|
108 |
* @param persistentClass
|
109 |
*/
|
110 |
public void removeDefinition(Class persistentClass); |
111 |
|
112 |
/**
|
113 |
* <p>Validates a persistent state by using the corresponding registered
|
114 |
* attribute definition. If there is no registered definition for the class
|
115 |
* represented by the PersistenteState, validation should fail.</p>
|
116 |
*
|
117 |
* <p>Some manager implementations may not support validation. In this case,
|
118 |
* they should always return a positive validation.</p>
|
119 |
*
|
120 |
* @param state
|
121 |
*
|
122 |
* @return The validation result
|
123 |
*/
|
124 |
public ValidationResult validate(PersistentState state);
|
125 |
|
126 |
/**
|
127 |
* <p>If the provided persistent class has registered an attribute definition in
|
128 |
* this manager, then this method returns that definition. Otherwise, it returns
|
129 |
* null.</p>
|
130 |
*
|
131 |
* @param persistentClass The class whose corresponding attribute definition is
|
132 |
* to be retrieved.
|
133 |
*
|
134 |
* @return The attribute definition corresponding to the provided persistent class,
|
135 |
* or null otherwise.
|
136 |
*/
|
137 |
public DynStruct getDefinition(Class persistentClass); |
138 |
|
139 |
/**
|
140 |
* <p>Gets a list of the registered Persistent classes.</p>
|
141 |
*
|
142 |
* @return An iterator over the registered Persistent classes.
|
143 |
*/
|
144 |
public Iterator getPersistentClasses(); |
145 |
|
146 |
/**
|
147 |
* <p>De-serializes an state from the data read from the provided
|
148 |
* <code>reader</code>. Depending on the implementation the serialized
|
149 |
* data may have different formats, such as XML or binary data.</p>
|
150 |
*
|
151 |
* <p>Note that a particular implementation will only be able to
|
152 |
* de-serialize data which has been serialized by the same
|
153 |
* implementation.</p>
|
154 |
*
|
155 |
* @param reader
|
156 |
* @return
|
157 |
*/
|
158 |
public PersistentState loadState(Reader reader) throws PersistenceException; |
159 |
|
160 |
/**
|
161 |
* <p>Sets the validation which will be applied in
|
162 |
* {@link #getState(Persistent)}, {@link #create(PersistentState)}
|
163 |
* methods. Validation ensures that persisted or de-persisted objects
|
164 |
* match the declared definition (which must have been previously
|
165 |
* registered by using {@link #addDefinition(Class, DynStruct)}).</p>
|
166 |
*
|
167 |
* <p>When automatic validation is enabled (MANDATORY or
|
168 |
* MANDATORY_IF_DECLARED), a ValidationException will be thrown by
|
169 |
* {@link #getState(Persistent)}, {@link #create(PersistentState)}
|
170 |
* if a validation error is found.</p>
|
171 |
*
|
172 |
* @param validationMode On of the following values:
|
173 |
* {@link #DISABLED}, {@link #MANDATORY}
|
174 |
* or {@link #MANDATORY_IF_DECLARED}
|
175 |
|
176 |
* @see #validate(PersistentState)
|
177 |
* @see #addDefinition(Class, DynStruct)
|
178 |
*
|
179 |
* @throws PersistenceException If the mode is not supported by this manager
|
180 |
*/
|
181 |
public void setAutoValidation(int validationMode) throws PersistenceException; |
182 |
|
183 |
/**
|
184 |
* <p>Gets the validation which will be applied in
|
185 |
* {@link #getState(Persistent)}, {@link #create(PersistentState)} methods.
|
186 |
*
|
187 |
* @return The current mode for automatic validation: {@link #DISABLED},
|
188 |
* {@link #MANDATORY} or {@link #MANDATORY_IF_DECLARED}
|
189 |
*
|
190 |
* @see #validate(PersistentState)
|
191 |
* @see #addDefinition(Class, DynStruct)
|
192 |
*/
|
193 |
public int getAutoValidation(); |
194 |
|
195 |
} |