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