Return-Path: X-Original-To: apmail-camel-commits-archive@www.apache.org Delivered-To: apmail-camel-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id E74951064E for ; Sun, 10 Nov 2013 12:56:34 +0000 (UTC) Received: (qmail 33262 invoked by uid 500); 10 Nov 2013 12:56:25 -0000 Delivered-To: apmail-camel-commits-archive@camel.apache.org Received: (qmail 33208 invoked by uid 500); 10 Nov 2013 12:56:21 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 33201 invoked by uid 99); 10 Nov 2013 12:56:19 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 10 Nov 2013 12:56:19 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 093808213A9; Sun, 10 Nov 2013 12:56:19 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: bvahdat@apache.org To: commits@camel.apache.org Message-Id: <5634a93e4cae41c993430d0563c0b9cd@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: git commit: CAMEL-6951: Improve ServiceHelper utility class. Date: Sun, 10 Nov 2013 12:56:19 +0000 (UTC) Updated Branches: refs/heads/master a185e7d76 -> 435026f23 CAMEL-6951: Improve ServiceHelper utility class. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/435026f2 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/435026f2 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/435026f2 Branch: refs/heads/master Commit: 435026f23c6b49ced53d4e301dee45e8bbe8361a Parents: a185e7d Author: Babak Vahdat Authored: Sun Nov 10 13:56:07 2013 +0100 Committer: Babak Vahdat Committed: Sun Nov 10 13:56:07 2013 +0100 ---------------------------------------------------------------------- .../org/apache/camel/impl/ProducerCache.java | 4 +- .../org/apache/camel/util/ServiceHelper.java | 185 +++++++++++++------ 2 files changed, 134 insertions(+), 55 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/435026f2/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java b/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java index 4b2a172..dd3b0a0 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java +++ b/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java @@ -259,7 +259,7 @@ public class ProducerCache extends ServiceSupport { ServiceHelper.stopAndShutdownService(producer); } catch (Exception e) { // ignore and continue - LOG.warn("Error stopping/shutdown producer: " + producer, e); + LOG.warn("Error stopping/shutting down producer: " + producer, e); } } } @@ -323,7 +323,7 @@ public class ProducerCache extends ServiceSupport { ServiceHelper.stopAndShutdownService(producer); } catch (Exception e) { // ignore and continue - LOG.warn("Error stopping/shutdown producer: " + producer, e); + LOG.warn("Error stopping/shutting down producer: " + producer, e); } } } finally { http://git-wip-us.apache.org/repos/asf/camel/blob/435026f2/camel-core/src/main/java/org/apache/camel/util/ServiceHelper.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/util/ServiceHelper.java b/camel-core/src/main/java/org/apache/camel/util/ServiceHelper.java index b613d13..9709723 100644 --- a/camel-core/src/main/java/org/apache/camel/util/ServiceHelper.java +++ b/camel-core/src/main/java/org/apache/camel/util/ServiceHelper.java @@ -33,9 +33,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * A collection of helper methods for working with {@link Service} objects - * - * @version + * A collection of helper methods for working with {@link Service} objects. + * + * @version */ public final class ServiceHelper { private static final Logger LOG = LoggerFactory.getLogger(ServiceHelper.class); @@ -47,7 +47,12 @@ public final class ServiceHelper { } /** - * Starts all of the given services + * Starts the given {@code value} if it's a {@link Service} or a collection of it. + *

+ * Calling this method has no effect if {@code value} is {@code null}. + * + * @see #startService(Service) + * @see #startServices(Collection) */ public static void startService(Object value) throws Exception { if (value instanceof Service) { @@ -58,7 +63,11 @@ public final class ServiceHelper { } /** - * Start the given service + * Starts the given {@code service}. + *

+ * Calling this method has no effect if {@code service} is {@code null}. + * + * @see Service#start() */ public static void startService(Service service) throws Exception { if (service != null) { @@ -67,19 +76,24 @@ public final class ServiceHelper { } /** - * Starts all of the given services + * Starts each element of the given {@code services} if {@code services} itself is + * not {@code null}, otherwise this method would return immediately. + * + * @see #startServices(Collection) */ public static void startServices(Object... services) throws Exception { if (services == null) { return; } - for (Object value : services) { - startService(value); - } + List list = Arrays.asList(services); + startServices(list); } /** - * Starts all of the given services + * Starts each element of the given {@code services} if {@code services} itself is + * not {@code null}, otherwise this method would return immediately. + * + * @see #startService(Object) */ public static void startServices(Collection services) throws Exception { if (services == null) { @@ -91,7 +105,13 @@ public final class ServiceHelper { } /** - * Stops all of the given services, throwing the first exception caught + * Stops each element of the given {@code services} if {@code services} itself is + * not {@code null}, otherwise this method would return immediately. + *

+ * If there's any exception being thrown while stopping the elements one after the + * other this method would rethrow the first such exception being thrown. + * + * @see #stopServices(Collection) */ public static void stopServices(Object... services) throws Exception { if (services == null) { @@ -102,7 +122,12 @@ public final class ServiceHelper { } /** - * Stops the given service, throwing the first exception caught + * Stops the given {@code value}, rethrowing the first exception caught. + *

+ * Calling this method has no effect if {@code value} is {@code null}. + * + * @see Service#stop() + * @see #stopServices(Collection) */ public static void stopService(Object value) throws Exception { if (isStopped(value)) { @@ -120,7 +145,13 @@ public final class ServiceHelper { } /** - * Stops all of the given services, throwing the first exception caught + * Stops each element of the given {@code services} if {@code services} itself is + * not {@code null}, otherwise this method would return immediately. + *

+ * If there's any exception being thrown while stopping the elements one after the + * other this method would rethrow the first such exception being thrown. + * + * @see #stopService(Object) */ public static void stopServices(Collection services) throws Exception { if (services == null) { @@ -145,7 +176,13 @@ public final class ServiceHelper { } /** - * Stops and shutdowns all of the given services, throwing the first exception caught + * Stops and shutdowns each element of the given {@code services} if {@code services} itself is + * not {@code null}, otherwise this method would return immediately. + *

+ * If there's any exception being thrown while stopping/shutting down the elements one after + * the other this method would rethrow the first such exception being thrown. + * + * @see #stopAndShutdownServices(Collection) */ public static void stopAndShutdownServices(Object... services) throws Exception { if (services == null) { @@ -156,7 +193,12 @@ public final class ServiceHelper { } /** - * Stops and shutdowns the given service, throwing the first exception caught + * Stops and shutdowns the given {@code service}, rethrowing the first exception caught. + *

+ * Calling this method has no effect if {@code value} is {@code null}. + * + * @see #stopService(Object) + * @see ShutdownableService#shutdown() */ public static void stopAndShutdownService(Object value) throws Exception { stopService(value); @@ -170,7 +212,14 @@ public final class ServiceHelper { } /** - * Stops and shutdowns all of the given services, throwing the first exception caught + * Stops and shutdowns each element of the given {@code services} if {@code services} + * itself is not {@code null}, otherwise this method would return immediately. + *

+ * If there's any exception being thrown while stopping/shutting down the elements one after + * the other this method would rethrow the first such exception being thrown. + * + * @see #stopService(Object) + * @see ShutdownableService#shutdown() */ public static void stopAndShutdownServices(Collection services) throws Exception { if (services == null) { @@ -180,22 +229,22 @@ public final class ServiceHelper { for (Object value : services) { - // must stop it first - stopService(value); + try { + // must stop it first + stopService(value); - // then try to shutdown - if (value instanceof ShutdownableService) { - ShutdownableService service = (ShutdownableService)value; - try { + // then try to shutdown + if (value instanceof ShutdownableService) { + ShutdownableService service = (ShutdownableService)value; LOG.trace("Shutting down service: {}", service); service.shutdown(); - } catch (Exception e) { - if (LOG.isDebugEnabled()) { - LOG.debug("Caught exception shutting down service: " + service, e); - } - if (firstException == null) { - firstException = e; - } + } + } catch (Exception e) { + if (LOG.isDebugEnabled()) { + LOG.debug("Caught exception shutting down service: " + value, e); + } + if (firstException == null) { + firstException = e; } } } @@ -204,6 +253,15 @@ public final class ServiceHelper { } } + /** + * Resumes each element of the given {@code services} if {@code services} itself is + * not {@code null}, otherwise this method would return immediately. + *

+ * If there's any exception being thrown while resuming the elements one after the + * other this method would rethrow the first such exception being thrown. + * + * @see #resumeService(Service) + */ public static void resumeServices(Collection services) throws Exception { if (services == null) { return; @@ -230,20 +288,25 @@ public final class ServiceHelper { } /** - * Resumes the given service. + * Resumes the given {@code service}. *

- * If the service is a {@link org.apache.camel.SuspendableService} then the resume - * operation is only invoked if the service is suspended. + * If {@code service} is a {@link org.apache.camel.SuspendableService} then + * it's {@link org.apache.camel.SuspendableService#resume()} is called but + * only if {@code service} is already {@link #isSuspended(Object) + * suspended}. *

- * If the service is a {@link org.apache.camel.support.ServiceSupport} then the start - * operation is only invoked if the service is startable. + * If {@code service} is not a + * {@link org.apache.camel.SuspendableService} then it's + * {@link org.apache.camel.Service#start()} is called. *

- * Otherwise the service is started. - * + * Calling this method has no effect if {@code service} is {@code null}. + * * @param service the service - * @return true if either resume or start was invoked, - * false if the service is already in the desired state. + * @return true if either resume method or + * {@link #startService(Service)} was called, false + * otherwise. * @throws Exception is thrown if error occurred + * @see #startService(Service) */ public static boolean resumeService(Service service) throws Exception { if (service instanceof SuspendableService) { @@ -261,6 +324,15 @@ public final class ServiceHelper { } } + /** + * Suspends each element of the given {@code services} if {@code services} itself is + * not {@code null}, otherwise this method would return immediately. + *

+ * If there's any exception being thrown while suspending the elements one after the + * other this method would rethrow the first such exception being thrown. + * + * @see #suspendService(Service) + */ public static void suspendServices(Collection services) throws Exception { if (services == null) { return; @@ -287,20 +359,25 @@ public final class ServiceHelper { } /** - * Suspends the given service. + * Suspends the given {@code service}. *

- * If the service is a {@link org.apache.camel.SuspendableService} then the suspend - * operation is only invoked if the service is not suspended. + * If {@code service} is a {@link org.apache.camel.SuspendableService} then + * it's {@link org.apache.camel.SuspendableService#suspend()} is called but + * only if {@code service} is not already + * {@link #isSuspended(Object) suspended}. *

- * If the service is a {@link org.apache.camel.support.ServiceSupport} then the stop - * operation is only invoked if the service is stoppable. + * If {@code service} is not a + * {@link org.apache.camel.SuspendableService} then it's + * {@link org.apache.camel.Service#stop()} is called. *

- * Otherwise the service is stopped. - * + * Calling this method has no effect if {@code service} is {@code null}. + * * @param service the service - * @return true if either suspend or stop was invoked, - * false if the service is already in the desired state. + * @return true if either the suspend method or + * {@link #stopService(Object)} was called, false + * otherwise. * @throws Exception is thrown if error occurred + * @see #stopService(Object) */ public static boolean suspendService(Service service) throws Exception { if (service instanceof SuspendableService) { @@ -321,7 +398,7 @@ public final class ServiceHelper { /** * Is the given service stopping or stopped? * - * @return true if already stopped, otherwise false + * @return true if already stopped, false otherwise */ public static boolean isStopped(Object value) { if (value instanceof StatefulService) { @@ -336,7 +413,7 @@ public final class ServiceHelper { /** * Is the given service starting or started? * - * @return true if already started, otherwise false + * @return true if already started, false otherwise */ public static boolean isStarted(Object value) { if (value instanceof StatefulService) { @@ -349,9 +426,9 @@ public final class ServiceHelper { } /** - * Is the given service suspended + * Is the given service suspended? * - * @return true if already suspended, otherwise false + * @return true if already suspended, false otherwise */ public static boolean isSuspended(Object value) { if (value instanceof StatefulService) { @@ -364,7 +441,9 @@ public final class ServiceHelper { } /** - * Gather all child services by navigating the service to recursively gather all child services. + * Gathers all child services by navigating the service to recursively gather all child services. + *

+ * The returned set does not include the childern being error handler. * * @param service the service * @return the services, including the parent service, and all its children @@ -374,7 +453,7 @@ public final class ServiceHelper { } /** - * Gather all child services by navigating the service to recursively gather all child services. + * Gathers all child services by navigating the service to recursively gather all child services. * * @param service the service * @param includeErrorHandler whether to include error handlers