Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / persistence / PersistenceManager.java @ 1447

History | View | Annotate | Download (16.9 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.tools.persistence;
25

    
26
import java.io.InputStream;
27
import java.io.OutputStream;
28
import java.util.List;
29
import java.util.Map;
30

    
31
import org.gvsig.tools.dynobject.DynStruct;
32
import org.gvsig.tools.persistence.exception.AddDefinitionException;
33
import org.gvsig.tools.persistence.exception.PersistenceClassNotRegistered;
34
import org.gvsig.tools.persistence.exception.PersistenceCreateException;
35
import org.gvsig.tools.persistence.exception.PersistenceException;
36
import org.gvsig.tools.persistence.exception.PersistenceValidateExceptions;
37

    
38
/**
39
 * <p>
40
 * This interface contains the necessary methods to get a persistent
41
 * representation of an object, suitable for storage or transmission, and to
42
 * create a an object from its persistent representation.
43
 * </p>
44
 * 
45
 * @author The gvSIG project <http://www.gvsig.org>
46
 * @author Cesar Martinez Izquierdo <cesar.martinez@iver.es>
47
 * @author IVER T.I. <http://www.iver.es>
48
 */
49
public interface PersistenceManager {
50

    
51
        public interface Factories extends List {
52

    
53
                /**
54
                 * Add a new factory to the list registering an alias.
55
                 * 
56
                 * If factory already registered anly add the alias.
57
                 * 
58
                 * @param factory
59
                 * @param classNameAlias
60
                 * @return true when ok.
61
                 */
62
                public boolean add(PersistenceFactory factory, String classNameAlias);
63

    
64
                /**
65
                 * Add a new class to the factory.
66
                 * 
67
                 * @param factory
68
                 * @return true when ok
69
                 */
70
                public boolean add(PersistenceFactory factory);
71

    
72
                /**
73
                 * Return factory associated to class passed as parameter.
74
                 * 
75
                 * @param class name
76
                 * 
77
                 * @return Persistence factory that manage the class
78
                 */
79
                public abstract PersistenceFactory get(Class theClass);
80

    
81
                /**
82
                 * Return factory associated to the object passed as parameter.
83
                 * 
84
                 * @param object
85
                 * 
86
                 * @return Persistence factory that manage object
87
                 */
88
                public PersistenceFactory get(Object object);
89

    
90
                /**
91
                 * Return factory associated to the object stored in the state.
92
                 * 
93
                 * @param state
94
                 * 
95
                 * @return Persistence factory that manage the state
96
                 */
97
                public PersistenceFactory get(PersistentState state);
98

    
99
        }
100

    
101
        /**
102
         * The namespace used on the definition of DynClasses used by the
103
         * persistence manager.
104
         */
105
        public static final String PERSISTENCE_NAMESPACE = "Persistence";
106
        public static final String PERSISTENCE_DYNOBJECT_NAMESPACE = "PersistenceDynObject";
107
        public static final String DEFAULT_DOMAIN_URL = "http://www.gvsig.org";
108
        public static final String DEFAULT_DOMAIN_NAME = "gvsig";
109

    
110
        /**
111
         * <p>
112
         * Validation Mode -- MANDATORY: Validation is active, so
113
         * {@link PersistenceManager#create(org.gvsig.tools.persistence.PersistentState)}
114
         * and {@link PersistenceManager#getState(Object)} will throw validation
115
         * exceptions if validation errors are found.
116
         * </p>
117
         * <p>
118
         * If an undeclared attribute or class is found, this will be considered a
119
         * validation error.
120
         * </p>
121
         */
122
        static public final int MANDATORY = 3;
123
        /**
124
         * <p>
125
         * Validation Mode -- MANDATORY_IF_DECLARED: Validation is active, but it
126
         * will be only applied to Persistent objects which have been registered.
127
         * {@link PersistenceManager#create(org.gvsig.tools.persistence.PersistentState)}
128
         * and {@link PersistenceManager#getState(Object)} methods will throw
129
         * validation exceptions if validation errors are found.
130
         * </p>
131
         */
132
        static public final int MANDATORY_IF_DECLARED = 2;
133
        /**
134
         * <p>
135
         * Validation Mode - DISABLED: No validation is performed at all. In this
136
         * mode, {@link PersistenceManager#ge}
137
         * </p>
138
         */
139
        static public final int DISABLED = 0;
140

    
141
        /**
142
         * <p>
143
         * Creates a persistent state from an Persistent object.
144
         * </p>
145
         * 
146
         * @param obj
147
         *            The Persistent object to be persisted
148
         * 
149
         * @return A PersistentState object, which stores the state of the provided
150
         *         Persistent Object.
151
         * @throws PersistenceException
152
         */
153
        public PersistentState getState(Object obj) throws PersistenceException;
154

    
155
        /**
156
         * Creates a persistent state from an Persistent object.
157
         *
158
         * When collectAllErrors is true, try to continue when an error
159
         * is detected, and raise all errors when finish the process.
160
         * 
161
         * @param obj
162
         * @param collectAllErrors
163
         * @return
164
         * @throws PersistenceException
165
         */
166
        public PersistentState getState(Object obj, boolean collectAllErrors) throws PersistenceException;
167

    
168
        /**
169
         * <p>
170
         * Instantiates an object from a persistent state. The PersistentState
171
         * object knows the class of the persisted object, and instantiates it by
172
         * using introspection. The object must implement the Persistent interface
173
         * so that it can understand the PersistentState.
174
         * </p>
175
         * 
176
         * @param state
177
         *            The state of the object to be instantiated
178
         * @return A new object whose state is the same as the provided
179
         *         <code>state</code> object.
180
         * 
181
         * @throws PersistenceException
182
         * @throws PersistenceCreateException
183
         */
184
        public Object create(PersistentState state) throws PersistenceException,
185
                        PersistenceCreateException;
186

    
187
        /**
188
         * <p>
189
         * Associates an alias with a class. This is similar to a symbolic link,
190
         * which allows to access the class by means of its alias.
191
         * </p>
192
         * 
193
         * <p>
194
         * When an alias is defined, it replaces any class whose qualified name is
195
         * equal to the alias. Therefore, this class will never be instantiated, and
196
         * instead the class pointed by the the alias will be instantiated.
197
         * </p>
198
         * 
199
         * <p>
200
         * For example, if the following alias is defined:
201
         * </p>
202
         * 
203
         * <pre>
204
         * Class aClass = org.gvsig.fmap.mapcontext.rendering.symbols.SimpleMarkerSymbol.class;
205
         * manager.addAlias(
206
         *                 &quot;org.gvsig.fmap.mapcontext.rendering.symbols.ArrowMarkerSymbol&quot;, aClass);
207
         * </pre>
208
         * <p>
209
         * then, SimpleMarkerSymbol will be instantiated instead of
210
         * ArrowMarkerSymbol from any PersistentState which references
211
         * ArrowMarkerSymbol.
212
         * </p>
213
         * 
214
         * <p>
215
         * Aliases are useful to provided backward-compatibility paths (as old no
216
         * existing classes may be aliased to substitution classes), but are also
217
         * useful to avoid limitations on ClassLoaders. As a Class object is
218
         * provided, it will be possible to instantiate it even if the current
219
         * ClassLoader has no direct visibility of the class to instantiate.
220
         * </p>
221
         * 
222
         * @param alias
223
         *            The alias to reference a class
224
         * @param aClass
225
         *            The class to be referenced by the alias
226
         * 
227
         * @throws PersistenceClassNotRegistered
228
         *             if <code>aClass</code> is not registered
229
         */
230
        public void addAlias(String alias, Class theClass)
231
                        throws PersistenceClassNotRegistered;
232

    
233
        /**
234
         * Add a new definition to the class in the persistent manager.
235
         * 
236
         * The new definition is defined in the namespace by default for DynClasses
237
         * used in persistence.
238
         * 
239
         * if domainName or domainURL are null, values by default arent supplied by
240
         * the manager.
241
         * 
242
         * <b>Note:</b> A <code>domainName</code> can by in only one
243
         * <code>domainURL</code>. If you try to register the same
244
         * <code>domainName</code> with two URL an exception will be raised. </p>
245
         * 
246
         * @param persistentClass
247
         * @param name
248
         *            of the new definition
249
         * @param description
250
         *            of the new definition, can be null.
251
         * @param domainName
252
         *            , can be null.
253
         * @param domainURL
254
         *            , can be null.
255
         * @return the new definition
256
         */
257
        public DynStruct addDefinition(Class persistentClass, String name,
258
                        String description, String domainName, String domainURL)
259
                        throws AddDefinitionException;
260

    
261
        
262
        /**
263
         * Add a new definition to the class in the persistent manager.
264
         * 
265
         * @param theClass
266
         *            to be persistent
267
         * @param name
268
         *            of the class definition to associate to theClass
269
         * @param definitions
270
         *            input stream from load the definitions of classes
271
         * @param loader
272
         *            , loader to resolve references to classes
273
         * @param domainName
274
         *            (can be null)
275
         * @param domainUrl
276
         *            (can be null)
277
         * @return
278
         * 
279
         * @see #addDefinition(Class, String, String, String, String)
280
         */
281
        public DynStruct addDefinition(Class theClass, String name,
282
                        InputStream definitions, ClassLoader loader, String domainName,
283
                        String domainUrl) throws AddDefinitionException;
284

    
285
        /**
286
         * Add a new definition to the class in the persistent manager.
287
         * 
288
         * @param theClass
289
         *            to be persistent
290
         * @param name
291
         *            of the class definition to associate to theClass
292
         * @param definitions
293
         *            , map from get the definition of the class
294
         * @param domainName
295
         *            (can be null)
296
         * @param domainUrl
297
         *            (can be null)
298
         * @return
299
         * 
300
         * @see #addDefinition(Class, String, String, String, String)
301
         */
302
        public DynStruct addDefinition(Class theClass, String name,
303
                        Map definitions, String domainName, String domainUrl)
304
                        throws AddDefinitionException;
305

    
306
        /**
307
         * Add a new definition to the class in the persistent manager.
308
         * 
309
         * @param definition
310
         *            , TODO
311
         * @param domainName
312
         *            (can be null)
313
         * @param domainUrl
314
         *            (can be null)
315
         * @return
316
         * 
317
         * @see #addDefinition(Class, String, String, String, String)
318
         */
319
        public DynStruct addDefinition(DynStruct definition, String domainName,
320
                        String domainUrl)
321
                        throws AddDefinitionException;
322

    
323
        /**
324
         * <p>
325
         * Register an instance of {@link PersistenceFactory} that can manage some
326
         * objects that requires an especial construction or persistence. This
327
         * factory will use the default domain name and URL.
328
         * </p>
329
         * 
330
         * 
331
         * @param factory
332
         */
333
        public void registerFactory(PersistenceFactory factory);
334

    
335
        public void unregisterFactory(PersistenceFactory factory);
336

    
337
        /**
338
         * <p>
339
         * Unregister a registered class from the manager.
340
         * </p>
341
         * 
342
         * @param persistentClass
343
         * 
344
         * @see #registerClass(Class, DynStruct, String)
345
         * 
346
         * @throws PersistenceClassNotRegistered
347
         *             if <code>persistentClass</code> is not registered
348
         */
349
        public void unregisterClass(Class theClass)
350
                        throws PersistenceClassNotRegistered;
351

    
352
        /**
353
         * <p>
354
         * Unregister a registered java class from the manager.
355
         * </p>
356
         * 
357
         * @param className the java class name to unregister
358
         * 
359
         * @see #registerClass(Class)
360
         * 
361
         * @throws PersistenceClassNotRegistered
362
         *             if <code>className</code> is not registered
363
         */
364
        public void unregisterClass(String className)
365
                        throws PersistenceClassNotRegistered;
366

    
367
        /**
368
         * <p>
369
         * Validates persistent state by using the corresponding registered
370
         * attribute definition. <code>mode</code> specifies what to do when a
371
         * definition of a state class is not registered.
372
         * </p>
373
         * 
374
         * @param state
375
         * @param mode
376
         * @throws PersistenceValidateExceptions
377
         * 
378
         */
379
        public void validate(PersistentState state, int mode)
380
                        throws PersistenceValidateExceptions;
381

    
382
        /**
383
         * <p>
384
         * Validates all persistent state related to the <code>state</code> by using
385
         * the corresponding registered attribute definition. <code>mode</code>
386
         * specifies what to do when a definition of a state class is not
387
         * registered.
388
         * </p>
389
         * 
390
         * @param state
391
         * @param mode
392
         * 
393
         * @throws PersistenceValidateExceptions
394
         * 
395
         * @see {@link #setAutoValidation(int)}
396
         * 
397
         */
398
        public void validateAll(PersistentState state, int mode)
399
                        throws PersistenceValidateExceptions;
400

    
401
        /**
402
         * <p>
403
         * If the provided persistent class has registered an attribute definition
404
         * in this manager, then this method returns that definition. Otherwise, it
405
         * returns null.
406
         * </p>
407
         * 
408
         * @param persistentClass
409
         *            The class whose corresponding attribute definition is to be
410
         *            retrieved.
411
         * 
412
         * @return The attribute definition corresponding to the provided persistent
413
         *         class, or null otherwise.
414
         */
415
        public DynStruct getDefinition(Class persistentClass);
416

    
417
        /**
418
         * <p>
419
         * Return the associated definition to the java class name passed as parameter.
420
         * </p>
421
         * 
422
         * @param name of the java class to retrieve definition
423
         * 
424
         * @return The attribute definition corresponding to the provided java
425
         *         class, or null otherwise.
426
         */
427
        public DynStruct getDefinition(String name);
428

    
429
        /**
430
         * <p>
431
         * Return the associated definition to the dynClass name passed as parameter.
432
         * </p>
433
         * 
434
         * @param name of the dynClass to retrieve definition
435
         * 
436
         * @return The attribute definition corresponding to the provided dynClass, or null otherwise.
437
         */
438
        public DynStruct getDynObjectDefinition(String className);
439

    
440
        /**
441
         * <p>
442
         * De-serializes an state from the data read from the provided
443
         * <code>reader</code>. Depending on the implementation the serialized data
444
         * may have different formats, such as XML or binary data.
445
         * </p>
446
         * 
447
         * <p>
448
         * Note that a particular implementation will only be able to de-serialize
449
         * data which has been serialized by the same implementation.
450
         * </p>
451
         * 
452
         * @param in
453
         * @return
454
         * @throws org.gvsig.tools.persistence.exception.PersistenceException
455
         */
456
        public PersistentState loadState(InputStream in)
457
                        throws PersistenceException;
458

    
459
        /**
460
         * <p>
461
         * Serializes the {@link PersistentState} and writes the serialized data in
462
         * the provided {@link OutputStream}. Depending on the implementation the
463
         * serialized data may have different formats, such as XML or binary data.
464
         * </p>
465
         * 
466
         * <p>
467
         * Note that a particular implementation will only be able to de-serialize
468
         * data which has been serialized by the same implementation.
469
         * </p>
470
         * 
471
         * @param state
472
         * @param out
473
         * @throws PersistenceValidateExceptions
474
         */
475
        public void saveState(PersistentState state, OutputStream out)
476
                        throws PersistenceException, PersistenceValidateExceptions;
477

    
478
        public void saveState(PersistentState state, OutputStream out, boolean collectErrors)
479
                        throws PersistenceException, PersistenceValidateExceptions;
480

    
481
        /**
482
         * <p>
483
         * De-serializes an state from the data read from the provided
484
         * <code>is</code>. Depending on the implementation the serialized data
485
         * may have different formats, such as XML or binary data.
486
         * </p>
487
         * 
488
         * <p>
489
         * Note that a particular implementation will only be able to de-serialize
490
         * data which has been serialized by the same implementation.
491
         * </p>
492
         * 
493
         * @param reader
494
         * @return
495
         * 
496
         * @param is
497
         * @return
498
         */
499
        public Object getObject(InputStream is);
500
        
501
        /**
502
         * <p>
503
         * Serializes the object "obj" and writes the serialized data in
504
         * the provided {@link OutputStream}. Depending on the implementation the
505
         * serialized data may have different formats, such as XML or binary data.
506
         * </p>
507
         * 
508
         * <p>
509
         * Note that a particular implementation will only be able to de-serialize
510
         * data which has been serialized by the same implementation.
511
         * </p>
512
         * 
513
         * @param os
514
         * @param obj
515
         */
516
        public void putObject(OutputStream os, Object obj);
517
        
518
        /**
519
         * <p>
520
         * Sets the validation which will be applied in {@link #getState(Object)},
521
         * {@link #create(PersistentState)} methods. Validation ensures that
522
         * persisted or de-persisted objects match the declared definition (which
523
         * must have been previously registered by using
524
         * {@link #registerClass(Class, DynStruct)}).
525
         * </p>
526
         * 
527
         * <p>
528
         * When automatic validation is enabled (MANDATORY or
529
         * MANDATORY_IF_DECLARED), a ValidationException will be thrown by
530
         * {@link #getState(Object)}, {@link #create(PersistentState)} if a
531
         * validation error is found.
532
         * </p>
533
         * 
534
         * @param validationMode
535
         *            On of the following values: {@link #DISABLED},
536
         *            {@link #MANDATORY} or {@link #MANDATORY_IF_DECLARED}
537
         * 
538
         * @see #validateAll(PersistentState)
539
         * @see #registerClass(Class, DynStruct)
540
         * 
541
         * @throws PersistenceException
542
         *             If the mode is not supported by this manager
543
         */
544
        public void setAutoValidation(int validationMode)
545
                        throws PersistenceException;
546

    
547
        /**
548
         * <p>
549
         * Gets the validation which will be applied in {@link #getState(Object)},
550
         * {@link #create(PersistentState)} methods.
551
         * 
552
         * @return The current mode for automatic validation: {@link #DISABLED},
553
         *         {@link #MANDATORY} or {@link #MANDATORY_IF_DECLARED}
554
         * 
555
         * @see #validateAll(PersistentState)
556
         * @see #registerClass(Class, DynStruct)
557
         */
558
        public int getAutoValidation();
559

    
560
        public Factories getFactories();
561

    
562
}