Example usage for org.apache.commons.io FileUtils moveDirectory

List of usage examples for org.apache.commons.io FileUtils moveDirectory

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils moveDirectory.

Prototype

public static void moveDirectory(File srcDir, File destDir) throws IOException 

Source Link

Document

Moves a directory.

Usage

From source file:com.linkedin.pinot.server.starter.helix.SegmentFetcherAndLoader.java

private String downloadSegmentToLocal(String uri, String tableName, String segmentId) throws Exception {
    File tempSegmentFile = null;// ww  w . j  a  v a 2  s . c  o  m
    File tempFile = null;
    try {
        tempSegmentFile = new File(_dataManager.getSegmentFileDirectory() + "/" + tableName + "/temp_"
                + segmentId + "_" + System.currentTimeMillis());
        tempFile = new File(_dataManager.getSegmentFileDirectory(), segmentId + ".tar.gz");
        SegmentFetcherFactory.getSegmentFetcherBasedOnURI(uri).fetchSegmentToLocal(uri, tempFile);
        LOGGER.info("Downloaded file from {} to {}; Length of downloaded file: {}; segmentName: {}; table: {}",
                uri, tempFile, tempFile.length(), segmentId, tableName);
        LOGGER.info("Trying to uncompress segment tar file from {} to {} for table {}", tempFile,
                tempSegmentFile, tableName);

        TarGzCompressionUtils.unTar(tempFile, tempSegmentFile);
        FileUtils.deleteQuietly(tempFile);
        final File segmentDir = new File(new File(_dataManager.getSegmentDataDirectory(), tableName),
                segmentId);
        Thread.sleep(1000);
        if (segmentDir.exists()) {
            LOGGER.info("Deleting the directory {} and recreating it again table {} ",
                    segmentDir.getAbsolutePath(), tableName);
            FileUtils.deleteDirectory(segmentDir);
        }
        LOGGER.info("Move the dir - " + tempSegmentFile.listFiles()[0] + " to " + segmentDir.getAbsolutePath()
                + " for " + segmentId + " of table " + tableName);
        FileUtils.moveDirectory(tempSegmentFile.listFiles()[0], segmentDir);
        FileUtils.deleteDirectory(tempSegmentFile);
        Thread.sleep(1000);
        LOGGER.info("Was able to succesfully rename the dir to match the segment {} for table {}", segmentId,
                tableName);

        new File(segmentDir, "finishedLoading").createNewFile();
        return segmentDir.getAbsolutePath();
    } catch (Exception e) {
        FileUtils.deleteQuietly(tempSegmentFile);
        FileUtils.deleteQuietly(tempFile);
        LOGGER.error("Caught exception downloading segment {} for table {}", segmentId, tableName, e);
        Utils.rethrowException(e);
        throw new AssertionError("Should not reach this");
    }
}

From source file:com.searchcode.app.service.JobService.java

public void moveDirectoryToTrash(String troublesome) throws IOException {
    Date date = new Date();
    DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    String newLocation = this.TRASHLOCATION + "/" + dateFormat.format(date);
    FileUtils.moveDirectory(new File(troublesome), new File(newLocation));
}

From source file:hd3gtv.mydmam.metadata.RenderedFile.java

public static void copyMoveAllMetadataContent(String metadata_reference_id_from,
        String metadata_reference_id_dest, boolean copy) throws IOException {
    if (metadata_reference_id_from == null) {
        throw new NullPointerException("\"metadata_reference_id_from\" can't to be null");
    }/*  w w  w . j  av a 2  s.c o m*/
    if (metadata_reference_id_dest == null) {
        throw new NullPointerException("\"metadata_reference_id_dest\" can't to be null");
    }
    if (local_directory == null) {
        throw new IOException("No configuration is set !");
    }
    if (local_directory.exists() == false) {
        throw new IOException("Invalid configuration is set !");
    }

    StringBuilder sb_from_directory = new StringBuilder();
    sb_from_directory.append(local_directory.getCanonicalPath());
    sb_from_directory.append(File.separator);
    sb_from_directory.append(metadata_reference_id_from.substring(0, 6));
    sb_from_directory.append(File.separator);
    sb_from_directory.append(metadata_reference_id_from.substring(6));

    StringBuilder sb_dest_directory = new StringBuilder();
    sb_dest_directory.append(local_directory.getCanonicalPath());
    sb_dest_directory.append(File.separator);
    sb_dest_directory.append(metadata_reference_id_dest.substring(0, 6));
    sb_dest_directory.append(File.separator);
    sb_dest_directory.append(metadata_reference_id_dest.substring(6));

    File from_dir = new File(sb_from_directory.toString()).getCanonicalFile();
    File dest_dir = new File(sb_dest_directory.toString()).getCanonicalFile();

    Log2Dump dump = new Log2Dump();
    dump.add("from", from_dir);
    dump.add("to", dest_dir);
    Log2.log.debug("Prepare operation", dump);

    /**
     * Create sub directories.
     */
    FileUtils.forceMkdir(dest_dir);
    FileUtils.deleteDirectory(dest_dir);

    if (copy == false) {
        FileUtils.moveDirectory(from_dir, dest_dir);
    } else {
        FileUtils.copyDirectory(from_dir, dest_dir, true);
    }
}

From source file:au.org.ala.names.search.DwcaNameIndexer.java

/**
 * Example run/*from www  .j  a v a2 s.  com*/
 *
 * java cp .:names.jar au.org.ala.checklist.lucene.DwcaNameIndexer
 * -all
 * -dwca /data/bie-staging/names-lists/dwca-col
 * -target /data/lucene/testdwc-namematching
 * -irmng /data/bie-staging/irmng/IRMNG_DWC_HOMONYMS
 * -common /data/bie-staging/ala-names/col_vernacular.txt
 *
 * @param args
 */
public static void main(String[] args) {

    final String DEFAULT_DWCA = "/data/lucene/sources/dwca-col";
    final String DEFAULT_IRMNG = "/data/lucene/sources/IRMNG_DWC_HOMONYMS";
    final String DEFAULT_COMMON_NAME = "/data/lucene/sources/col_vernacular.txt";
    final String DEFAULT_TARGET_DIR = "/data/lucene/namematching";
    final String DEFAULT_TMP_DIR = "/data/lucene/nmload-tmp";

    Options options = new Options();
    options.addOption("v", "version", false, "Retrieve version information");
    options.addOption("h", "help", false, "Retrieve options");
    options.addOption("all", false, "Generates the load index and search index");
    options.addOption("load", false,
            "Generate the load index only. "
                    + "The load index is a temporary index generated from the raw data files"
                    + " used to load the main search index");
    options.addOption("search", false,
            "Generates the search index. A load index must already be created for this to run.");
    options.addOption("irmng", true,
            "The absolute path to the unzipped irmng DwCA. IRMNG is used to detect homonyms. Defaults to "
                    + DEFAULT_IRMNG);
    options.addOption("dwca", true,
            "The absolute path to the unzipped DwCA for the scientific names. Defaults to " + DEFAULT_DWCA);
    options.addOption("target", true,
            "The target directory to write the new name index to. Defaults to " + DEFAULT_TARGET_DIR);
    options.addOption("tmp", true, "The tmp directory for the load index. Defaults to " + DEFAULT_TMP_DIR);
    options.addOption("common", true, "The common (vernacular) name file. Defaults to " + DEFAULT_COMMON_NAME);
    options.addOption("testSearch", true,
            "Debug a name search. This uses the target directory to search against.");

    CommandLineParser parser = new BasicParser();

    try {
        // parse the command line arguments
        CommandLine line = parser.parse(options, args);

        if (line.hasOption("v")) {
            //only load the properties file if it exists otherwise default to the biocache-test-config.properties on the classpath
            InputStream stream = DwcaNameIndexer.class.getResourceAsStream("/git.properties");
            Properties properties = new Properties();
            if (stream != null) {
                properties.load(stream);
                properties.list(System.out);
            } else {
                System.err.println("Unable to retrieve versioning information");
            }
            System.exit(-1);
        }

        if (line.hasOption("help")) {
            //only load the properties file if it exists otherwise default to the biocache-test-config.properties on the classpath
            new HelpFormatter().printHelp("nameindexer", options);
            System.exit(-1);
        }

        if (line.hasOption("testSearch")) {

            boolean indexExists = (new File(DEFAULT_TARGET_DIR).exists());

            if (indexExists) {
                //do a name search - with option flag pointing to index location
                System.out.println("Search for name");
                ALANameSearcher searcher = new ALANameSearcher(
                        line.getOptionValue("target", DEFAULT_TARGET_DIR));
                NameSearchResult nsr = searcher.searchForRecord(line.getOptionValue("testSearch"));
                if (nsr != null) {
                    Map<String, String> props = nsr.toMap();
                    for (Map.Entry<String, String> entry : props.entrySet()) {
                        System.out.println(entry.getKey() + ": " + entry.getValue());
                    }
                } else {
                    System.err.println("No match for " + line.getOptionValue("testSearch"));
                }
            } else {
                System.err.println("Index unreadable. Check " + DEFAULT_TARGET_DIR);
            }
            System.exit(-1);
        }

        boolean load = line.hasOption("load") || line.hasOption("all");
        boolean search = line.hasOption("search") || line.hasOption("all");

        if (!line.hasOption("load") && !line.hasOption("search") && !line.hasOption("all")) {
            load = true;
            search = true;
        }

        log.info("Generating loading index: " + load);
        log.info("Generating searching index: " + search);

        boolean defaultIrmngReadable = (new File(DEFAULT_IRMNG).exists());
        boolean defaultCommonReadable = (new File(DEFAULT_COMMON_NAME).exists());
        boolean defaultDwcaReadable = (new File(DEFAULT_DWCA).exists());

        if (line.getOptionValue("dwca") != null) {
            log.info("Using the  DwCA name file: " + line.getOptionValue("dwca"));
        } else if (defaultDwcaReadable) {
            log.info("Using the default DwCA name file: " + DEFAULT_DWCA);
        } else {
            log.error(
                    "No DwC Archive specified and the default file path does not exist or is inaccessible. Default path: "
                            + DEFAULT_DWCA);
            System.exit(-1);
        }

        if (line.getOptionValue("irmng") == null && !defaultIrmngReadable) {
            log.warn(
                    "No IRMNG export specified and the default file path does not exist or is inaccessible. Default path: "
                            + DEFAULT_IRMNG);
        } else {
            log.info("Using the default IRMNG name file: " + DEFAULT_IRMNG);
        }

        if (line.getOptionValue("common") == null && !defaultCommonReadable) {
            log.warn(
                    "No common name export specified and the default file path does not exist or is inaccessible. Default path: "
                            + DEFAULT_COMMON_NAME);
        } else {
            log.info("Using the default common name file: " + DEFAULT_COMMON_NAME);
        }

        File targetDirectory = new File(line.getOptionValue("target", DEFAULT_TARGET_DIR));
        if (targetDirectory.exists()) {
            String newPath = targetDirectory.getAbsolutePath() + "_"
                    + DateFormatUtils.format(new Date(), "yyyy-MM-dd_hh-mm-ss");
            log.info("Target directory already exists. Backing up to : " + newPath);
            File newTargetDirectory = new File(newPath);
            FileUtils.moveDirectory(targetDirectory, newTargetDirectory);
            FileUtils.forceMkdir(targetDirectory);
        }

        DwcaNameIndexer indexer = new DwcaNameIndexer();
        indexer.create(load, search, line.getOptionValue("target", DEFAULT_TARGET_DIR),
                line.getOptionValue("tmp", DEFAULT_TMP_DIR), line.getOptionValue("dwca", DEFAULT_DWCA),
                line.getOptionValue("irmng", DEFAULT_IRMNG),
                line.getOptionValue("common", DEFAULT_COMMON_NAME));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:kr.co.leem.system.FileSystemUtils.java

/**
 *  ??./*from w ww  .j a  v  a2  s.  c o  m*/
 *
 * @param srcDir ?  .
 * @param destDir ??  .
 * @see FileUtils#moveDirectory(File, File)
 */
public static void moveDirectory(final String srcDir, final String destDir) {
    processIO(new IOCallback<Object>() {
        public Object doInProcessIO() throws IOException, NullPointerException {
            FileUtils.moveDirectory(new File(srcDir), new File(destDir));
            return null;
        }
    });
}

From source file:hudson.model.Items.java

/**
 * Moves an item between folders (or top level).
 * Fires all relevant events but does not verify that the items directory is not currently being used in some way (for example by a running build).
 * Does not check any permissions./*from  w  w  w  . ja v  a  2 s.  c  o m*/
 * @param item some item (job or folder)
 * @param destination the destination of the move (a folder or {@link Jenkins}); not the current parent (or you could just call {@link AbstractItem#renameTo})
 * @return the new item (usually the same object as {@code item})
 * @throws IOException if the move fails, or some subsequent step fails (directory might have already been moved)
 * @throws IllegalArgumentException if the move would really be a rename, or the destination cannot accept the item, or the destination already has an item of that name
 * @since 1.548
 */
public static <I extends AbstractItem & TopLevelItem> I move(I item,
        DirectlyModifiableTopLevelItemGroup destination) throws IOException, IllegalArgumentException {
    DirectlyModifiableTopLevelItemGroup oldParent = (DirectlyModifiableTopLevelItemGroup) item.getParent();
    if (oldParent == destination) {
        throw new IllegalArgumentException();
    }
    // TODO verify that destination is to not equal to, or inside, item
    if (!destination.canAdd(item)) {
        throw new IllegalArgumentException();
    }
    String name = item.getName();
    if (destination.getItem(name) != null) {
        throw new IllegalArgumentException(name + " already exists");
    }
    String oldFullName = item.getFullName();
    // TODO AbstractItem.renameTo has a more baroque implementation; factor it out into a utility method perhaps?
    File destDir = destination.getRootDirFor(item);
    FileUtils.forceMkdir(destDir.getParentFile());
    FileUtils.moveDirectory(item.getRootDir(), destDir);
    oldParent.remove(item);
    I newItem = destination.add(item, name);
    item.movedTo(destination, newItem, destDir);
    ItemListener.fireLocationChange(newItem, oldFullName);
    return newItem;
}

From source file:com.edgenius.wiki.service.impl.BackupServiceImpl.java

@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public void restore(File file) throws BackupException, InvalidLicenseException {

    log.info("Restore starting....");
    long time = System.currentTimeMillis();
    FileInputStream zipis = null;
    try {/*from  w w  w  . j av a2 s  .c o m*/
        //unzip to temporary directory first
        String dir = FileUtil.createTempDirectory(TMP_RESTORE);
        zipis = new FileInputStream(file);
        ZipFileUtil.expandZipToFolder(zipis, dir);

        log.info("Restore file unzipped to {}. Took {}s", dir, (System.currentTimeMillis() - time) / 1000);

        //get databinder, to check options
        File binderFile = new File(FileUtil.getFullPath(dir, OBJS_BINDER_NAME));
        //version check - it may has different fields name etc to cause need do migrate
        int binderVersion = versionCheck(binderFile);
        log.info("Data binder version is {}", binderVersion);

        time = System.currentTimeMillis();
        FileInputStream bis = new FileInputStream(binderFile);
        InputStreamReader reader = new InputStreamReader(bis, Constants.UTF8);

        XStream xstream = createXStreamInstance();
        DataBinder binder = (DataBinder) xstream.fromXML(reader);
        reader.close();
        bis.close();
        log.info("Parse binder XML took {}s", (System.currentTimeMillis() - time) / 1000);

        int leftUserCount = licenseCheck(binder);

        //As HL Robert's export, Page has list has duplicated: such as page id=93 is enclosed DataObject normally,
        //but, unexpected, same page appear again in page list, this time it is as a referenece object, but this cause same page
        //has duplicate ojbect in return list
        //<com.edgenius.wiki.model.Page reference="93"/>
        //Here is just temporary fix only for HL. But the fix is Hibernate.loadAll() see BaseDAOHibernate.getAll()
        List<Page> pages = (List<Page>) binder.get(Page.class.getName());
        Set<Integer> dup = new HashSet<Integer>();
        for (Iterator<Page> iter = pages.iterator(); iter.hasNext();) {
            Integer uid = iter.next().getUid();
            if (dup.contains(uid)) {
                log.error("There are duplciated pages while import data, UID:" + uid);
                iter.remove();
                continue;
            }
            dup.add(uid);
        }

        Map<Integer, String> spaceMap = null;
        if (binderVersion <= 2180) {
            //a bug fix - for older 2.18 version as customized theme use spaceUid as key, which changed after import...
            //Since 2.19, customized theme XML is removed, this problem doesn't exist any more. I found this bug when 2.19:(
            List<Space> spaces = (List<Space>) binder.get(Space.class.getName());
            //save old version spaceUname and spaceUid into a map 
            spaceMap = new HashMap<Integer, String>();
            for (Iterator<Space> iter = spaces.iterator(); iter.hasNext();) {
                Space space = iter.next();
                spaceMap.put(space.getUid(), space.getUnixName());
            }
        }
        int options = binder.getOptions();

        if ((options & BACKUP_DATA) > 0) {
            time = System.currentTimeMillis();
            importData(binder, dir, binderVersion);
            log.info("Restore database table took {}s", (System.currentTimeMillis() - time) / 1000);

            //delete binder file after import success
            if (!binderFile.delete())
                binderFile.deleteOnExit();
        }

        time = System.currentTimeMillis();
        if ((options & BACKUP_ATTACHMENT) > 0) {
            FileUtils.deleteDirectory(repositoryLocation.getFile());
            FileUtils.moveDirectory(new File(FileUtil.getFullPath(dir, binder.getDir(BACKUP_ATTACHMENT))),
                    repositoryLocation.getFile());
        }
        if ((options & BACKUP_RSS) > 0) {
            FileUtils.deleteDirectory(rssLocation.getFile());
            FileUtils.moveDirectory(new File(FileUtil.getFullPath(dir, binder.getDir(BACKUP_RSS))),
                    rssLocation.getFile());
        }
        if ((options & BACKUP_INDEX) > 0) {
            FileUtils.deleteDirectory(indexLocation.getFile());
            FileUtils.moveDirectory(new File(FileUtil.getFullPath(dir, binder.getDir(BACKUP_INDEX))),
                    indexLocation.getFile());
        }
        if ((options & BACKUP_SKIN) > 0) {
            FileUtils.deleteDirectory(skinLocation.getFile());
            FileUtils.moveDirectory(new File(FileUtil.getFullPath(dir, binder.getDir(BACKUP_SKIN))),
                    skinLocation.getFile());
        }
        if ((options & BACKUP_THEME) > 0) {
            FileUtils.deleteDirectory(themeLocation.getFile());
            FileUtils.moveDirectory(new File(FileUtil.getFullPath(dir, binder.getDir(BACKUP_THEME))),
                    themeLocation.getFile());
            if (binderVersion <= 2180) {
                //rename customized theme to new spaceUid
                File customizedDir = new File(themeLocation.getFile(), "customized");
                File customizedSubDir = new File(themeLocation.getFile(), "customizedTemp");

                String[] files = customizedDir.list(FileFilterUtils.suffixFileFilter(".xml"));
                if (files.length > 0) {
                    customizedSubDir.mkdirs();
                }
                for (String name : files) {
                    int uid = NumberUtils.toInt(name.substring(0, name.length() - 4), -1);
                    if (uid == -1) {
                        log.info("Unable to get correct space UID from theme file name {}", name);
                        continue;
                    }
                    String uname = spaceMap.get(uid);
                    if (uname == null) {
                        log.warn("Unable to get old spaceUname by UID {}", uid);
                        continue;
                    }

                    Space space = spaceDAO.getByUname(uname);
                    if (space == null) {
                        log.warn("Unable to get space by Uname {}", uname);
                        continue;
                    }
                    uid = space.getUid();
                    FileUtils.moveFile(new File(customizedDir, name), new File(customizedSubDir, uid + ".xml"));
                }

                if (customizedSubDir.exists()) {
                    //replace old by renamed themes
                    FileUtils.deleteDirectory(customizedDir);
                    FileUtils.moveDirectory(customizedSubDir, customizedDir);
                }
            }
        }

        //upgrade data file under DataRoot -- Here assume theme, index, rss etc. All use default name from DataRoot!!!
        try {
            upgradeService.doBackupPackageUpgardeForDataFiles(String.valueOf((float) binderVersion / 1000));
        } catch (Exception e) {
            log.error("Unexpected erorr while upgrade backup export package from " + binderVersion + " to "
                    + Version.VERSION, e);
        }

        log.info("Restore data root files tooks {}s", (System.currentTimeMillis() - time) / 1000);
        try {
            FileUtil.deleteDir(dir);
        } catch (IOException e) {
            log.error("Unable to delete restore temp directory " + dir);
        }

        Version.LEFT_USERS = leftUserCount;
        log.info("Restore success complete. Database transaction will submit.");
    } catch (InvalidLicenseException e) {
        log.error("Restore failed", e);
        throw e;
    } catch (Exception e) {
        log.error("Restore failed", e);
        throw new BackupException(e);
    } finally {
        if (zipis != null) {
            try {
                zipis.close();
            } catch (Exception e) {
            }
        }
    }

}

From source file:com.photon.maven.plugins.android.phase05compile.NdkBuildMojo.java

public void execute() throws MojoExecutionException, MojoFailureException {

    // Validate the NDK
    final File ndkBuildFile = new File(getAndroidNdk().getNdkBuildPath());
    NativeHelper.validateNDKVersion(ndkBuildFile.getParentFile());

    // This points 
    File nativeLibDirectory = new File(nativeLibrariesOutputDirectory, ndkArchitecture);

    final boolean libsDirectoryExists = nativeLibDirectory.exists();

    File directoryToRemove = nativeLibDirectory;

    if (!libsDirectoryExists) {
        getLog().info("Creating native output directory " + nativeLibDirectory);

        if (nativeLibDirectory.getParentFile().exists()) {
            nativeLibDirectory.mkdir();/*from  ww w. j a  v  a2  s .  co  m*/
        } else {
            nativeLibDirectory.mkdirs();
            directoryToRemove = nativeLibDirectory.getParentFile();
        }

    }

    final CommandExecutor executor = CommandExecutor.Factory.createDefaultCommmandExecutor();

    executor.setErrorListener(new CommandExecutor.ErrorListener() {
        @Override
        public boolean isError(String error) {

            Pattern pattern = Pattern.compile(buildWarningsRegularExpression);
            Matcher matcher = pattern.matcher(error);

            if (ignoreBuildWarnings && matcher.matches()) {
                return false;
            }
            return true;
        }
    });

    final Set<Artifact> nativeLibraryArtifacts = findNativeLibraryDependencies();
    // If there are any static libraries the code needs to link to, include those in the make file
    final Set<Artifact> resolveNativeLibraryArtifacts = AetherHelper.resolveArtifacts(nativeLibraryArtifacts,
            repoSystem, repoSession, projectRepos);

    try {
        File f = File.createTempFile("android_maven_plugin_makefile", ".mk");
        f.deleteOnExit();

        String makeFile = MakefileHelper.createMakefileFromArtifacts(f.getParentFile(),
                resolveNativeLibraryArtifacts, useHeaderArchives, repoSession, projectRepos, repoSystem);
        IOUtil.copy(makeFile, new FileOutputStream(f));

        // Add the path to the generated makefile
        executor.addEnvironment("ANDROID_MAVEN_PLUGIN_MAKEFILE", f.getAbsolutePath());

        // Only add the LOCAL_STATIC_LIBRARIES
        if (NativeHelper.hasStaticNativeLibraryArtifact(resolveNativeLibraryArtifacts)) {
            executor.addEnvironment("ANDROID_MAVEN_PLUGIN_LOCAL_STATIC_LIBRARIES",
                    MakefileHelper.createStaticLibraryList(resolveNativeLibraryArtifacts, true));
        }
        if (NativeHelper.hasSharedNativeLibraryArtifact(resolveNativeLibraryArtifacts)) {
            executor.addEnvironment("ANDROID_MAVEN_PLUGIN_LOCAL_SHARED_LIBRARIES",
                    MakefileHelper.createStaticLibraryList(resolveNativeLibraryArtifacts, false));
        }

    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage());
    }

    File localCIncludesFile = null;
    //
    try {
        localCIncludesFile = File.createTempFile("android_maven_plugin_makefile_captures", ".tmp");
        localCIncludesFile.deleteOnExit();
        executor.addEnvironment("ANDROID_MAVEN_PLUGIN_LOCAL_C_INCLUDES_FILE",
                localCIncludesFile.getAbsolutePath());
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage());
    }

    // Add any defined system properties
    if (systemProperties != null && !systemProperties.isEmpty()) {
        for (Map.Entry<String, String> entry : systemProperties.entrySet()) {
            executor.addEnvironment(entry.getKey(), entry.getValue());
        }
    }

    executor.setLogger(this.getLog());
    final List<String> commands = new ArrayList<String>();

    commands.add("-C");
    if (ndkBuildDirectory == null) {
        ndkBuildDirectory = project.getBasedir().getAbsolutePath();
    }
    commands.add(ndkBuildDirectory);

    if (ndkBuildAdditionalCommandline != null) {
        String[] additionalCommands = ndkBuildAdditionalCommandline.split(" ");
        for (final String command : additionalCommands) {
            commands.add(command);
        }
    }

    // If a build target is specified, tag that onto the command line as the
    // very last of the parameters
    if (target != null) {
        commands.add(target);
    } else {
        commands.add(project.getArtifactId());
    }

    final String ndkBuildPath = resolveNdkBuildExecutable();
    getLog().info(ndkBuildPath + " " + commands.toString());

    try {
        executor.executeCommand(ndkBuildPath, commands, project.getBasedir(), true);
    } catch (ExecutionException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }

    // Cleanup libs/armeabi directory if needed - this implies moving any native artifacts into target/libs
    if (clearNativeArtifacts) {

        final File destinationDirectory = new File(ndkOutputDirectory.getAbsolutePath(), "/" + ndkArchitecture);

        try {
            if (!libsDirectoryExists) {
                FileUtils.moveDirectory(nativeLibDirectory, destinationDirectory);
            } else {
                FileUtils.copyDirectory(nativeLibDirectory, destinationDirectory);
                FileUtils.cleanDirectory(nativeLibDirectory);
            }

            nativeLibDirectory = destinationDirectory;

        } catch (IOException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }

    if (!libsDirectoryExists) {
        getLog().info("Cleaning up native library output directory after build");
        getLog().debug("Removing directory: " + directoryToRemove);
        if (!directoryToRemove.delete()) {
            getLog().warn("Could not remove directory, marking as delete on exit");
            directoryToRemove.deleteOnExit();
        }
    }

    // Attempt to attach the native library if the project is defined as a "pure" native Android library
    // (packaging is 'so' or 'a') or if the plugin has been configured to attach the native library to the build
    if ("so".equals(project.getPackaging()) || "a".equals(project.getPackaging()) || attachNativeArtifacts) {

        File[] files = nativeLibDirectory.listFiles(new FilenameFilter() {
            public boolean accept(final File dir, final String name) {
                if ("a".equals(project.getPackaging())) {
                    return name.startsWith("lib" + (target != null ? target : project.getArtifactId()))
                            && name.endsWith(".a");
                } else {
                    return name.startsWith("lib" + (target != null ? target : project.getArtifactId()))
                            && name.endsWith(".so");
                }
            }
        });

        // slight limitation at this stage - we only handle a single .so artifact
        if (files == null || files.length != 1) {
            getLog().warn("Error while detecting native compile artifacts: "
                    + (files == null || files.length == 0 ? "None found" : "Found more than 1 artifact"));
            if (files != null && files.length > 1) {
                getLog().error("Currently, only a single, final native library is supported by the build");
                throw new MojoExecutionException(
                        "Currently, only a single, final native library is supported by the build");
            } else {
                getLog().error(
                        "No native compiled library found, did the native compile complete successfully?");
                throw new MojoExecutionException(
                        "No native compiled library found, did the native compile complete successfully?");
            }
        } else {
            getLog().debug("Adding native compile artifact: " + files[0]);
            final String artifactType = resolveArtifactType(files[0]);
            projectHelper.attachArtifact(this.project, artifactType,
                    (ndkClassifier != null ? ndkClassifier : ndkArchitecture), files[0]);
        }

    }

    // Process conditionally any of the headers to include into the header archive file
    processHeaderFileIncludes(localCIncludesFile);

}

From source file:it.geosolutions.geobatch.flow.event.consumer.file.FileBasedEventConsumer.java

/***************************************************************************
 * Main Thread cycle.//from www. j av  a2s.co  m
 * 
 * <LI>Create needed dirs</LI> <LI>Optionally backup files</LI> <LI>Move
 * files into a job-specific working dir</LI> <LI>Run the actions</LI>
 */
public Queue<FileSystemEvent> call() throws Exception {
    this.canceled = false;

    boolean jobResultSuccessful = false;
    Throwable exceptionOccurred = null;

    getListenerForwarder().setTask("Configuring");
    getListenerForwarder().started();

    try {

        // create live working dir
        getListenerForwarder().progressing(10, "Managing events");

        //
        // Management of current working directory
        //
        // if we work on the input directory, we do not move around
        // anything, unless we want to
        // perform a backup

        if (configuration.isPerformBackup() || !configuration.isPreserveInput()) {
            if (!flowInstanceTempDir.exists() && !flowInstanceTempDir.mkdirs()) {
                throw new IllegalStateException("Could not create consumer backup directory!");
            }
        }
        // set the consumer running context
        // don't know how this running context will be used in a FileBased* hiererchy, anyway let's force the use of proper methods.
        setRunningContext("DONT_USE_AS_FILEPATH_" + flowInstanceTempDir.getAbsolutePath());

        // create backup dir. Creation is deferred until first usage
        getListenerForwarder().progressing(20, "Creating backup dir");

        final File backupDirectory = new File(flowInstanceTempDir, "backup");
        if (configuration.isPerformBackup()) {
            if (!backupDirectory.exists() && !backupDirectory.mkdirs()) {
                throw new IllegalStateException("Could not create consumer backup directory!");
            }
        }

        //
        // Cycling on all the input events
        //
        Queue<FileSystemEvent> fileEventList = new LinkedList<FileSystemEvent>();
        int numProcessedFiles = 0;
        for (FileSystemEvent event : this.eventsQueue) {
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info(
                        "[" + Thread.currentThread().getName() + "]: new element retrieved from the MailBox.");
            }

            // get info for the input file event
            final File sourceDataFile = event.getSource();
            final String fileBareName;
            if ((sourceDataFile != null) && sourceDataFile.exists()) {
                fileBareName = FilenameUtils.getName(sourceDataFile.toString());
                getListenerForwarder().progressing(30 + (10f / this.eventsQueue.size() * numProcessedFiles++),
                        "Preprocessing event " + fileBareName);
                //
                // copy input file/dir to current working directory
                //
                if (IOUtils.acquireLock(this, sourceDataFile)) {

                    //
                    // Backing up inputs?
                    //
                    if (this.configuration.isPerformBackup()) {

                        // Backing up files and delete sources.
                        getListenerForwarder().progressing(
                                30 + (10f / this.eventsQueue.size() * numProcessedFiles++),
                                "Creating backup files");

                        // In case we do not work on the input as is, we
                        // move it to our
                        // current working directory
                        final File destDataFile = new File(backupDirectory, fileBareName);
                        if (sourceDataFile.isDirectory()) {
                            FileUtils.copyDirectory(sourceDataFile, destDataFile);
                        } else {
                            FileUtils.copyFile(sourceDataFile, destDataFile);
                        }
                    }

                    //
                    // Working on input events directly without moving to
                    // working dir?
                    //
                    if (!configuration.isPreserveInput()) {

                        // In case we do not work on the input as is, we
                        // move it to our current working directory
                        final File destDataFile = new File(flowInstanceTempDir, fileBareName);
                        if (sourceDataFile.isDirectory()) {
                            FileUtils.moveDirectory(sourceDataFile, destDataFile);
                        } else {
                            FileUtils.moveFile(sourceDataFile, destDataFile);
                        }

                        // adjust event sources since we moved the files
                        // locally
                        fileEventList.offer(new FileSystemEvent(destDataFile, event.getEventType()));
                    } else {
                        // we are going to work directly on the input files
                        fileEventList.offer(event);

                    }
                    if (LOGGER.isInfoEnabled()) {
                        LOGGER.info(
                                "[" + Thread.currentThread().getName() + "]: accepted file " + sourceDataFile);
                    }
                } else {
                    if (LOGGER.isErrorEnabled()) {
                        LOGGER.error(new StringBuilder("[").append(Thread.currentThread().getName())
                                .append("]: could not lock file ").append(sourceDataFile).toString());
                    }

                    /*
                     * TODO: lock not acquired: what else?
                     */
                }

            } // event.getSource()!=null && sourceDataFile.exists()
            else {

                /*
                 * event.getSource()==null || !sourceDataFile.exists() this
                 * could be an empty file representing a POLLING event
                 */
                fileEventList.offer(event);
            }

        }

        // //
        // TODO if no further processing is necessary or can be
        // done due to some error, set eventConsumerStatus to Finished or
        // Failure. (etj: ???)
        // //
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("[" + Thread.currentThread().getName() + "]: new element processed.");
        }

        // // Finally, run the Actions on the files
        getListenerForwarder().progressing(50, "Running actions");

        try {
            // apply actions into the actual context (currentRunDirectory)
            fileEventList = this.applyActions(fileEventList);
            this.setStatus(EventConsumerStatus.COMPLETED);
            jobResultSuccessful = true;
        } catch (ActionException ae) {
            this.setStatus(EventConsumerStatus.FAILED);
            throw ae;
        }

        return fileEventList;
    } catch (ActionException e) {
        String msg = "[" + Thread.currentThread().getName() + "] Error during " + e.getType().getSimpleName()
                + " execution: " + e.getLocalizedMessage();
        if (LOGGER.isDebugEnabled()) {
            LOGGER.error(msg, e);
        } else {
            LOGGER.error(msg);
        }
        this.setStatus(EventConsumerStatus.FAILED);
        exceptionOccurred = e;

    } catch (IOException e) {
        if (LOGGER.isErrorEnabled()) {
            LOGGER.error("[" + Thread.currentThread().getName() + "] could not move file "
                    + " due to the following IO error: " + e.getLocalizedMessage(), e);
        }
        this.setStatus(EventConsumerStatus.FAILED);
        exceptionOccurred = e;

    } catch (InterruptedException e) {
        if (LOGGER.isErrorEnabled()) {
            LOGGER.error("[" + Thread.currentThread().getName() + "] could not move file "
                    + " due to an InterruptedException: " + e.getLocalizedMessage(), e);
        }
        this.setStatus(EventConsumerStatus.FAILED);
        exceptionOccurred = e;

    } catch (RuntimeException e) {
        exceptionOccurred = e;
        throw e;

    } finally {
        getListenerForwarder().progressing(100, "Completed");
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info(Thread.currentThread().getName() + " DONE!");
        }
        // this.dispose();

        if (jobResultSuccessful && (exceptionOccurred == null)) {
            getListenerForwarder().completed();
        } else {
            getListenerForwarder().failed(exceptionOccurred);
        }
    }

    return null;
}

From source file:com.jayway.maven.plugins.android.phase05compile.NdkBuildMojo.java

private void compileForArchitecture(String architecture)
        throws MojoExecutionException, IOException, ExecutionException {
    getLog().debug("Resolving for NDK architecture : " + architecture);

    // Start setting up the command line to be executed
    final CommandExecutor executor = CommandExecutor.Factory.createDefaultCommmandExecutor();
    // Add an error listener to the build - this allows the build to conditionally fail
    // depending on a) the output of the build b) whether or not build errors (output on stderr) should be
    // ignored and c) whether the pattern matches or not
    executor.setErrorListener(getNdkErrorListener());

    final Set<Artifact> nativeLibraryArtifacts = findNativeLibraryDependencies();

    // If there are any static libraries the code needs to link to, include those in the make file
    final Set<Artifact> resolveNativeLibraryArtifacts = getArtifactResolverHelper()
            .resolveArtifacts(nativeLibraryArtifacts);

    getLog().debug("resolveArtifacts found " + resolveNativeLibraryArtifacts.size() + ": "
            + resolveNativeLibraryArtifacts.toString());

    final File buildFolder = new File(ndkBuildDirectory, architecture);
    buildFolder.mkdirs();/*from w  ww  .j a  v  a2 s  .  c o m*/

    final File androidMavenMakefile = new File(buildFolder, "android_maven_plugin_makefile.mk");
    final MakefileHelper makefileHelper = new MakefileHelper(getLog(), getArtifactResolverHelper(),
            harArtifactHandler, getUnpackedLibsDirectory());

    final MakefileHelper.MakefileHolder makefileHolder = makefileHelper.createMakefileFromArtifacts(
            resolveNativeLibraryArtifacts, architecture, "armeabi", useHeaderArchives);

    final FileOutputStream output = new FileOutputStream(androidMavenMakefile);
    try {
        IOUtil.copy(makefileHolder.getMakeFile(), output);
    } finally {
        output.close();
    }

    // Add the path to the generated makefile - this is picked up by the build (by an include from the user)
    executor.addEnvironment("ANDROID_MAVEN_PLUGIN_MAKEFILE", androidMavenMakefile.getAbsolutePath());

    setupNativeLibraryEnvironment(makefileHelper, executor, resolveNativeLibraryArtifacts, architecture);

    // Adds the location of the Makefile capturer file - this file will after the build include
    // things like header files, flags etc.  It is processed after the build to retrieve the headers
    // and also capture flags etc ...
    final File makefileCaptureFile = File.createTempFile("android_maven_plugin_makefile_captures", ".tmp");
    makefileCaptureFile.deleteOnExit();
    executor.addEnvironment(MakefileHelper.MAKEFILE_CAPTURE_FILE, makefileCaptureFile.getAbsolutePath());

    // Add any defined system properties
    if (systemProperties != null && !systemProperties.isEmpty()) {
        for (Map.Entry<String, String> entry : systemProperties.entrySet()) {
            executor.addEnvironment(entry.getKey(), entry.getValue());
        }
    }
    executor.setLogger(this.getLog());
    // Setup the command line for the make
    final List<String> commands = new ArrayList<String>();
    // Setup the build directory (defaults to the current directory) but may be different depending
    // on user configuration
    commands.add("-C");
    commands.add(project.getBasedir().getAbsolutePath());

    // If the build should use a custom makefile or not - some validation is done to ensure
    // this exists and all
    if (makefile != null) {
        File makeFile = new File(project.getBasedir(), makefile);
        if (!makeFile.exists()) {
            getLog().error("Specified makefile " + makeFile + " does not exist");
            throw new MojoExecutionException("Specified makefile " + makeFile + " does not exist");
        }
        commands.add("-f");
        commands.add(makefile);
    }

    configureApplicationMakefile(commands);
    configureMaxJobs(commands);
    configureNdkToolchain(architecture, commands);
    configureAdditionalCommands(commands);

    // If a build target is specified, tag that onto the command line as the very last of the parameters
    if (target != null) {
        commands.add(target);
    } else /*if ( Const.ArtifactType.NATIVE_IMPLEMENTATION_ARCHIVE.equals( project.getPackaging() ) )*/
    {
        commands.add(project.getArtifactId());
    }

    final String ndkBuildPath = resolveNdkBuildExecutable();
    getLog().debug(ndkBuildPath + " " + commands.toString());
    getLog().info("Executing NDK " + architecture + " make at : " + ndkBuildDirectory);

    executor.setCaptureStdOut(true);
    executor.executeCommand(ndkBuildPath, commands, ndkBuildDirectory, true);
    getLog().debug("Executed NDK " + architecture + " make at : " + ndkBuildDirectory);

    // Where the NDK build creates the libs.
    final File nativeLibOutputDirectory = new File(nativeLibrariesOutputDirectory, architecture);
    nativeLibOutputDirectory.mkdirs();

    // Move the built native libs into the packaging folder.
    // We don't create them there to start with because the NDK build seems determined to create them in the root.
    final File destinationDirectory = new File(ndkOutputDirectory, architecture);
    FileUtils.moveDirectory(nativeLibOutputDirectory, destinationDirectory);

    // Attempt to attach the native library if the project is defined as a "pure" native Android library
    // (packaging is 'so' or 'a') or if the plugin has been configured to attach the native library to the build
    if (Const.ArtifactType.NATIVE_SYMBOL_OBJECT.equals(project.getPackaging())
            || Const.ArtifactType.NATIVE_IMPLEMENTATION_ARCHIVE.equals(project.getPackaging())
            || attachNativeArtifacts) {
        attachNativeLib(destinationDirectory, architecture);
    }

    // Process conditionally any of the headers to include into the header archive file
    if (attachHeaderFiles) {
        attachHeaderFiles(makefileCaptureFile, architecture);
    }

    // If we created a makefile for the build we should be polite and remove any extracted include
    // directories after we're done
    getLog().info("Cleaning up extracted include directories used for build");
    MakefileHelper.cleanupAfterBuild(makefileHolder);
}