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.camel.util;
018
019 import java.util.Collection;
020
021 import org.apache.camel.Service;
022 import org.apache.commons.logging.Log;
023 import org.apache.commons.logging.LogFactory;
024
025 /**
026 * A collection of helper methods for working with {@link Service} objects
027 *
028 * @version $Revision: 788067 $
029 */
030 public final class ServiceHelper {
031 private static final transient Log LOG = LogFactory.getLog(ServiceHelper.class);
032
033 /**
034 * Utility classes should not have a public constructor.
035 */
036 private ServiceHelper() {
037 }
038
039 public static void startService(Object value) throws Exception {
040 if (value instanceof Service) {
041 Service service = (Service)value;
042 if (LOG.isTraceEnabled()) {
043 LOG.trace("Starting service: " + service);
044 }
045 service.start();
046 } else if (value instanceof Collection) {
047 startServices((Collection)value);
048 }
049 }
050
051 /**
052 * Starts all of the given services
053 */
054 public static void startServices(Object... services) throws Exception {
055 for (Object value : services) {
056 startService(value);
057 }
058 }
059
060 /**
061 * Starts all of the given services
062 */
063 public static void startServices(Collection services) throws Exception {
064 for (Object value : services) {
065 if (value instanceof Service) {
066 Service service = (Service)value;
067 if (LOG.isTraceEnabled()) {
068 LOG.trace("Starting service: " + service);
069 }
070 service.start();
071 }
072 }
073 }
074
075 /**
076 * Stops all of the given services, throwing the first exception caught
077 */
078 public static void stopServices(Object... services) throws Exception {
079 Exception firstException = null;
080 for (Object value : services) {
081 if (value instanceof Service) {
082 Service service = (Service)value;
083 try {
084 if (LOG.isTraceEnabled()) {
085 LOG.trace("Stopping service: " + service);
086 }
087 service.stop();
088 } catch (Exception e) {
089 LOG.debug("Caught exception shutting down: " + e, e);
090 if (firstException == null) {
091 firstException = e;
092 }
093 }
094 }
095 }
096 if (firstException != null) {
097 throw firstException;
098 }
099 }
100
101 public static void stopService(Object value) throws Exception {
102 if (value instanceof Service) {
103 Service service = (Service)value;
104 service.stop();
105 } else if (value instanceof Collection) {
106 stopServices((Collection)value);
107 }
108 }
109
110 /**
111 * Stops all of the given services, throwing the first exception caught
112 */
113 public static void stopServices(Collection services) throws Exception {
114 Exception firstException = null;
115 for (Object value : services) {
116 if (value instanceof Service) {
117 Service service = (Service)value;
118 try {
119 if (LOG.isTraceEnabled()) {
120 LOG.trace("Stopping service: " + service);
121 }
122 service.stop();
123 } catch (Exception e) {
124 LOG.debug("Caught exception shutting down: " + e, e);
125 if (firstException == null) {
126 firstException = e;
127 }
128 }
129 }
130 }
131 if (firstException != null) {
132 throw firstException;
133 }
134 }
135 }