1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.lifecycle;
17
18 import java.lang.reflect.Constructor;
19 import java.lang.reflect.InvocationTargetException;
20 import java.util.Collections;
21 import java.util.Map;
22 import java.util.WeakHashMap;
23
24 import org.kuali.rice.krad.uif.util.RecycleUtils;
25
26
27
28
29
30
31
32
33
34
35 public final class LifecycleTaskFactory {
36
37
38
39
40 private final static Map<Class<?>, Constructor<?>> TASK_CONSTRUCTOR =
41 Collections.synchronizedMap(new WeakHashMap<Class<?>, Constructor<?>>());
42
43
44
45
46
47
48
49
50
51 public static <T extends ViewLifecycleTaskBase<?>> T getTask(Class<T> taskClass, LifecycleElementState elementState) {
52 T task = RecycleUtils.getRecycledInstance(taskClass);
53
54 if (task == null) {
55 try {
56 task = taskClass.cast(getConstructor(taskClass).newInstance(elementState));
57 } catch (InstantiationException e) {
58 throw new IllegalStateException("Error creating lifecycle task", e);
59 } catch (IllegalAccessException e) {
60 throw new IllegalStateException("Error creating lifecycle task", e);
61 } catch (InvocationTargetException e) {
62 throw new IllegalStateException("Error creating lifecycle task", e);
63 }
64 } else {
65 task.setElementState(elementState);
66 }
67
68 return task;
69 }
70
71
72
73
74
75
76 static void recycle(ViewLifecycleTaskBase<?> task) {
77 task.recycle();
78 RecycleUtils.recycle(task);
79 }
80
81
82
83
84
85
86
87
88 private final static Constructor<?> getConstructor(Class<?> taskClass) {
89 Constructor<?> constructor = TASK_CONSTRUCTOR.get(taskClass);
90
91 if (constructor == null) {
92 try {
93 constructor = taskClass.getDeclaredConstructor(LifecycleElementState.class);
94 } catch (NoSuchMethodException e) {
95
96 try {
97 constructor = taskClass.getDeclaredConstructor(ViewLifecyclePhase.class);
98 } catch (NoSuchMethodException e2) {
99 throw new IllegalArgumentException(taskClass
100 + " doesn't define an available lifecycle phase constructor.", e);
101 }
102 } catch (SecurityException e) {
103 throw new IllegalArgumentException(taskClass
104 + " doesn't define an available lifecycle phase constructor.", e);
105 }
106
107 constructor.setAccessible(true);
108
109 TASK_CONSTRUCTOR.put(taskClass, constructor);
110 }
111
112 return constructor;
113 }
114
115 }