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 org.codehaus.plexus.classworlds.realm.ClassRealm;
20  import org.codehaus.plexus.component.manager.ComponentManager;
21  import org.codehaus.plexus.lifecycle.phase.Phase;
22  import org.codehaus.plexus.personality.plexus.lifecycle.phase.PhaseExecutionException;
23  
24  import java.util.ArrayList;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  public abstract class AbstractLifecycleHandler
29      implements LifecycleHandler
30  {
31      private List beginSegment;
32  
33      private List endSegment;
34  
35      // ----------------------------------------------------------------------
36      // Begin Segment
37      // ----------------------------------------------------------------------
38  
39      public void addBeginSegment( Phase phase )
40      {
41          if ( beginSegment == null )
42          {
43              beginSegment = new ArrayList();
44          }
45  
46          beginSegment.add( phase );
47      }
48  
49      public List getBeginSegment()
50      {
51          return beginSegment;
52      }
53  
54      public void addEndSegment( Phase phase )
55      {
56          if ( endSegment == null )
57          {
58              endSegment = new ArrayList();
59          }
60  
61          endSegment.add( phase );
62      }
63  
64      public List getEndSegment()
65      {
66          return endSegment;
67      }
68  
69      // ----------------------------------------------------------------------
70      // Lifecylce Management
71      // ----------------------------------------------------------------------
72  
73      /**
74       * @deprecated
75       */
76      public void start( Object component, ComponentManager manager )
77          throws PhaseExecutionException
78      {
79          start( component, manager, manager.getContainer().getLookupRealm( component ) );
80      }
81  
82      /**
83       * Start a component's lifecycle.
84       */
85      public void start( Object component, ComponentManager manager, ClassRealm realm  )
86          throws PhaseExecutionException
87      {
88          if ( segmentIsEmpty( getBeginSegment() ) )
89          {
90              return;
91          }
92  
93          for (Object o : getBeginSegment()) {
94              Phase phase = (Phase) o;
95  
96              phase.execute(component, manager, realm);
97          }
98      }
99  
100     /**
101      * End a component's lifecycle.
102      * @deprecated
103      */
104     public void end( Object component, ComponentManager manager )
105         throws PhaseExecutionException
106     {
107         end( component, manager, manager.getContainer().getLookupRealm( component ) );
108     }
109 
110     /**
111      * End a component's lifecycle.
112      */
113     public void end( Object component, ComponentManager manager, ClassRealm contextRealm )
114         throws PhaseExecutionException
115     {
116         if ( segmentIsEmpty( getEndSegment() ) )
117         {
118             return;
119         }
120 
121         for (Object o : getEndSegment()) {
122             Phase phase = (Phase) o;
123 
124             phase.execute(component, manager, contextRealm);
125         }
126     }
127 
128     private boolean segmentIsEmpty( List segment )
129     {
130         if ( segment == null || segment.size() == 0 )
131         {
132             return true;
133         }
134 
135         return false;
136     }
137 }