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

History | View | Annotate | Download (17.7 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
import org.apache.commons.lang3.mutable.MutableInt;
31

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

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

    
53
        public interface Factories extends List {
54

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

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

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

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

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

    
101
        }
102

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

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

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

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

    
170
  public PersistentState getState(Object obj, boolean collectAllErrors, MutableInt referenceCounter) throws PersistenceException;
171

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

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

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

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

    
293
    public void addDefinition(Class theClass, String name, String resourceName);
294

    
295
    public void addDefinition(Class theClass, String name, InputStream resource);     
296
    
297
        /**
298
         * Add a new definition to the class in the persistent manager.
299
         * 
300
         * @param theClass
301
         *            to be persistent
302
         * @param name
303
         *            of the class definition to associate to theClass
304
         * @param definitions
305
         *            , map from get the definition of the class
306
         * @param domainName
307
         *            (can be null)
308
         * @param domainUrl
309
         *            (can be null)
310
         * @return
311
         * 
312
         * @see #addDefinition(Class, String, String, String, String)
313
         */
314
        public DynStruct addDefinition(Class theClass, String name,
315
                        Map definitions, String domainName, String domainUrl)
316
                        throws AddDefinitionException;
317

    
318
        /**
319
         * Add a new definition to the class in the persistent manager.
320
         * 
321
         * @param definition
322
         *            , TODO
323
         * @param domainName
324
         *            (can be null)
325
         * @param domainUrl
326
         *            (can be null)
327
         * @return
328
         * 
329
         * @see #addDefinition(Class, String, String, String, String)
330
         */
331
        public DynStruct addDefinition(DynStruct definition, String domainName,
332
                        String domainUrl)
333
                        throws AddDefinitionException;
334

    
335
        /**
336
         * <p>
337
         * Register an instance of {@link PersistenceFactory} that can manage some
338
         * objects that requires an especial construction or persistence. This
339
         * factory will use the default domain name and URL.
340
         * </p>
341
         * 
342
         * 
343
         * @param factory
344
         */
345
        public void registerFactory(PersistenceFactory factory);
346

    
347
        public void unregisterFactory(PersistenceFactory factory);
348

    
349
        /**
350
         * <p>
351
         * Unregister a registered class from the manager.
352
         * </p>
353
         * 
354
         * @param persistentClass
355
         * 
356
         * @see #registerClass(Class, DynStruct, String)
357
         * 
358
         * @throws PersistenceClassNotRegistered
359
         *             if <code>persistentClass</code> is not registered
360
         */
361
        public void unregisterClass(Class theClass)
362
                        throws PersistenceClassNotRegistered;
363

    
364
        /**
365
         * <p>
366
         * Unregister a registered java class from the manager.
367
         * </p>
368
         * 
369
         * @param className the java class name to unregister
370
         * 
371
         * @see #registerClass(Class)
372
         * 
373
         * @throws PersistenceClassNotRegistered
374
         *             if <code>className</code> is not registered
375
         */
376
        public void unregisterClass(String className)
377
                        throws PersistenceClassNotRegistered;
378

    
379
        /**
380
         * <p>
381
         * Validates persistent state by using the corresponding registered
382
         * attribute definition. <code>mode</code> specifies what to do when a
383
         * definition of a state class is not registered.
384
         * </p>
385
         * 
386
         * @param state
387
         * @param mode
388
         * @throws PersistenceValidateExceptions
389
         * 
390
         */
391
        public void validate(PersistentState state, int mode)
392
                        throws PersistenceValidateExceptions;
393

    
394
        /**
395
         * <p>
396
         * Validates all persistent state related to the <code>state</code> by using
397
         * the corresponding registered attribute definition. <code>mode</code>
398
         * specifies what to do when a definition of a state class is not
399
         * registered.
400
         * </p>
401
         * 
402
         * @param state
403
         * @param mode
404
         * 
405
         * @throws PersistenceValidateExceptions
406
         * 
407
         * @see {@link #setAutoValidation(int)}
408
         * 
409
         */
410
        public void validateAll(PersistentState state, int mode)
411
                        throws PersistenceValidateExceptions;
412

    
413
        /**
414
         * <p>
415
         * If the provided persistent class has registered an attribute definition
416
         * in this manager, then this method returns that definition. Otherwise, it
417
         * returns null.
418
         * </p>
419
         * 
420
         * @param persistentClass
421
         *            The class whose corresponding attribute definition is to be
422
         *            retrieved.
423
         * 
424
         * @return The attribute definition corresponding to the provided persistent
425
         *         class, or null otherwise.
426
         */
427
        public DynStruct getDefinition(Class persistentClass);
428

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

    
441
        /**
442
         * <p>
443
         * Return the associated definition to the dynClass name passed as parameter.
444
         * </p>
445
         * 
446
         * @param name of the dynClass to retrieve definition
447
         * 
448
         * @return The attribute definition corresponding to the provided dynClass, or null otherwise.
449
         */
450
        public DynStruct getDynObjectDefinition(String className);
451

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

    
472
        public PersistentState loadState(InputStream in)
473
                        throws PersistenceException;
474

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

    
494
        public void saveState(PersistentState state, OutputStream out, boolean collectErrors)
495
                        throws PersistenceException, PersistenceValidateExceptions;
496

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

    
563
        /**
564
         * <p>
565
         * Gets the validation which will be applied in {@link #getState(Object)},
566
         * {@link #create(PersistentState)} methods.
567
         * 
568
         * @return The current mode for automatic validation: {@link #DISABLED},
569
         *         {@link #MANDATORY} or {@link #MANDATORY_IF_DECLARED}
570
         * 
571
         * @see #validateAll(PersistentState)
572
         * @see #registerClass(Class, DynStruct)
573
         */
574
        public int getAutoValidation();
575

    
576
        public Factories getFactories();
577
    
578
        public PersistentContext getNewContext();
579

    
580
        public PersistentContext getNewContext(MutableInt referenceCounter);
581

    
582
}