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.FileSupplier;
19  import org.codehaus.plexus.components.io.functions.ResourceAttributeSupplier;
20  import org.codehaus.plexus.components.io.functions.SymlinkDestinationSupplier;
21  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
22  
23  import javax.annotation.Nonnull;
24  import java.lang.reflect.Proxy;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  public class ProxyFactory
29  {
30      public static PlexusIoResource createProxy( @Nonnull PlexusIoResource target, Object alternateSupplier )
31      {
32  
33          List<Class> interfaces = new ArrayList<>();
34          interfaces.add( PlexusIoResource.class );
35          if ( target instanceof SymlinkDestinationSupplier )
36              interfaces.add( SymlinkDestinationSupplier.class );
37          if ( target instanceof FileSupplier )
38              interfaces.add( FileSupplier.class );
39          if ( target instanceof ResourceAttributeSupplier )
40              interfaces.add( ResourceAttributeSupplier.class );
41  
42          return (PlexusIoResource) Proxy.newProxyInstance( PlexusIoResource.class.getClassLoader(),
43                                                            interfaces.toArray( new Class[interfaces.size()] ),
44                                                            new ResourceInvocationHandler( target, alternateSupplier ) );
45      }
46  }