Search in sources :

Example 1 with TokenResolver

use of com.sun.enterprise.universal.glassfish.TokenResolver in project Payara by payara.

the class UpdateNodeCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    ActionReport report = context.getActionReport();
    Logger logger = context.getLogger();
    Node node = nodes.getNode(name);
    if (node == null) {
        // node doesn't exist
        String msg = Strings.get("noSuchNode", name);
        logger.warning(msg);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage(msg);
        return;
    }
    // validate installdir if passed and running on localhost
    if (StringUtils.ok(nodehost)) {
        if (NetUtils.isThisHostLocal(nodehost) && StringUtils.ok(installdir)) {
            TokenResolver resolver = null;
            // Create a resolver that can replace system properties in strings
            Map<String, String> systemPropsMap = new HashMap<String, String>((Map) (System.getProperties()));
            resolver = new TokenResolver(systemPropsMap);
            String resolvedInstallDir = resolver.resolve(installdir);
            File actualInstallDir = new File(resolvedInstallDir + "/" + NodeUtils.LANDMARK_FILE);
            if (!actualInstallDir.exists()) {
                report.setMessage(Strings.get("invalid.installdir", installdir));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
        }
    }
    // like the install directory or node directory.
    if (node.nodeInUse()) {
        String badparam = null;
        String configNodedir = node.getNodeDir();
        String configInstalldir = node.getInstallDir();
        if (!allowableChange(nodedir, configNodedir)) {
            badparam = "nodedir";
        }
        if (!allowableChange(installdir, configInstalldir)) {
            badparam = "installdir";
        }
        if (StringUtils.ok(badparam)) {
            String msg = Strings.get("noUpdate.nodeInUse", name, badparam);
            logger.warning(msg);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(msg);
            return;
        }
    }
    try {
        updateNodeElement(name);
    } catch (TransactionFailure e) {
        logger.warning("failed.to.update.node " + name);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage(e.getMessage());
    }
}
Also used : TokenResolver(com.sun.enterprise.universal.glassfish.TokenResolver) HashMap(java.util.HashMap) Node(com.sun.enterprise.config.serverbeans.Node) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) File(java.io.File)

Example 2 with TokenResolver

use of com.sun.enterprise.universal.glassfish.TokenResolver in project Payara by payara.

the class GFLauncher method resolveAllTokens.

private void resolveAllTokens() {
    // resolve jvm-options against:
    // 1. itself
    // 2. <system-property>'s from domain.xml
    // 3. system properties -- essential there is, e.g. "${path.separator}" in domain.xml
    // 4. asenvProps
    // 5. env variables
    // i.e. add in reverse order to get the precedence right
    Map<String, String> all = new HashMap<String, String>();
    Map<String, String> envProps = System.getenv();
    Map<String, String> sysProps = CollectionUtils.propertiesToStringMap(System.getProperties());
    // TODO: Uncomment when admin password processing & aliasing is sorted out.
    // Map<String, String> passwordAliases = new HashMap<String, String>();
    // try {
    // String masterPassword = "changeit";
    // if (IdentityManager.getMasterPassword() != null)
    // masterPassword = IdentityManager.getMasterPassword();
    // PasswordAdapter pa = new PasswordAdapter(masterPassword.toCharArray());
    // Enumeration e = pa.getAliases();
    // if (e.hasMoreElements()) {
    // String alias = (String) e.nextElement();
    // passwordAliases.put(alias, pa.getPasswordForAlias(alias));
    // }
    // } catch (Exception e) {
    // // TODO: ignore now. Defaults to not resolving password aliases
    // }
    // all.putAll(passwordAliases);
    all.putAll(envProps);
    all.putAll(asenvProps);
    all.putAll(sysProps);
    all.put(SystemPropertyConstants.SERVER_NAME, getInfo().getInstanceName());
    all.putAll(sysPropsFromXml);
    all.putAll(jvmOptions.getCombinedMap());
    all.putAll(profiler.getConfig());
    TokenResolver resolver = new TokenResolver(all);
    resolver.resolve(jvmOptions.xProps);
    resolver.resolve(jvmOptions.xxProps);
    resolver.resolve(jvmOptions.plainProps);
    resolver.resolve(jvmOptions.sysProps);
    resolver.resolve(javaConfig.getMap());
    resolver.resolve(profiler.getConfig());
    resolver.resolve(debugOptions);
    // resolver.resolve(sysPropsFromXml);
    logFilename = resolver.resolve(logFilename);
    adminFileRealmKeyFile = resolver.resolve(adminFileRealmKeyFile);
// TODO ?? Resolve sysPropsFromXml ???
}
Also used : TokenResolver(com.sun.enterprise.universal.glassfish.TokenResolver)

Example 3 with TokenResolver

use of com.sun.enterprise.universal.glassfish.TokenResolver in project Payara by payara.

the class AppClientFacade method replaceTokensForParsing.

private static InputSource replaceTokensForParsing(final Reader reader) throws FileNotFoundException, IOException, URISyntaxException {
    char[] buffer = new char[1024];
    CharArrayWriter writer = new CharArrayWriter();
    int charsRead;
    while ((charsRead = reader.read(buffer)) != -1) {
        writer.write(buffer, 0, charsRead);
    }
    writer.close();
    reader.close();
    Map<String, String> mapping = new HashMap<String, String>();
    Properties props = System.getProperties();
    for (Enumeration e = props.propertyNames(); e.hasMoreElements(); ) {
        String propName = (String) e.nextElement();
        mapping.put(propName, props.getProperty(propName));
    }
    TokenResolver resolver = new TokenResolver(mapping);
    String configWithTokensReplaced = resolver.resolve(writer.toString());
    InputSource inputSource = new InputSource(new StringReader(configWithTokensReplaced));
    return inputSource;
}
Also used : TokenResolver(com.sun.enterprise.universal.glassfish.TokenResolver) InputSource(org.xml.sax.InputSource) Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) StringReader(java.io.StringReader) Properties(java.util.Properties) CharArrayWriter(java.io.CharArrayWriter)

Example 4 with TokenResolver

use of com.sun.enterprise.universal.glassfish.TokenResolver in project Payara by payara.

the class CreateNodeConfigCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    ActionReport report = context.getActionReport();
    // validate installdir if passed and running on localhost
    if (StringUtils.ok(nodehost)) {
        if (NetUtils.isThisHostLocal(nodehost) && StringUtils.ok(installdir)) {
            TokenResolver resolver = null;
            // Create a resolver that can replace system properties in strings
            Map<String, String> systemPropsMap = new HashMap<String, String>((Map) (System.getProperties()));
            resolver = new TokenResolver(systemPropsMap);
            String resolvedInstallDir = resolver.resolve(installdir);
            File actualInstallDir = new File(resolvedInstallDir + "/" + NodeUtils.LANDMARK_FILE);
            if (!actualInstallDir.exists()) {
                report.setMessage(Strings.get("invalid.installdir", installdir));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
        }
    }
    CommandInvocation ci = cr.getCommandInvocation("_create-node", report, context.getSubject());
    ParameterMap map = new ParameterMap();
    map.add("DEFAULT", name);
    if (StringUtils.ok(nodedir))
        map.add(NodeUtils.PARAM_NODEDIR, nodedir);
    if (StringUtils.ok(installdir))
        map.add(NodeUtils.PARAM_INSTALLDIR, installdir);
    if (StringUtils.ok(nodehost))
        map.add(NodeUtils.PARAM_NODEHOST, nodehost);
    map.add(NodeUtils.PARAM_TYPE, "CONFIG");
    ci.parameters(map);
    ci.execute();
    NodeUtils.sanitizeReport(report);
}
Also used : TokenResolver(com.sun.enterprise.universal.glassfish.TokenResolver) HashMap(java.util.HashMap) ActionReport(org.glassfish.api.ActionReport) File(java.io.File) CommandInvocation(org.glassfish.api.admin.CommandRunner.CommandInvocation)

Example 5 with TokenResolver

use of com.sun.enterprise.universal.glassfish.TokenResolver in project Payara by payara.

the class UpdateNodeCommand method allowableChange.

/**
 * If the node is in use, is it OK to change currentvalue to newvalue?
 */
private static boolean allowableChange(String newvalue, String currentvalue) {
    // If the new value is not specified, then we aren't changing anything
    if (newvalue == null) {
        return true;
    }
    // that was created without one.
    if (!StringUtils.ok(currentvalue)) {
        return true;
    }
    // If the values are the same, then we aren't changing anything.
    if (newvalue.equals(currentvalue)) {
        return true;
    }
    if (newvalue.contains("$") || currentvalue.contains("$")) {
        // One or both of the values may contain an unexpanded
        // property. Expand them then compare
        Map<String, String> systemPropsMap = new HashMap<String, String>((Map) (System.getProperties()));
        TokenResolver resolver = new TokenResolver(systemPropsMap);
        newvalue = resolver.resolve(newvalue);
        currentvalue = resolver.resolve(currentvalue);
        return newvalue.equals(currentvalue);
    }
    // Values don't match.
    return false;
}
Also used : TokenResolver(com.sun.enterprise.universal.glassfish.TokenResolver) HashMap(java.util.HashMap)

Aggregations

TokenResolver (com.sun.enterprise.universal.glassfish.TokenResolver)5 HashMap (java.util.HashMap)4 File (java.io.File)2 ActionReport (org.glassfish.api.ActionReport)2 Node (com.sun.enterprise.config.serverbeans.Node)1 CharArrayWriter (java.io.CharArrayWriter)1 StringReader (java.io.StringReader)1 Enumeration (java.util.Enumeration)1 Properties (java.util.Properties)1 Logger (java.util.logging.Logger)1 CommandInvocation (org.glassfish.api.admin.CommandRunner.CommandInvocation)1 InputSource (org.xml.sax.InputSource)1