Search in sources :

Example 66 with Server

use of com.sun.enterprise.config.serverbeans.Server in project Payara by payara.

the class OSGiShellCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    ActionReport report = context.getActionReport();
    if (instance != null) {
        Server svr = domain.getServerNamed(instance);
        if (svr == null) {
            report.setMessage("No server target found for " + instance);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
        String host = svr.getAdminHost();
        int port = svr.getAdminPort();
        try {
            ServerRemoteAdminCommand remote = new ServerRemoteAdminCommand(locator, "osgi", host, port, false, "admin", "", log);
            ParameterMap params = new ParameterMap();
            if (commandLine == null) {
                params.set("DEFAULT".toLowerCase(Locale.US), "asadmin-osgi-shell");
            } else if (commandLine instanceof String) {
                params.set("DEFAULT".toLowerCase(Locale.US), (String) commandLine);
            } else if (commandLine instanceof List) {
                params.set("DEFAULT".toLowerCase(Locale.US), (List<String>) commandLine);
            }
            if (sessionOp != null) {
                params.set("session", sessionOp);
            }
            if (sessionId != null) {
                params.set("session-id", sessionId);
            }
            report.setMessage(remote.executeCommand(params));
            report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
            return;
        } catch (CommandException x) {
            report.setMessage("Remote execution failed: " + x.getMessage());
            report.setFailureCause(x);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
    }
    String cmdName = "";
    String cmd = "";
    if (commandLine == null) {
        cmd = "asadmin-osgi-shell";
        cmdName = cmd;
    } else if (commandLine instanceof String) {
        cmd = (String) commandLine;
        cmdName = cmd;
    } else if (commandLine instanceof List) {
        for (Object arg : (List) commandLine) {
            if (cmd.length() == 0) {
                // first arg
                cmd = (String) arg;
                cmdName = cmd;
            } else {
                cmd += " " + (String) arg;
            }
        }
    } else if (commandLine instanceof String[]) {
        for (Object arg : (String[]) commandLine) {
            if (cmd.length() == 0) {
                // first arg
                cmd = (String) arg;
                cmdName = cmd;
            } else {
                cmd += " " + (String) arg;
            }
        }
    } else {
        // shouldn't happen...
        report.setMessage("Unable to deal with argument list of type " + commandLine.getClass().getName());
        report.setActionExitCode(ActionReport.ExitCode.WARNING);
        return;
    }
    // standard output...
    ByteArrayOutputStream bOut = new ByteArrayOutputStream(512);
    PrintStream out = new PrintStream(bOut);
    // error output...
    ByteArrayOutputStream bErr = new ByteArrayOutputStream(512);
    PrintStream err = new PrintStream(bErr);
    try {
        Object shell = null;
        ServiceReference sref = ctx.getServiceReference("org.apache.felix.service.command.CommandProcessor");
        if (sref != null) {
            shell = ctx.getService(sref);
        }
        if (shell == null) {
            // try with felix...
            sref = ctx.getServiceReference("org.apache.felix.shell.ShellService");
            if (sref != null) {
                shell = ctx.getService(sref);
            }
            if (shell == null) {
                report.setMessage("No Shell Service available");
                report.setActionExitCode(ActionReport.ExitCode.WARNING);
                return;
            } else if ("asadmin-osgi-shell".equals(cmdName)) {
                out.println("felix");
            } else {
                ShellService s = (ShellService) shell;
                s.executeCommand(cmd, out, err);
            }
        } else {
            // try with gogo...
            // GLASSFISH-19126 - prepare fake input stream...
            InputStream in = new InputStream() {

                @Override
                public int read() throws IOException {
                    return -1;
                }

                @Override
                public int available() throws IOException {
                    return 0;
                }

                @Override
                public int read(byte[] b) throws IOException {
                    return -1;
                }

                @Override
                public int read(byte[] b, int off, int len) throws IOException {
                    return -1;
                }
            };
            CommandProcessor cp = (CommandProcessor) shell;
            if (sessionOp == null) {
                if ("asadmin-osgi-shell".equals(cmdName)) {
                    out.println("gogo");
                } else {
                    CommandSession session = cp.createSession(in, out, err);
                    session.execute(cmd);
                    session.close();
                }
            } else if ("new".equals(sessionOp)) {
                CommandSession session = cp.createSession(null, null, null);
                RemoteCommandSession remote = new RemoteCommandSession(session);
                log.log(Level.FINE, "Remote session established: {0}", remote.getId());
                sessions.put(remote.getId(), remote);
                out.println(remote.getId());
            } else if ("list".equals(sessionOp)) {
                for (String id : sessions.keySet()) {
                    out.println(id);
                }
            } else if ("execute".equals(sessionOp)) {
                RemoteCommandSession remote = sessions.get(sessionId);
                CommandSession session = remote.attach(in, out, err);
                session.execute(cmd);
                remote.detach();
            } else if ("stop".equals(sessionOp)) {
                RemoteCommandSession remote = sessions.remove(sessionId);
                CommandSession session = remote.attach(in, out, err);
                session.close();
                log.log(Level.FINE, "Remote session closed: {0}", remote.getId());
            }
        }
        out.flush();
        err.flush();
        String output = bOut.toString("UTF-8");
        String errors = bErr.toString("UTF-8");
        report.setMessage(output);
        if (errors.length() > 0) {
            report.setMessage(errors);
            report.setActionExitCode(ActionReport.ExitCode.WARNING);
        } else {
            report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
        }
    } catch (Exception ex) {
        report.setMessage(ex.getMessage());
        report.setActionExitCode(ActionReport.ExitCode.WARNING);
    }
}
Also used : PrintStream(java.io.PrintStream) Server(com.sun.enterprise.config.serverbeans.Server) InputStream(java.io.InputStream) ServerRemoteAdminCommand(com.sun.enterprise.admin.remote.ServerRemoteAdminCommand) ParameterMap(org.glassfish.api.admin.ParameterMap) CommandException(org.glassfish.api.admin.CommandException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ActionReport(org.glassfish.api.ActionReport) RestEndpoint(org.glassfish.api.admin.RestEndpoint) CommandException(org.glassfish.api.admin.CommandException) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference) ShellService(org.apache.felix.shell.ShellService) CommandSession(org.apache.felix.service.command.CommandSession) CommandProcessor(org.apache.felix.service.command.CommandProcessor) List(java.util.List)

Example 67 with Server

use of com.sun.enterprise.config.serverbeans.Server in project Payara by payara.

the class ClusterOperationUtil method replicateCommand.

/**
 * Replicates a given command on the given list of targets, optionally gathering
 * downloaded result payloads from the instance commands into a directory.
 * <p>
 * If intermediateDownloadDir is non-null, then any files returned from
 * the instances in the payload of the HTTP response will be stored in a
 * directory tree like this:
 * <pre>
 * ${intermediateDownloadDir}/
 *     ${instanceA}/
 *         file(s) returned from instance A
 *     ${instanceB}/
 *         file(s) returned from instance B
 *     ...
 * </pre>
 * where ${instanceA}, ${instanceB}, etc. are the names of the instances to
 * which the command was replicated.  This method does no further processing
 * on the downloaded files but leaves that to the calling command.
 */
public static ActionReport.ExitCode replicateCommand(String commandName, FailurePolicy failPolicy, FailurePolicy offlinePolicy, FailurePolicy neverStartedPolicy, Collection<String> targetNames, AdminCommandContext context, ParameterMap parameters, ServiceLocator habitat, File intermediateDownloadDir) {
    ActionReport.ExitCode result = ActionReport.ExitCode.SUCCESS;
    Target targetService = habitat.getService(Target.class);
    for (String t : targetNames) {
        if (CommandTarget.DAS.isValid(habitat, t) || CommandTarget.DOMAIN.isValid(habitat, t))
            continue;
        parameters.set("target", t);
        List<Server> instances = targetService.getInstances(t);
        ActionReport.ExitCode returnValue = replicateCommand(commandName, failPolicy, offlinePolicy, neverStartedPolicy, instances, context, parameters, habitat, intermediateDownloadDir);
        if (!returnValue.equals(ActionReport.ExitCode.SUCCESS)) {
            result = returnValue;
        }
    }
    return result;
}
Also used : Target(org.glassfish.internal.api.Target) CommandTarget(org.glassfish.config.support.CommandTarget) Server(com.sun.enterprise.config.serverbeans.Server) ActionReport(org.glassfish.api.ActionReport)

Example 68 with Server

use of com.sun.enterprise.config.serverbeans.Server in project Payara by payara.

the class AddInstanceToDeploymentGroupCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    Server server = domain.getServerNamed(instanceName);
    ActionReport report = context.getActionReport();
    if (server == null && env.isDas()) {
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage("Instance " + instanceName + " does not exist");
        return;
    }
    DeploymentGroup dg = domain.getDeploymentGroupNamed(deploymentGroup);
    if (dg == null && env.isDas()) {
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage("Deployment Group " + deploymentGroup + " does not exist");
        return;
    }
    // OK set up the reference
    try {
        ConfigSupport.apply((DeploymentGroup dg1) -> {
            DGServerRef ref = dg1.createChild(DGServerRef.class);
            ref.setRef(instanceName);
            dg1.getDGServerRef().add(ref);
            return ref;
        }, dg);
    } catch (TransactionFailure e) {
        report.setMessage("Failed to add instance to deployment group");
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(e);
    }
    // now run the command to add application ref to the instance
    for (ApplicationRef applicationRef : dg.getApplicationRef()) {
        CommandInvocation inv = commandRunner.getCommandInvocation("create-application-ref", report, context.getSubject());
        ParameterMap parameters = new ParameterMap();
        parameters.add("target", instanceName);
        parameters.add("name", applicationRef.getRef());
        parameters.add("virtualservers", applicationRef.getVirtualServers());
        parameters.add("enabled", applicationRef.getEnabled());
        parameters.add("lbenabled", applicationRef.getLbEnabled());
        inv.parameters(parameters).execute();
    }
    // for all resource refs add resource ref to instance
    for (ResourceRef resourceRef : dg.getResourceRef()) {
        CommandInvocation inv = commandRunner.getCommandInvocation("create-resource-ref", report, context.getSubject());
        ParameterMap parameters = new ParameterMap();
        parameters.add("target", instanceName);
        parameters.add("reference_name", resourceRef.getRef());
        parameters.add("enabled", resourceRef.getEnabled());
        inv.parameters(parameters).execute();
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Server(com.sun.enterprise.config.serverbeans.Server) DGServerRef(fish.payara.enterprise.config.serverbeans.DGServerRef) ParameterMap(org.glassfish.api.admin.ParameterMap) ResourceRef(com.sun.enterprise.config.serverbeans.ResourceRef) ActionReport(org.glassfish.api.ActionReport) ApplicationRef(com.sun.enterprise.config.serverbeans.ApplicationRef) DeploymentGroup(fish.payara.enterprise.config.serverbeans.DeploymentGroup) CommandInvocation(org.glassfish.api.admin.CommandRunner.CommandInvocation)

Example 69 with Server

use of com.sun.enterprise.config.serverbeans.Server in project Payara by payara.

the class RemoveInstanceFromDeploymentGroupCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    Server server = domain.getServerNamed(instanceName);
    ActionReport report = context.getActionReport();
    if (server == null) {
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage("Instance " + instanceName + " does not exist");
        return;
    }
    DeploymentGroup dg = domain.getDeploymentGroupNamed(deploymentGroup);
    if (dg == null) {
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage("Deployment Group " + deploymentGroup + " does not exist");
        return;
    }
    DGServerRef ref = dg.getDGServerRefByRef(instanceName);
    if (ref == null) {
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage("Deployment Group " + deploymentGroup + " does not contain server " + instanceName);
        return;
    }
    // OK set up the reference
    try {
        ConfigSupport.apply((DeploymentGroup dg1) -> {
            dg1.getDGServerRef().remove(ref);
            return null;
        }, dg);
    } catch (TransactionFailure e) {
        report.setMessage("Failed to remove instance from the deployment group");
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(e);
    }
    // now run the command to remove application ref to the instance
    for (ApplicationRef applicationRef : dg.getApplicationRef()) {
        CommandRunner.CommandInvocation inv = commandRunner.getCommandInvocation("delete-application-ref", report, context.getSubject());
        ParameterMap parameters = new ParameterMap();
        parameters.add("target", instanceName);
        parameters.add("name", applicationRef.getRef());
        inv.parameters(parameters).execute();
    }
    // now run the command to remove resource ref to the instance
    for (ResourceRef resourceRef : dg.getResourceRef()) {
        CommandRunner.CommandInvocation inv = commandRunner.getCommandInvocation("delete-resource-ref", report, context.getSubject());
        ParameterMap parameters = new ParameterMap();
        parameters.add("target", instanceName);
        parameters.add("reference_name", resourceRef.getRef());
        inv.parameters(parameters).execute();
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Server(com.sun.enterprise.config.serverbeans.Server) DGServerRef(fish.payara.enterprise.config.serverbeans.DGServerRef) ParameterMap(org.glassfish.api.admin.ParameterMap) ResourceRef(com.sun.enterprise.config.serverbeans.ResourceRef) ActionReport(org.glassfish.api.ActionReport) ApplicationRef(com.sun.enterprise.config.serverbeans.ApplicationRef) CommandRunner(org.glassfish.api.admin.CommandRunner) DeploymentGroup(fish.payara.enterprise.config.serverbeans.DeploymentGroup)

Example 70 with Server

use of com.sun.enterprise.config.serverbeans.Server in project Payara by payara.

the class CombinedJavaConfigSystemPropertyListener method changed.

/* force serial behavior; don't allow more than one thread to make a mess here */
@Override
public synchronized UnprocessedChangeEvents changed(PropertyChangeEvent[] events) {
    // ignore a REMOVE and an ADD of the same value
    final UnprocessedChangeEvents unp = ConfigSupport.sortAndDispatch(events, new Changed() {

        @Override
        public <T extends ConfigBeanProxy> NotProcessed changed(TYPE type, Class<T> tc, T t) {
            NotProcessed result = null;
            if (tc == Profiler.class) {
                result = new NotProcessed("Creation or changes to a profiler require restart");
            } else if (tc == Property.class && t.getParent().getClass() == JavaConfig.class) {
                result = new NotProcessed("Addition of properties to JavaConfig requires restart");
            } else if (tc == JavaConfig.class && t instanceof JavaConfig) {
                final JavaConfig njc = (JavaConfig) t;
                logFine(type, njc);
                // we must *always* check the jvm options, no way to know except by comparing,
                // plus we should send an appropriate message back for each removed/added item
                final List<String> curProps = new ArrayList<String>(njc.getJvmOptions());
                final boolean jvmOptionsWereChanged = !oldProps.equals(curProps);
                final List<String> reasons = handle(oldProps, curProps);
                oldProps = curProps;
                // something in the JavaConfig itself changed
                // to do this well, we ought to keep a list of attributes, so we can make a good message
                // saying exactly which attribute what changed
                final Map<String, String> curAttrs = collectAttrs(njc);
                reasons.addAll(handleAttrs(oldAttrs, curAttrs));
                oldAttrs = curAttrs;
                result = reasons.isEmpty() ? null : new NotProcessed(CombinedJavaConfigSystemPropertyListener.toString(reasons));
            } else if (tc == SystemProperty.class && t instanceof SystemProperty) {
                final SystemProperty sp = (SystemProperty) t;
                // check to see if this system property is for this instance
                ConfigBeanProxy proxy = sp.getParent();
                ConfigView p = ConfigSupport.getImpl(proxy);
                if (p == ConfigSupport.getImpl(server) || p == ConfigSupport.getImpl(config) || (cluster != null && p == ConfigSupport.getImpl(cluster)) || p == ConfigSupport.getImpl(domain)) {
                    // check to see if this system property is referenced by any of the options
                    String pname = sp.getName();
                    if (referencesProperty(pname, oldProps) || referencesProperty(pname, oldAttrs.values())) {
                        result = new NotProcessed("The system-property, " + pname + ", that is referenced by the Java configuration, was modified");
                    }
                }
                if (type == TYPE.ADD || type == TYPE.CHANGE) {
                    // create-system-properties
                    if (proxy instanceof Domain) {
                        return addToDomain(sp);
                    } else if (proxy instanceof Config && p == ConfigSupport.getImpl(config)) {
                        return addToConfig(sp);
                    } else if (cluster != null && proxy instanceof Cluster && p == ConfigSupport.getImpl(cluster)) {
                        return addToCluster(sp);
                    } else if (proxy instanceof Server && p == ConfigSupport.getImpl(server)) {
                        return addToServer(sp);
                    }
                } else if (type == TYPE.REMOVE) {
                    if (proxy instanceof Domain) {
                        return removeFromDomain(sp);
                    } else if (proxy instanceof Config && p == ConfigSupport.getImpl(config)) {
                        return removeFromConfig(sp);
                    } else if (cluster != null && proxy instanceof Cluster && p == ConfigSupport.getImpl(cluster)) {
                        return removeFromCluster(sp);
                    } else if (proxy instanceof Server && p == ConfigSupport.getImpl(server)) {
                        return removeFromServer(sp);
                    }
                }
            } else {
            // ignore other changes that are reported
            }
            return result;
        }
    }, logger);
    return unp;
}
Also used : UnprocessedChangeEvents(org.jvnet.hk2.config.UnprocessedChangeEvents) Server(com.sun.enterprise.config.serverbeans.Server) TranslatedConfigView(org.glassfish.config.support.TranslatedConfigView) ConfigView(org.jvnet.hk2.config.ConfigView) JavaConfig(com.sun.enterprise.config.serverbeans.JavaConfig) Config(com.sun.enterprise.config.serverbeans.Config) Cluster(com.sun.enterprise.config.serverbeans.Cluster) SystemProperty(com.sun.enterprise.config.serverbeans.SystemProperty) JavaConfig(com.sun.enterprise.config.serverbeans.JavaConfig) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) Profiler(com.sun.enterprise.config.serverbeans.Profiler) Changed(org.jvnet.hk2.config.Changed) NotProcessed(org.jvnet.hk2.config.NotProcessed) ArrayList(java.util.ArrayList) List(java.util.List) Domain(com.sun.enterprise.config.serverbeans.Domain) TYPE(org.jvnet.hk2.config.Changed.TYPE) Property(org.jvnet.hk2.config.types.Property) SystemProperty(com.sun.enterprise.config.serverbeans.SystemProperty) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Server (com.sun.enterprise.config.serverbeans.Server)86 ActionReport (org.glassfish.api.ActionReport)27 Cluster (com.sun.enterprise.config.serverbeans.Cluster)26 Domain (com.sun.enterprise.config.serverbeans.Domain)16 Config (com.sun.enterprise.config.serverbeans.Config)15 ArrayList (java.util.ArrayList)12 DeploymentGroup (fish.payara.enterprise.config.serverbeans.DeploymentGroup)11 PropertyVetoException (java.beans.PropertyVetoException)11 Properties (java.util.Properties)10 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)10 HashMap (java.util.HashMap)9 ApplicationRef (com.sun.enterprise.config.serverbeans.ApplicationRef)8 File (java.io.File)8 IOException (java.io.IOException)8 Test (org.junit.Test)8 Property (org.jvnet.hk2.config.types.Property)8 Node (com.sun.enterprise.config.serverbeans.Node)7 Map (java.util.Map)7 Logger (java.util.logging.Logger)7 ParameterMap (org.glassfish.api.admin.ParameterMap)7