View Javadoc

1   /*
2    * Copyright 2007-2008 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.core.lifecycle;
17  
18  import java.util.LinkedList;
19  import java.util.List;
20  
21  import org.kuali.rice.core.lifecycle.Lifecycle;
22  
23  public abstract class BaseCompositeLifecycle extends BaseLifecycle {
24  
25  	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BaseCompositeLifecycle.class);
26  
27  	private List<Lifecycle> lifecycles;
28  
29  	protected abstract List<Lifecycle> loadLifecycles() throws Exception;
30  
31  	@Override
32  	public void start() throws Exception {
33  	    this.lifecycles = loadLifecycles();
34  		for (Lifecycle lifecycle : this.lifecycles) {
35  			lifecycle.start();
36  		}
37  		super.start();
38  	}
39  
40  	@Override
41  	public void stop() throws Exception {
42  		for (Lifecycle lifecycle : reverseLifecycles()) {
43  			try {
44  				lifecycle.stop();
45  			} catch (Throwable t) {
46  				LOG.error("Failed to stop Lifecycle: " + lifecycle.getClass().getName(), t);
47  			}
48  		}
49  		super.stop();
50  	}
51  
52  	private List<Lifecycle> reverseLifecycles() {
53  		LinkedList<Lifecycle> reversed = new LinkedList<Lifecycle>();
54  		for (Lifecycle lifecycle : this.lifecycles) {
55  			reversed.addFirst(lifecycle);
56  		}
57  		return reversed;
58  	}
59  
60  }