forked from eclipse-ee4j/glassfish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractListCommandProxy.java
155 lines (131 loc) · 5.48 KB
/
AbstractListCommandProxy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.batch;
import com.sun.enterprise.config.serverbeans.Server;
import com.sun.enterprise.util.SystemPropertyConstants;
import org.glassfish.api.ActionReport;
import org.glassfish.api.Param;
import org.glassfish.api.admin.*;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.internal.api.Target;
import jakarta.inject.Inject;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Command to list batch jobs info
*
* 1 * 1 *
* jobName --------> instanceId --------> executionId
*
* @author Mahesh Kannan
*/
public abstract class AbstractListCommandProxy
implements AdminCommand {
@Inject
protected ServiceLocator serviceLocator;
@Inject
protected Target targetUtil;
@Inject
protected Logger logger;
@Param(name = "terse", optional=true, defaultValue="false", shortName="t")
protected boolean isTerse = false;
@Param(name = "output", shortName = "o", optional = true)
protected String outputHeaderList;
@Param(name = "header", shortName = "h", optional = true)
protected boolean header;
@Param(name = "target", optional = true, defaultValue = SystemPropertyConstants.DEFAULT_SERVER_INSTANCE_NAME)
protected String target;
@Param(name = "long", shortName = "l", optional = true)
protected boolean useLongFormat;
protected ActionReport.ExitCode commandsExitCode = ActionReport.ExitCode.SUCCESS;
@Override
public final void execute(AdminCommandContext context) {
ActionReport actionReport = context.getActionReport();
Properties extraProperties = actionReport.getExtraProperties();
if (extraProperties == null) {
extraProperties = new Properties();
actionReport.setExtraProperties(extraProperties);
}
ActionReport subReport = null;
if (! preInvoke(context, actionReport)) {
commandsExitCode = ActionReport.ExitCode.FAILURE;
actionReport.setActionExitCode(commandsExitCode);
return;
}
if (targetUtil.isCluster(target)) {
for (Server serverInst : targetUtil.getInstances(target)) {
try {
subReport = executeInternalCommand(context, serverInst.getName());
break;
} catch (Throwable ex) {
logger.log(Level.INFO, "Got exception: " + ex.toString());
}
}
} else {
subReport = executeInternalCommand(context, target);
}
if (subReport != null) {
if (subReport.getExtraProperties() != null && subReport.getExtraProperties().size() > 0)
postInvoke(context, subReport);
else {
if (subReport.getSubActionsReport() != null && subReport.getSubActionsReport().size() > 0
&& subReport.getSubActionsReport().get(0).getExtraProperties() != null) {
postInvoke(context, subReport.getSubActionsReport().get(0));
} else {
actionReport.setMessage(subReport.getMessage());
}
}
commandsExitCode = subReport.getActionExitCode();
}
actionReport.setActionExitCode(commandsExitCode);
}
protected boolean preInvoke(AdminCommandContext ctx, ActionReport subReport) {
return true;
}
protected abstract String getCommandName();
protected abstract void postInvoke(AdminCommandContext context, ActionReport subReport);
private ActionReport executeInternalCommand(AdminCommandContext context, String targetInstanceName) {
String commandName = getCommandName();
ParameterMap params = new ParameterMap();
params.add("target", targetInstanceName);
fillParameterMap(params);
CommandRunner runner = serviceLocator.getService(CommandRunner.class);
ActionReport subReport = context.getActionReport().addSubActionsReport();
CommandRunner.CommandInvocation inv = runner.getCommandInvocation(commandName, subReport, context.getSubject());
inv.parameters(params);
inv.execute();
return subReport;
}
protected void fillParameterMap(ParameterMap parameterMap) {
if (isTerse)
parameterMap.add("terse", ""+isTerse);
if (outputHeaderList != null)
parameterMap.add("output", outputHeaderList);
if (header)
parameterMap.add("header", ""+header);
if (useLongFormat)
parameterMap.add("long", ""+useLongFormat);
}
protected boolean isLongNumber(String str) {
try {
Long.parseLong(str);
} catch (NumberFormatException nEx) {
return false;
}
return true;
}
}