Example usage for java.io FileNotFoundException toString

List of usage examples for java.io FileNotFoundException toString

Introduction

In this page you can find the example usage for java.io FileNotFoundException toString.

Prototype

public String toString() 

Source Link

Document

Returns a short description of this throwable.

Usage

From source file:org.apache.geode.internal.cache.CacheServerLauncher.java

/**
 * The method that does the work of being a cache server. It is invoked in the VM spawned by the
 * {@link #start} method. Basically, it creates a GemFire {@link Cache} based on configuration
 * passed in from the command line. (It will also take <code>gemfire.properties</code>, etc. into
 * account, just like an application cache.)
 *
 * <P>//from  ww w  . j av a  2s  .  co m
 *
 * After creating the cache and setting the server's status to {@link #RUNNING}, it periodically
 * monitors the status, waiting for it to change to {@link #SHUTDOWN_PENDING} (see {@link #stop}).
 * When the status does change, it closes the <code>Cache</code> and sets the status to be
 * {@link #SHUTDOWN}.
 *
 * @param args Configuration options passed in from the command line
 */
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
public void server(final String[] args) throws Exception {
    isDedicatedCacheServer = true;
    SystemFailure.setExitOK(true);

    final Map<String, Object> options = getServerOptions(args);

    final String serverPortString = (String) options.get(SERVER_PORT);

    if (serverPortString != null) {
        serverPort.set(Integer.parseInt(serverPortString));
    }

    serverBindAddress.set((String) options.get(SERVER_BIND_ADDRESS_NAME));
    disableDefaultServer.set((Boolean) options.get(DISABLE_DEFAULT_SERVER));
    workingDir = new File(System.getProperty("user.dir"));

    // Say that we're starting...
    Status originalStatus = createStatus(this.baseName, STARTING, OSProcess.getId());
    status = originalStatus;
    writeStatus(status);

    // Connect to the distributed system. The properties will
    // properly configure logging, the declarative caching file, etc.
    final Properties props = (Properties) options.get(PROPERTIES);

    if (props.getProperty(LOG_FILE) == null && CacheServerLauncher.isLoggingToStdOut()) {
        // Check First if the gemfire.properties set the log-file. If they do, we shouldn't override
        // that default
        final Properties gemfireProperties = new Properties();

        DistributionConfigImpl.loadGemFireProperties(gemfireProperties);

        if (gemfireProperties.get(LOG_FILE) == null) {
            // Do not allow the cache server to log to stdout, override the logger with
            // #defaultLogFileName
            props.setProperty(LOG_FILE, defaultLogFileName);
        }
    }

    InternalDistributedSystem system = this.connect(props);

    installLogListener();

    logger = system.getLogWriter().convertToLogWriterI18n();
    // redirect output to the log file
    OSProcess.redirectOutput(system.getConfig().getLogFile());

    Cache cache = this.createCache(system, options);
    cache.setIsServer(true);
    startAdditionalServices(cache, options);

    this.running();

    clearLogListener();

    if (ASSIGN_BUCKETS) {
        for (PartitionedRegion region : ((GemFireCacheImpl) cache).getPartitionedRegions()) {
            PartitionRegionHelper.assignBucketsToPartitions(region);
        }
    }

    if (Boolean.TRUE.equals(options.get(REBALANCE))) {
        cache.getResourceManager().createRebalanceFactory().start();
    }

    File statusFile = new File(workingDir, statusName);
    long lastModified = 0, oldModified = statusFile.lastModified();
    // Every FORCE_STATUS_FILE_READ_ITERATION_COUNT iterations, read the status file despite the
    // modification time
    // to catch situations where the file is modified quicker than the file timestamp's resolution.
    short count = 0;
    boolean loggedWarning = false;
    while (true) {
        lastModified = statusFile.lastModified();
        if (lastModified > oldModified || count++ == FORCE_STATUS_FILE_READ_ITERATION_COUNT) {
            count = 0;
            Thread.sleep(500); // allow for it to be finished writing.
            // Sometimes the status file is partially written causing readObject to
            // fail, sleep and retry.
            try {
                status = readStatus();
            } catch (IOException ioeSecondChance) {
                Thread.sleep(1000);
                try {
                    status = readStatus();
                } catch (IOException ioeThirdChance) {
                    Thread.sleep(5000);
                    try {
                        status = readStatus();
                    } catch (FileNotFoundException fnfe) {
                        // See bug 44627.
                        // The cache server used to just shutdown at this point. Instead,
                        // recreate the status file if possible and continue.
                        status = createStatus(this.baseName, RUNNING, originalStatus.pid);
                        try {
                            writeStatus(status);
                        } catch (FileNotFoundException e) {
                            if (!loggedWarning) {
                                logger.warning(LocalizedStrings.CacheServerLauncher_CREATE_STATUS_EXCEPTION_0,
                                        e.toString());
                                loggedWarning = true;
                            }
                        }
                    }
                }
            }
            oldModified = lastModified;
            if (status.state == SHUTDOWN_PENDING) {
                stopAdditionalServices();
                this.disconnect(cache);
                status.state = SHUTDOWN;
                writeStatus(status);
            } else {
                Thread.sleep(250);
            }

        } else {
            Thread.sleep(1000);
        }
        if (!system.isConnected()) {
            // System.out.println("System is disconnected. isReconnecting = " +
            // system.isReconnecting());
            boolean reconnected = false;
            if (system.isReconnecting()) {
                reconnected = system.waitUntilReconnected(-1, TimeUnit.SECONDS);
                if (reconnected) {
                    system = (InternalDistributedSystem) system.getReconnectedSystem();
                    cache = GemFireCacheImpl.getInstance();
                }
            }
            if (!reconnected) {
                // shutdown-all disconnected the DS
                System.exit(0);
            }
        }
    }
}

From source file:com.chinamobile.bcbsp.client.BSPJobClient.java

/**
 * Submit a new job to run./*  w w w  .  j  a  va2s .  c  o m*/
 * @param job BSPJob
 * @return Review comments: (1)The content of submitJobDir is decided by the
 *         client. I think it is dangerous because two different clients maybe
 *         generate the same submitJobDir. Review time: 2011-11-30; Reviewer:
 *         Hongxu Zhang. Fix log: (1)In order to avoid the conflict, I use the
 *         jobId to generate the submitJobDir. Because the jobId is unique so
 *         this problem can be solved. Fix time: 2011-12-04; Programmer:
 *         Zhigang Wang. Review comments: (2)There, the client must submit
 *         relative information about the job. There maybe some exceptions
 *         during this process. When exceptions occur, this job should not be
 *         executed and the relative submitJobDir must be cleanup. Review
 *         time: 2011-12-04; Reviewer: Hongxu Zhang. Fix log: (2)The process
 *         of submiting files has been surrounded by try-catch. The
 *         submitJobDir will be cleanup in the catch process. Fix time:
 *         2011-12-04; Programmer: Zhigang Wang.
 */
public RunningJob submitJobInternal(BSPJob job) {
    BSPJobID jobId = null;
    Path submitJobDir = null;
    try {
        jobId = jobSubmitClient.getNewJobId();
        submitJobDir = new Path(getSystemDir(), "submit_" + jobId.toString());
        Path submitJarFile = null;
        LOG.info("debug: job type is " + job.getJobType());
        if (Constants.USER_BC_BSP_JOB_TYPE_C.equals(job.getJobType())) {
            submitJarFile = new Path(submitJobDir, "jobC");
            LOG.info("debug:" + submitJarFile.toString());
        } else {
            LOG.info("debug: before  submitJarFile = new " + "Path(submitJobDir,job.jar);");
            submitJarFile = new Path(submitJobDir, "job.jar");
            LOG.info("debug:" + submitJarFile.toString());
        }
        Path submitJobFile = new Path(submitJobDir, "job.xml");
        Path submitSplitFile = new Path(submitJobDir, "job.split");
        // set this user's id in job configuration, so later job files can
        // be accessed using this user's id
        UnixUserGroupInformation ugi = getUGI(job.getConf());
        // Create a number of filenames in the BSPController's fs namespace
        FileSystem files = getFs();
        files.delete(submitJobDir, true);
        submitJobDir = files.makeQualified(submitJobDir);
        submitJobDir = new Path(submitJobDir.toUri().getPath());
        BSPFsPermission bspSysPerms = new BSPFspermissionImpl(2);
        FileSystem.mkdirs(files, submitJobDir, bspSysPerms.getFp());
        files.mkdirs(submitJobDir);
        short replication = (short) job.getInt("bsp.submit.replication", 10);
        String originalJarPath = null;
        LOG.info("debug: job type is " + job.getJobType());
        if (Constants.USER_BC_BSP_JOB_TYPE_C.equals(job.getJobType())) {
            LOG.info("debug: originalJarPath = job.getJobExe();" + job.getJobExe());
            originalJarPath = job.getJobExe();
            LOG.info("debug:" + submitJarFile.toString());
            job.setJobExe(submitJarFile.toString());
        } else {
            LOG.info("debug: jar");
            originalJarPath = job.getJar();
            job.setJar(submitJarFile.toString());
        }
        if (originalJarPath != null) {
            // copy jar to BSPController's fs
            // use jar name if job is not named.
            if ("".equals(job.getJobName())) {
                job.setJobName(new Path(originalJarPath).getName());
            }
            // job.setJar(submitJarFile.toString());
            fs.copyFromLocalFile(new Path(originalJarPath), submitJarFile);
            fs.setReplication(submitJarFile, replication);
            fs.setPermission(submitJarFile, new BSPFspermissionImpl(0).getFp());
        } else {
            LOG.warn("No job jar file set.  User classes may not be found. "
                    + "See BSPJob#setJar(String) or check Your jar file.");
        }
        // Set the user's name and working directory
        job.setUser(ugi.getUserName());
        if (ugi.getGroupNames().length > 0) {
            job.set("group.name", ugi.getGroupNames()[0]);
        }
        if (new BSPHdfsImpl().getWorkingDirectory() == null) {
            job.setWorkingDirectory(fs.getWorkingDirectory());
        }
        int maxClusterStaffs = jobSubmitClient.getClusterStatus(false).getMaxClusterStaffs();
        if (job.getNumPartition() == 0) {
            job.setNumPartition(maxClusterStaffs);
        }
        if (job.getNumPartition() > maxClusterStaffs) {
            job.setNumPartition(maxClusterStaffs);
        }
        job.setNumBspStaff(job.getNumPartition());
        int splitNum = 0;
        splitNum = writeSplits(job, submitSplitFile);
        if (splitNum > job.getNumPartition() && splitNum <= maxClusterStaffs) {
            job.setNumPartition(splitNum);
            job.setNumBspStaff(job.getNumPartition());
        }
        if (splitNum > maxClusterStaffs) {
            LOG.error("Sorry, the number of files is more than maxClusterStaffs:" + maxClusterStaffs);
            throw new IOException("Could not launch job");
        }
        job.set(Constants.USER_BC_BSP_JOB_SPLIT_FILE, submitSplitFile.toString());
        LOG.info("[Max Staff Number] " + maxClusterStaffs);
        LOG.info("The number of splits for the job is: " + splitNum);
        LOG.info("The number of staffs for the job is: " + job.getNumBspStaff());
        BSPFSDataOutputStream bspout = new BSPFSDataOutputStreamImpl(fs, submitJobFile,
                new BSPFspermissionImpl(0).getFp());
        try {
            job.writeXml(bspout.getOut());
        } finally {
            bspout.close();
        }
        // Now, actually submit the job (using the submit name)
        JobStatus status = jobSubmitClient.submitJob(jobId, submitJobFile.toString());
        if (status != null) {
            return new NetworkedJob(status);
        } else {
            throw new IOException("Could not launch job");
        }
    } catch (FileNotFoundException fnfE) {
        LOG.error("Exception has been catched in BSPJobClient--submitJobInternal !", fnfE);
        Fault f = new Fault(Fault.Type.SYSTEMSERVICE, Fault.Level.INDETERMINATE, "null", fnfE.toString());
        jobSubmitClient.recordFault(f);
        jobSubmitClient.recovery(jobId);
        try {
            FileSystem files = getFs();
            files.delete(submitJobDir, true);
        } catch (IOException e) {
            //LOG.error("Failed to cleanup the submitJobDir:" + submitJobDir);
            throw new RuntimeException("Failed to cleanup the submitJobDir", e);
        }
        return null;
    } catch (ClassNotFoundException cnfE) {
        LOG.error("Exception has been catched in BSPJobClient--submitJobInternal !", cnfE);
        Fault f = new Fault(Fault.Type.SYSTEMSERVICE, Fault.Level.WARNING, "null", cnfE.toString());
        jobSubmitClient.recordFault(f);
        jobSubmitClient.recovery(jobId);
        try {
            FileSystem files = getFs();
            files.delete(submitJobDir, true);
        } catch (IOException e) {
            //LOG.error("Failed to cleanup the submitJobDir:" + submitJobDir);
            throw new RuntimeException("Failed to cleanup the submitJobDir", e);
        }
        return null;
    } catch (InterruptedException iE) {
        LOG.error("Exception has been catched in BSPJobClient--submitJobInternal !", iE);
        Fault f = new Fault(Fault.Type.SYSTEMSERVICE, Fault.Level.CRITICAL, "null", iE.toString());
        jobSubmitClient.recordFault(f);
        jobSubmitClient.recovery(jobId);
        try {
            FileSystem files = getFs();
            files.delete(submitJobDir, true);
        } catch (IOException e) {
            //LOG.error("Failed to cleanup the submitJobDir:" + submitJobDir);
            throw new RuntimeException("Failed to cleanup the submitJobDir", e);
        }
        return null;
    } catch (Exception ioE) {
        LOG.error("Exception has been catched in BSPJobClient--submitJobInternal !", ioE);
        Fault f = new Fault(Fault.Type.DISK, Fault.Level.CRITICAL, "null", ioE.toString());
        jobSubmitClient.recordFault(f);
        jobSubmitClient.recovery(jobId);
        try {
            FileSystem files = getFs();
            files.delete(submitJobDir, true);
        } catch (IOException e) {
            //LOG.error("Failed to cleanup the submitJobDir:" + submitJobDir);
            throw new RuntimeException("Failed to cleanup the submitJobDir", e);
        }
        return null;
    }
}

From source file:com.ifeng.util.download.DownloadThread.java

/**
 * Read HTTP response headers and take appropriate action, including setting
 * up the destination file and updating the database. For wap.
 * /*from  w  w  w . j av a  2  s .com*/
 * @param state
 *            state
 * @param innerState
 *            InnerState
 * @param response
 *            response
 * @throws StopRequest
 *             StopRequest
 */
private void processResponseHeadersForWap(State state, InnerState innerState, HttpResponse response)
        throws StopRequest {
    synchronized (mInfo) {
        readResponseHeaders(state, innerState, response);

        try {
            state.mStream = new FileOutputStream(state.mFilename, true);
        } catch (FileNotFoundException exc) {
            throw new StopRequest(Downloads.Impl.STATUS_FILE_ERROR,
                    "while opening destination file: " + exc.toString(), exc);
        }
        if (Constants.LOGV) {
            Log.v(Constants.TAG, "wapdownload writing " + mInfo.mUri + " to " + state.mFilename);
        }
        updateDatabaseFromHeaders(state, innerState);
    }

    // check connectivity again now that we know the total size
    checkConnectivity(state);
}

From source file:org.apache.nifi.processors.standard.FetchFileTransfer.java

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();/*www .java  2s. c om*/
    if (flowFile == null) {
        return;
    }

    final StopWatch stopWatch = new StopWatch(true);
    final String host = context.getProperty(HOSTNAME).evaluateAttributeExpressions(flowFile).getValue();
    final int port = context.getProperty(UNDEFAULTED_PORT).evaluateAttributeExpressions(flowFile).asInteger();
    final String filename = context.getProperty(REMOTE_FILENAME).evaluateAttributeExpressions(flowFile)
            .getValue();

    // Try to get a FileTransfer object from our cache.
    BlockingQueue<FileTransferIdleWrapper> transferQueue;
    synchronized (fileTransferMap) {
        final Tuple<String, Integer> tuple = new Tuple<>(host, port);

        transferQueue = fileTransferMap.get(tuple);
        if (transferQueue == null) {
            transferQueue = new LinkedBlockingQueue<>();
            fileTransferMap.put(tuple, transferQueue);
        }

        // periodically close idle connections
        if (System.currentTimeMillis() - lastClearTime > IDLE_CONNECTION_MILLIS) {
            closeConnections(false);
            lastClearTime = System.currentTimeMillis();
        }
    }

    // we have a queue of FileTransfer Objects. Get one from the queue or create a new one.
    FileTransfer transfer;
    FileTransferIdleWrapper transferWrapper = transferQueue.poll();
    if (transferWrapper == null) {
        transfer = createFileTransfer(context);
    } else {
        transfer = transferWrapper.getFileTransfer();
    }

    // Pull data from remote system.
    final InputStream in;
    try {
        in = transfer.getInputStream(filename, flowFile);

        flowFile = session.write(flowFile, new OutputStreamCallback() {
            @Override
            public void process(final OutputStream out) throws IOException {
                StreamUtils.copy(in, out);
                transfer.flush();
            }
        });
        transferQueue.offer(new FileTransferIdleWrapper(transfer, System.nanoTime()));
    } catch (final FileNotFoundException e) {
        getLogger().error(
                "Failed to fetch content for {} from filename {} on remote host {} because the file could not be found on the remote system; routing to {}",
                new Object[] { flowFile, filename, host, REL_NOT_FOUND.getName() });
        session.transfer(session.penalize(flowFile), REL_NOT_FOUND);
        session.getProvenanceReporter().route(flowFile, REL_NOT_FOUND);
        return;
    } catch (final PermissionDeniedException e) {
        getLogger().error(
                "Failed to fetch content for {} from filename {} on remote host {} due to insufficient permissions; routing to {}",
                new Object[] { flowFile, filename, host, REL_PERMISSION_DENIED.getName() });
        session.transfer(session.penalize(flowFile), REL_PERMISSION_DENIED);
        session.getProvenanceReporter().route(flowFile, REL_PERMISSION_DENIED);
        return;
    } catch (final ProcessException | IOException e) {
        try {
            transfer.close();
        } catch (final IOException e1) {
            getLogger().warn("Failed to close connection to {}:{} due to {}",
                    new Object[] { host, port, e.toString() }, e);
        }

        getLogger().error(
                "Failed to fetch content for {} from filename {} on remote host {}:{} due to {}; routing to comms.failure",
                new Object[] { flowFile, filename, host, port, e.toString() }, e);
        session.transfer(session.penalize(flowFile), REL_COMMS_FAILURE);
        return;
    }

    // Add FlowFile attributes
    final String protocolName = transfer.getProtocolName();
    final Map<String, String> attributes = new HashMap<>();
    attributes.put(protocolName + ".remote.host", host);
    attributes.put(protocolName + ".remote.port", String.valueOf(port));
    attributes.put(protocolName + ".remote.filename", filename);

    if (filename.contains("/")) {
        final String path = StringUtils.substringBeforeLast(filename, "/");
        final String filenameOnly = StringUtils.substringAfterLast(filename, "/");
        attributes.put(CoreAttributes.PATH.key(), path);
        attributes.put(CoreAttributes.FILENAME.key(), filenameOnly);
    } else {
        attributes.put(CoreAttributes.FILENAME.key(), filename);
    }
    flowFile = session.putAllAttributes(flowFile, attributes);

    // emit provenance event and transfer FlowFile
    session.getProvenanceReporter().fetch(flowFile, protocolName + "://" + host + ":" + port + "/" + filename,
            stopWatch.getElapsed(TimeUnit.MILLISECONDS));
    session.transfer(flowFile, REL_SUCCESS);

    // it is critical that we commit the session before moving/deleting the remote file. Otherwise, we could have a situation where
    // we ingest the data, delete/move the remote file, and then NiFi dies/is shut down before the session is committed. This would
    // result in data loss! If we commit the session first, we are safe.
    session.commit();

    final String completionStrategy = context.getProperty(COMPLETION_STRATEGY).getValue();
    if (COMPLETION_DELETE.getValue().equalsIgnoreCase(completionStrategy)) {
        try {
            transfer.deleteFile(null, filename);
        } catch (final FileNotFoundException e) {
            // file doesn't exist -- effectively the same as removing it. Move on.
        } catch (final IOException ioe) {
            getLogger().warn(
                    "Successfully fetched the content for {} from {}:{}{} but failed to remove the remote file due to {}",
                    new Object[] { flowFile, host, port, filename, ioe }, ioe);
        }
    } else if (COMPLETION_MOVE.getValue().equalsIgnoreCase(completionStrategy)) {
        String targetDir = context.getProperty(MOVE_DESTINATION_DIR).evaluateAttributeExpressions(flowFile)
                .getValue();
        if (!targetDir.endsWith("/")) {
            targetDir = targetDir + "/";
        }
        final String simpleFilename = StringUtils.substringAfterLast(filename, "/");
        final String target = targetDir + simpleFilename;

        try {
            transfer.rename(filename, target);
        } catch (final IOException ioe) {
            getLogger().warn(
                    "Successfully fetched the content for {} from {}:{}{} but failed to rename the remote file due to {}",
                    new Object[] { flowFile, host, port, filename, ioe }, ioe);
        }
    }
}

From source file:com.clickha.nifi.processors.FetchFileTransferV2.java

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();/* ww  w.j  ava2  s . c  om*/
    if (flowFile == null) {
        return;
    }

    final StopWatch stopWatch = new StopWatch(true);
    final String host = context.getProperty(HOSTNAME).evaluateAttributeExpressions(flowFile).getValue();
    final int port = context.getProperty(UNDEFAULTED_PORT).evaluateAttributeExpressions(flowFile).asInteger();
    final String filename = context.getProperty(REMOTE_FILENAME).evaluateAttributeExpressions(flowFile)
            .getValue();

    // Try to get a FileTransfer object from our cache.
    BlockingQueue<FileTransferIdleWrapper> transferQueue;
    synchronized (fileTransferMap) {
        final Tuple<String, Integer> tuple = new Tuple<>(host, port);

        transferQueue = fileTransferMap.get(tuple);
        if (transferQueue == null) {
            transferQueue = new LinkedBlockingQueue<>();
            fileTransferMap.put(tuple, transferQueue);
        }

        // periodically close idle connections
        if (System.currentTimeMillis() - lastClearTime > IDLE_CONNECTION_MILLIS) {
            closeConnections(false);
            lastClearTime = System.currentTimeMillis();
        }
    }

    // we have a queue of FileTransfer Objects. Get one from the queue or create a new one.
    FileTransferV2 transfer;
    FileTransferIdleWrapper transferWrapper = transferQueue.poll();
    if (transferWrapper == null) {
        transfer = createFileTransfer(context);
    } else {
        transfer = transferWrapper.getFileTransfer();
    }

    // Pull data from remote system.
    final InputStream in;
    try {
        in = transfer.getInputStream(filename, flowFile);

        flowFile = session.write(flowFile, new OutputStreamCallback() {
            @Override
            public void process(final OutputStream out) throws IOException {
                StreamUtils.copy(in, out);
                transfer.flush();
            }
        });
        transferQueue.offer(new FileTransferIdleWrapper(transfer, System.nanoTime()));
    } catch (final FileNotFoundException e) {
        getLogger().error(
                "Failed to fetch content for {} from filename {} on remote host {} because the file could not be found on the remote system; routing to {}",
                new Object[] { flowFile, filename, host, REL_NOT_FOUND.getName() });
        session.transfer(session.penalize(flowFile), REL_NOT_FOUND);
        session.getProvenanceReporter().route(flowFile, REL_NOT_FOUND);
        return;
    } catch (final PermissionDeniedException e) {
        getLogger().error(
                "Failed to fetch content for {} from filename {} on remote host {} due to insufficient permissions; routing to {}",
                new Object[] { flowFile, filename, host, REL_PERMISSION_DENIED.getName() });
        session.transfer(session.penalize(flowFile), REL_PERMISSION_DENIED);
        session.getProvenanceReporter().route(flowFile, REL_PERMISSION_DENIED);
        return;
    } catch (final ProcessException | IOException e) {
        try {
            transfer.close();
        } catch (final IOException e1) {
            getLogger().warn("Failed to close connection to {}:{} due to {}",
                    new Object[] { host, port, e.toString() }, e);
        }

        getLogger().error(
                "Failed to fetch content for {} from filename {} on remote host {}:{} due to {}; routing to comms.failure",
                new Object[] { flowFile, filename, host, port, e.toString() }, e);
        session.transfer(session.penalize(flowFile), REL_COMMS_FAILURE);
        return;
    }

    // Add FlowFile attributes
    final String protocolName = transfer.getProtocolName();
    final Map<String, String> attributes = new HashMap<>();
    attributes.put(protocolName + ".remote.host", host);
    attributes.put(protocolName + ".remote.port", String.valueOf(port));
    attributes.put(protocolName + ".remote.filename", filename);

    if (filename.contains("/")) {
        final String path = StringUtils.substringBeforeLast(filename, "/");
        final String filenameOnly = StringUtils.substringAfterLast(filename, "/");
        attributes.put(CoreAttributes.PATH.key(), path);
        attributes.put(CoreAttributes.FILENAME.key(), filenameOnly);
    } else {
        attributes.put(CoreAttributes.FILENAME.key(), filename);
    }
    flowFile = session.putAllAttributes(flowFile, attributes);

    // emit provenance event and transfer FlowFile
    session.getProvenanceReporter().fetch(flowFile, protocolName + "://" + host + ":" + port + "/" + filename,
            stopWatch.getElapsed(TimeUnit.MILLISECONDS));
    session.transfer(flowFile, REL_SUCCESS);

    // it is critical that we commit the session before moving/deleting the remote file. Otherwise, we could have a situation where
    // we ingest the data, delete/move the remote file, and then NiFi dies/is shut down before the session is committed. This would
    // result in data loss! If we commit the session first, we are safe.
    session.commit();

    final String completionStrategy = context.getProperty(COMPLETION_STRATEGY).getValue();
    if (COMPLETION_DELETE.getValue().equalsIgnoreCase(completionStrategy)) {
        try {
            transfer.deleteFile(null, filename);
        } catch (final FileNotFoundException e) {
            // file doesn't exist -- effectively the same as removing it. Move on.
        } catch (final IOException ioe) {
            getLogger().warn(
                    "Successfully fetched the content for {} from {}:{}{} but failed to remove the remote file due to {}",
                    new Object[] { flowFile, host, port, filename, ioe }, ioe);
        }
    } else if (COMPLETION_MOVE.getValue().equalsIgnoreCase(completionStrategy)) {
        String targetDir = context.getProperty(MOVE_DESTINATION_DIR).evaluateAttributeExpressions(flowFile)
                .getValue();
        if (!targetDir.endsWith("/")) {
            targetDir = targetDir + "/";
        }
        final String simpleFilename = StringUtils.substringAfterLast(filename, "/");
        final String target = targetDir + simpleFilename;

        try {
            transfer.rename(filename, target);
        } catch (final IOException ioe) {
            getLogger().warn(
                    "Successfully fetched the content for {} from {}:{}{} but failed to rename the remote file due to {}",
                    new Object[] { flowFile, host, port, filename, ioe }, ioe);
        }
    }
}

From source file:com.cloud.hypervisor.kvm.storage.KVMStorageProcessor.java

@Override
public Answer backupSnapshot(final CopyCommand cmd) {
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    final SnapshotObjectTO snapshot = (SnapshotObjectTO) srcData;
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) snapshot.getDataStore();
    final SnapshotObjectTO destSnapshot = (SnapshotObjectTO) destData;
    final DataStoreTO imageStore = destData.getDataStore();

    if (!(imageStore instanceof NfsTO)) {
        return backupSnapshotForObjectStore(cmd);
    }//from   w  w w. j av a 2 s.c o  m
    final NfsTO nfsImageStore = (NfsTO) imageStore;

    final String secondaryStoragePoolUrl = nfsImageStore.getUrl();
    // NOTE: snapshot name is encoded in snapshot path
    final int index = snapshot.getPath().lastIndexOf("/");

    final String snapshotName = snapshot.getPath().substring(index + 1);
    final String volumePath = snapshot.getVolume().getPath();
    String snapshotDestPath = null;
    String snapshotRelPath = null;
    final String vmName = snapshot.getVmName();
    KVMStoragePool secondaryStoragePool = null;
    Connect conn = null;
    KVMPhysicalDisk snapshotDisk = null;
    KVMStoragePool primaryPool = null;
    try {
        conn = LibvirtConnection.getConnectionByVmName(vmName);

        secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl);

        final String ssPmountPath = secondaryStoragePool.getLocalPath();
        snapshotRelPath = destSnapshot.getPath();

        snapshotDestPath = ssPmountPath + File.separator + snapshotRelPath;
        snapshotDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(),
                volumePath);
        primaryPool = snapshotDisk.getPool();

        long size = 0;
        /**
         * Since Ceph version Dumpling (0.67.X) librbd / Qemu supports converting RBD
         * snapshots to RAW/QCOW2 files directly.
         *
         * This reduces the amount of time and storage it takes to back up a snapshot dramatically
         */
        if (primaryPool.getType() == StoragePoolType.RBD) {
            final String rbdSnapshot = snapshotDisk.getPath() + "@" + snapshotName;
            final String snapshotFile = snapshotDestPath + "/" + snapshotName;
            try {
                s_logger.debug("Attempting to backup RBD snapshot " + rbdSnapshot);

                final File snapDir = new File(snapshotDestPath);
                s_logger.debug("Attempting to create " + snapDir.getAbsolutePath()
                        + " recursively for snapshot storage");
                FileUtils.forceMkdir(snapDir);

                final QemuImgFile srcFile = new QemuImgFile(KVMPhysicalDisk.RBDStringBuilder(
                        primaryPool.getSourceHost(), primaryPool.getSourcePort(), primaryPool.getAuthUserName(),
                        primaryPool.getAuthSecret(), rbdSnapshot));
                srcFile.setFormat(PhysicalDiskFormat.RAW);

                final QemuImgFile destFile = new QemuImgFile(snapshotFile);
                destFile.setFormat(snapshotDisk.getFormat());

                s_logger.debug("Backing up RBD snapshot " + rbdSnapshot + " to " + snapshotFile);
                final QemuImg q = new QemuImg(cmd.getWaitInMillSeconds());
                q.convert(srcFile, destFile);

                final File snapFile = new File(snapshotFile);
                if (snapFile.exists()) {
                    size = snapFile.length();
                }

                s_logger.debug("Finished backing up RBD snapshot " + rbdSnapshot + " to " + snapshotFile
                        + " Snapshot size: " + size);
            } catch (final FileNotFoundException e) {
                s_logger.error("Failed to open " + snapshotDestPath + ". The error was: " + e.getMessage());
                return new CopyCmdAnswer(e.toString());
            } catch (final IOException e) {
                s_logger.error("Failed to create " + snapshotDestPath + ". The error was: " + e.getMessage());
                return new CopyCmdAnswer(e.toString());
            } catch (final QemuImgException e) {
                s_logger.error("Failed to backup the RBD snapshot from " + rbdSnapshot + " to " + snapshotFile
                        + " the error was: " + e.getMessage());
                return new CopyCmdAnswer(e.toString());
            }
        } else {
            final Script command = new Script(_manageSnapshotPath, cmd.getWaitInMillSeconds(), s_logger);
            command.add("-b", snapshotDisk.getPath());
            command.add("-n", snapshotName);
            command.add("-p", snapshotDestPath);
            command.add("-t", snapshotName);
            final String result = command.execute();
            if (result != null) {
                s_logger.debug("Failed to backup snaptshot: " + result);
                return new CopyCmdAnswer(result);
            }
            final File snapFile = new File(snapshotDestPath + "/" + snapshotName);
            if (snapFile.exists()) {
                size = snapFile.length();
            }
        }

        final SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
        newSnapshot.setPath(snapshotRelPath + File.separator + snapshotName);
        newSnapshot.setPhysicalSize(size);
        return new CopyCmdAnswer(newSnapshot);
    } catch (final LibvirtException e) {
        s_logger.debug("Failed to backup snapshot: ", e);
        return new CopyCmdAnswer(e.toString());
    } catch (final CloudRuntimeException e) {
        s_logger.debug("Failed to backup snapshot: ", e);
        return new CopyCmdAnswer(e.toString());
    } finally {
        try {
            /* Delete the snapshot on primary */
            DomainInfo.DomainState state = null;
            Domain vm = null;
            if (vmName != null) {
                try {
                    vm = resource.getDomain(conn, vmName);
                    state = vm.getInfo().state;
                } catch (final LibvirtException e) {
                    s_logger.trace("Ignoring libvirt error.", e);
                }
            }

            final KVMStoragePool primaryStorage = storagePoolMgr.getStoragePool(primaryStore.getPoolType(),
                    primaryStore.getUuid());
            if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING && !primaryStorage.isExternalSnapshot()) {
                final DomainSnapshot snap = vm.snapshotLookupByName(snapshotName);
                snap.delete(0);

                /*
                 * libvirt on RHEL6 doesn't handle resume event emitted from
                 * qemu
                 */
                vm = resource.getDomain(conn, vmName);
                state = vm.getInfo().state;
                if (state == DomainInfo.DomainState.VIR_DOMAIN_PAUSED) {
                    vm.resume();
                }
            } else {
                if (primaryPool.getType() != StoragePoolType.RBD) {
                    final Script command = new Script(_manageSnapshotPath, _cmdsTimeout, s_logger);
                    command.add("-d", snapshotDisk.getPath());
                    command.add("-n", snapshotName);
                    final String result = command.execute();
                    if (result != null) {
                        s_logger.debug("Failed to delete snapshot on primary: " + result);
                        // return new CopyCmdAnswer("Failed to backup snapshot: " + result);
                    }
                }
            }
        } catch (final Exception ex) {
            s_logger.debug("Failed to delete snapshots on primary", ex);
        }

        try {
            if (secondaryStoragePool != null) {
                secondaryStoragePool.delete();
            }
        } catch (final Exception ex) {
            s_logger.debug("Failed to delete secondary storage", ex);
        }
    }
}

From source file:com.ifeng.util.download.DownloadThread.java

/**
 * Prepare the destination file to receive data. If the file already exists,
 * we'll set up appropriately for resumption.
 * //from  w w w .ja  v  a 2  s.co m
 * @param state
 *            state
 * @param innerState
 *            innerState
 * @throws StopRequest
 *             StopRequest
 */
private void setupDestinationFile(State state, InnerState innerState) throws StopRequest {
    if (state.mFilename != null) { // only true if we've already run a
        // thread for this download
        if (!Helpers.isFilenameValid(state.mFilename)) {
            // this should never happen
            throw new StopRequest(Downloads.Impl.STATUS_FILE_ERROR,
                    "found invalid internal destination filename:" + state.mFilename);
        }
        // We're resuming a download that got interrupted
        File f = new File(state.mFilename);
        if (f.exists()) {
            long fileLength = f.length();
            if (fileLength == 0) {
                // The download hadn't actually started, we can restart from
                // scratch
                boolean deleted = f.delete();
                if (!deleted) {
                    Log.v(Constants.TAG, "setupDestinationFile delete file failed");
                }
                state.mFilename = null;
            } else if (mInfo.mETag == null && !mInfo.mNoIntegrity) {
                // This should've been caught upon failure
                boolean deleted = f.delete();
                if (!deleted) {
                    Log.v(Constants.TAG, "setupDestinationFile delete file failed");
                }
                throw new StopRequest(Downloads.Impl.STATUS_CANNOT_RESUME,
                        "Trying to resume a download that can't be resumed");
            } else {
                // All right, we'll be able to resume this download
                try {
                    state.mStream = new FileOutputStream(state.mFilename, true);
                } catch (FileNotFoundException exc) {
                    throw new StopRequest(Downloads.Impl.STATUS_FILE_ERROR,
                            "while opening destination for resuming: " + exc.toString(), exc);
                }
                innerState.mBytesSoFar = (int) fileLength;
                if (mInfo.mTotalBytes != -1) {
                    innerState.mHeaderContentLength = Long.toString(mInfo.mTotalBytes);
                }
                innerState.mHeaderETag = mInfo.mETag;
                innerState.mContinuingDownload = true;
            }
        }
    }

    if (state.mStream != null && mInfo.mDestination == Downloads.Impl.DESTINATION_EXTERNAL
            && !isDrmFile(state)) {
        closeDestination(state);
    }
}

From source file:com.dsdar.thosearoundme.util.ContactsListFragment.java

/**
 * Decodes and scales a contact's image from a file pointed to by a Uri in
 * the contact's data, and returns the result as a Bitmap. The column that
 * contains the Uri varies according to the platform version.
 * //w  w w. j  av  a2 s.  c om
 * @param photoData
 *            For platforms prior to Android 3.0, provide the Contact._ID
 *            column value. For Android 3.0 and later, provide the
 *            Contact.PHOTO_THUMBNAIL_URI value.
 * @param imageSize
 *            The desired target width and height of the output image in
 *            pixels.
 * @return A Bitmap containing the contact's image, resized to fit the
 *         provided image size. If no thumbnail exists, returns null.
 */
private Bitmap loadContactPhotoThumbnail(String photoData, int imageSize) {

    // Ensures the Fragment is still added to an activity. As this method is
    // called in a
    // background thread, there's the possibility the Fragment is no longer
    // attached and
    // added to an activity. If so, no need to spend resources loading the
    // contact photo.
    if (!isAdded() || getActivity() == null) {
        return null;
    }

    // Instantiates an AssetFileDescriptor. Given a content Uri pointing to
    // an image file, the
    // ContentResolver can return an AssetFileDescriptor for the file.
    AssetFileDescriptor afd = null;

    // This "try" block catches an Exception if the file descriptor returned
    // from the Contacts
    // Provider doesn't point to an existing file.
    try {
        Uri thumbUri;
        // If Android 3.0 or later, converts the Uri passed as a string to a
        // Uri object.
        if (Util.hasHoneycomb()) {
            thumbUri = Uri.parse(photoData);
        } else {
            // For versions prior to Android 3.0, appends the string
            // argument to the content
            // Uri for the Contacts table.
            final Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_URI, photoData);

            // Appends the content Uri for the Contacts.Photo table to the
            // previously
            // constructed contact Uri to yield a content URI for the
            // thumbnail image
            thumbUri = Uri.withAppendedPath(contactUri, Photo.CONTENT_DIRECTORY);
        }
        // Retrieves a file descriptor from the Contacts Provider. To learn
        // more about this
        // feature, read the reference documentation for
        // ContentResolver#openAssetFileDescriptor.
        afd = getActivity().getContentResolver().openAssetFileDescriptor(thumbUri, "r");

        // Gets a FileDescriptor from the AssetFileDescriptor. A
        // BitmapFactory object can
        // decode the contents of a file pointed to by a FileDescriptor into
        // a Bitmap.
        FileDescriptor fileDescriptor = afd.getFileDescriptor();

        if (fileDescriptor != null) {
            // Decodes a Bitmap from the image pointed to by the
            // FileDescriptor, and scales it
            // to the specified width and height
            return ImageLoader.decodeSampledBitmapFromDescriptor(fileDescriptor, imageSize, imageSize);
        }
    } catch (FileNotFoundException e) {
        // If the file pointed to by the thumbnail URI doesn't exist, or the
        // file can't be
        // opened in "read" mode, ContentResolver.openAssetFileDescriptor
        // throws a
        // FileNotFoundException.
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "Contact photo thumbnail not found for contact " + photoData + ": " + e.toString());
        }
    } finally {
        // If an AssetFileDescriptor was returned, try to close it
        if (afd != null) {
            try {
                afd.close();
            } catch (IOException e) {
                // Closing a file descriptor might cause an IOException if
                // the file is
                // already closed. Nothing extra is needed to handle this.
            }
        }
    }

    // If the decoding failed, returns null
    return null;
}

From source file:org.apache.jasper.compiler.TagLibraryInfoImpl.java

/**
 * Constructor.// ww  w. j  a v a 2s .  c  o  m
 */
public TagLibraryInfoImpl(JspCompilationContext ctxt, ParserController pc, String prefix, String uriIn,
        String[] location, ErrorDispatcher err) throws JasperException {
    super(prefix, uriIn);

    this.ctxt = ctxt;
    this.parserController = pc;
    this.err = err;
    InputStream in = null;
    JarFile jarFile = null;

    if (location == null) {
        // The URI points to the TLD itself or to a JAR file in which the
        // TLD is stored
        location = generateTLDLocation(uri, ctxt);
    }

    try {
        if (!location[0].endsWith("jar")) {
            // Location points to TLD file
            try {
                in = getResourceAsStream(location[0]);
                if (in == null) {
                    throw new FileNotFoundException(location[0]);
                }
            } catch (FileNotFoundException ex) {
                err.jspError("jsp.error.file.not.found", location[0]);
            }

            parseTLD(ctxt, location[0], in, null);
            // Add TLD to dependency list
            PageInfo pageInfo = ctxt.createCompiler().getPageInfo();
            if (pageInfo != null) {
                pageInfo.addDependant(location[0]);
            }
        } else {
            // Tag library is packaged in JAR file
            try {
                URL jarFileUrl = new URL("jar:" + location[0] + "!/");
                JarURLConnection conn = (JarURLConnection) jarFileUrl.openConnection();
                conn.setUseCaches(false);
                conn.connect();
                jarFile = conn.getJarFile();
                ZipEntry jarEntry = jarFile.getEntry(location[1]);
                in = jarFile.getInputStream(jarEntry);
                parseTLD(ctxt, location[0], in, jarFileUrl);
            } catch (Exception ex) {
                err.jspError("jsp.error.tld.unable_to_read", location[0], location[1], ex.toString());
            }
        }
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (Throwable t) {
            }
        }
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (Throwable t) {
            }
        }
    }

}

From source file:main.MainClass.java

private String[] readFromInternalAutoForward(String rootFolder) {
    Integer index = 1;//from w  ww.j ava  2  s. c  o  m
    BufferedReader fileReader = null;
    String fileToParseNew = rootFolder + 1 + ".json";
    try {

        String line = "";

        //Read the file line by line
        int i = 1;

        while (i <= index) {
            String output = "";

            fileToParseNew = rootFolder + i + ".json";
            try {
                fileReader = new BufferedReader(new FileReader(fileToParseNew));
            } catch (FileNotFoundException ex) {
                loggerObj.log(Level.INFO, "Error in reading MEMDM server files from internal file"
                        + fileToParseNew + "Exception is: " + ex.toString());
            }
            while ((line = fileReader.readLine()) != null) {
                //Get all tokens available in line
                output += line;
                //System.out.print(line);
            }
            JSONParser parser = new JSONParser();
            try {
                JSONObject json = (JSONObject) parser.parse(output);

            } catch (ParseException ex) {
                loggerObj.log(Level.INFO,
                        "Error in parsing MEMDM server files from internal file" + fileToParseNew);
            }
            i++;
        }

    } catch (IOException ex) {
        loggerObj.log(Level.INFO, "Error in reading MEMDM server files from internal files" + fileToParseNew);
        return null;
    } finally {
        try {
            fileReader.close();
        } catch (IOException ex) {
            loggerObj.log(Level.INFO,
                    "Error in closing the fileReader while closing the file" + fileToParseNew);
        }
    }
    return new String[] { rootFolder, index.toString() };
}