Search in sources :

Example 1 with Channel

use of java.nio.channels.Channel in project camel by apache.

the class FileLockExclusiveReadLockStrategy method doReleaseExclusiveReadLock.

@Override
protected void doReleaseExclusiveReadLock(GenericFileOperations<File> operations, GenericFile<File> file, Exchange exchange) throws Exception {
    // must call super
    super.doReleaseExclusiveReadLock(operations, file, exchange);
    FileLock lock = exchange.getProperty(asReadLockKey(file, Exchange.FILE_LOCK_EXCLUSIVE_LOCK), FileLock.class);
    RandomAccessFile rac = exchange.getProperty(asReadLockKey(file, Exchange.FILE_LOCK_EXCLUSIVE_LOCK), RandomAccessFile.class);
    String target = file.getFileName();
    if (lock != null) {
        Channel channel = lock.acquiredBy();
        try {
            lock.release();
        } finally {
            // close channel as well
            IOHelper.close(channel, "while releasing exclusive read lock for file: " + target, LOG);
            IOHelper.close(rac, "while releasing exclusive read lock for file: " + target, LOG);
        }
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) Channel(java.nio.channels.Channel) FileLock(java.nio.channels.FileLock)

Example 2 with Channel

use of java.nio.channels.Channel in project felix by apache.

the class Closure method execute.

@SuppressWarnings("unchecked")
private Object execute(List<Object> values, Channel capturingOutput) throws Exception {
    if (null != values) {
        parmv = new ArrayList<>(values);
        parms = new ArgList(parmv);
    } else if (null != parent) {
        // inherit parent closure parameters
        parmv = parent.parmv != null ? new ArrayList<>(parent.parmv) : null;
        parms = parmv != null ? new ArgList(parmv) : null;
    } else {
        // inherit session parameters
        Object args = session.get("args");
        if (null != args && args instanceof List<?>) {
            parmv = new ArrayList<>((List<Object>) args);
            parms = new ArgList(parmv);
        }
    }
    Result last = null;
    Operator operator = null;
    for (Iterator<Executable> iterator = program.tokens().iterator(); iterator.hasNext(); ) {
        Operator prevOperator = operator;
        Executable executable = iterator.next();
        if (iterator.hasNext()) {
            operator = (Operator) iterator.next();
        } else {
            operator = null;
        }
        if (prevOperator != null) {
            if (Token.eq("&&", prevOperator)) {
                if (!last.isSuccess()) {
                    continue;
                }
            } else if (Token.eq("||", prevOperator)) {
                if (last.isSuccess()) {
                    continue;
                }
            }
        }
        Channel[] streams;
        boolean[] toclose = new boolean[10];
        if (Pipe.getCurrentPipe() != null) {
            streams = Pipe.getCurrentPipe().streams.clone();
        } else {
            streams = new Channel[10];
            System.arraycopy(session.channels, 0, streams, 0, 3);
        }
        if (capturingOutput != null) {
            streams[1] = capturingOutput;
            toclose[1] = true;
        }
        CommandSessionImpl.JobImpl job;
        if (executable instanceof Pipeline) {
            Pipeline pipeline = (Pipeline) executable;
            List<Executable> exec = pipeline.tokens();
            Token s = exec.get(0);
            Token e = exec.get(exec.size() - 1);
            Token t = program.subSequence(s.start - program.start, e.start + e.length - program.start);
            job = session().createJob(t);
            for (int i = 0; i < exec.size(); i++) {
                Statement ex = (Statement) exec.get(i);
                Operator op = i < exec.size() - 1 ? (Operator) exec.get(++i) : null;
                Channel[] nstreams;
                boolean[] ntoclose;
                boolean endOfPipe;
                if (i == exec.size() - 1) {
                    nstreams = streams;
                    ntoclose = toclose;
                    endOfPipe = true;
                } else if (Token.eq("|", op)) {
                    PipedInputStream pis = new PipedInputStream();
                    PipedOutputStream pos = new PipedOutputStream(pis);
                    nstreams = streams.clone();
                    nstreams[1] = Channels.newChannel(pos);
                    ntoclose = toclose.clone();
                    ntoclose[1] = true;
                    streams[0] = Channels.newChannel(pis);
                    toclose[0] = true;
                    endOfPipe = false;
                } else if (Token.eq("|&", op)) {
                    PipedInputStream pis = new PipedInputStream();
                    PipedOutputStream pos = new PipedOutputStream(pis);
                    nstreams = streams.clone();
                    nstreams[1] = nstreams[2] = Channels.newChannel(pos);
                    ntoclose = toclose.clone();
                    ntoclose[1] = ntoclose[2] = true;
                    streams[0] = Channels.newChannel(pis);
                    toclose[0] = true;
                    endOfPipe = false;
                } else {
                    throw new IllegalStateException("Unrecognized pipe operator: '" + op + "'");
                }
                Pipe pipe = new Pipe(this, job, ex, nstreams, ntoclose, endOfPipe);
                job.addPipe(pipe);
            }
        } else {
            job = session().createJob(executable);
            Pipe pipe = new Pipe(this, job, (Statement) executable, streams, toclose, true);
            job.addPipe(pipe);
        }
        // Start pipe in background
        if (operator != null && Token.eq("&", operator)) {
            job.start(Status.Background);
            last = new Result((Object) null);
        } else // Start in foreground and wait for results
        {
            last = job.start(Status.Foreground);
            if (last == null) {
                last = new Result((Object) null);
            } else if (last.exception != null) {
                throw last.exception;
            }
        }
    }
    return last == null ? null : last.result;
}
Also used : Operator(org.apache.felix.gogo.runtime.Parser.Operator) Statement(org.apache.felix.gogo.runtime.Parser.Statement) Channel(java.nio.channels.Channel) ArrayList(java.util.ArrayList) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) Result(org.apache.felix.gogo.runtime.Pipe.Result) Pipeline(org.apache.felix.gogo.runtime.Parser.Pipeline) ArrayList(java.util.ArrayList) List(java.util.List) Executable(org.apache.felix.gogo.runtime.Parser.Executable)

Example 3 with Channel

use of java.nio.channels.Channel in project uavstack by uavorg.

the class UpgradeProcessLock method releaseFileLock.

public void releaseFileLock() {
    if (fileLock != null) {
        if (log.isTraceEnable()) {
            log.info(this, "Releasing the file lock of " + this.filePath.getFileName());
        }
        Channel fc = fileLock.acquiredBy();
        try {
            fileLock.release();
            fileLock = null;
            if (fc != null) {
                fc.close();
            }
        } catch (IOException e) {
        }
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) Channel(java.nio.channels.Channel) IOException(java.io.IOException)

Example 4 with Channel

use of java.nio.channels.Channel in project dataverse by IQSS.

the class StoredOriginalFile method retreive.

public static StorageIO<DataFile> retreive(StorageIO<DataFile> storageIO) {
    String originalMimeType;
    DataFile dataFile = storageIO.getDataFile();
    if (dataFile == null) {
        return null;
    }
    if (dataFile.getDataTable() != null) {
        originalMimeType = dataFile.getDataTable().getOriginalFileFormat();
    } else {
        return null;
    }
    long storedOriginalSize;
    InputStreamIO inputStreamIO;
    try {
        storageIO.open();
        Channel storedOriginalChannel = storageIO.openAuxChannel(SAVED_ORIGINAL_FILENAME_EXTENSION);
        storedOriginalSize = storageIO.getAuxObjectSize(SAVED_ORIGINAL_FILENAME_EXTENSION);
        inputStreamIO = new InputStreamIO(Channels.newInputStream((ReadableByteChannel) storedOriginalChannel), storedOriginalSize);
        logger.fine("Opened stored original file as Aux " + SAVED_ORIGINAL_FILENAME_EXTENSION);
    } catch (IOException ioEx) {
        // The original file not saved, or could not be opened.
        logger.fine("Failed to open stored original file as Aux " + SAVED_ORIGINAL_FILENAME_EXTENSION + "!");
        return null;
    }
    if (originalMimeType != null && !originalMimeType.isEmpty()) {
        if (originalMimeType.matches("application/x-dvn-.*-zip")) {
            inputStreamIO.setMimeType("application/zip");
        } else {
            inputStreamIO.setMimeType(originalMimeType);
        }
    } else {
        inputStreamIO.setMimeType("application/x-unknown");
    }
    String fileName = storageIO.getFileName();
    if (fileName != null) {
        if (originalMimeType != null) {
            String origFileExtension = generateOriginalExtension(originalMimeType);
            inputStreamIO.setFileName(fileName.replaceAll(".tab$", origFileExtension));
        } else {
            inputStreamIO.setFileName(fileName.replaceAll(".tab$", ""));
        }
    }
    return inputStreamIO;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) ReadableByteChannel(java.nio.channels.ReadableByteChannel) Channel(java.nio.channels.Channel) IOException(java.io.IOException)

Example 5 with Channel

use of java.nio.channels.Channel in project neo4j by neo4j.

the class LinuxNativeAccessTest method preallocate.

private void preallocate(Path file, long bytes) throws IOException, IllegalAccessException, ClassNotFoundException {
    try (Channel channel = FileChannel.open(file, READ, WRITE, CREATE)) {
        int descriptor = getDescriptor(channel);
        assertFalse(nativeAccess.tryPreallocateSpace(descriptor, bytes).isError());
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) Channel(java.nio.channels.Channel)

Aggregations

Channel (java.nio.channels.Channel)34 IOException (java.io.IOException)22 FileChannel (java.nio.channels.FileChannel)10 ReadableByteChannel (java.nio.channels.ReadableByteChannel)8 SocketChannel (java.nio.channels.SocketChannel)7 WritableByteChannel (java.nio.channels.WritableByteChannel)7 InputStream (java.io.InputStream)6 ServerSocketChannel (java.nio.channels.ServerSocketChannel)5 FileNotFoundException (java.io.FileNotFoundException)4 InetSocketAddress (java.net.InetSocketAddress)4 ArrayList (java.util.ArrayList)4 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)4 StreamSinkChannel (org.xnio.channels.StreamSinkChannel)4 DataFile (edu.harvard.iq.dataverse.DataFile)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 OutputStream (java.io.OutputStream)3 ByteBuffer (java.nio.ByteBuffer)3 List (java.util.List)3