Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.uberfire.io.impl.cluster;
  
  import static org.uberfire.commons.validation.PortablePreconditions.*;
  import static org.uberfire.commons.validation.Preconditions.*;
  import static org.uberfire.io.impl.cluster.ClusterMessageType.*;
  
 import java.net.URI;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import  org.uberfire.commons.async.DescriptiveRunnable;
 import  org.uberfire.commons.async.SimpleAsyncExecutorService;
 import  org.uberfire.commons.cluster.ClusterService;
 import  org.uberfire.commons.cluster.ClusterServiceFactory;
 import  org.uberfire.commons.cluster.LockExecuteNotifySyncReleaseTemplate;
 import  org.uberfire.commons.data.Pair;
 import  org.uberfire.commons.lock.LockExecuteReleaseTemplate;
 import  org.uberfire.commons.message.AsyncCallback;
 import  org.uberfire.commons.message.MessageHandler;
 import  org.uberfire.commons.message.MessageHandlerResolver;
 import  org.uberfire.commons.message.MessageType;
 import  org.uberfire.java.nio.IOException;
 import  org.uberfire.java.nio.base.FileSystemId;
 import  org.uberfire.java.nio.base.SeekableByteChannelWrapperImpl;
 import  org.uberfire.java.nio.channels.SeekableByteChannel;
 import  org.uberfire.java.nio.file.AtomicMoveNotSupportedException;
 import  org.uberfire.java.nio.file.CopyOption;
 import  org.uberfire.java.nio.file.DeleteOption;
 import  org.uberfire.java.nio.file.DirectoryNotEmptyException;
 import  org.uberfire.java.nio.file.DirectoryStream;
 import  org.uberfire.java.nio.file.FileAlreadyExistsException;
 import  org.uberfire.java.nio.file.FileSystem;
 import  org.uberfire.java.nio.file.FileSystemAlreadyExistsException;
 import  org.uberfire.java.nio.file.FileSystemNotFoundException;
 import  org.uberfire.java.nio.file.NoSuchFileException;
 import  org.uberfire.java.nio.file.NotDirectoryException;
 import  org.uberfire.java.nio.file.OpenOption;
 import  org.uberfire.java.nio.file.Option;
 import  org.uberfire.java.nio.file.Path;
 import  org.uberfire.java.nio.file.ProviderNotFoundException;
 import  org.uberfire.java.nio.file.attribute.FileAttribute;
 import  org.uberfire.java.nio.file.attribute.FileAttributeView;
 import  org.uberfire.java.nio.file.attribute.FileTime;
 
 public class IOServiceClusterImpl implements IOClusteredService {
 
     private static final Logger logger = LoggerFactory.getLoggerIOServiceClusterImpl.class );
 
     private final IOServiceIdentifiable service;
     private final ClusterService clusterService;
     private final AtomicBoolean started = new AtomicBooleanfalse );
 
     private NewFileSystemListener newFileSystemListener = null;
 
     public IOServiceClusterImplfinal IOService service,
                                  final ClusterServiceFactory clusterServiceFactory ) {
         thisserviceclusterServiceFactorytrue );
     }
 
     public IOServiceClusterImplfinal IOService service,
                                  final ClusterServiceFactory clusterServiceFactory,
                                  final boolean autoStart ) {
         checkNotNull( "clusterServiceFactory"clusterServiceFactory );
         this. = checkInstanceOf"service"serviceIOServiceIdentifiable.class );
 
         .debug"Creating instance of cluster service with auto start {}"autoStart );
         this. = clusterServiceFactory.build( new MessageHandlerResolver() {
 
             final MessageHandler newFs = new NewFileSystemMessageHandler();
             final MessageHandler syncFs = new SyncFileSystemMessageHandler();
             final MessageHandler queryFs = new QueryFileSystemMessageHandler();
 
             @Override
             public String getServiceId() {
                 return IOServiceClusterImpl.this..getId();
             }
 
             @Override
             public MessageHandler resolveHandlerfinal String serviceId,
                                                   final MessageType type ) {
                if ( serviceId.equalsIOServiceClusterImpl.this..getId() ) ) {
                    if ( .equalstype ) ) {
                        return ;
                    } else if ( .equalstype ) ) {
                        return ;
                    } else if ( .equalstype ) ) {
                        return ;
                    }
                }
                return null;
            }
        } );
        this..onStart( new Runnable() {
            @Override
            public void run() {
                start();
            }
        } );
    }
    @Override
    public void start() {
        .settrue );
        .debug"Starting cluster service {}"this );
        //New cluster members are executed within locked
        new LockExecuteReleaseTemplate<Void>().execute( new FutureTask<Void>( new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                // 10 seconds
                int timeout = 10000;
                final AtomicBoolean msgAnsweredOrTimedout = new AtomicBooleanfalse );
                final AtomicBoolean onSync = new AtomicBooleanfalse );
                final Map<IntegerFileSystemInfofileSystems = new HashMap<IntegerFileSystemInfo>();
                .broadcastAndWait( .getId(), , Collections.<StringString>emptyMap(), timeoutnew AsyncCallback() {
                    @Override
                    public void onTimeOut() {
                        msgAnsweredOrTimedout.settrue );
                    }
                    @Override
                    public void onReplyfinal MessageType type,
                                         final Map<StringStringcontent ) {
                        if ( msgAnsweredOrTimedout.get() || onSync.get() ) {
                            return;
                        }
                        onSync.settrue );
                        SimpleAsyncExecutorService.getUnmanagedInstance().execute( new DescriptiveRunnable() {
                            @Override
                            public String getDescription() {
                                return "Cluster Messaging Reply [" + .getId() + "/QUERY_FOR_FS]";
                            }
                            @Override
                            public void run() {
                                for ( final Map.Entry<StringStringentry : content.entrySet() ) {
                                    if ( entry.getKey().startsWith"fs_" ) ) {
                                        int index = Integer.valueOfentry.getKey().substringentry.getKey().lastIndexOf"_" ) + 1 ) );
                                        if ( !fileSystems.containsKeyindex ) ) {
                                            fileSystems.putindexnew FileSystemInfo() );
                                        }
                                        final FileSystemInfo fsInfo = fileSystems.getindex );
                                        if ( entry.getKey().startsWith"fs_id_" ) ) {
                                            fsInfo.setIdentry.getValue() );
                                        } else if ( entry.getKey().startsWith"fs_scheme_" ) ) {
                                            fsInfo.setSchemeentry.getValue() );
                                        } else if ( entry.getKey().startsWith"fs_uri_" ) ) {
                                            fsInfo.setUrientry.getValue() );
                                        }
                                    }
                                }
                                for ( final FileSystemInfo fileSystemInfo : new HashSet<FileSystemInfo>( fileSystems.values() ) ) {
                                    try {
                                        final URI newFS = URI.createfileSystemInfo.getScheme() + "://" + fileSystemInfo.getId() );
                                        .newFileSystemnewFS, Collections.<StringObject>emptyMap() );
                                    } catch ( FileSystemAlreadyExistsException ex ) {
                                    }
                                    final URI fs = URI.createfileSystemInfo.getScheme() + "://" + fileSystemInfo.getId() + "?sync=" + fileSystemInfo.getUri().split"\n" )[ 0 ] + "&force" );
                                    .getFileSystemfs );
                                }
                                msgAnsweredOrTimedout.settrue );
                            }
                        } );
                    }
                } );
                while ( !msgAnsweredOrTimedout.get() ) {
                    try {
                        Thread.sleep( 100 );
                    } catch ( InterruptedException ignored ) {
                    }
                }
                return null;
            }
        } ) );
    }
    @Override
    public void dispose() {
        .dispose();
        .dispose();
    }
    @Override
    public void startBatch( FileSystem fs ) throws InterruptedException {
        startBatchnew FileSystem[]{ fs } );
    }
    @Override
    public void startBatch( FileSystem[] fs,
                            final Option... options ) throws InterruptedException {
        .lock();
        .startBatchfsoptions );
    }
    @Override
    public void startBatch( FileSystem fs,
                            Option... options ) throws InterruptedException {
        .lock();
        .startBatchfsoptions );
    }
    @Override
    public void startBatch( FileSystem... fs ) throws InterruptedException {
        .lock();
        .startBatchfs );
    }
    @Override
    public void endBatch() {
        .endBatch();
        .unlock();
    }
    @Override
    public FileAttribute<?>[] convertfinal Map<String, ?> attrs ) {
        return .convertattrs );
    }
    @Override
    public Path getfinal String first,
                     final String... more ) throws IllegalArgumentException {
        return .getfirstmore );
    }
    @Override
    public Path getfinal URI uri ) throws IllegalArgumentException, FileSystemNotFoundException, SecurityException {
        return .geturi );
    }
    @Override
    public Iterable<FileSystem> getFileSystems() {
        return .getFileSystems();
    }
    @Override
    public FileSystem getFileSystemfinal URI uri ) throws IllegalArgumentException, FileSystemNotFoundException, ProviderNotFoundException, SecurityException {
        return .getFileSystemuri );
    }
    @Override
    public FileSystem newFileSystemfinal URI uri,
                                     final Map<String, ?> env ) throws IllegalArgumentException, FileSystemAlreadyExistsException, ProviderNotFoundException, IOException, SecurityException {
        return new LockExecuteNotifySyncReleaseTemplate<FileSystem>() {
            @Override
            public MessageType getMessageType() {
                return ;
            }
            @Override
            public String getServiceId() {
                return .getId();
            }
            @Override
            public Map<StringStringbuildContent() {
                return new HashMap<StringString>() {{
                    put"uri"uri.toString() );
                    for ( final Map.Entry<String, ?> entry : env.entrySet() ) {
                        putentry.getKey(), entry.getValue().toString() );
                    }
                }};
            }
            @Override
            public int timeOut() {
                return TIMEOUT;
            }
        }.execute( new FutureTask<FileSystem>( new Callable<FileSystem>() {
            @Override
            public FileSystem call() throws Exception {
                return .newFileSystemurinew HashMap<StringObject>( env ) {{
                    put"clusterService" );
                }} );
            }
        } ) );
    }
    @Override
    public void onNewFileSystemNewFileSystemListener listener ) {
        this. = listener;
    }
    @Override
    public InputStream newInputStreamfinal Path path,
                                       final OpenOption... options ) throws IllegalArgumentException, NoSuchFileException, UnsupportedOperationException, IOException, SecurityException {
        return .newInputStreampathoptions );
    }
    @Override
    public DirectoryStream<Path> newDirectoryStreamfinal Path dir ) throws IllegalArgumentException, NotDirectoryException, IOException, SecurityException {
        return .newDirectoryStreamdir );
    }
    @Override
    public DirectoryStream<Path> newDirectoryStream( final Path dir,
                                                     final DirectoryStream.Filter<Path> filter ) throws IllegalArgumentException, NotDirectoryException, IOException, SecurityException {
        return .newDirectoryStreamdirfilter );
    }
    @Override
    public Path createFilefinal Path path,
                            final FileAttribute<?>... attrs ) throws IllegalArgumentExceptionUnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException {
        return new FileSystemSyncLock<Path>( .getId(), path.getFileSystem() ).execute( new FutureTask<Path>( new Callable<Path>() {
            @Override
            public Path call() throws Exception {
                return .createFilepathattrs );
            }
        } ) );
    }
    @Override
    public Path createDirectoryfinal Path dir,
                                 final FileAttribute<?>... attrs ) throws IllegalArgumentExceptionUnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException {
        return new FileSystemSyncLock<Path>( .getId(), dir.getFileSystem() ).execute( new FutureTask<Path>( new Callable<Path>() {
            @Override
            public Path call() throws Exception {
                return .createDirectorydirattrs );
            }
        } ) );
    }
    @Override
    public Path createDirectoriesfinal Path dir,
                                   final FileAttribute<?>... attrs ) throws UnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException {
        return new FileSystemSyncLock<Path>( .getId(), dir.getFileSystem() ).execute( new FutureTask<Path>( new Callable<Path>() {
            @Override
            public Path call() throws Exception {
                return .createDirectoriesdirattrs );
            }
        } ) );
    }
    @Override
    public Path createDirectoryfinal Path dir,
                                 final Map<String, ?> attrs ) throws IllegalArgumentExceptionUnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException {
        return new FileSystemSyncLock<Path>( .getId(), dir.getFileSystem() ).execute( new FutureTask<Path>( new Callable<Path>() {
            @Override
            public Path call() throws Exception {
                return .createDirectorydirattrs );
            }
        } ) );
    }
    @Override
    public Path createDirectoriesfinal Path dir,
                                   final Map<String, ?> attrs ) throws UnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException {
        return new FileSystemSyncLock<Path>( .getId(), dir.getFileSystem() ).execute( new FutureTask<Path>( new Callable<Path>() {
            @Override
            public Path call() throws Exception {
                return .createDirectoriesdirattrs );
            }
        } ) );
    }
    @Override
    public void deletefinal Path path,
                        final DeleteOption... options ) throws IllegalArgumentException, NoSuchFileException, DirectoryNotEmptyException, IOException, SecurityException {
        new FileSystemSyncLock<Void>( .getId(), path.getFileSystem() ).execute( new FutureTask<Void>( new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                .deletepathoptions );
                return null;
            }
        } ) );
    }
    @Override
    public boolean deleteIfExistsfinal Path path,
                                   final DeleteOption... options ) throws IllegalArgumentException, DirectoryNotEmptyException, IOException, SecurityException {
        return new FileSystemSyncLock<Boolean>( .getId(), path.getFileSystem() ).execute( new FutureTask<Boolean>( new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                return .deleteIfExistspathoptions );
            }
        } ) );
    }
    @Override
    public Path createTempFilefinal String prefix,
                                final String suffix,
                                final FileAttribute<?>... attrs ) throws IllegalArgumentExceptionUnsupportedOperationException, IOException, SecurityException {
        return .createTempFileprefixsuffixattrs );
    }
    @Override
    public Path createTempFilefinal Path dir,
                                final String prefix,
                                final String suffix,
                                final FileAttribute<?>... attrs ) throws IllegalArgumentExceptionUnsupportedOperationException, IOException, SecurityException {
        return .createTempFiledirprefixsuffixattrs );
    }
    @Override
    public Path createTempDirectoryfinal String prefix,
                                     final FileAttribute<?>... attrs ) throws IllegalArgumentExceptionUnsupportedOperationException, IOException, SecurityException {
        return .createTempDirectoryprefixattrs );
    }
    @Override
    public Path createTempDirectoryfinal Path dir,
                                     final String prefix,
                                     final FileAttribute<?>... attrs ) throws IllegalArgumentExceptionUnsupportedOperationException, IOException, SecurityException {
        return .createTempDirectorydirprefixattrs );
    }
    @Override
    public Path copyfinal Path source,
                      final Path target,
                      final CopyOption... options ) throws UnsupportedOperationException, FileAlreadyExistsException, DirectoryNotEmptyException, IOException, SecurityException {
        return new FileSystemSyncLock<Path>( .getId(), target.getFileSystem() ).execute( new FutureTask<Path>( new Callable<Path>() {
            @Override
            public Path call() throws Exception {
                return .copysourcetargetoptions );
            }
        } ) );
    }
    @Override
    public long copyfinal InputStream in,
                      final Path target,
                      final CopyOption... options ) throws IOException, FileAlreadyExistsException, DirectoryNotEmptyException, UnsupportedOperationExceptionSecurityException {
        return new FileSystemSyncLock<Long>( .getId(), target.getFileSystem() ).execute( new FutureTask<Long>( new Callable<Long>() {
            @Override
            public Long call() throws Exception {
                return .copyintargetoptions );
            }
        } ) );
    }
    @Override
    public long copyfinal Path source,
                      final OutputStream out ) throws IOException, SecurityException {
        return .copysourceout );
    }
    @Override
    public Path movefinal Path source,
                      final Path target,
                      final CopyOption... options ) throws UnsupportedOperationException, FileAlreadyExistsException, DirectoryNotEmptyException, AtomicMoveNotSupportedException, IOException, SecurityException {
        return new FileSystemSyncLock<Path>( .getId(), source.getFileSystem() ).execute( new FutureTask<Path>( new Callable<Path>() {
            @Override
            public Path call() throws Exception {
                return new FileSystemSyncLock<Path>( .getId(), target.getFileSystem() ).execute( new FutureTask<Path>( new Callable<Path>() {
                    @Override
                    public Path call() throws Exception {
                        return .movesourcetargetoptions );
                    }
                } ) );
            }
        } ) );
    }
    @Override
    public <V extends FileAttributeView> V getFileAttributeViewfinal Path path,
                                                                 final Class<V> type ) throws IllegalArgumentException {
        return .getFileAttributeViewpathtype );
    }
    @Override
    public Map<StringObjectreadAttributesfinal Path path ) throws UnsupportedOperationException, NoSuchFileException, IllegalArgumentException, IOException, SecurityException {
        return .readAttributespath );
    }
    @Override
    public Map<StringObjectreadAttributesfinal Path path,
                                               final String attributes ) throws UnsupportedOperationException, NoSuchFileException, IllegalArgumentException, IOException, SecurityException {
        return .readAttributespathattributes );
    }
    @Override
    public Path setAttributesfinal Path path,
                               final FileAttribute<?>... attrs ) throws UnsupportedOperationExceptionIllegalArgumentExceptionClassCastException, IOException, SecurityException {
        return new FileSystemSyncLock<Path>( .getId(), path.getFileSystem() ).execute( new FutureTask<Path>( new Callable<Path>() {
            @Override
            public Path call() throws Exception {
                return .setAttributespathattrs );
            }
        } ) );
    }
    @Override
    public Path setAttributesfinal Path path,
                               final Map<StringObjectattrs ) throws UnsupportedOperationExceptionIllegalArgumentExceptionClassCastException, IOException, SecurityException {
        return new FileSystemSyncLock<Path>( .getId(), path.getFileSystem() ).execute( new FutureTask<Path>( new Callable<Path>() {
            @Override
            public Path call() throws Exception {
                return .setAttributespathattrs );
            }
        } ) );
    }
    @Override
    public Path setAttributefinal Path path,
                              final String attribute,
                              final Object value ) throws UnsupportedOperationExceptionIllegalArgumentExceptionClassCastException, IOException, SecurityException {
        return new FileSystemSyncLock<Path>( .getId(), path.getFileSystem() ).execute( new FutureTask<Path>( new Callable<Path>() {
            @Override
            public Path call() throws Exception {
                return .setAttributepathattributevalue );
            }
        } ) );
    }
    @Override
    public Object getAttributefinal Path path,
                                final String attribute ) throws UnsupportedOperationExceptionIllegalArgumentException, IOException, SecurityException {
        return .getAttributepathattribute );
    }
    @Override
    public FileTime getLastModifiedTimefinal Path path ) throws IllegalArgumentException, IOException, SecurityException {
        return .getLastModifiedTimepath );
    }
    @Override
    public long sizefinal Path path ) throws IllegalArgumentException, IOException, SecurityException {
        return .sizepath );
    }
    @Override
    public boolean existsfinal Path path ) throws IllegalArgumentExceptionSecurityException {
        return .existspath );
    }
    @Override
    public boolean notExistsfinal Path path ) throws IllegalArgumentExceptionSecurityException {
        return .notExistspath );
    }
    @Override
    public boolean isSameFilefinal Path path,
                               final Path path2 ) throws IllegalArgumentException, IOException, SecurityException {
        return .isSameFilepathpath2 );
    }
    @Override
    public BufferedReader newBufferedReaderfinal Path path,
                                             final Charset cs ) throws IllegalArgumentException, NoSuchFileException, IOException, SecurityException {
        return .newBufferedReaderpathcs );
    }
    @Override
    public byte[] readAllBytesfinal Path path ) throws IOException, OutOfMemoryErrorSecurityException {
        return .readAllBytespath );
    }
    @Override
    public List<StringreadAllLinesfinal Path path ) throws IllegalArgumentException, NoSuchFileException, IOException, SecurityException {
        return .readAllLinespath );
    }
    @Override
    public List<StringreadAllLinesfinal Path path,
                                      final Charset cs ) throws IllegalArgumentException, NoSuchFileException, IOException, SecurityException {
        return .readAllLinespathcs );
    }
    @Override
    public String readAllStringfinal Path path,
                                 final Charset cs ) throws IllegalArgumentException, NoSuchFileException, IOException {
        return .readAllStringpathcs );
    }
    @Override
    public String readAllStringfinal Path path ) throws IllegalArgumentException, NoSuchFileException, IOException {
        return .readAllStringpath );
    }
    @Override
    public Path writefinal Path path,
                       final byte[] bytes,
                       final OpenOption... options ) throws IOException, UnsupportedOperationExceptionSecurityException {
        return new FileSystemSyncLock<Path>( .getId(), path.getFileSystem() ).execute( new FutureTask<Path>( new Callable<Path>() {
            @Override
            public Path call() throws Exception {
                return .writepathbytesoptions );
            }
        } ) );
    }
    @Override
    public Path writefinal Path path,
                       final byte[] bytes,
                       final Map<String, ?> attrs,
                       final OpenOption... options ) throws IOException, UnsupportedOperationExceptionSecurityException {
        return new FileSystemSyncLock<Path>( .getId(), path.getFileSystem() ).execute( new FutureTask<Path>( new Callable<Path>() {
            @Override
            public Path call() throws Exception {
                return .writepathbytesattrsoptions );
            }
        } ) );
    }
    @Override
    public Path writefinal Path path,
                       final byte[] bytes,
                       final Set<? extends OpenOption> options,
                       final FileAttribute<?>... attrs ) throws IllegalArgumentException, IOException, UnsupportedOperationException {
        return new FileSystemSyncLock<Path>( .getId(), path.getFileSystem() ).execute( new FutureTask<Path>( new Callable<Path>() {
            @Override
            public Path call() throws Exception {
                return .writepathbytesoptionsattrs );
            }
        } ) );
    }
    @Override
    public Path writefinal Path path,
                       final Iterable<? extends CharSequencelines,
                       final Charset cs,
                       final OpenOption... options ) throws IllegalArgumentException, IOException, UnsupportedOperationExceptionSecurityException {
        return new FileSystemSyncLock<Path>( .getId(), path.getFileSystem() ).execute( new FutureTask<Path>( new Callable<Path>() {
            @Override
            public Path call() throws Exception {
                return .writepathlinescsoptions );
            }
        } ) );
    }
    @Override
    public Path writefinal Path path,
                       final String content,
                       final OpenOption... options ) throws IllegalArgumentException, IOException, UnsupportedOperationException {
        return new FileSystemSyncLock<Path>( .getId(), path.getFileSystem() ).execute( new FutureTask<Path>( new Callable<Path>() {
            @Override
            public Path call() throws Exception {
                return .writepathcontentoptions );
            }
        } ) );
    }
    @Override
    public Path writefinal Path path,
                       final String content,
                       final Charset cs,
                       final OpenOption... options ) throws IllegalArgumentException, IOException, UnsupportedOperationException {
        return new FileSystemSyncLock<Path>( .getId(), path.getFileSystem() ).execute( new FutureTask<Path>( new Callable<Path>() {
            @Override
            public Path call() throws Exception {
                return .writepathcontentcsoptions );
            }
        } ) );
    }
    @Override
    public Path writefinal Path path,
                       final String content,
                       final Set<? extends OpenOption> options,
                       final FileAttribute<?>... attrs ) throws IllegalArgumentException, IOException, UnsupportedOperationException {
        return new FileSystemSyncLock<Path>( .getId(), path.getFileSystem() ).execute( new FutureTask<Path>( new Callable<Path>() {
            @Override
            public Path call() throws Exception {
                return .writepathcontentoptionsattrs );
            }
        } ) );
    }
    @Override
    public Path writefinal Path path,
                       final String content,
                       final Charset cs,
                       final Set<? extends OpenOption> options,
                       final FileAttribute<?>... attrs ) throws IllegalArgumentException, IOException, UnsupportedOperationException {
        return new FileSystemSyncLock<Path>( .getId(), path.getFileSystem() ).execute( new FutureTask<Path>( new Callable<Path>() {
            @Override
            public Path call() throws Exception {
                return .writepathcontentcsoptionsattrs );
            }
        } ) );
    }
    @Override
    public Path writefinal Path path,
                       final String content,
                       final Map<String, ?> attrs,
                       final OpenOption... options ) throws IllegalArgumentException, IOException, UnsupportedOperationException {
        return new FileSystemSyncLock<Path>( .getId(), path.getFileSystem() ).execute( new FutureTask<Path>( new Callable<Path>() {
            @Override
            public Path call() throws Exception {
                return .writepathcontentattrsoptions );
            }
        } ) );
    }
    @Override
    public Path writefinal Path path,
                       final String content,
                       final Charset cs,
                       final Map<String, ?> attrs,
                       final OpenOption... options ) throws IllegalArgumentException, IOException, UnsupportedOperationException {
        return new FileSystemSyncLock<Path>( .getId(), path.getFileSystem() ).execute( new FutureTask<Path>( new Callable<Path>() {
            @Override
            public Path call() throws Exception {
                return .writepathcontentcsattrsoptions );
            }
        } ) );
    }
    @Override
    public OutputStream newOutputStreamfinal Path path,
                                         final OpenOption... options ) throws IllegalArgumentExceptionUnsupportedOperationException, IOException, SecurityException {
        final OutputStream out = .newOutputStreampathoptions );
        return new OutputStream() {
            @Override
            public void writefinal int b ) throws java.io.IOException {
                out.writeb );
            }
            @Override
            public void close() throws java.io.IOException {
                new FileSystemSyncLock<Void>( .getId(), path.getFileSystem() ).execute( new FutureTask<Void>( new Callable<Void>() {
                    @Override
                    public Void call() throws Exception {
                        out.close();
                        return null;
                    }
                } ) );
            }
        };
    }
    @Override
    public SeekableByteChannel newByteChannelfinal Path path,
                                               final OpenOption... options ) throws IllegalArgumentExceptionUnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException {
        final SeekableByteChannel sbc = .newByteChannelpathoptions );
        return new SeekableByteChannelWrapperImpl( sbc ) {
            @Override
            public void close() throws java.io.IOException {
                new FileSystemSyncLock<Void>( .getId(), path.getFileSystem() ).execute( new FutureTask<Void>( new Callable<Void>() {
                    @Override
                    public Void call() throws Exception {
                        sbc.close();
                        return null;
                    }
                } ) );
            }
        };
    }
    @Override
    public SeekableByteChannel newByteChannelfinal Path path,
                                               final Set<? extends OpenOption> options,
                                               final FileAttribute<?>... attrs ) throws IllegalArgumentExceptionUnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException {
        final SeekableByteChannel sbc = .newByteChannelpathoptionsattrs );
        return new SeekableByteChannelWrapperImpl( sbc ) {
            @Override
            public void close() throws java.io.IOException {
                new FileSystemSyncLock<Void>( .getId(), path.getFileSystem() ).execute( new FutureTask<Void>( new Callable<Void>() {
                    @Override
                    public Void call() throws Exception {
                        sbc.close();
                        return null;
                    }
                } ) );
            }
        };
    }
    @Override
    public BufferedWriter newBufferedWriterfinal Path path,
                                             final Charset cs,
                                             final OpenOption... options ) throws IllegalArgumentException, IOException, UnsupportedOperationExceptionSecurityException {
        return new BufferedWriter.newBufferedWriterpathcsoptions ) ) {
            @Override
            public void close() throws java.io.IOException {
                new FileSystemSyncLock<Void>( .getId(), path.getFileSystem() ).execute( new FutureTask<Void>( new Callable<Void>() {
                    @Override
                    public Void call() throws Exception {
                        superClose();
                        return null;
                    }
                } ) );
            }
            private void superClose() {
                try {
                    super.close();
                } catch ( java.io.IOException e ) {
                    throw new RuntimeExceptione );
                }
            }
        };
    }
    class NewFileSystemMessageHandler implements MessageHandler {
        @Override
        public Pair<MessageType, Map<StringString>> handleMessagefinal MessageType type,
                                                                     final Map<StringStringcontent ) {
            if ( .equalstype ) ) {
                final String _uri = content.get"uri" );
                final Map<StringStringenv = new HashMap<StringString>();
                for ( final Map.Entry<StringStringentry : content.entrySet() ) {
                    if ( !( entry.getKey().equals"uri" ) || entry.getKey().equals"type" ) ) ) {
                        env.putentry.getKey(), entry.getValue() );
                    }
                }
                final URI uri = URI.create_uri );
                final FileSystem fs = .newFileSystemurienv );
                .executefsuri.getScheme(), ( (FileSystemId) fs ).id(), env );
            }
            return null;
        }
    }
    class SyncFileSystemMessageHandler implements MessageHandler {
        @Override
        public Pair<MessageType, Map<StringString>> handleMessagefinal MessageType type,
                                                                     final Map<StringStringcontent ) {
            if ( .equalstype ) ) {
                final String scheme = content.get"fs_scheme" );
                final String id = content.get"fs_id" );
                String uris = content.get"fs_uri" );
                String[] supportedUris = uris.split"\n" );
                for ( String supportedUri : supportedUris ) {
                    try {
                        String origin;
                        try {
                            origin = URLEncoder.encodesupportedUri"UTF-8" );
                        } catch ( UnsupportedEncodingException e ) {
                            origin = supportedUri;
                        }
                        if ( origin != null ) {
                            final URI fs = URI.createscheme + "://" + id + "?sync=" + origin + "&force" );
                            .getFileSystemfs );
                        }
                        break;
                    } catch ( Exception e ) {
                        // try the other supported uri in case of failure
                        .warn"File system synchronization for origin {} failed with error {}, trying another if available",
                                     supportedUrie.getMessage() );
                    }
                }
            }
            return null;
        }
    }
    class QueryFileSystemMessageHandler implements MessageHandler {
        @Override
        public Pair<MessageType, Map<StringString>> handleMessagefinal MessageType type,
                                                                     final Map<StringStringcontent ) {
            if ( .equalstype ) ) {
                Map<StringStringreplyContent = new HashMap<StringString>();
                int i = 0;
                final Set<FileSystem> fileSystems = new HashSet<FileSystem>();
                for ( FileSystem fs : .getFileSystems() ) {
                    fileSystems.add( fs );
                }
                for ( final FileSystem fs : fileSystems ) {
                    replyContent.put"fs_scheme_" + ifs.getRootDirectories().iterator().next().toUri().getScheme() );
                    replyContent.put"fs_id_" + i, ( (FileSystemId) fs ).id() );
                    replyContent.put"fs_uri_" + ifs.toString() );
                    i++;
                }
                return new Pair<MessageType, Map<StringString>>( replyContent );
            }
            return null;
        }
    }
    static class FileSystemInfo {
        private String id;
        private String scheme;
        private String uri;
        FileSystemInfo() {
        }
        FileSystemInfoString id,
                        String scheme,
                        String uri ) {
            this. = id;
            this. = scheme;
            this. = uri;
        }
        String getId() {
            return ;
        }
        void setIdString id ) {
            this. = id;
        }
        String getScheme() {
            return ;
        }
        void setSchemeString scheme ) {
            this. = scheme;
        }
        String getUri() {
            return ;
        }
        void setUriString uri ) {
            this. = uri;
        }
    }