View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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 boolean ensureDirectoryExists(Path dir) throws IOException
71      {
72          if (exists(dir))
73          {
74              // exists already, nothing to do
75              return false;
76          }
77          Files.createDirectories(dir);
78          return true;
79      }
80  
81      public static void ensureDirectoryWritable(Path dir) throws IOException
82      {
83          if (!Files.exists(dir))
84          {
85              throw new IOException("Path does not exist: " + dir.toAbsolutePath());
86          }
87          if (!Files.isDirectory(dir))
88          {
89              throw new IOException("Directory does not exist: " + dir.toAbsolutePath());
90          }
91          if (!Files.isWritable(dir))
92          {
93              throw new IOException("Unable to write to directory: " + dir.toAbsolutePath());
94          }
95      }
96  
97      public static boolean exists(Path path)
98      {
99          return Files.exists(path);
100     }
101 
102     public static boolean isValidDirectory(Path path)
103     {
104         if (!Files.exists(path))
105         {
106             // doesn't exist, not a valid directory
107             return false;
108         }
109 
110         if (!Files.isDirectory(path))
111         {
112             // not a directory (as expected)
113             StartLog.warn("Not a directory: " + path);
114             return false;
115         }
116 
117         return true;
118     }
119 
120     public static boolean isXml(String filename)
121     {
122         return filename.toLowerCase(Locale.ENGLISH).endsWith(".xml");
123     }
124     
125     public static String toRelativePath(File baseDir, File path)
126     {
127         return baseDir.toURI().relativize(path.toURI()).toASCIIString();
128     }
129     
130     public static boolean isPropertyFile(String filename)
131     {
132         return filename.toLowerCase(Locale.ENGLISH).endsWith(".properties");
133     }
134     
135     public static String separators(String path)
136     {
137         StringBuilder ret = new StringBuilder();
138         for (char c : path.toCharArray())
139         {
140             if ((c == '/') || (c == '\\'))
141             {
142                 ret.append(File.separatorChar);
143             }
144             else
145             {
146                 ret.append(c);
147             }
148         }
149         return ret.toString();
150     }
151 
152     public static Path toPath(String path)
153     {
154         return FileSystems.getDefault().getPath(FS.separators(path));
155     }
156 
157     public static void touch(Path path) throws IOException
158     {
159         FileTime now = FileTime.fromMillis(System.currentTimeMillis());
160         Files.setLastModifiedTime(path,now);
161     }
162 
163     public static Path toRealPath(Path path) throws IOException
164     {
165         return path.toRealPath();
166     }
167 }