1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package javax.faces.internal; |
17 | |
|
18 | |
import java.io.Serializable; |
19 | |
|
20 | |
import javax.faces.component.StateHolder; |
21 | |
import javax.faces.context.FacesContext; |
22 | |
|
23 | |
import org.seasar.framework.log.Logger; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class AttachedObjectStateWrapper implements Serializable { |
35 | |
|
36 | 1 | private static final Logger logger = Logger |
37 | 1 | .getLogger(AttachedObjectStateWrapper.class); |
38 | |
|
39 | |
private static final long serialVersionUID = 3256726169255885111L; |
40 | |
|
41 | 153 | private Object savedState = null; |
42 | |
|
43 | 153 | private String className = null; |
44 | |
|
45 | 153 | private boolean isSavedStateHolder = false; |
46 | |
|
47 | 153 | public AttachedObjectStateWrapper(FacesContext context, Object obj) { |
48 | 153 | if (obj == null) { |
49 | 0 | throw new IllegalArgumentException(); |
50 | |
} |
51 | 153 | className = obj.getClass().getName(); |
52 | 153 | if (obj instanceof StateHolder) { |
53 | 99 | StateHolder stateHolder = (StateHolder) obj; |
54 | 99 | if (!stateHolder.isTransient()) { |
55 | 99 | savedState = stateHolder.saveState(context); |
56 | 99 | isSavedStateHolder = true; |
57 | |
} |
58 | |
} else { |
59 | 54 | if (!(obj instanceof Serializable)) { |
60 | 44 | logger.debug("class : " + className |
61 | |
+ " should be serializable."); |
62 | |
} |
63 | 54 | savedState = obj; |
64 | |
} |
65 | 153 | } |
66 | |
|
67 | |
public Object restore(FacesContext context) throws IllegalStateException { |
68 | 153 | Object result = null; |
69 | 153 | if (!isSavedStateHolder) { |
70 | 54 | result = savedState; |
71 | 99 | } else if (isSavedStateHolder) { |
72 | |
try { |
73 | 99 | ClassLoader loader = ClassLoaderUtil.getClassLoader(context); |
74 | 99 | Class clazz = ClassLoaderUtil.loadClass(loader, className); |
75 | 99 | result = clazz.newInstance(); |
76 | 0 | } catch (ClassNotFoundException e) { |
77 | 0 | throw new IllegalStateException(e.getMessage()); |
78 | 0 | } catch (InstantiationException e) { |
79 | 0 | throw new IllegalStateException(e.getMessage()); |
80 | 0 | } catch (IllegalAccessException e) { |
81 | 0 | throw new IllegalStateException(e.getMessage()); |
82 | 99 | } |
83 | 99 | if (savedState != null && result instanceof StateHolder) { |
84 | 98 | ((StateHolder) result).restoreState(context, savedState); |
85 | |
} |
86 | |
} |
87 | 153 | return result; |
88 | |
} |
89 | |
|
90 | |
} |