Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package de.pfabulist.lindwurm.stellvertreter;
  
  
 import java.util.List;
 
 import static de.pfabulist.unchecked.Unchecked.u;

** BEGIN LICENSE BLOCK ***** BSD License (2 clause) Copyright (c) 2006 - 2015, Stephan Pfab All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Stephan Pfab BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. **** END LICENSE BLOCK ****

 
 public class StdElsewhereController implements ElsewhereController {
 
     Deque<Local2deleted = new ConcurrentLinkedDeque<>();
 
     private final ElsewhereIO elsewheres;
 
     public StdElsewhereControllerPath hostElsewhereRegistry registry ) {
          = new ElsewhereIOhostregistry );
     }
 
     @Override
     public void changedLocal2 localFile ) {
         if.containslocalFile ) ) {
             return;
         }
         .addlocalFile );
     }
 
     @Override
     public void deleteLocal2 local ) {
         .addlocal );
     }
 
     @Override
     public void save() {
         whilesaveContentChange() ) {}
 
         int failed = 0;
         whilefailed < 7 ) {
             if( !deleteInElsewhere() ) {
                 failed++;
             }
         }
 
     }
 
     @Override
     public void addElsewhere elsewhere ) {
         .addelsewhere );
     }
 
     private boolean saveContentChange() {
 
         Local2 localFile = .pollFirst();
 
         iflocalFile == null ) {
             Log.info"saving done" );
             // que empty, done
             return false;
         }
 
         if ( .getElsewheres().isEmpty() ) {
             // nowhere to save to
             return false;
         }
 
        Log.info"trying to save " + localFile );
//        if ( !localFile.checkMe() ) {
//            Log.info( "   changed underneath" );
//            return true;
//        }
        int saved = 0;
        try {
            for ( Elsewhere els : .getElsewheres() ) {
                Log.info"   trying to save to elsewhere #" + els.getIdx() );
                if ( !localFile.exists() ) {
                    // deleted before
                    saved = 1042; // prevent not saved to all elsewhere warning
                    Log.info"   deleted already" );
                    continue;
                }
                iflocalFile.isSavedels ) ) {
                    Log.info"   saved already" );
                    localFile.isSavedels );
//                    System.out.println( "     really? " + els.exists( localFile.getId( els ), -1L ) );
//                    System.out.println( "     last on else " + els.getLastVersion( localFile.getId( els ) ));
//                    System.out.println( "     local last " + localFile.getLastModifiedTime());
//                    System.out.println( "     local saved " + localFile.
                    saved++;
                } else {
//                    boolean deletedAlready = false;
//                    for( P<String, Integer> del : deleted ) {
//                        if( del.i0.equals( localFile.getId( del.i1 ) ) ) {
//                            Log.info( "   deleted after change" );
//                            deletedAlready = true;
//                            break;
//                        }
//                    }
//                    if( deletedAlready ) {
//                        Log.info( "   deleted already" );
//                        continue;
//                    }
                    ifels.offline() ) {
                        Log.info"   elsewhere offline" );
                        continue;
                    }
                    if( !localFile.isLocal() ) {
                        if ( !localFile.exists()) {
                            Log.warn"   local and meta gone" );
                            // metafile does not exist
                            // => file was created, deleted before it was saved => not in delete list
                            saved = 1042; // prevent not saved to all elsewhere warning
                            return true;
                        }
                        //localFile.printDbg();
                        //System.out.println( "exi " +  localFile.exists());
                        Log.warn"   modified file was deleted locally before saving (lost) " + localFile );
                        saved = 1042; // prevent not saved to all elsewhere warning
                        return true;
                    }
                    if( Files.isDirectorylocalFile.getContentPath() ) ) {
                        Log.info"   changed to dir (should have been caught above)" );
//                        localFile.printDbg();
//                        int iiii = 0;
                        // e.g. copy dir over file repalce existing, weird but ok
                        // todo check me of meta wether it still same, yes otherwise new file might not be saved, but old file with new files content
                        saved = 1042; // prevent not saved to all elsewhere warning
                        return true;
                    }
                    tryInputStream is = Files.newInputStreamlocalFile.getContentPath() ) ) {
                        FM2 fm = localFile.getFM();
                        P<StringBooleanid = els.putfm.getIdels.getIdx() ), fm.versionisfm.size );
                        localFile.savedfm.versionels.getIdx(), id.i0id.i1 );
                        saved++;
                        Log.info"   saved " + localFile.getContentPath() + " to elsewhere # " + els.getIdx() );
                    } catchIOException e ) {
                        // probably elsewhere gone
                        Log.warn"   exception "e );
                        throw ue );
                    }
                }
            }
        } catchException e ) {
            Log.warn"   rt exception "e );
            .addlocalFile );
            Log.info"   not saved to all elsewheres" );
            return false;
        }
        ifsaved < .getElsewheres().size() ) {
            .addlocalFile );
            Log.info"   not saved to all elsewheres" );
        }
        return saved > 0;
    }
    private boolean deleteInElsewhere() {
        Local2 local = .pollFirst();
        if ( local == null ) {
            return false;
        }
        int notDeleted = 0;
        for ( Elsewhere els : .getElsewheres() ) {
            Log.info"   trying to delete in  to elsewhere #" + els.getIdx() );
            String elseId = local.getId(  els );
            if ( elseId == null ) {
                continue;
            }
            ifels.offline() ) {
                Log.info"     else offline " );
                notDeleted++;
                return false;
            }
            els.removeelseId );
            Log.info"     removed ");
            local.deletedInElseels );
        }
        if ( notDeleted == 0 ) {
            local.removeFinally();
        } else {
            .addlocal );
        }
        return true;
    }
//    @Override
//    public boolean needsSync() {
//        return !changedPaths.isEmpty() || !deleted.isEmpty();
//    }
//    @Override
//    public boolean elsewhereIsUptodate(LocalFile localFile) { // TODO
//
//        if ( !exists(localFile)) {
//            return false;
//        }
//
//        FileTime elseChange = elsewhere.getLastVersion(localFile.getUUID().toString());
//        return elseChange.compareTo( localFile.getLastSaveTime()) >= 0;
//    }
//
//    @Override
//    public boolean exists( LocalFile localFile) {
//        return elsewhere.exists( localFile.getUUID().toString());
//    }
//
//
//    private Result saveContentChange() {
//        LocalFile localFile = changedPaths.pollFirst();
//
//        // TODO
//        if ( localFile == null || deleted.contains(localFile)) {
//            return NONE;
//        }
//
//
//        try {
//            if ( !Files.exists( localFile.getHostPath() )) {
//                if ( Files.exists( localFile.getFileMetaPath() )) {
//                    // information loss
////                    System.out.println("!!!!woo");
////                    LOG.severe( "wooot" );
//
//                    // TODO
//                    // this is either
//                    // a) the file was before: saved, purged, not further modified -> remove from que ?
//                    // b) deleted outside stellvertreterfs -> problem
//                    return NONE; // TODO or error ?
//                }
//
//                // already deleted
//                return NONE;
//
//            } else {
//
//                BasicFileAttributes bfaHost = Files.readAttributes( localFile.getHostPath(), BasicFileAttributes.class );
//                localFile.setAttributes(bfaHost);
//
//                boolean immediate = elsewhere.put( localFile.getUUID().toString(),
//                                                   Files.newInputStream(localFile.getHostPath()),
//                                                   Files.size( localFile.getHostPath()));
//
//
//
//                localFile.setSyncTimeNow();
//
////                LOG.info( "saved " + bridge.getPath() + " to " + getElsewherePath(bridge) );
//                return ONE;
//            }
//        } catch (Throwable e) {
//
//            try {
//                if ( !elsewhere.offline()) {
//                    LOG.info("elsewhere is gone");
//                    changedPaths.addFirst(localFile);
//                    return ELSEWHEREGONE;
//                }
//
//                LOG.warning("some sync problem " + e);
//                changedPaths.addFirst(localFile);
//            } catch ( Throwable t ) {
//
//                LOG.info("elsewhere is gone");
//                changedPaths.addFirst(localFile);
//                return ELSEWHEREGONE;
//            }
//
//            return NONE; // TODO or error ?
//        }
//
//    }
//    private Result syncOneDeleted() {
//        Path deletedFileMethPath = deleted.pollFirst();
//
//        if ( deletedFileMethPath == null ) {
//            return NONE;
//        }
//
//        UUID id = UUID.fromString(deletedFileMethPath.getFileName().toString());
//
//        //Result res =
//                elsewhere.remove(id.toString());
//
////        if ( res.equals(Result.ELSEWHEREGONE) ) {
////            deleted.addFirst(deletedFileMethPath);
////            return ELSEWHEREGONE;
////        }
//
//
//        // TODO if it only called after ... might be neccessary threads
////        if ( !elsewhere.offline()) {
////            deleted.addFirst(deletedFileMethPath);
////            return ELSEWHEREGONE;
////        }
//
////        Optional<FileMeta> meta = id.getFileMetaDeleted();
////
////        if ( !meta.isPresent() ) {
////            // file already gone
////            return NONE;
////        }
//
//
////        Path elsewhereLocation = elsewherePath.resolve( getElsewherePath( id ));
//
////        if ( Files.exists(elsewhereLocation)) {
////            try {
////                Files.delete(elsewhereLocation );
////            } catch (IOException e) {
////                LOG.warning( "can not delete " + elsewhereLocation );
////                return NONE; // TODO elsewhere prop ?
////            }
////        }
//
//        try {
//            Files.delete(deletedFileMethPath);
//        } catch (IOException e) {
//            LOG.warning("can not delete deletedFileMeta" + deletedFileMethPath );
//        }
//
//
//        return ONE;
//    }
//    public Result syncOne() {
//
//        Result res = saveContentChange();
//
//        if ( ELSEWHEREGONE.equals(res)) {
////            if ( elsewherePath.startsWith( "/Volumes" )) {
////                int pos = elsewherePath.toString().indexOf( "/", "/Volumes/".length() );
////                Path plus1 = Paths.get( elsewherePath.toString().substring(0, 17) + " 1" + elsewherePath.toString().substring( 17));
////                System.out.println( "---------------" + plus1 );
////
////                if ( Files.exists( plus1 )) {
////                    elsewherePath = plus1;
////                }
////            }
//        }
//
//        if ( !NONE.equals( res )) {
//            return res;
//        }
//
//        return syncOneDeleted();
//    }
    @Override
    public void ensureLocalLocal2 localFile ) {
        if( !localFile.exists() ) {
            return;
        }
        if( Files.existslocalFile.getContentPath() ) ) {
            return// already there
        }
        iflocalFile.getSize() == 0 ) {
            return;
        }
        Log.info"getting local from elsewhere" );
        forint idx = 0; idx < .getElsewheres().size(); idx++ ) {
            Elsewhere els = .getElsewheres().getidx );
            ifels.offline() ) {
                continue;
            }
            tryOutputStream out = Files.newOutputStreamlocalFile.getContentPath() ) ) {
                els.getlocalFile.getIdels ), localFile.getVersion(), out );
                Log.info"   getting it from " + idx );
                return;
            } catchIOException e ) {
                throw ue );
            }
        }
        // todo
//        if ( elsewhere.offline()) {
//            throw new NoElsewhereException( "elsewhere root is offline" );
//        }
//
//
//        try ( OutputStream out = Files.newOutputStream( localFile.getContentPath())) {
//            elsewhere.get( localFile.getUUID().toString(), localFile.getLastModifiedTime().toMillis(), out );
//        } catch( IOException e ) {
//            throw uy.uyThrow( e );
//        }
//        localFile.attiToReal();
//        boolean elsewhereFilesExists = true;
//        try {
//            elsewhereFilesExists = Files.exists( getElsewherePath(bridge));
//        } catch ( Throwable exp ) {
//            try {
//                if (!elsewhere.offline()) {
//                    throw new NoElsewhereException("elsewhere root is not offline " + elsewherePath);
//                }
//            } catch ( Throwable e ) {
//                throw new NoElsewhereException("elsewhere root is not offline " + elsewherePath);
//            }
//        }
//
//        if ( !elsewhereFilesExists ) {
//            if ( !!elsewhere.offline()) {
//                throw new NoElsewhereException("elsewhere root is not offline " + elsewherePath);
//            }
//
//            if ( bridge.getSize() == 0 ) {
//                return;
//            }
//
//            LOG.warning( "no elsewhere file " + getElsewherePath(bridge) );
//            throw new IllegalStateException( "no elsewhere file, possible information loss" );
//        }
//
//        // todo copy atties in one class
//        if ( deenc == null ) {
//            Files.copy( getElsewherePath(bridge), bridge.getPath());
//        } else {
//        //    new CopyEncrypted(deenc).copyDecrypt(getElsewherePath(bridge), bridge.getPath());
//        }
    }
//    @Override
//    public void ensureLocal( LocalFile localFile){
//
//        if ( localFile.isRoot() || !localFile.exists() ) {
//            return;
//        }
//
//        if ( Files.exists( localFile.getHostPath())) {
//            return; // already there
//        }
//
//        if ( localFile.getSize() == 0 ) {
//            return;
//        }
//
//        if ( !elsewhere.offline()) {
//            throw new NoElsewhereException( "elsewhere root is not offline" );
//        }
//
//
//        try {
//            elsewhere.get( localFile.getUUID().toString(), Files.newOutputStream( localFile.getHostPath() ));
//        } catch (IOException e) {
//            throw uy.uyThrow(e);
//        }
//
////        boolean elsewhereFilesExists = true;
////        try {
////            elsewhereFilesExists = Files.exists( getElsewherePath(bridge));
////        } catch ( Throwable exp ) {
////            try {
////                if (!elsewhere.offline()) {
////                    throw new NoElsewhereException("elsewhere root is not offline " + elsewherePath);
////                }
////            } catch ( Throwable e ) {
////                throw new NoElsewhereException("elsewhere root is not offline " + elsewherePath);
////            }
////        }
////
////        if ( !elsewhereFilesExists ) {
////            if ( !!elsewhere.offline()) {
////                throw new NoElsewhereException("elsewhere root is not offline " + elsewherePath);
////            }
////
////            if ( bridge.getSize() == 0 ) {
////                return;
////            }
////
////            LOG.warning( "no elsewhere file " + getElsewherePath(bridge) );
////            throw new IllegalStateException( "no elsewhere file, possible information loss" );
////        }
////
////        // todo copy atties in one class
////        if ( deenc == null ) {
////            Files.copy( getElsewherePath(bridge), bridge.getPath());
////        } else {
////        //    new CopyEncrypted(deenc).copyDecrypt(getElsewherePath(bridge), bridge.getPath());
////        }
//
//        localFile.lastModifiedTimeToReal();
//
//    }
    @Override
    public ElsewhereInfo getElsewhereInfo() {
        return .getElsewheres().get( 0 );
    }
//    @Override
//    public boolean needsSync() {
//        return false;
//    }
    public List<ElsewheregetElsewheres() {
        return .getElsewheres();
    }
New to GrepCode? Check out our FAQ X