diff --git a/concurrent/src/main/java/org/jclouds/concurrent/FutureExceptionParser.java b/concurrent/src/main/java/org/jclouds/concurrent/FutureExceptionParser.java index b4324ae17e..9b3ee0a146 100755 --- a/concurrent/src/main/java/org/jclouds/concurrent/FutureExceptionParser.java +++ b/concurrent/src/main/java/org/jclouds/concurrent/FutureExceptionParser.java @@ -28,6 +28,8 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import org.jclouds.logging.Logger; + import com.google.common.base.Function; /** @@ -39,10 +41,16 @@ public class FutureExceptionParser implements Future { private final Future delegate; private final Function function; + private final Logger logger; public FutureExceptionParser(Future delegate, Function function) { + this(delegate, function, Logger.NULL); + } + + public FutureExceptionParser(Future delegate, Function function, Logger logger) { this.delegate = delegate; this.function = function; + this.logger = logger; } public boolean cancel(boolean mayInterruptIfRunning) { @@ -59,7 +67,10 @@ public class FutureExceptionParser implements Future { private T attemptConvert(ExecutionException e) throws ExecutionException { if (e.getCause() instanceof Exception) { + + logger.debug("Processing exception for: %s", e.getCause()); T returnVal = function.apply((Exception) e.getCause()); + logger.debug("Processed exception for: %s", e.getCause()); if (returnVal != null) return returnVal; } @@ -83,4 +94,4 @@ public class FutureExceptionParser implements Future { return delegate.isDone(); } -} \ No newline at end of file +} diff --git a/concurrent/src/main/java/org/jclouds/concurrent/FutureFunctionCallable.java b/concurrent/src/main/java/org/jclouds/concurrent/FutureFunctionCallable.java index 3438d7f2c6..1465399e03 100755 --- a/concurrent/src/main/java/org/jclouds/concurrent/FutureFunctionCallable.java +++ b/concurrent/src/main/java/org/jclouds/concurrent/FutureFunctionCallable.java @@ -27,6 +27,8 @@ import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; +import org.jclouds.logging.Logger; + import com.google.common.base.Function; /** @@ -38,18 +40,28 @@ public class FutureFunctionCallable implements Callable { private final Future future; private final Function function; + private final Logger logger; public FutureFunctionCallable(Future future, Function function) { + this(future, function, Logger.NULL); + } + + public FutureFunctionCallable(Future future, Function function, Logger logger) { this.future = future; this.function = function; + this.logger = logger; } public T call() throws Exception { try { - return function.apply(future.get()); + F input = future.get(); + logger.debug("Processing intermediate result for: %s", input); + T result = function.apply(input); + logger.debug("Processed intermediate result for: %s", input); + return result; } catch (ExecutionException e) { throw (Exception) e.getCause(); } } -} \ No newline at end of file +} diff --git a/concurrent/src/main/java/org/jclouds/concurrent/WithinThreadExecutor.java b/concurrent/src/main/java/org/jclouds/concurrent/WithinThreadExecutor.java index e3cbd9ff2d..00b48a447f 100755 --- a/concurrent/src/main/java/org/jclouds/concurrent/WithinThreadExecutor.java +++ b/concurrent/src/main/java/org/jclouds/concurrent/WithinThreadExecutor.java @@ -32,6 +32,7 @@ import java.util.concurrent.Executor; * @author Adrian Cole */ @SingleThreadCompatible +@SingleThreaded public class WithinThreadExecutor implements Executor { public void execute(Runnable command) { diff --git a/concurrent/src/main/java/org/jclouds/logging/Logger.java b/concurrent/src/main/java/org/jclouds/logging/Logger.java new file mode 100755 index 0000000000..705c153f8f --- /dev/null +++ b/concurrent/src/main/java/org/jclouds/logging/Logger.java @@ -0,0 +1,89 @@ +/** + * + * Copyright (C) 2009 Global Cloud Specialists, Inc. + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + */ +package org.jclouds.logging; + +/** + * JCloud log abstraction layer. + *

+ * Implementations of logging are optional and injected if they are configured. + *

+ * @Resource Logger logger = Logger.NULL; The above will get you a + * null-safe instance of Logger. If configured, this logger will be + * swapped with a real Logger implementation with category set to the current + * class name. This is done post-object construction, so do not attempt to use + * these loggers in your constructor. + *

+ * If you wish to initialize loggers like these yourself, do not use the @Resource + * annotation. + *

+ * This implementation first checks to see if the level is enabled before + * issuing the log command. In other words, don't do the following + * if (logger.isTraceEnabled()) logger.trace("message");. + *

+ * + * @author Adrian Cole + */ +public interface Logger { + + /** + * Assign to member to avoid NPE when no logging module is configured. + */ + public static final Logger NULL = new NullLogger(); + + String getCategory(); + + void trace(String message, Object... args); + + boolean isTraceEnabled(); + + void debug(String message, Object... args); + + boolean isDebugEnabled(); + + void info(String message, Object... args); + + boolean isInfoEnabled(); + + void warn(String message, Object... args); + + void warn(Throwable throwable, String message, Object... args); + + boolean isWarnEnabled(); + + void error(String message, Object... args); + + void error(Throwable throwable, String message, Object... args); + + boolean isErrorEnabled(); + + /** + * Produces instances of {@link Logger} relevant to the specified category + * + * @author Adrian Cole + * + */ + public static interface LoggerFactory { + public Logger getLogger(String category); + } +} \ No newline at end of file diff --git a/concurrent/src/main/java/org/jclouds/logging/NullLogger.java b/concurrent/src/main/java/org/jclouds/logging/NullLogger.java new file mode 100755 index 0000000000..9691802492 --- /dev/null +++ b/concurrent/src/main/java/org/jclouds/logging/NullLogger.java @@ -0,0 +1,93 @@ +/** + * + * Copyright (C) 2009 Global Cloud Specialists, Inc. + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + */ +package org.jclouds.logging; + +/** + * Logger that doesn't do anything. + *

+ * Useful to get baseline performance unaffected by logging. + * + * @author Adrian Cole + * + */ +public class NullLogger implements Logger { + + public void debug(String message, Object... args) { + + } + + public void error(String message, Object... args) { + + } + + public void error(Throwable throwable, String message, Object... args) { + + } + + public String getCategory() { + + return null; + } + + public void info(String message, Object... args) { + + } + + public boolean isDebugEnabled() { + + return false; + } + + public boolean isErrorEnabled() { + + return false; + } + + public boolean isInfoEnabled() { + + return false; + } + + public boolean isTraceEnabled() { + + return false; + } + + public boolean isWarnEnabled() { + + return false; + } + + public void trace(String message, Object... args) { + + } + + public void warn(String message, Object... args) { + + } + + public void warn(Throwable throwable, String message, Object... args) { + + } +} \ No newline at end of file