View Javadoc
1   package org.codehaus.plexus.lifecycle;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.codehaus.plexus.classworlds.realm.ClassRealm;
23  import org.codehaus.plexus.component.manager.ComponentManager;
24  import org.codehaus.plexus.lifecycle.phase.Phase;
25  import org.codehaus.plexus.personality.plexus.lifecycle.phase.PhaseExecutionException;
26  
27  public abstract class AbstractLifecycleHandler implements LifecycleHandler {
28      private List beginSegment;
29  
30      private List endSegment;
31  
32      // ----------------------------------------------------------------------
33      // Begin Segment
34      // ----------------------------------------------------------------------
35  
36      public void addBeginSegment(Phase phase) {
37          if (beginSegment == null) {
38              beginSegment = new ArrayList();
39          }
40  
41          beginSegment.add(phase);
42      }
43  
44      public List getBeginSegment() {
45          return beginSegment;
46      }
47  
48      public void addEndSegment(Phase phase) {
49          if (endSegment == null) {
50              endSegment = new ArrayList();
51          }
52  
53          endSegment.add(phase);
54      }
55  
56      public List getEndSegment() {
57          return endSegment;
58      }
59  
60      // ----------------------------------------------------------------------
61      // Lifecylce Management
62      // ----------------------------------------------------------------------
63  
64      /**
65       * @deprecated
66       */
67      public void start(Object component, ComponentManager manager) throws PhaseExecutionException {
68          start(component, manager, manager.getContainer().getLookupRealm(component));
69      }
70  
71      /**
72       * Start a component's lifecycle.
73       */
74      public void start(Object component, ComponentManager manager, ClassRealm realm) throws PhaseExecutionException {
75          if (segmentIsEmpty(getBeginSegment())) {
76              return;
77          }
78  
79          for (Object o : getBeginSegment()) {
80              Phase phase = (Phase) o;
81  
82              phase.execute(component, manager, realm);
83          }
84      }
85  
86      /**
87       * End a component's lifecycle.
88       * @deprecated
89       */
90      public void end(Object component, ComponentManager manager) throws PhaseExecutionException {
91          end(component, manager, manager.getContainer().getLookupRealm(component));
92      }
93  
94      /**
95       * End a component's lifecycle.
96       */
97      public void end(Object component, ComponentManager manager, ClassRealm contextRealm)
98              throws PhaseExecutionException {
99          if (segmentIsEmpty(getEndSegment())) {
100             return;
101         }
102 
103         for (Object o : getEndSegment()) {
104             Phase phase = (Phase) o;
105 
106             phase.execute(component, manager, contextRealm);
107         }
108     }
109 
110     private boolean segmentIsEmpty(List segment) {
111         if (segment == null || segment.size() == 0) {
112             return true;
113         }
114 
115         return false;
116     }
117 }