root / branches / v2_0_0_prep / libraries / libTools / src / org / gvsig / tools / persistence / AbstractPersistenceManager.java @ 24060
History | View | Annotate | Download (1.59 KB)
1 |
package org.gvsig.tools.persistence; |
---|---|
2 |
|
3 |
import java.util.HashMap; |
4 |
import java.util.Map; |
5 |
|
6 |
import org.gvsig.tools.ToolsLocator; |
7 |
|
8 |
public abstract class AbstractPersistenceManager implements PersistenceManager { |
9 |
|
10 |
protected Map alias; |
11 |
|
12 |
public static PersistentState getState(Persistent obj) |
13 |
throws PersistenceException {
|
14 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
15 |
PersistentState state = manager.createState(obj, true);
|
16 |
return state;
|
17 |
} |
18 |
|
19 |
protected AbstractPersistenceManager() {
|
20 |
alias = new HashMap(); |
21 |
} |
22 |
|
23 |
public void addAlias(String name, Class aClass) { |
24 |
alias.put(name, aClass); |
25 |
} |
26 |
|
27 |
public void addAlias(String name, String className) { |
28 |
alias.put(name, className); |
29 |
} |
30 |
|
31 |
public PersistentState createState(Object obj) throws PersistenceException { |
32 |
return createState(obj, true); |
33 |
} |
34 |
|
35 |
public Object create(PersistentState state) throws PersistenceException { |
36 |
String className = state.getTheClassName();
|
37 |
if (className == null) { |
38 |
throw new PersistenceException(null); // FIXME |
39 |
} |
40 |
try {
|
41 |
Class theClass;
|
42 |
|
43 |
Object x = alias.get(className);
|
44 |
if (x instanceof Class) { |
45 |
theClass = (Class) x;
|
46 |
} else if (x instanceof String) { |
47 |
theClass = Class.forName((String) x); |
48 |
} else { // x is null |
49 |
theClass = Class.forName(className);
|
50 |
} |
51 |
Persistent obj = (Persistent) theClass.newInstance(); |
52 |
obj.setState(state); |
53 |
return obj;
|
54 |
} catch (ClassNotFoundException e) { |
55 |
throw new PersistenceException(e); |
56 |
} catch (InstantiationException e) { |
57 |
throw new PersistenceException(e); |
58 |
} catch (IllegalAccessException e) { |
59 |
throw new PersistenceException(e); |
60 |
} |
61 |
} |
62 |
|
63 |
} |