1 /**
2 * Copyright 2005-2013 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 org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22 * Base abstract implementation for a lifecycle task.
23 *
24 * @author Kuali Rice Team (rice.collab@kuali.org)
25 */
26 public abstract class AbstractViewLifecycleTask implements ViewLifecycleTask {
27
28 private final Logger LOG = LoggerFactory.getLogger(AbstractViewLifecycleTask.class);
29
30 private ViewLifecyclePhase phase;
31
32 /**
33 * Create a lifecycle processing task.
34 *
35 * @param phase The phase this task is a part of.
36 */
37 protected AbstractViewLifecycleTask(ViewLifecyclePhase phase) {
38 this.phase = phase;
39 }
40
41 /**
42 * Perform phase-specific lifecycle processing tasks.
43 */
44 protected abstract void performLifecycleTask();
45
46 /**
47 * Reset this task, to facilitate recycling.
48 */
49 void recycle() {
50 this.phase = null;
51 }
52
53 /**
54 * Set the phase on a recycled task.
55 *
56 * @param phase The phase to set.
57 */
58 void setPhase(ViewLifecyclePhase phase) {
59 this.phase = phase;
60 }
61
62 /**
63 * @see org.kuali.rice.krad.uif.lifecycle.ViewLifecycleTask#getPhase()
64 */
65 @Override
66 public ViewLifecyclePhase getPhase() {
67 return phase;
68 }
69
70 /**
71 * Execute the lifecycle phase.
72 *
73 * <p>
74 * This method performs state validation and updates component view status. Override
75 * {@link #performLifecyclePhase()} to provide phase-specific behavior.
76 * </p>
77 *
78 * @see java.lang.Runnable#run()
79 */
80 @Override
81 public final void run() {
82 try {
83 if (ViewLifecycle.getPhase() != phase) {
84 throw new IllegalStateException("The phase this task is a part of is not active.");
85 }
86
87 performLifecycleTask();
88
89 // Only recycle successfully processed tasks
90 LifecycleTaskFactory.recycle(this);
91
92 } catch (Throwable t) {
93 LOG.warn("Error in lifecycle phase " + this, t);
94
95 if (t instanceof RuntimeException) {
96 throw (RuntimeException) t;
97 } else if (t instanceof Error) {
98 throw (Error) t;
99 } else {
100 throw new IllegalStateException("Unexpected error in lifecycle phase " + this, t);
101 }
102 }
103 }
104
105 /**
106 * @see java.lang.Object#toString()
107 */
108 @Override
109 public String toString() {
110 return getClass().getSimpleName()
111 + " " + getPhase().getComponent().getClass().getSimpleName()
112 + " " + getPhase().getComponent().getId();
113 }
114
115 }