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 @ 1624

History | View | Annotate | Download (17.2 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
    public void addDefinition(Class theClass, String name, String resourceName);
286

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

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

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

    
339
        public void unregisterFactory(PersistenceFactory factory);
340

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

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

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

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

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

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

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

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

    
464
        public PersistentState loadState(InputStream in)
465
                        throws PersistenceException;
466

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

    
486
        public void saveState(PersistentState state, OutputStream out, boolean collectErrors)
487
                        throws PersistenceException, PersistenceValidateExceptions;
488

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

    
555
        /**
556
         * <p>
557
         * Gets the validation which will be applied in {@link #getState(Object)},
558
         * {@link #create(PersistentState)} methods.
559
         * 
560
         * @return The current mode for automatic validation: {@link #DISABLED},
561
         *         {@link #MANDATORY} or {@link #MANDATORY_IF_DECLARED}
562
         * 
563
         * @see #validateAll(PersistentState)
564
         * @see #registerClass(Class, DynStruct)
565
         */
566
        public int getAutoValidation();
567

    
568
        public Factories getFactories();
569
    
570
        public PersistentContext getNewContext();
571

    
572
}