Skip to content

Commit 104c7b0

Browse files
committed
Improved performance of java7 file attribute retrieval
1 parent 7197e2e commit 104c7b0

File tree

3 files changed

+57
-20
lines changed

3 files changed

+57
-20
lines changed

src/main/java/org/codehaus/plexus/components/io/attributes/Java7AttributeUtils.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.codehaus.plexus.components.io.attributes;
1717

18-
import javax.annotation.Nonnull;
19-
import javax.annotation.Nullable;
2018
import java.io.File;
2119
import java.io.IOException;
2220
import java.nio.file.Files;
@@ -29,6 +27,9 @@
2927
import java.util.HashSet;
3028
import java.util.Set;
3129

30+
import javax.annotation.Nonnull;
31+
import javax.annotation.Nullable;
32+
3233
/**
3334
* @author Kristian Rosenvold
3435
*/
@@ -124,7 +125,7 @@ public static BasicFileAttributes getFileAttributes( @Nonnull File file )
124125
public static BasicFileAttributes getFileAttributes( Path path )
125126
throws IOException
126127
{
127-
if ( path.getFileSystem().supportedFileAttributeViews().contains( "posix" ) )
128+
if (isUnix(path))
128129
{
129130

130131
try
@@ -139,6 +140,10 @@ public static BasicFileAttributes getFileAttributes( Path path )
139140
return Files.readAttributes( path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS );
140141
}
141142

143+
public static boolean isUnix(Path path) {
144+
return path.getFileSystem().supportedFileAttributeViews().contains("unix");
145+
}
146+
142147
@Nullable
143148
public static FileOwnerAttributeView getFileOwnershipInfo( @Nonnull File file )
144149
throws IOException

src/main/java/org/codehaus/plexus/components/io/attributes/Java7FileAttributes.java

+15-12
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,21 @@
1616
* limitations under the License.
1717
*/
1818

19-
import javax.annotation.Nonnull;
20-
import javax.annotation.Nullable;
2119
import java.io.File;
2220
import java.io.IOException;
2321
import java.nio.file.Files;
24-
import java.nio.file.attribute.BasicFileAttributes;
22+
import java.nio.file.LinkOption;
2523
import java.nio.file.attribute.FileOwnerAttributeView;
26-
import java.nio.file.attribute.PosixFileAttributes;
2724
import java.nio.file.attribute.PosixFilePermission;
25+
import java.security.Principal;
2826
import java.util.Collections;
2927
import java.util.HashMap;
3028
import java.util.Map;
3129
import java.util.Set;
3230

31+
import javax.annotation.Nonnull;
32+
import javax.annotation.Nullable;
33+
3334
/*
3435
* File attributes for a java7 file that are backed on disk by a file.
3536
* Immutable
@@ -59,12 +60,13 @@ public Java7FileAttributes( @Nonnull File file, @Nonnull Map<Integer, String> u
5960
throws IOException
6061
{
6162

62-
BasicFileAttributes basicFileAttributes = Java7AttributeUtils.getFileAttributes( file );
6363

64-
if ( basicFileAttributes instanceof PosixFileAttributes )
64+
if (Java7AttributeUtils.isUnix(file.toPath()))
6565
{
66-
this.permissions = ( (PosixFileAttributes) basicFileAttributes ).permissions();
67-
groupId = (Integer) Files.readAttributes( file.toPath(), "unix:gid" ).get( "gid" );
66+
Map<String, Object> attrs = Files.readAttributes(file.toPath(), "unix:*", LinkOption.NOFOLLOW_LINKS);
67+
this.permissions = (Set<PosixFilePermission>) attrs.get("permissions");
68+
69+
groupId = (Integer) attrs.get("gid");
6870

6971
String groupName = groupCache.get( groupId );
7072
if ( groupName != null )
@@ -73,21 +75,22 @@ public Java7FileAttributes( @Nonnull File file, @Nonnull Map<Integer, String> u
7375
}
7476
else
7577
{
76-
this.groupName = ( (PosixFileAttributes) basicFileAttributes ).group().getName();
78+
this.groupName = ((Principal) attrs.get("group")).getName();
7779
groupCache.put( groupId, this.groupName );
7880
}
79-
userId = (Integer) Files.readAttributes( file.toPath(), "unix:uid" ).get( "uid" );
81+
userId = (Integer) attrs.get("uid");
8082
String userName = userCache.get( userId );
8183
if ( userName != null )
8284
{
8385
this.userName = userName;
8486
}
8587
else
8688
{
87-
this.userName = ( (PosixFileAttributes) basicFileAttributes ).owner().getName();
89+
this.userName = ((Principal) attrs.get("owner")).getName();
8890
userCache.put( userId, this.userName );
8991
}
9092
octalMode = calculatePosixOctalMode();
93+
symbolicLink = (Boolean) attrs.get("isSymbolicLink");
9194
} else {
9295
FileOwnerAttributeView fa = Java7AttributeUtils.getFileOwnershipInfo( file );
9396
this.userName = fa.getOwner().getName();
@@ -96,9 +99,9 @@ public Java7FileAttributes( @Nonnull File file, @Nonnull Map<Integer, String> u
9699
this.groupId = null;
97100
octalMode = -1;
98101
permissions = Collections.emptySet();
102+
symbolicLink = Files.isSymbolicLink(file.toPath());
99103
}
100104

101-
symbolicLink = Files.isSymbolicLink( file.toPath() );
102105
}
103106

104107
public static @Nonnull PlexusIoResourceAttributes uncached( @Nonnull File file )

src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResourceCollection.java

+34-5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import java.util.Iterator;
3535
import java.util.List;
3636
import java.util.Map;
37+
import java.util.concurrent.Callable;
38+
import java.util.concurrent.ExecutorService;
3739

3840
/**
3941
* Implementation of {@link PlexusIoResourceCollection} for the set
@@ -209,21 +211,48 @@ public void forEach( PlexusIoResourceConsumer resourceConsumer )
209211
throws IOException
210212
{
211213
Iterator<PlexusIoResource> resources = getResources();
212-
while (resources.hasNext()){
214+
while ( resources.hasNext() )
215+
{
213216
PlexusIoResource next = resources.next();
214-
if (isSelected( next ))
217+
if ( isSelected( next ) )
215218
{
216219
resourceConsumer.accept( next );
217220
}
218221
}
219-
if (resources instanceof Closeable )
222+
if ( resources instanceof Closeable )
223+
{
224+
( (Closeable) resources ).close();
225+
}
226+
227+
}
228+
229+
public void forEach( ExecutorService es, final PlexusIoResourceConsumer resourceConsumer )
230+
throws IOException
231+
{
232+
Iterator<PlexusIoResource> resources = getResources();
233+
while ( resources.hasNext() )
234+
{
235+
final PlexusIoResource next = resources.next();
236+
Callable future = new Callable()
237+
{
238+
public Object call()
239+
throws Exception
240+
{
241+
resourceConsumer.accept( next );
242+
return this;
243+
}
244+
};
245+
es.submit( future );
246+
}
247+
if ( resources instanceof Closeable )
220248
{
221-
((Closeable)resources).close();
249+
( (Closeable) resources ).close();
222250
}
223251

224252
}
225253
};
226-
}
254+
255+
};
227256

228257
public Iterator<PlexusIoResource> getResources()
229258
throws IOException

0 commit comments

Comments
 (0)