Coverage Report - org.apache.camel.impl.ServiceSupport
 
Classes in this File Line Coverage Branch Coverage Complexity
ServiceSupport
90% 
83% 
1.636
 
 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.camel.impl;
 18  
 
 19  
 import org.apache.camel.Service;
 20  
 import org.apache.camel.util.ServiceHelper;
 21  
 
 22  
 import java.util.ArrayList;
 23  
 import java.util.Collection;
 24  
 import java.util.concurrent.atomic.AtomicBoolean;
 25  
 
 26  
 import org.springframework.jmx.export.annotation.ManagedAttribute;
 27  
 import org.springframework.jmx.export.annotation.ManagedOperation;
 28  
 import org.springframework.jmx.export.annotation.ManagedResource;
 29  
 
 30  
 /**
 31  
  * A useful base class which ensures that a service is only initialized once and
 32  
  * provides some helper methods for enquiring of its status
 33  
  *
 34  
  * @version $Revision: 565270 $
 35  
  */
 36  3129
 public abstract class ServiceSupport implements Service {
 37  
     private static int threadCounter;
 38  3129
     private AtomicBoolean started = new AtomicBoolean(false);
 39  3129
     private AtomicBoolean stopping = new AtomicBoolean(false);
 40  3129
     private AtomicBoolean stopped = new AtomicBoolean(false);
 41  
     private Collection childServices;
 42  
 
 43  
     public void start() throws Exception {
 44  2052
         if (started.compareAndSet(false, true)) {
 45  1815
             if (childServices != null) {
 46  18
                 ServiceHelper.startServices(childServices);
 47  
             }
 48  1815
             doStart();
 49  
         }
 50  2052
     }
 51  
 
 52  
     public void stop() throws Exception {
 53  2517
         if (started.get() && stopping.compareAndSet(false, true)) {
 54  
             try {
 55  1593
                 doStop();
 56  
             }
 57  
             finally {
 58  1593
                 if (childServices != null) {
 59  18
                     ServiceHelper.stopServices(childServices);
 60  
                 }
 61  1593
                 stopped.set(true);
 62  1593
                 started.set(false);
 63  1593
                 stopping.set(false);
 64  1593
             }
 65  
         }
 66  2517
     }
 67  
 
 68  
     /**
 69  
      * @return true if this service has been started
 70  
      */
 71  
     public boolean isStarted() {
 72  654
         return started.get();
 73  
     }
 74  
 
 75  
     /**
 76  
      * @return true if this service is in the process of closing
 77  
      */
 78  
     public boolean isStopping() {
 79  120
         return stopping.get();
 80  
     }
 81  
 
 82  
     /**
 83  
      * @return true if this service is closed
 84  
      */
 85  
     public boolean isStopped() {
 86  18
         return stopped.get();
 87  
     }
 88  
 
 89  
     protected abstract void doStart() throws Exception;
 90  
 
 91  
     protected abstract void doStop() throws Exception;
 92  
 
 93  
     /**
 94  
      * Creates a new thread name with the given prefix
 95  
      */
 96  
     protected String getThreadName(String prefix) {
 97  24
         return prefix + " thread:" + nextThreadCounter();
 98  
     }
 99  
 
 100  
     protected static synchronized int nextThreadCounter() {
 101  24
         return ++threadCounter;
 102  
     }
 103  
 
 104  
     protected void addChildService(Object childService) {
 105  24
         if (childServices == null) {
 106  18
             childServices = new ArrayList();
 107  
         }
 108  24
         childServices.add(childService);
 109  24
     }
 110  
 
 111  
     protected boolean removeChildService(Object childService) {
 112  0
         if (childServices != null) {
 113  0
             return childServices.remove(childService);
 114  
         }
 115  
         else {
 116  0
             return false;
 117  
         }
 118  
     }
 119  
 }