Revision 639
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/test/java/org/gvsig/tools/persistence/AbstractBasicTest.java | ||
---|---|---|
26 | 26 |
manager.saveState(state, out); |
27 | 27 |
out.close(); |
28 | 28 |
} |
29 |
|
|
29 |
|
|
30 | 30 |
protected PersistentState saveAndRestoreState(Object obj) throws Exception { |
31 | 31 |
PersistentState state1 = manager.getState(obj); |
32 | 32 |
|
... | ... | |
40 | 40 |
return state2; |
41 | 41 |
} |
42 | 42 |
|
43 |
protected void saveObject(Object obj) throws Exception { |
|
44 |
File f = File.createTempFile("gvsig-test-pers-"+getTestId(), "OS.zip"); |
|
45 |
OutputStream out = new FileOutputStream(f); |
|
46 |
manager.putObject(out,obj); |
|
47 |
out.close(); |
|
48 |
} |
|
49 |
|
|
50 |
protected Object saveAndRestoreObject(Object obj) throws Exception { |
|
51 |
File f = File.createTempFile("gvsig-test-pers-"+getTestId(), "OSR.zip"); |
|
52 |
OutputStream out = new FileOutputStream(f); |
|
53 |
manager.putObject(out,obj); |
|
54 |
out.close(); |
|
55 |
|
|
56 |
InputStream in = new FileInputStream(f); |
|
57 |
Object obj2 = manager.getObject(in); |
|
58 |
in.close(); |
|
59 |
return obj2; |
|
60 |
} |
|
61 |
|
|
43 | 62 |
public void doSetUp() throws Exception { |
44 | 63 |
this.manager = ToolsLocator.getPersistenceManager(); |
45 | 64 |
manager.setAutoValidation(PersistenceManager.MANDATORY); |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/test/java/org/gvsig/tools/persistence/BasicTypesTest.java | ||
---|---|---|
170 | 170 |
obj1.check(obj2); |
171 | 171 |
} |
172 | 172 |
|
173 |
public void testPutObject() throws Exception { |
|
174 |
MyObject obj = new MyObject(); |
|
175 |
saveObject(obj); |
|
176 |
} |
|
177 |
|
|
178 |
public void testGetObject() throws Exception { |
|
179 |
MyObject obj1 = new MyObject(); |
|
180 |
MyObject obj2 = (MyObject) this.saveAndRestoreObject(obj1); |
|
181 |
|
|
182 |
obj1.check(obj2); |
|
183 |
} |
|
184 |
|
|
173 | 185 |
|
174 | 186 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/persistence/impl/AbstractPersistenceManager.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import java.io.IOException; |
4 | 4 |
import java.io.InputStream; |
5 |
import java.io.OutputStream; |
|
5 | 6 |
import java.util.ArrayList; |
6 | 7 |
import java.util.Collections; |
7 | 8 |
import java.util.Iterator; |
... | ... | |
26 | 27 |
import org.gvsig.tools.persistence.exception.DuplicatePersistentDefinitionException; |
27 | 28 |
import org.gvsig.tools.persistence.exception.FieldNotDeclaredException; |
28 | 29 |
import org.gvsig.tools.persistence.exception.PersistenceClassNotRegistered; |
30 |
import org.gvsig.tools.persistence.exception.PersistenceCreateException; |
|
29 | 31 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
30 | 32 |
import org.gvsig.tools.persistence.exception.PersistenceTypeNotSupportedException; |
31 | 33 |
import org.gvsig.tools.persistence.exception.PersistenceValidateExceptions; |
34 |
import org.gvsig.tools.persistence.exception.PersistenceWriteExeption; |
|
32 | 35 |
import org.gvsig.tools.persistence.impl.exception.CantFindDefinitionInStreamException; |
33 | 36 |
import org.gvsig.tools.persistence.impl.exception.PersistenceDomainNotRegisterdException; |
34 | 37 |
import org.gvsig.tools.persistence.impl.exception.PersistenceIllegalStateTheClassNameNotSetException; |
... | ... | |
83 | 86 |
return ref.getObject(); |
84 | 87 |
} |
85 | 88 |
|
89 |
public Object getObject(InputStream is) { |
|
90 |
Object obj = null; |
|
91 |
PersistentState state = null; |
|
92 |
String theClassName = null; |
|
93 |
try { |
|
94 |
state = this.loadState(is); |
|
95 |
theClassName = state.getTheClassName(); |
|
96 |
obj = this.create(state); |
|
97 |
} catch (PersistenceException e) { |
|
98 |
throw new PersistenceCreateException(theClassName,e); |
|
99 |
} |
|
100 |
return obj; |
|
101 |
} |
|
102 |
|
|
103 |
public void putObject(OutputStream os, Object obj) { |
|
104 |
PersistentState state; |
|
105 |
try { |
|
106 |
state = this.getState(obj); |
|
107 |
this.saveState(state, os); |
|
108 |
} catch (PersistenceException e) { |
|
109 |
throw new PersistenceWriteExeption(e); |
|
110 |
} |
|
111 |
} |
|
112 |
|
|
86 | 113 |
public DynStruct getDefinition(Class persistentClass) { |
87 | 114 |
return getDefinition(persistentClass.getName()); |
88 | 115 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/persistence/exception/PersistenceCreateException.java | ||
---|---|---|
14 | 14 |
|
15 | 15 |
public PersistenceCreateException(Class theClass, Throwable cause) { |
16 | 16 |
super(MESSAGE_FORMAT1, cause, MESSAGE_KEY, serialVersionUID); |
17 |
this.setValue("class", theClass.getName()); |
|
18 |
if( cause instanceof InstantiationException ) { |
|
19 |
boolean needEmptyConstructor = true; |
|
20 |
Constructor[] constructors = theClass.getConstructors(); |
|
21 |
for( int i=0; i<constructors.length; i++ ) { |
|
22 |
Constructor constructor = constructors[i]; |
|
23 |
Class[] parameters = constructor.getParameterTypes(); |
|
24 |
if( parameters.length == 0) { |
|
25 |
needEmptyConstructor = false; |
|
17 |
if( theClass == null ) { |
|
18 |
this.setValue("class", "UNKNOW"); |
|
19 |
} else { |
|
20 |
this.setValue("class", theClass.getName()); |
|
21 |
if( cause instanceof InstantiationException ) { |
|
22 |
boolean needEmptyConstructor = true; |
|
23 |
Constructor[] constructors = theClass.getConstructors(); |
|
24 |
for( int i=0; i<constructors.length; i++ ) { |
|
25 |
Constructor constructor = constructors[i]; |
|
26 |
Class[] parameters = constructor.getParameterTypes(); |
|
27 |
if( parameters.length == 0) { |
|
28 |
needEmptyConstructor = false; |
|
29 |
} |
|
26 | 30 |
} |
31 |
if( needEmptyConstructor ) { |
|
32 |
this.setFormatString(MESSAGE_FORMAT2); |
|
33 |
} |
|
27 | 34 |
} |
28 |
if( needEmptyConstructor ) { |
|
29 |
this.setFormatString(MESSAGE_FORMAT2); |
|
30 |
} |
|
31 | 35 |
} |
32 | 36 |
} |
37 |
|
|
38 |
public PersistenceCreateException(String theClass, Throwable cause) { |
|
39 |
super(MESSAGE_FORMAT1, cause, MESSAGE_KEY, serialVersionUID); |
|
40 |
if( theClass == null ) { |
|
41 |
this.setValue("class", "UNKNOW"); |
|
42 |
} else { |
|
43 |
this.setValue("class", theClass); |
|
44 |
} |
|
45 |
} |
|
33 | 46 |
|
34 | 47 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/persistence/exception/PersistenceWriteExeption.java | ||
---|---|---|
1 |
package org.gvsig.tools.persistence.exception; |
|
2 |
|
|
3 |
|
|
4 |
public class PersistenceWriteExeption extends PersistenceRuntimeException { |
|
5 |
|
|
6 |
/** |
|
7 |
* |
|
8 |
*/ |
|
9 |
private static final long serialVersionUID = -6059143552335997976L; |
|
10 |
private final static String MESSAGE_FORMAT1 = "Can't write object."; |
|
11 |
private final static String MESSAGE_KEY = "_Cant_write_object"; |
|
12 |
|
|
13 |
public PersistenceWriteExeption(Throwable cause) { |
|
14 |
super(MESSAGE_FORMAT1, cause, MESSAGE_KEY, serialVersionUID); |
|
15 |
} |
|
16 |
} |
|
0 | 17 |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/persistence/PersistenceManager.java | ||
---|---|---|
444 | 444 |
* data which has been serialized by the same implementation. |
445 | 445 |
* </p> |
446 | 446 |
* |
447 |
* @param reader
|
|
447 |
* @param out
|
|
448 | 448 |
* @return |
449 | 449 |
* @throws PersistenceValidateExceptions |
450 | 450 |
*/ |
... | ... | |
453 | 453 |
|
454 | 454 |
/** |
455 | 455 |
* <p> |
456 |
* De-serializes an state from the data read from the provided |
|
457 |
* <code>is</code>. Depending on the implementation the serialized data |
|
458 |
* may have different formats, such as XML or binary data. |
|
459 |
* </p> |
|
460 |
* |
|
461 |
* <p> |
|
462 |
* Note that a particular implementation will only be able to de-serialize |
|
463 |
* data which has been serialized by the same implementation. |
|
464 |
* </p> |
|
465 |
* |
|
466 |
* @param reader |
|
467 |
* @return |
|
468 |
* |
|
469 |
* @param is |
|
470 |
* @return |
|
471 |
*/ |
|
472 |
public Object getObject(InputStream is); |
|
473 |
|
|
474 |
/** |
|
475 |
* <p> |
|
476 |
* Serializes the object "obj" and writes the serialized data in |
|
477 |
* the provided {@link OutputStream}. Depending on the implementation the |
|
478 |
* serialized data may have different formats, such as XML or binary data. |
|
479 |
* </p> |
|
480 |
* |
|
481 |
* <p> |
|
482 |
* Note that a particular implementation will only be able to de-serialize |
|
483 |
* data which has been serialized by the same implementation. |
|
484 |
* </p> |
|
485 |
* |
|
486 |
* @param os |
|
487 |
* @param obj |
|
488 |
*/ |
|
489 |
public void putObject(OutputStream os, Object obj); |
|
490 |
|
|
491 |
/** |
|
492 |
* <p> |
|
456 | 493 |
* Sets the validation which will be applied in {@link #getState(Object)}, |
457 | 494 |
* {@link #create(PersistentState)} methods. Validation ensures that |
458 | 495 |
* persisted or de-persisted objects match the declared definition (which |
Also available in: Unified diff