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