View Javadoc

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