com.sun.enterprise.v3.admin.cluster.ListInstancesCommand Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of payara-micro Show documentation
Show all versions of payara-micro Show documentation
Micro Distribution of the Payara Project
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2010-2013 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2018-2023] [Payara Foundation and/or its affiliates]
package com.sun.enterprise.v3.admin.cluster;
import com.sun.enterprise.admin.util.InstanceStateService;
import com.sun.enterprise.admin.util.RemoteInstanceCommandHelper;
import com.sun.enterprise.config.serverbeans.*;
import com.sun.enterprise.util.StringUtils;
import com.sun.enterprise.util.SystemPropertyConstants;
import com.sun.enterprise.util.cluster.InstanceInfo;
import static com.sun.enterprise.v3.admin.cluster.Constants.*;
import fish.payara.enterprise.config.serverbeans.DeploymentGroup;
import java.util.*;
import java.util.logging.*;
import jakarta.inject.Inject;
import org.glassfish.api.ActionReport;
import org.glassfish.api.ActionReport.ExitCode;
import org.glassfish.api.I18n;
import org.glassfish.api.Param;
import org.glassfish.api.admin.*;
import org.glassfish.api.admin.config.ReferenceContainer;
import org.glassfish.hk2.api.PerLookup;
import org.glassfish.hk2.api.ServiceLocator;
import org.jvnet.hk2.annotations.Service;
/**
* AdminCommand to list all instances and their states
*
* This is so clumsy & hard to remember I leave it here as a comment:
* @Inject(name = ServerEnvironment.DEFAULT_INSTANCE_NAME)
* @author Byron Nevins
*/
@org.glassfish.api.admin.ExecuteOn(RuntimeType.DAS)
@Service(name = "list-instances")
@CommandLock(CommandLock.LockType.NONE)
@I18n("list.instances.command")
@PerLookup
@RestEndpoints({
@RestEndpoint(configBean=Cluster.class,
opType=RestEndpoint.OpType.GET,
path="list-instances",
description="List Cluster Instances",
params={
@RestParam(name="id", value="$parent")
}),
@RestEndpoint(configBean=Domain.class,
opType=RestEndpoint.OpType.GET,
path="list-instances",
description="List Instances"),
@RestEndpoint(configBean=DeploymentGroup.class,
opType=RestEndpoint.OpType.GET,
path="list-instances",
description="List Cluster Instances",
params={
@RestParam(name="id", value="$parent")
})
})
public class ListInstancesCommand implements AdminCommand {
@Inject
private ServiceLocator habitat;
@Inject
private Domain domain;
@Inject
private ServerEnvironment env;
@Inject
private Servers allServers;
@Inject
InstanceStateService stateService;
@Param(optional = true, defaultValue = "false", name = "long", shortName = "l")
private boolean long_opt;
@Param(optional = true, defaultValue = "-1")
private String timeoutmsec;
@Param(optional = true, defaultValue = "false")
private boolean standaloneonly;
@Param(optional = true, defaultValue = "false")
private boolean nostatus;
// We are setting the whichTarget to an empty String because FindBugs - LL
// does not understand that HK2 is going to set it to a Sting for us later.
// This garbage empty String will be replaced by HK2 soon...
@Param(optional = true, primary = true, defaultValue = "domain")
String whichTarget = "";
private List infos = new LinkedList<>();
private List serverList;
private ActionReport report;
private ActionReport.MessagePart top = null;
private static final String EOL = "\n";
private static final String READ_TIMEOUT_KEY = "AS_ADMIN_READTIMEOUT";
private static final String LIST_INSTANCES_TIMEOUT_KEY = "AS_ADMIN_LISTINSTANCESTIMEOUT";
private int getDefaultTimeout() {
String ct = System.getProperty(LIST_INSTANCES_TIMEOUT_KEY);
if (ct == null) {
ct = System.getenv(LIST_INSTANCES_TIMEOUT_KEY);
}
if (ct == null) {
ct = System.getProperty(READ_TIMEOUT_KEY);
}
if (ct == null) {
ct = System.getenv(READ_TIMEOUT_KEY);
}
if (ct != null) {
return Integer.parseInt(ct);
} else {
return 10 * 1000; // 10 seconds
}
}
@Override
public void execute(AdminCommandContext context) {
// setup
int timeoutInMsec;
try {
timeoutInMsec = Integer.parseInt(timeoutmsec);
if (timeoutInMsec < 0) {
timeoutInMsec = getDefaultTimeout();
}
}
catch (Exception e) {
timeoutInMsec = getDefaultTimeout();
}
report = context.getActionReport();
top = report.getTopMessagePart();
Logger logger = context.getLogger();
if (!validateParams()) {
return;
}
serverList = createServerList();
if (serverList == null) {
fail(Strings.get("list.instances.badTarget", whichTarget ));
return;
}
// Require that we be a DAS
if (!env.isDas()) {
String msg = Strings.get("list.instances.onlyRunsOnDas");
logger.warning(msg);
fail(msg);
return;
}
if (nostatus) {
noStatus(serverList);
}
else {
yesStatus(serverList, timeoutInMsec, logger);
}
report.setActionExitCode(ExitCode.SUCCESS);
}
private void noStatus(List serverList) {
if (serverList.isEmpty()) {
report.setMessage(NONE);
return;
}
StringBuilder sb = new StringBuilder();
boolean firstServer = true;
Properties extraProps = new Properties();
List