001 /** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 package org.apache.camel.util; 019 020 import org.apache.camel.Service; 021 import org.apache.commons.logging.Log; 022 import org.apache.commons.logging.LogFactory; 023 024 import java.util.Collection; 025 026 /** 027 * A collection of helper methods for working with {@link Service} objects 028 * 029 * @version $Revision: 550575 $ 030 */ 031 public class ServiceHelper { 032 private static final transient Log log = LogFactory.getLog(ServiceHelper.class); 033 034 035 /** 036 * Starts all of the given services 037 */ 038 public static void startServices(Object... services) throws Exception { 039 for (Object value : services) { 040 startService(value); 041 } 042 } 043 044 /** 045 * Starts all of the given services 046 */ 047 public static void startServices(Collection services) throws Exception { 048 for (Object value : services) { 049 if (value instanceof Service) { 050 Service service = (Service) value; 051 service.start(); 052 } 053 } 054 } 055 056 public static void startService(Object value) throws Exception { 057 if (value instanceof Service) { 058 Service service = (Service) value; 059 service.start(); 060 } 061 else if (value instanceof Collection) { 062 startServices((Collection) value); 063 } 064 } 065 066 /** 067 * Stops all of the given services, throwing the first exception caught 068 */ 069 public static void stopServices(Object... services) throws Exception { 070 Exception firstException = null; 071 for (Object value : services) { 072 if (value instanceof Service) { 073 Service service = (Service) value; 074 try { 075 service.stop(); 076 } 077 catch (Exception e) { 078 log.debug("Caught exception shutting down: " + e, e); 079 if (firstException == null) { 080 firstException = e; 081 } 082 } 083 } 084 } 085 if (firstException != null) { 086 throw firstException; 087 } 088 } 089 /** 090 * Stops all of the given services, throwing the first exception caught 091 */ 092 public static void stopServices(Collection services) throws Exception { 093 Exception firstException = null; 094 for (Object value : services) { 095 if (value instanceof Service) { 096 Service service = (Service) value; 097 try { 098 service.stop(); 099 } 100 catch (Exception e) { 101 log.debug("Caught exception shutting down: " + e, e); 102 if (firstException == null) { 103 firstException = e; 104 } 105 } 106 } 107 } 108 if (firstException != null) { 109 throw firstException; 110 } 111 } 112 }