Example usage for org.apache.commons.vfs FileObject exists

List of usage examples for org.apache.commons.vfs FileObject exists

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileObject exists.

Prototype

public boolean exists() throws FileSystemException;

Source Link

Document

Determines if this file exists.

Usage

From source file:org.jclouds.vfs.tools.blobstore.BlobStoreShell.java

/**
 * Does a 'touch' command./* w  w w. j a  v  a 2 s  . co  m*/
 */
private void touch(final String[] cmd) throws Exception {
    if (cmd.length < 2) {
        throw new Exception("USAGE: touch <path>");
    }
    final FileObject file = remoteMgr.resolveFile(remoteCwd, cmd[1]);
    if (!file.exists()) {
        file.createFile();
    }
    file.getContent().setLastModifiedTime(System.currentTimeMillis());
}

From source file:org.josso.tooling.gshell.install.installer.JBossInstaller.java

@Override
public void validatePlatform() throws InstallException {
    super.validatePlatform();

    boolean valid = true;

    try {//from www .  j av  a  2 s .c  o m

        String jbossWeb = "jbossweb.sar"; // jboss 5 and 6 by default

        if (getPlatformVersion().startsWith("4.2"))
            jbossWeb = "jboss-web.deployer";
        if (getPlatformVersion().startsWith("4.0"))
            jbossWeb = "jbossweb-tomcat55.sar";
        else if (getPlatformVersion().startsWith("3.2"))
            jbossWeb = "jbossweb-tomcat50.sar";

        FileObject serverXml = targetDeployDir.resolveFile(jbossWeb + "/server.xml");
        if (serverXml == null || !serverXml.exists()
                || !serverXml.getType().getName().equals(FileType.FILE.getName())) {
            valid = false;
            getPrinter().printErrStatus("JBossHome", "Cannot find server.xml");
        }

        if (!valid)
            throw new InstallException(
                    "Target does not seem a " + getTargetPlatform().getDescription() + " install.");

        FileObject loginConfigXml = targetConfDir.resolveFile("login-config.xml");
        if (loginConfigXml == null || !loginConfigXml.exists()
                || !loginConfigXml.getType().getName().equals(FileType.FILE.getName())) {
            valid = false;
            getPrinter().printErrStatus("JBossHome", "Cannot find login-config.xml");
        }

        if (!valid)
            throw new InstallException(
                    "Target does not seem a " + getTargetPlatform().getDescription() + " install.");

    } catch (IOException e) {
        getPrinter().printErrStatus("JBossHome", e.getMessage());
        throw new InstallException(e.getMessage(), e);
    }

    getPrinter().printOkStatus("JBossHome");
    // TODO : Validate version ?
}

From source file:org.josso.tooling.gshell.install.installer.JBossInstaller.java

public void installApplication(JOSSOArtifact artifact, boolean replace) throws InstallException {
    try {/*from www.jav  a2s.co  m*/
        // If the war is already expanded, copy it with a new name.
        FileObject srcFile = getFileSystemManager().resolveFile(artifact.getLocation());

        // Is this the josso gateaway ?
        String name = artifact.getBaseName();
        boolean isFolder = srcFile.getType().equals(FileType.FOLDER);

        if (artifact.getType().equals("war") && name.startsWith("josso-gateway-web")) {
            // INSTALL GWY
            String newName = "josso.war";

            // Do we have to explode the war ?
            if (getTargetPlatform().isJOSSOWarExploded() && !isFolder) {
                installJar(srcFile, this.targetDeployDir, newName, true, replace);

                if (getTargetPlatform().getVersion().startsWith("6")) {
                    // Under JBoss 6 remove commons logging JAR files which conflict with slf4j replacement
                    FileObject webInfLibFolder = targetDeployDir.resolveFile(newName + "/WEB-INF/lib");

                    boolean exists = webInfLibFolder.exists();

                    if (exists) {
                        FileObject[] sharedLibs = webInfLibFolder.getChildren();
                        for (int i = 0; i < sharedLibs.length; i++) {
                            FileObject jarFile = sharedLibs[i];

                            if (!jarFile.getType().getName().equals(FileType.FILE.getName())) {
                                // ignore folders
                                continue;
                            }
                            if (jarFile.getName().getBaseName().startsWith("commons-logging")
                                    && jarFile.getName().getBaseName().endsWith(".jar")) {
                                jarFile.delete();
                            }
                        }
                    }
                }
            } else {
                installFile(srcFile, this.targetDeployDir, replace);
            }
            return;
        }

        if ((getTargetPlatform().getVersion().startsWith("5")
                || getTargetPlatform().getVersion().startsWith("6")) && artifact.getType().equals("ear")
                && artifact.getBaseName().startsWith("josso-partner-jboss5")) {
            installFile(srcFile, this.targetDeployDir, replace);
            return;
        } else if (!(getTargetPlatform().getVersion().startsWith("5")
                || getTargetPlatform().getVersion().startsWith("6")) && artifact.getType().equals("ear")
                && artifact.getBaseName().startsWith("josso-partner-jboss-app")) {
            installFile(srcFile, this.targetDeployDir, replace);
            return;
        }

        log.debug("Skipping partner application : " + srcFile.getName().getFriendlyURI());

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

From source file:org.josso.tooling.gshell.install.installer.TomcatInstaller.java

@Override
public void validatePlatform() throws InstallException {

    super.validatePlatform();
    try {/*from   w w w .jav  a  2s  .com*/

        boolean valid = true;

        FileObject catalinaJar = targetLibDir.resolveFile("catalina.jar");

        if (catalinaJar == null || !catalinaJar.exists()
                || !catalinaJar.getType().getName().equals(FileType.FILE.getName())) {
            valid = false;
            getPrinter().printErrStatus("CatalinaHome", "Cannot find catalina");
        }

        FileObject serverXml = targetConfDir.resolveFile("server.xml");
        if (serverXml == null || !serverXml.exists()
                || !serverXml.getType().getName().equals(FileType.FILE.getName())) {
            valid = false;
            getPrinter().printErrStatus("CatalinaHome", "Cannot find server.xml");
        }

        // TODO : Validate Version ?

        if (!valid)
            throw new InstallException(
                    "Target does not seem a " + getTargetPlatform().getDescription() + " install.");

    } catch (IOException e) {
        getPrinter().printErrStatus("CatalinaHome", e.getMessage());
        throw new InstallException(e.getMessage(), e);
    }

    getPrinter().printOkStatus("CatalinaHome");
}

From source file:org.josso.tooling.gshell.install.installer.TomcatInstaller.java

@Override
public boolean backupAgentConfigurations(boolean remove) {
    try {//  w w  w.j av  a 2  s .  co m
        super.backupAgentConfigurations(remove);
        // backup jaas.conf
        FileObject jaasConfigFile = targetConfDir.resolveFile("jaas.conf");
        if (jaasConfigFile.exists()) {
            // backup file in the same folder it is installed
            backupFile(jaasConfigFile, jaasConfigFile.getParent());
            if (remove) {
                jaasConfigFile.delete();
            }
        }
        // backup setenv.sh and setenv.bat
        FileObject[] libs = targetBinDir.getChildren();
        for (int i = 0; i < libs.length; i++) {
            FileObject cfgFile = libs[i];

            if (!cfgFile.getType().getName().equals(FileType.FILE.getName())) {
                // ignore folders
                continue;
            }
            if (cfgFile.getName().getBaseName().startsWith("setenv")
                    && (cfgFile.getName().getBaseName().endsWith(".sh")
                            || cfgFile.getName().getBaseName().endsWith(".bat"))) {
                // backup files in the same folder they're installed in
                backupFile(cfgFile, cfgFile.getParent());
                if (remove) {
                    cfgFile.delete();
                }
            }
        }
    } catch (Exception e) {
        getPrinter().printErrStatus("BackupAgentConfigurations", e.getMessage());
        return false;
    }
    return true;
}

From source file:org.josso.tooling.gshell.install.installer.VFSInstaller.java

protected boolean backupFile(FileObject src, FileObject targetDir) {

    try {//from w  ww.ja  v a  2  s.c om
        int attempt = 1;
        FileObject bkp = targetDir.resolveFile(src.getName().getBaseName() + ".bkp." + attempt);

        while (bkp.exists() && attempt < 99) {
            attempt++;
            bkp = targetDir.resolveFile(src.getName().getBaseName() + ".bkp." + attempt);
        }

        if (bkp.exists()) {
            getPrinter().printActionErrStatus("Backup", src.getName().getBaseName(),
                    "Too many backups already");
            return false;
        }

        bkp.copyFrom(src, Selectors.SELECT_SELF);
        getPrinter().printActionOkStatus("Backup", src.getName().getBaseName(), bkp.getName().getFriendlyURI());

        return true;

    } catch (IOException e) {
        getPrinter().printActionErrStatus("Backup", src.getName().getBaseName(), e.getMessage());
        return false;
    }
}

From source file:org.josso.tooling.gshell.install.installer.VFSInstaller.java

public boolean backupAgentConfigurations(boolean remove) {
    try {/* ww  w . j  a va  2s.c o  m*/
        // backup josso-agent-config.xml
        FileObject agentConfigFile = targetJOSSOConfDir.resolveFile("josso-agent-config.xml");
        if (agentConfigFile.exists()) {
            // backup file in the same folder it is installed
            backupFile(agentConfigFile, agentConfigFile.getParent());
            if (remove) {
                agentConfigFile.delete();
            }
        }
    } catch (Exception e) {
        getPrinter().printErrStatus("BackupAgentConfigurations", e.getMessage());
        return false;
    }
    return true;
}

From source file:org.josso.tooling.gshell.install.installer.VFSInstaller.java

public boolean updateAgentConfiguration(String idpHostName, String idpPort, String idpType) {
    if (agentConfigFileInstalled) {
        String hostAndPort = idpHostName;
        if (!idpPort.equals("80")) {
            hostAndPort += ":" + idpPort;
        }// www  .  j a v  a2  s  . c o m

        if (!hostAndPort.equals("localhost:8080") || idpType.equals("atricore-idbus")) {
            try {
                FileObject agentConfigFile = targetJOSSOConfDir.resolveFile("josso-agent-config.xml");
                if (agentConfigFile.exists()) {

                    log.error("DEPRECATED API CALL:updateAgentConfiguration");

                    // Get a DOM document of the josso-agent-config.xml
                    Node configXmlDom = readContentAsDom(agentConfigFile);

                    String gatewayLoginUrl = "http://" + hostAndPort;
                    String gatewayLogoutUrl = "http://" + hostAndPort;

                    // TODO : PB-1 should also be variable!
                    if (idpType.equals("atricore-idbus")) {
                        gatewayLoginUrl += "/IDBUS/BP-1/JOSSO/SSO/REDIR";
                        gatewayLogoutUrl += "/IDBUS/BP-1/JOSSO/SLO/REDIR";
                    } else {
                        gatewayLoginUrl += "/josso/signon/login.do";
                        gatewayLogoutUrl += "/josso/signon/logout.do";
                    }

                    String updateIDP = "<xupdate:update select=\"//gatewayLoginUrl\">" + gatewayLoginUrl
                            + "</xupdate:update>";

                    updateIDP += "<xupdate:update select=\"//gatewayLogoutUrl\">" + gatewayLogoutUrl
                            + "</xupdate:update>";

                    updateIDP += "<xupdate:update select=\"//@endpoint\">" + hostAndPort + "</xupdate:update>";

                    updateIDP += "<xupdate:update select=\"//property[@name='ignoredReferrers']/list/value\">"
                            + "http://" + hostAndPort + "/IDBUS</xupdate:update>";

                    if (idpType.equals("atricore-idbus")) {
                        updateIDP += "<xupdate:append select=\"//protocol:ws-service-locator\">";
                        updateIDP += "<xupdate:attribute name=\"identityManagerServicePath\">IDBUS/BP-1/JOSSO/SSOIdentityManager/SOAP</xupdate:attribute>";
                        updateIDP += "<xupdate:attribute name=\"identityProviderServicePath\">IDBUS/BP-1/JOSSO/SSOIdentityProvider/SOAP</xupdate:attribute>";
                        updateIDP += "<xupdate:attribute name=\"sessionManagerServicePath\">IDBUS/BP-1/JOSSO/SSOSessionManager/SOAP</xupdate:attribute>";
                        updateIDP += "</xupdate:append>";
                    }

                    String updateIDPQryStr = XUpdateUtil.XUPDATE_START + updateIDP + XUpdateUtil.XUPDATE_END;
                    log.debug("XUPDATE QUERY: \n" + updateIDPQryStr);

                    XUpdateQuery updateIDPQry = new XUpdateQueryImpl();
                    updateIDPQry.setQString(updateIDPQryStr);
                    updateIDPQry.execute(configXmlDom);

                    getPrinter().printActionOkStatus("Configure", "IDP type", idpType);
                    getPrinter().printActionOkStatus("Configure", "IDP host and port", hostAndPort);

                    // Write modifications to file
                    writeContentFromDom(configXmlDom, agentConfigFile);
                    getPrinter().printActionOkStatus("Save", agentConfigFile.getName().getBaseName(),
                            agentConfigFile.getName().getFriendlyURI());

                    return true;
                }
            } catch (Exception e) {
                getPrinter().printErrStatus("UpdateAgentConfiguration", e.getMessage());
                return false;
            }
        }
    }

    return false;
}

From source file:org.josso.tooling.gshell.install.installer.VFSInstaller.java

protected boolean installFile(FileObject srcFile, FileObject destDir, String newName, boolean replace)
        throws IOException {

    try {//from  w w w  . j ava 2s .com

        String fname = srcFile.getName().getBaseName();
        if (srcFile == null || !srcFile.exists()) {
            printInstallErrStatus(fname, "Source file not found " + fname);
            throw new IOException("Source file not found " + fname);
        }

        // Validates src and dest files
        if (destDir == null || !destDir.exists()) {
            printInstallErrStatus(fname, "Target directory not found " + destDir.getURL());
            throw new IOException("Target directory not found " + destDir.getURL());
        }

        FileObject destFile = destDir.resolveFile(newName);

        boolean exists = destFile.exists();

        if (!replace && exists) {
            printInstallWarnStatus(fname, "Not replaced (see --replace option)");
            if (fname.equals("josso-agent-config.xml")) {
                agentConfigFileInstalled = false;
            }
            return false;
        }

        FileUtil.copyContent(srcFile, destFile);
        printInstallOkStatus(fname, (exists ? "Replaced " : "Created ") + destFile.getName().getFriendlyURI());

        return true;

    } finally {
        printer.flush();
    }

}

From source file:org.josso.tooling.gshell.install.installer.VFSInstaller.java

/**
 * @param srcFolder//from   w  w w  .  ja v  a2 s.  c  o  m
 * @param destDir
 * @param replace
 * @throws java.io.IOException
 */
protected void installFolder(FileObject srcFolder, FileObject destDir, String newName, boolean replace)
        throws IOException {

    // Check destination folder
    if (!destDir.exists()) {
        printInstallErrStatus(srcFolder.getName().getBaseName(),
                "Target directory not found " + destDir.getName().getBaseName());
        throw new IOException("Target directory not found " + destDir.getName().getBaseName());
    }

    // This is the new folder we're creating in destination.
    FileObject newFolder = destDir.resolveFile(newName);

    boolean exists = newFolder.exists();

    if (!replace && exists) {
        printInstallWarnStatus(newFolder.getName().getBaseName(), "Not replaced (see --replace option)");
        return;
    }

    if (exists) {
        // remove all descendents (removes old jars and other files when upgrading josso gateway)
        newFolder.delete(Selectors.EXCLUDE_SELF);
    }

    newFolder.copyFrom(srcFolder, Selectors.SELECT_ALL); // Copy ALL descendats

    printInstallOkStatus(srcFolder.getName().getBaseName(),
            (exists ? "Replaced " : "Created ") + newFolder.getName().getFriendlyURI());
}