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