001 /** 002 Licensed to the Apache Software Foundation (ASF) under one or more 003 contributor license agreements. See the NOTICE file distributed with 004 this work for additional information regarding copyright ownership. 005 The ASF licenses this file to You under the Apache License, Version 2.0 006 (the "License"); you may not use this file except in compliance with 007 the License. You may obtain a copy of the License at 008 009 http://www.apache.org/licenses/LICENSE-2.0 010 011 Unless required by applicable law or agreed to in writing, software 012 distributed under the License is distributed on an "AS IS" BASIS, 013 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 See the License for the specific language governing permissions and 015 limitations under the License. 016 */ 017 package org.apache.commons.inject.impl; 018 019 import java.util.ArrayList; 020 import java.util.List; 021 022 import org.apache.commons.inject.api.ILifecycleController; 023 import org.apache.commons.inject.api.ILifecycleListener; 024 import org.apache.commons.inject.util.Exceptions; 025 026 public class DefaultLifecycleController implements ILifecycleController { 027 private static final int NOT_STARTED = 0; 028 private static final int STARTED = 1; 029 private static final int TERMINATED = 2; 030 031 private int state = NOT_STARTED; 032 private List<ILifecycleListener> listeners = new ArrayList<ILifecycleListener>(); 033 private List<ILifecycleListener> initializedListeners = new ArrayList<ILifecycleListener>(); 034 035 @Override 036 public synchronized void start() { 037 System.err.println("DefaultLifecycleController.start: -> " + state); 038 if (state == NOT_STARTED) { 039 for (ILifecycleListener listener : listeners) { 040 try { 041 listener.start(); 042 initializedListeners.add(listener); 043 } catch (Throwable t) { 044 throw Exceptions.show(t); 045 } 046 } 047 state = STARTED; 048 } 049 System.err.println("DefaultLifecycleController.start: <- " + state); 050 } 051 052 @Override 053 public synchronized void shutdown() { 054 System.err.println("DefaultLifecycleController.shutdown: -> " + state); 055 if (state == STARTED) { 056 Throwable th = null; 057 // Shutdown in reverse order. 058 for (int i = initializedListeners.size()-1; i >= 0; i--) { 059 final ILifecycleListener listener = initializedListeners.get(i); 060 try { 061 listener.shutdown(); 062 listeners.remove(i); 063 } catch (Throwable t) { 064 if (th == null) { 065 th = t; 066 } 067 } 068 if (th != null) { 069 throw Exceptions.show(th); 070 } 071 } 072 state = TERMINATED; 073 } 074 System.err.println("DefaultLifecycleController.shutdown: <- " + state); 075 } 076 077 @Override 078 public synchronized boolean add(ILifecycleListener pListener) { 079 System.err.println("DefaultLifecycleController.add: -> " + pListener + ", " + state); 080 if (state == STARTED) { 081 try { 082 pListener.start(); 083 initializedListeners.add(pListener); 084 } catch (Throwable t) { 085 throw Exceptions.show(t); 086 } 087 } 088 System.err.println("DefaultLifecycleController.add: <- " + listeners.size() + ", " + initializedListeners.size()); 089 return listeners.add(pListener); 090 } 091 092 @Override 093 public synchronized boolean remove(ILifecycleListener pListener) { 094 System.err.println("DefaultLifecycleController.remove: -> " + pListener + ", " + state); 095 boolean result = initializedListeners.remove(pListener); 096 if (state == TERMINATED && result) { 097 try { 098 pListener.shutdown(); 099 } catch (Throwable t) { 100 throw Exceptions.show(t); 101 } 102 } 103 System.err.println("DefaultLifecycleController.remove: <- " + result + ", " + listeners.size() + ", " + initializedListeners.size()); 104 return result; 105 } 106 }