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
018 package org.apache.commons.proxy.interceptor;
019
020 import EDU.oswego.cs.dl.util.concurrent.Executor;
021 import org.apache.commons.proxy.Interceptor;
022 import org.apache.commons.proxy.Invocation;
023
024 /**
025 * A method interceptor that uses an {@link Executor} to execute the method invocation.
026 * <p/>
027 * <b>Note</b>: Only <em>void</em> methods can be intercepted using this class! Any attempts to intercept non-void
028 * methods will result in an {@link IllegalArgumentException}. If the proxy interfaces include non-void methods, try
029 * using a {@link FilteredInterceptor} along with a
030 * {@link org.apache.commons.proxy.interceptor.filter.ReturnTypeFilter} to wrap an instance of this class.
031 * <p/>
032 * <p>
033 * <b>Dependencies</b>:
034 * <ul>
035 * <li>Concurrent API version 1.3.4 or greater</li>
036 * </ul>
037 * </p>
038 *
039 * @author James Carman
040 * @since 1.0
041 */
042 public class ExecutorInterceptor implements Interceptor
043 {
044 //**********************************************************************************************************************
045 // Fields
046 //**********************************************************************************************************************
047
048 private final Executor executor;
049
050 //**********************************************************************************************************************
051 // Constructors
052 //**********************************************************************************************************************
053
054 public ExecutorInterceptor( Executor executor )
055 {
056 this.executor = executor;
057 }
058
059 //**********************************************************************************************************************
060 // Interceptor Implementation
061 //**********************************************************************************************************************
062
063
064 public Object intercept( final Invocation invocation ) throws Throwable
065 {
066 if( Void.TYPE.equals(invocation.getMethod().getReturnType()) )
067 {
068 // Special case for finalize() method (should not be run in a different thread)...
069 if( !( invocation.getMethod().getName().equals("finalize") &&
070 invocation.getMethod().getParameterTypes().length == 0 ) )
071 {
072 executor.execute(new Runnable()
073 {
074 public void run()
075 {
076 try
077 {
078 invocation.proceed();
079 }
080 catch( Throwable t )
081 {
082 // What to do here? I can't convey the failure back to the caller.
083 }
084 }
085 });
086 return null;
087 }
088 else
089 {
090 return invocation.proceed();
091 }
092 }
093 else
094 {
095 throw new IllegalArgumentException("Only void methods can be executed in a different thread.");
096 }
097 }
098 }