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.archiver.resources;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.net.URL;
22  import javax.annotation.Nonnull;
23  import org.codehaus.plexus.components.io.attributes.AttributeUtils;
24  import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
25  import org.codehaus.plexus.components.io.functions.ResourceAttributeSupplier;
26  import org.codehaus.plexus.components.io.resources.AbstractPlexusIoResource;
27  
28  /**
29   * A file resource that does not necessarily exist (anywhere).
30   */
31  public class PlexusIoVirtualFileResource
32      extends AbstractPlexusIoResource
33      implements ResourceAttributeSupplier
34  {
35  
36      private final File file;
37  
38      protected PlexusIoVirtualFileResource( File file, String name )
39      {
40          super( name, file.lastModified(), file.length(), file.isFile(), file.isDirectory(), file.exists() );
41          this.file = file;
42      }
43  
44      protected static String getName( File file )
45      {
46          return file.getPath().replace( '\\', '/' );
47      }
48  
49      /**
50       * Returns the resources file.
51       */
52      public File getFile()
53      {
54          return file;
55      }
56  
57      @Nonnull
58      @Override
59      public InputStream getContents()
60          throws IOException
61      {
62          throw new UnsupportedOperationException( "We're not really sure we can do this" );
63          //return new FileInputStream( getFile() );
64      }
65  
66      @Override
67      public URL getURL()
68          throws IOException
69      {
70          return getFile().toURI().toURL();
71      }
72  
73      @Override
74      public long getSize()
75      {
76          return getFile().length();
77      }
78  
79      @Override
80      public boolean isDirectory()
81      {
82          return getFile().isDirectory();
83      }
84  
85      @Override
86      public boolean isExisting()
87      {
88          return getFile().exists();
89      }
90  
91      @Override
92      public boolean isFile()
93      {
94          return getFile().isFile();
95      }
96  
97      @Override
98      public PlexusIoResourceAttributes getAttributes()
99      {
100         return null;
101     }
102 
103     @Override
104     public long getLastModified()
105     {
106         if ( file.exists() )
107         {
108             return AttributeUtils.getLastModified( getFile() );
109         }
110         else
111         {
112             return System.currentTimeMillis();
113         }
114     }
115 
116     @Override public boolean isSymbolicLink()
117     {
118         return getAttributes().isSymbolicLink();
119     }
120 
121 }