View Javadoc
1   /*
2    * Copyright 2014 The Codehaus Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.plexus.components.io.resources.proxy;
17  
18  import org.codehaus.plexus.components.io.functions.ContentSupplier;
19  import org.codehaus.plexus.components.io.functions.NameSupplier;
20  import org.codehaus.plexus.components.io.functions.ResourceAttributeSupplier;
21  import org.codehaus.plexus.components.io.functions.SizeSupplier;
22  import org.codehaus.plexus.components.io.functions.SymlinkDestinationSupplier;
23  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
24  
25  import javax.annotation.Nonnull;
26  import java.lang.reflect.InvocationHandler;
27  import java.lang.reflect.Method;
28  
29  class ResourceInvocationHandler
30      implements InvocationHandler
31  {
32      private PlexusIoResource testImpl;
33  
34      private final ContentSupplier contentSupplier;
35      private final NameSupplier nameSupplier;
36      private final SizeSupplier sizeSupplier;
37      private final SymlinkDestinationSupplier symlinkDestinationSupplier;
38      private final ResourceAttributeSupplier resourceAttributeSupplier;
39  
40      public ResourceInvocationHandler( @Nonnull PlexusIoResource target, Object alternativeHandler )
41      {
42          this.testImpl = target;
43          this.contentSupplier = asOrNull( alternativeHandler, ContentSupplier.class );
44          this.nameSupplier = asOrNull( alternativeHandler, NameSupplier.class );
45          this.sizeSupplier = asOrNull( alternativeHandler, SizeSupplier.class );
46          this.symlinkDestinationSupplier = asOrNull( alternativeHandler, SymlinkDestinationSupplier.class );
47          this.resourceAttributeSupplier = asOrNull( alternativeHandler, ResourceAttributeSupplier.class );
48  
49      }
50  
51      @SuppressWarnings( "unchecked" )
52      private static <T> T asOrNull( Object instance, Class<T> clazz )
53      {
54          if ( instance != null && clazz.isAssignableFrom( instance.getClass() ) )
55              return (T) instance;
56          else
57              return null;
58      }
59  
60      public Object invoke( Object proxy, Method method, Object[] args )
61          throws Throwable
62      {
63          String name = method.getName();
64          if ( contentSupplier != null && "getContents".equals( name ) )
65          {
66              return contentSupplier.getContents();
67          }
68          if ( nameSupplier != null && "getName".equals( name ) )
69          {
70              return nameSupplier.getName();
71          }
72          if ( sizeSupplier != null && "getSize".equals( name ) )
73          {
74              return sizeSupplier.getSize();
75          }
76          if ( symlinkDestinationSupplier != null && "getSymlinkDestination".equals( name ) )
77          {
78              return symlinkDestinationSupplier.getSymlinkDestination();
79          }
80  
81          if ( resourceAttributeSupplier != null && "getAttributes".equals( name ) )
82          {
83              return resourceAttributeSupplier.getAttributes();
84          }
85  
86          return method.invoke( testImpl, args );
87      }
88  }
89