View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.start;
20  
21  import java.io.Closeable;
22  import java.io.File;
23  import java.io.IOException;
24  import java.nio.file.FileSystems;
25  import java.nio.file.Files;
26  import java.nio.file.Path;
27  import java.nio.file.attribute.FileTime;
28  import java.util.Locale;
29  
30  public class FS
31  {
32      public static boolean canReadDirectory(Path path)
33      {
34          return Files.exists(path) && Files.isDirectory(path) && Files.isReadable(path);
35      }
36  
37      public static boolean canReadFile(Path path)
38      {
39          return Files.exists(path) && Files.isRegularFile(path) && Files.isReadable(path);
40      }
41  
42      public static boolean canWrite(Path path)
43      {
44          return Files.isWritable(path);
45      }
46  
47      public static void close(Closeable c)
48      {
49          if (c == null)
50          {
51              return;
52          }
53  
54          try
55          {
56              c.close();
57          }
58          catch (IOException ignore)
59          {
60              /* ignore */
61          }
62      }
63  
64      public static boolean createNewFile(Path path) throws IOException
65      {
66          Path ret = Files.createFile(path);
67          return Files.exists(ret);
68      }
69  
70      public static void ensureDirectoryExists(Path dir) throws IOException
71      {
72          if (exists(dir))
73          {
74              // exists already, nothing to do
75              return;
76          }
77          Files.createDirectories(dir);
78      }
79  
80      public static void ensureDirectoryWritable(Path dir) throws IOException
81      {
82          if (!Files.exists(dir))
83          {
84              throw new IOException("Path does not exist: " + dir.toAbsolutePath());
85          }
86          if (!Files.isDirectory(dir))
87          {
88              throw new IOException("Directory does not exist: " + dir.toAbsolutePath());
89          }
90          if (!Files.isWritable(dir))
91          {
92              throw new IOException("Unable to write to directory: " + dir.toAbsolutePath());
93          }
94      }
95  
96      public static boolean exists(Path path)
97      {
98          return Files.exists(path);
99      }
100 
101     public static boolean isValidDirectory(Path path)
102     {
103         if (!Files.exists(path))
104         {
105             // doesn't exist, not a valid directory
106             return false;
107         }
108 
109         if (!Files.isDirectory(path))
110         {
111             // not a directory (as expected)
112             StartLog.warn("Not a directory: " + path);
113             return false;
114         }
115 
116         return true;
117     }
118 
119     public static boolean isXml(String filename)
120     {
121         return filename.toLowerCase(Locale.ENGLISH).endsWith(".xml");
122     }
123     
124     public static String toRelativePath(File baseDir, File path)
125     {
126         return baseDir.toURI().relativize(path.toURI()).toASCIIString();
127     }
128     
129     public static boolean isPropertyFile(String filename)
130     {
131         return filename.toLowerCase(Locale.ENGLISH).endsWith(".properties");
132     }
133     
134     public static String separators(String path)
135     {
136         StringBuilder ret = new StringBuilder();
137         for (char c : path.toCharArray())
138         {
139             if ((c == '/') || (c == '\\'))
140             {
141                 ret.append(File.separatorChar);
142             }
143             else
144             {
145                 ret.append(c);
146             }
147         }
148         return ret.toString();
149     }
150 
151     public static Path toPath(String path)
152     {
153         return FileSystems.getDefault().getPath(FS.separators(path));
154     }
155 
156     public static void touch(Path path) throws IOException
157     {
158         FileTime now = FileTime.fromMillis(System.currentTimeMillis());
159         Files.setLastModifiedTime(path,now);
160     }
161 
162     public static Path toRealPath(Path path) throws IOException
163     {
164         return path.toRealPath();
165     }
166 }