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: 382 $
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                service.start();
043            } else if (value instanceof Collection) {
044                startServices((Collection)value);
045            }
046        }
047    
048        /**
049         * Starts all of the given services
050         */
051        public static void startServices(Object... services) throws Exception {
052            for (Object value : services) {
053                startService(value);
054            }
055        }
056    
057        /**
058         * Starts all of the given services
059         */
060        public static void startServices(Collection services) throws Exception {
061            for (Object value : services) {
062                if (value instanceof Service) {
063                    Service service = (Service)value;
064                    service.start();
065                }
066            }
067        }
068    
069        /**
070         * Stops all of the given services, throwing the first exception caught
071         */
072        public static void stopServices(Object... services) throws Exception {
073            Exception firstException = null;
074            for (Object value : services) {
075                if (value instanceof Service) {
076                    Service service = (Service)value;
077                    try {
078                        service.stop();
079                    } catch (Exception e) {
080                        LOG.debug("Caught exception shutting down: " + e, e);
081                        if (firstException == null) {
082                            firstException = e;
083                        }
084                    }
085                }
086            }
087            if (firstException != null) {
088                throw firstException;
089            }
090        }
091    
092        public static void stopService(Object value) throws Exception {
093            if (value instanceof Service) {
094                Service service = (Service)value;
095                service.stop();
096            } else if (value instanceof Collection) {
097                stopServices((Collection)value);
098            }
099        }
100    
101        /**
102         * Stops all of the given services, throwing the first exception caught
103         */
104        public static void stopServices(Collection services) throws Exception {
105            Exception firstException = null;
106            for (Object value : services) {
107                if (value instanceof Service) {
108                    Service service = (Service)value;
109                    try {
110                        service.stop();
111                    } catch (Exception e) {
112                        LOG.debug("Caught exception shutting down: " + e, e);
113                        if (firstException == null) {
114                            firstException = e;
115                        }
116                    }
117                }
118            }
119            if (firstException != null) {
120                throw firstException;
121            }
122        }
123    }