1 /**
2 * Copyright 2005-2014 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/ecl2.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * Responsible for creating lifecycle tasks.
28 *
29 * <p>
30 * This factory recycles completed tasks to reduce object creation during the lifecycle.
31 * </p>
32 *
33 * @author Kuali Rice Team (rice.collab@kuali.org)
34 */
35 public final class LifecycleTaskFactory {
36
37 /**
38 * Weak mapping of task constructors by task class.
39 */
40 private final static Map<Class<?>, Constructor<?>> TASK_CONSTRUCTOR =
41 Collections.synchronizedMap(new WeakHashMap<Class<?>, Constructor<?>>());
42
43 /**
44 * Gets a task instance by class.
45 *
46 * @param <T> The lifecycle task type to return.
47 * @param taskClass The task class.
48 * @param elementState The lifecycle element state..
49 * @return A lifecycle processing task for the indicated phase, ready for processing.
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 * Recycles a task instance after processing.
73 *
74 * @param task The task to recycle.
75 */
76 static void recycle(ViewLifecycleTaskBase<?> task) {
77 task.recycle();
78 RecycleUtils.recycle(task);
79 }
80
81 /**
82 * Gets a single-arg constructor on for the task class for creating new tasks based on a view.
83 *
84 * @param taskClass The task class.
85 * @return A single-arg constructor on for the task class for creating new tasks based on a
86 * view.
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 // TODO: ensure tasks are no longer using this method and remove this extraneous lookup
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 }