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.Arrays;
020 import java.util.Collection;
021 import java.util.List;
022
023 import org.apache.camel.Service;
024 import org.apache.camel.SuspendableService;
025 import org.apache.camel.impl.ServiceSupport;
026 import org.apache.commons.logging.Log;
027 import org.apache.commons.logging.LogFactory;
028
029 /**
030 * A collection of helper methods for working with {@link Service} objects
031 *
032 * @version $Revision: 895918 $
033 */
034 public final class ServiceHelper {
035 private static final transient Log LOG = LogFactory.getLog(ServiceHelper.class);
036
037 /**
038 * Utility classes should not have a public constructor.
039 */
040 private ServiceHelper() {
041 }
042
043 public static void startService(Object value) throws Exception {
044 if (value instanceof Service) {
045 Service service = (Service)value;
046 if (LOG.isTraceEnabled()) {
047 LOG.trace("Starting service: " + service);
048 }
049 service.start();
050 } else if (value instanceof Collection) {
051 startServices((Collection<?>)value);
052 }
053 }
054
055 /**
056 * Starts all of the given services
057 */
058 public static void startServices(Object... services) throws Exception {
059 for (Object value : services) {
060 startService(value);
061 }
062 }
063
064 /**
065 * Starts all of the given services
066 */
067 public static void startServices(Collection<?> services) throws Exception {
068 for (Object value : services) {
069 if (value instanceof Service) {
070 Service service = (Service)value;
071 if (LOG.isTraceEnabled()) {
072 LOG.trace("Starting service: " + service);
073 }
074 service.start();
075 }
076 }
077 }
078
079 /**
080 * Stops all of the given services, throwing the first exception caught
081 */
082 public static void stopServices(Object... services) throws Exception {
083 List<Object> list = Arrays.asList(services);
084 stopServices(list);
085 }
086
087 public static void stopService(Object value) throws Exception {
088 if (value instanceof Service) {
089 Service service = (Service)value;
090 if (LOG.isTraceEnabled()) {
091 LOG.trace("Stopping service " + value);
092 }
093 service.stop();
094 } else if (value instanceof Collection) {
095 stopServices((Collection<?>)value);
096 }
097 }
098
099 /**
100 * Stops all of the given services, throwing the first exception caught
101 */
102 public static void stopServices(Collection<?> services) throws Exception {
103 Exception firstException = null;
104 for (Object value : services) {
105 if (value instanceof Service) {
106 Service service = (Service)value;
107 try {
108 if (LOG.isTraceEnabled()) {
109 LOG.trace("Stopping service: " + service);
110 }
111 service.stop();
112 } catch (Exception e) {
113 LOG.debug("Caught exception shutting down: " + e, e);
114 if (firstException == null) {
115 firstException = e;
116 }
117 }
118 }
119 }
120 if (firstException != null) {
121 throw firstException;
122 }
123 }
124
125 /**
126 * Resumes the given service.
127 * <p/>
128 * If the service is a {@link org.apache.camel.SuspendableService} then the <tt>resume</tt>
129 * operation is <b>only</b> invoked if the service is suspended.
130 * <p/>
131 * If the service is a {@link org.apache.camel.impl.ServiceSupport} then the <tt>start</tt>
132 * operation is <b>only</b> invoked if the service is startable.
133 * <p/>
134 * Otherwise the service is started.
135 *
136 * @param service the service
137 * @return <tt>true</tt> if either <tt>resume</tt> or <tt>start</tt> was invoked,
138 * <tt>false</tt> if the service is already in the desired state.
139 * @throws Exception is thrown if error occurred
140 */
141 public static boolean resumeService(Service service) throws Exception {
142 if (service instanceof SuspendableService) {
143 SuspendableService ss = (SuspendableService) service;
144 if (ss.isSuspended()) {
145 if (LOG.isTraceEnabled()) {
146 LOG.trace("Resuming service " + service);
147 }
148 ss.resume();
149 return true;
150 } else {
151 return false;
152 }
153 } else if (service instanceof ServiceSupport) {
154 ServiceSupport ss = (ServiceSupport) service;
155 if (ss.getStatus().isStartable()) {
156 startService(service);
157 return true;
158 } else {
159 return false;
160 }
161 } else {
162 startService(service);
163 return true;
164 }
165 }
166
167 /**
168 * Suspends the given service.
169 * <p/>
170 * If the service is a {@link org.apache.camel.SuspendableService} then the <tt>suspend</tt>
171 * operation is <b>only</b> invoked if the service is <b>not</b> suspended.
172 * <p/>
173 * If the service is a {@link org.apache.camel.impl.ServiceSupport} then the <tt>stop</tt>
174 * operation is <b>only</b> invoked if the service is stopable.
175 * <p/>
176 * Otherwise the service is stopped.
177 *
178 * @param service the service
179 * @return <tt>true</tt> if either <tt>suspend</tt> or <tt>stop</tt> was invoked,
180 * <tt>false</tt> if the service is already in the desired state.
181 * @throws Exception is thrown if error occurred
182 */
183 public static boolean suspendService(Service service) throws Exception {
184 if (service instanceof SuspendableService) {
185 SuspendableService ss = (SuspendableService) service;
186 if (!ss.isSuspended()) {
187 if (LOG.isTraceEnabled()) {
188 LOG.trace("Suspending service " + service);
189 }
190 ss.suspend();
191 return true;
192 } else {
193 return false;
194 }
195 } else if (service instanceof ServiceSupport) {
196 ServiceSupport ss = (ServiceSupport) service;
197 if (ss.getStatus().isStoppable()) {
198 stopServices(service);
199 return true;
200 } else {
201 return false;
202 }
203 } else {
204 stopService(service);
205 return true;
206 }
207 }
208
209 }