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