Example usage for org.apache.commons.lang StringUtils stripEnd

List of usage examples for org.apache.commons.lang StringUtils stripEnd

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils stripEnd.

Prototype

public static String stripEnd(String str, String stripChars) 

Source Link

Document

Strips any of a set of characters from the end of a String.

Usage

From source file:com.enonic.cms.upgrade.task.datasource.DataSourceConverterUpgradeModel206.java

private String getStrippedElExpression(String expression) {
    expression = StringUtils.stripStart(expression, "${");
    expression = StringUtils.stripEnd(expression, "}");
    return expression;
}

From source file:com.microsoft.alm.plugin.external.utils.WorkspaceHelper.java

/**
 * This metho appends the one level mapping suffix to the server path.
 *//*from   ww  w .  j a v a2s . c  o  m*/
public static String getOneLevelServerPath(final String serverPath) {
    if (StringUtils.isEmpty(serverPath)) {
        // This is a strange case, but it seems correct to simply return the suffix
        return ONE_LEVEL_MAPPING_SUFFIX;
    }

    if (!isOneLevelMapping(serverPath)) {
        // remove any remaining /'s and add the /*
        return StringUtils.stripEnd(serverPath, "/") + ONE_LEVEL_MAPPING_SUFFIX;
    }

    // It already has the /* at the end
    return serverPath;
}

From source file:com.cloud.baremetal.networkservice.BaremetalKickStartPxeResource.java

private Answer execute(VmDataCommand cmd) {
    com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(_ip, 22);
    try {//from  w  ww  .  j  a  v a  2 s .  c o  m
        List<String[]> vmData = cmd.getVmData();
        StringBuilder sb = new StringBuilder();
        for (String[] data : vmData) {
            String folder = data[0];
            String file = data[1];
            String contents = (data[2] == null) ? "none" : data[2];
            sb.append(cmd.getVmIpAddress());
            sb.append(",");
            sb.append(folder);
            sb.append(",");
            sb.append(file);
            sb.append(",");
            sb.append(contents);
            sb.append(";");
        }
        String arg = StringUtils.stripEnd(sb.toString(), ";");

        sshConnection.connect(null, 60000, 60000);
        if (!sshConnection.authenticateWithPassword(_username, _password)) {
            s_logger.debug("SSH Failed to authenticate");
            throw new ConfigurationException(
                    String.format("Cannot connect to PING PXE server(IP=%1$s, username=%2$s, password=%3$s",
                            _ip, _username, _password));
        }

        String script = String.format("python /usr/bin/baremetal_user_data.py '%s'", arg);
        if (!SSHCmdHelper.sshExecuteCmd(sshConnection, script)) {
            return new Answer(cmd, false, "Failed to add user data, command:" + script);
        }

        return new Answer(cmd, true, "Success");
    } catch (Exception e) {
        s_logger.debug("Prepare for creating baremetal template failed", e);
        return new Answer(cmd, false, e.getMessage());
    } finally {
        if (sshConnection != null) {
            sshConnection.close();
        }
    }
}

From source file:com.squid.core.jdbc.formatter.DefaultJDBCDataFormatter.java

public Object unboxJDBCObject(final Object jdbcObject, final int colType) throws SQLException {
    if (colType == Types.CHAR && jdbcObject != null) {
        return StringUtils.stripEnd((String) jdbcObject, " ");
    }/*from w w  w .  j a va  2 s  .co m*/
    return jdbcObject;
}

From source file:com.zotoh.maedr.etc.AppRunner.java

@SuppressWarnings("serial")
private static boolean parseArgs(String[] args) throws Exception {

    if (args.length < 2) {
        return false;
    }/*from   w  ww  .  j ava2s.c  om*/

    inizBundle();

    String home = StringUtils.stripEnd(niceFPath(new File(args[0])), "/");
    final File h = new File(home);
    final File cwd = getCWD();
    List<Cmdline> cmds = new ArrayList<Cmdline>() {
        {
            add(new CmdSamples(h, cwd));
            add(new CmdCrypto(h, cwd));
            add(new CmdCloud(h, cwd));
            add(new CmdDevice(h, cwd));
            add(new CmdAppOps(h, cwd));
            add(new CmdMiscOps(h, cwd));
        }
    };

    for (Cmdline c : cmds) {
        if (c.getCmds().contains(args[1])) {
            c.eval(Arrays.copyOfRange(args, 1, args.length));
            return true;
        }
    }

    return false;
}

From source file:com.cloud.hypervisor.xen.resource.Xenserver625StorageProcessor.java

@Override
public Answer copyTemplateToPrimaryStorage(CopyCommand cmd) {
    DataTO srcData = cmd.getSrcTO();/*from w  w w  . j  a  va  2s. com*/
    DataTO destData = cmd.getDestTO();
    int wait = cmd.getWait();
    DataStoreTO srcStore = srcData.getDataStore();
    Connection conn = hypervisorResource.getConnection();
    SR srcSr = null;
    Task task = null;

    try {
        if ((srcStore instanceof NfsTO) && (srcData.getObjectType() == DataObjectType.TEMPLATE)) {
            NfsTO srcImageStore = (NfsTO) srcStore;
            TemplateObjectTO srcTemplate = (TemplateObjectTO) srcData;
            String storeUrl = srcImageStore.getUrl();
            URI uri = new URI(storeUrl);
            String volumePath = srcData.getPath();

            volumePath = StringUtils.stripEnd(volumePath, "/");

            String[] splits = volumePath.split("/");
            String volumeDirectory = volumePath;

            if (splits.length > 4) {
                //"template/tmpl/dcid/templateId/templatename"
                int index = volumePath.lastIndexOf("/");

                volumeDirectory = volumePath.substring(0, index);
            }

            srcSr = createFileSr(conn, uri.getHost() + ":" + uri.getPath(), volumeDirectory);

            Set<VDI> setVdis = srcSr.getVDIs(conn);

            if (setVdis.size() != 1) {
                return new CopyCmdAnswer("Can't find template VDI under: " + uri.getHost() + ":" + uri.getPath()
                        + "/" + volumeDirectory);
            }

            VDI srcVdi = setVdis.iterator().next();

            boolean managed = false;
            String storageHost = null;
            String managedStoragePoolName = null;
            String managedStoragePoolRootVolumeName = null;
            String managedStoragePoolRootVolumeSize = null;
            String chapInitiatorUsername = null;
            String chapInitiatorSecret = null;

            PrimaryDataStoreTO destStore = (PrimaryDataStoreTO) destData.getDataStore();

            Map<String, String> details = destStore.getDetails();

            if (details != null) {
                managed = Boolean.parseBoolean(details.get(PrimaryDataStoreTO.MANAGED));

                if (managed) {
                    storageHost = details.get(PrimaryDataStoreTO.STORAGE_HOST);
                    managedStoragePoolName = details.get(PrimaryDataStoreTO.MANAGED_STORE_TARGET);
                    managedStoragePoolRootVolumeName = details
                            .get(PrimaryDataStoreTO.MANAGED_STORE_TARGET_ROOT_VOLUME);
                    managedStoragePoolRootVolumeSize = details.get(PrimaryDataStoreTO.VOLUME_SIZE);
                    chapInitiatorUsername = details.get(PrimaryDataStoreTO.CHAP_INITIATOR_USERNAME);
                    chapInitiatorSecret = details.get(PrimaryDataStoreTO.CHAP_INITIATOR_SECRET);
                }
            }

            final SR destSr;

            if (managed) {
                details = new HashMap<String, String>();

                details.put(DiskTO.STORAGE_HOST, storageHost);
                details.put(DiskTO.IQN, managedStoragePoolName);
                details.put(DiskTO.VOLUME_SIZE, managedStoragePoolRootVolumeSize);
                details.put(DiskTO.CHAP_INITIATOR_USERNAME, chapInitiatorUsername);
                details.put(DiskTO.CHAP_INITIATOR_SECRET, chapInitiatorSecret);

                destSr = hypervisorResource.prepareManagedSr(conn, details);
            } else {
                String srName = destStore.getUuid();
                Set<SR> srs = SR.getByNameLabel(conn, srName);

                if (srs.size() != 1) {
                    String msg = "There are " + srs.size() + " SRs with same name: " + srName;

                    s_logger.warn(msg);

                    return new CopyCmdAnswer(msg);
                } else {
                    destSr = srs.iterator().next();
                }
            }

            task = srcVdi.copyAsync2(conn, destSr, null, null);

            // poll every 1 seconds ,
            hypervisorResource.waitForTask(conn, task, 1000, wait * 1000);
            hypervisorResource.checkForSuccess(conn, task);

            VDI tmplVdi = Types.toVDI(task, conn);

            final String uuidToReturn;

            if (managed) {
                uuidToReturn = tmplVdi.getUuid(conn);

                tmplVdi.setNameLabel(conn, managedStoragePoolRootVolumeName);
            } else {
                VDI snapshotVdi = tmplVdi.snapshot(conn, new HashMap<String, String>());

                uuidToReturn = snapshotVdi.getUuid(conn);

                snapshotVdi.setNameLabel(conn, "Template " + srcTemplate.getName());

                tmplVdi.destroy(conn);
            }

            destSr.scan(conn);

            try {
                Thread.sleep(5000);
            } catch (Exception e) {
            }

            TemplateObjectTO newVol = new TemplateObjectTO();

            newVol.setUuid(uuidToReturn);
            newVol.setPath(uuidToReturn);
            newVol.setFormat(Storage.ImageFormat.VHD);

            return new CopyCmdAnswer(newVol);
        }
    } catch (Exception e) {
        String msg = "Catch Exception " + e.getClass().getName() + " for template due to " + e.toString();

        s_logger.warn(msg, e);

        return new CopyCmdAnswer(msg);
    } finally {
        if (task != null) {
            try {
                task.destroy(conn);
            } catch (Exception e) {
                s_logger.debug("unable to destroy task (" + task.toWireString() + ") due to " + e.toString());
            }
        }

        if (srcSr != null) {
            hypervisorResource.removeSR(conn, srcSr);
        }
    }

    return new CopyCmdAnswer("not implemented yet");
}

From source file:com.fiveamsolutions.nci.commons.web.struts2.interceptor.ParameterTrimInterceptor.java

private String truncate(String input) {
    String result = this.trimFromFront ? StringUtils.strip(input) : StringUtils.stripEnd(input, null);
    if (this.trimToNull && StringUtils.isEmpty(result)) {
        result = null;/*from www.  jav  a2 s.  c  om*/
    }
    return result;
}

From source file:com.dream.messaging.engine.fixformat.FixformatDataHandler.java

@Override
public Object createValue(Object msgData, Node node) throws DataConvertException {

    Object result = null;//from  w  ww .  j a  va  2  s .c  om

    if (msgData instanceof byte[]) {
        // parser the byte[] to a String which presents the object value
        String clsname = QueryNode.getAttribute(node, ElementAttr.Attr_Class);

        boolean isNumber = this.isNumber(clsname);

        String encoding = (String) this.engine.getProperties().get(FixformatMessageEngine.ENCORDING);
        String type = isNumber ? FixformatMessageEngine.NUMBER : FixformatMessageEngine.TEXT;
        boolean fromLeft = Boolean.getBoolean(
                (String) this.engine.getProperties().get(type + FixformatMessageEngine.START_FROM_LEFT));
        String fillUpWith = (String) this.engine.getProperties()
                .get(type + FixformatMessageEngine.FILL_UP_WITH);

        String aString = null;
        try {
            aString = new String((byte[]) msgData, encoding);
        } catch (UnsupportedEncodingException e) {
            throw new DataConvertException("encoding " + encoding + " is not supported,please check it.", e);
        }

        // remove fill up
        if (fromLeft) {
            aString = StringUtils.stripEnd(aString, fillUpWith);
        } else {
            aString = StringUtils.stripStart(aString, fillUpWith);
        }

        // check if the string empty
        if (StringUtils.isEmpty(aString)) {
            if (isNumber) {
                aString = "0";
            } else {
                aString = "";
            }

            return this.createValue(aString, node);
        }

        if (isNumber) {
            // remove sign
            String sign = "";
            if (aString.equals("+") || aString.equals("-")) {
                aString = aString + "0";
            }
            if (aString.endsWith("+") || aString.endsWith("-")) {
                sign = aString.substring(aString.length() - 1);
                aString = sign + aString.substring(0, aString.length() - 1);
            }

            if (clsname.equalsIgnoreCase(ElementClassType.CLASS_DECIMAL)
                    || clsname.equalsIgnoreCase(ElementClassType.CLASS_FLOAT)
                    || (clsname.equalsIgnoreCase(ElementClassType.CLASS_DOUBLE))) {
                String format = QueryNode.getAttribute(node, ElementAttr.Attr_Format);
                int index = format.indexOf('p');
                if (index < 0) {
                    index = format.indexOf('P');
                }
                int fractionLen = Integer.valueOf(format.substring(index + 1));
                if (aString.length() - fractionLen > 0) {
                    aString = aString.substring(0, aString.length() - fractionLen) + "."
                            + aString.substring(aString.length() - fractionLen);
                } else {
                    StringBuffer sb = new StringBuffer(aString);
                    while (sb.length() < fractionLen) {
                        sb.insert(0, fillUpWith);
                    }
                    aString = sb.toString();
                    aString = "0." + aString;
                }

            }
        }
        result = this.createValue(aString, node);
    } else {
        throw new UnsupportedOperationException("The object for createValue must be a byte array");
    }

    return result;

}

From source file:io.hawkcd.services.FileManagementService.java

@Override
public String urlCombine(String... args) {

    String output = null;//from  w w  w.j a v  a  2  s .  c  o m

    StringBuilder argsHolder = new StringBuilder();
    for (String arg : args) {
        arg = this.normalizePath(arg);
        argsHolder.append(arg);
        argsHolder.append("/");
    }
    output = argsHolder.toString();
    output = StringUtils.stripEnd(output, "/");

    return output;
}

From source file:com.cloud.hypervisor.xenserver.resource.Xenserver625StorageProcessor.java

@Override
public Answer copyTemplateToPrimaryStorage(final CopyCommand cmd) {
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    final int wait = cmd.getWait();
    final DataStoreTO srcStore = srcData.getDataStore();
    final Connection conn = hypervisorResource.getConnection();
    SR srcSr = null;//from  w ww. j a v  a 2 s.  c  o m
    SR destSr = null;
    boolean removeSrAfterCopy = false;
    Task task = null;

    try {
        if (srcStore instanceof NfsTO && srcData.getObjectType() == DataObjectType.TEMPLATE) {
            final NfsTO srcImageStore = (NfsTO) srcStore;
            final TemplateObjectTO srcTemplate = (TemplateObjectTO) srcData;
            final String storeUrl = srcImageStore.getUrl();
            final URI uri = new URI(storeUrl);
            String volumePath = srcData.getPath();

            volumePath = StringUtils.stripEnd(volumePath, "/");

            final String[] splits = volumePath.split("/");
            String volumeDirectory = volumePath;

            if (splits.length > 4) {
                // "template/tmpl/dcid/templateId/templatename"
                final int index = volumePath.lastIndexOf("/");

                volumeDirectory = volumePath.substring(0, index);
            }

            srcSr = createFileSr(conn, uri.getHost() + ":" + uri.getPath(), volumeDirectory);

            final Set<VDI> setVdis = srcSr.getVDIs(conn);

            if (setVdis.size() != 1) {
                return new CopyCmdAnswer("Expected 1 VDI template, but found " + setVdis.size()
                        + " VDI templates on: " + uri.getHost() + ":" + uri.getPath() + "/" + volumeDirectory);
            }

            final VDI srcVdi = setVdis.iterator().next();

            boolean managed = false;
            String storageHost = null;
            String managedStoragePoolName = null;
            String managedStoragePoolRootVolumeName = null;
            String managedStoragePoolRootVolumeSize = null;
            String chapInitiatorUsername = null;
            String chapInitiatorSecret = null;

            final PrimaryDataStoreTO destStore = (PrimaryDataStoreTO) destData.getDataStore();

            Map<String, String> details = destStore.getDetails();

            if (details != null) {
                managed = Boolean.parseBoolean(details.get(PrimaryDataStoreTO.MANAGED));

                if (managed) {
                    storageHost = details.get(PrimaryDataStoreTO.STORAGE_HOST);
                    managedStoragePoolName = details.get(PrimaryDataStoreTO.MANAGED_STORE_TARGET);
                    managedStoragePoolRootVolumeName = details
                            .get(PrimaryDataStoreTO.MANAGED_STORE_TARGET_ROOT_VOLUME);
                    managedStoragePoolRootVolumeSize = details.get(PrimaryDataStoreTO.VOLUME_SIZE);
                    chapInitiatorUsername = details.get(PrimaryDataStoreTO.CHAP_INITIATOR_USERNAME);
                    chapInitiatorSecret = details.get(PrimaryDataStoreTO.CHAP_INITIATOR_SECRET);
                    removeSrAfterCopy = Boolean.parseBoolean(details.get(PrimaryDataStoreTO.REMOVE_AFTER_COPY));
                }
            }

            if (managed) {
                details = new HashMap<String, String>();

                details.put(DiskTO.STORAGE_HOST, storageHost);
                details.put(DiskTO.IQN, managedStoragePoolName);
                details.put(DiskTO.VOLUME_SIZE, managedStoragePoolRootVolumeSize);
                details.put(DiskTO.CHAP_INITIATOR_USERNAME, chapInitiatorUsername);
                details.put(DiskTO.CHAP_INITIATOR_SECRET, chapInitiatorSecret);

                destSr = hypervisorResource.prepareManagedSr(conn, details);
            } else {
                final String srName = destStore.getUuid();
                final Set<SR> srs = SR.getByNameLabel(conn, srName);

                if (srs.size() != 1) {
                    final String msg = "There are " + srs.size() + " SRs with same name: " + srName;

                    s_logger.warn(msg);

                    return new CopyCmdAnswer(msg);
                } else {
                    destSr = srs.iterator().next();
                }
            }

            task = srcVdi.copyAsync(conn, destSr, null, null);

            // poll every 1 seconds ,
            hypervisorResource.waitForTask(conn, task, 1000, wait * 1000);
            hypervisorResource.checkForSuccess(conn, task);

            final VDI tmplVdi = Types.toVDI(task, conn);

            final String uuidToReturn;
            final Long physicalSize = tmplVdi.getPhysicalUtilisation(conn);

            if (managed) {
                uuidToReturn = tmplVdi.getUuid(conn);

                tmplVdi.setNameLabel(conn, managedStoragePoolRootVolumeName);
            } else {
                final VDI snapshotVdi = tmplVdi.snapshot(conn, new HashMap<String, String>());

                uuidToReturn = snapshotVdi.getUuid(conn);

                snapshotVdi.setNameLabel(conn, "Template " + srcTemplate.getName());

                tmplVdi.destroy(conn);
            }

            destSr.scan(conn);

            try {
                Thread.sleep(5000);
            } catch (final Exception e) {
            }

            final TemplateObjectTO newVol = new TemplateObjectTO();

            newVol.setUuid(uuidToReturn);
            newVol.setPath(uuidToReturn);

            if (physicalSize != null) {
                newVol.setSize(physicalSize);
            }

            newVol.setFormat(Storage.ImageFormat.VHD);

            return new CopyCmdAnswer(newVol);
        }
    } catch (final Exception e) {
        final String msg = "Catch Exception " + e.getClass().getName() + " for template due to " + e.toString();

        s_logger.warn(msg, e);

        return new CopyCmdAnswer(msg);
    } finally {
        if (task != null) {
            try {
                task.destroy(conn);
            } catch (final Exception e) {
                s_logger.debug("unable to destroy task (" + task.toWireString() + ") due to " + e.toString());
            }
        }

        if (srcSr != null) {
            hypervisorResource.removeSR(conn, srcSr);
        }

        if (removeSrAfterCopy && destSr != null) {
            hypervisorResource.removeSR(conn, destSr);
        }
    }

    return new CopyCmdAnswer("not implemented yet");
}