Coverage Report - org.apache.commons.inject.impl.DefaultLifecycleController
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultLifecycleController
65%
32/49
50%
9/18
5,25
 
 1  
 /**
 2  
  Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  contributor license agreements.  See the NOTICE file distributed with
 4  
  this work for additional information regarding copyright ownership.
 5  
  The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  (the "License"); you may not use this file except in compliance with
 7  
  the License.  You may obtain a copy of the License at
 8  
 
 9  
       http://www.apache.org/licenses/LICENSE-2.0
 10  
 
 11  
  Unless required by applicable law or agreed to in writing, software
 12  
  distributed under the License is distributed on an "AS IS" BASIS,
 13  
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  See the License for the specific language governing permissions and
 15  
  limitations under the License.
 16  
 */
 17  
 package org.apache.commons.inject.impl;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.List;
 21  
 
 22  
 import org.apache.commons.inject.api.ILifecycleController;
 23  
 import org.apache.commons.inject.api.ILifecycleListener;
 24  
 import org.apache.commons.inject.util.Exceptions;
 25  
 
 26  2
 public class DefaultLifecycleController implements ILifecycleController {
 27  
         private static final int NOT_STARTED = 0;
 28  
         private static final int STARTED = 1;
 29  
         private static final int TERMINATED = 2;
 30  
 
 31  2
         private int state = NOT_STARTED;
 32  2
         private List<ILifecycleListener> listeners = new ArrayList<ILifecycleListener>();
 33  2
         private List<ILifecycleListener> initializedListeners = new ArrayList<ILifecycleListener>();
 34  
 
 35  
         @Override
 36  
         public synchronized void start() {
 37  2
                 System.err.println("DefaultLifecycleController.start: -> " + state);
 38  2
                 if (state == NOT_STARTED) {
 39  2
                         for (ILifecycleListener listener : listeners) {
 40  
                                 try {
 41  1
                                         listener.start();
 42  1
                                         initializedListeners.add(listener);
 43  0
                                 } catch (Throwable t) {
 44  0
                                         throw Exceptions.show(t);
 45  1
                                 }
 46  
                         }
 47  2
                         state = STARTED;
 48  
                 }
 49  2
                 System.err.println("DefaultLifecycleController.start: <- " + state);
 50  2
         }
 51  
 
 52  
         @Override
 53  
         public synchronized void shutdown() {
 54  2
                 System.err.println("DefaultLifecycleController.shutdown: -> " + state);
 55  2
                 if (state == STARTED) {
 56  2
                         Throwable th = null;
 57  
                         // Shutdown in reverse order.
 58  7
                         for (int i = initializedListeners.size()-1;  i >= 0;  i--) {
 59  5
                                 final ILifecycleListener listener = initializedListeners.get(i);
 60  
                                 try {
 61  5
                                         listener.shutdown();
 62  5
                                         listeners.remove(i);
 63  0
                                 } catch (Throwable t) {
 64  0
                                         if (th == null) {
 65  0
                                                 th = t;
 66  
                                         }
 67  5
                                 }
 68  5
                                 if (th != null) {
 69  0
                                         throw Exceptions.show(th);
 70  
                                 }
 71  
                         }
 72  2
                         state = TERMINATED;
 73  
                 }
 74  2
                 System.err.println("DefaultLifecycleController.shutdown: <- " + state);
 75  2
         }
 76  
 
 77  
         @Override
 78  
         public synchronized boolean add(ILifecycleListener pListener) {
 79  5
                 System.err.println("DefaultLifecycleController.add: -> " + pListener + ", " + state);
 80  5
                 if (state == STARTED) {
 81  
                         try {
 82  4
                                 pListener.start();
 83  4
                                 initializedListeners.add(pListener);
 84  0
                         } catch (Throwable t) {
 85  0
                                 throw Exceptions.show(t);
 86  4
                         }
 87  
                 }
 88  5
                 System.err.println("DefaultLifecycleController.add: <- " + listeners.size() + ", " + initializedListeners.size());
 89  5
                 return listeners.add(pListener);
 90  
         }
 91  
 
 92  
         @Override
 93  
         public synchronized boolean remove(ILifecycleListener pListener) {
 94  0
                 System.err.println("DefaultLifecycleController.remove: -> " + pListener + ", " + state);
 95  0
                 boolean result = initializedListeners.remove(pListener);
 96  0
                 if (state == TERMINATED  &&  result) {
 97  
                         try {
 98  0
                                 pListener.shutdown();
 99  0
                         } catch (Throwable t) {
 100  0
                                 throw Exceptions.show(t);
 101  0
                         }
 102  
                 }
 103  0
                 System.err.println("DefaultLifecycleController.remove: <- " + result + ", " + listeners.size() + ", " + initializedListeners.size());
 104  0
                 return result;
 105  
         }
 106  
 }