forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCliHandlerProxy.java
More file actions
171 lines (147 loc) · 4.78 KB
/
CliHandlerProxy.java
File metadata and controls
171 lines (147 loc) · 4.78 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package act.handler.builtin.cli;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.Act;
import act.app.App;
import act.cli.CliContext;
import act.cli.CliException;
import act.cli.CommandExecutor;
import act.cli.bytecode.ReflectedCommandExecutor;
import act.cli.meta.CommandMethodMetaInfo;
import act.cli.meta.CommanderClassMetaInfo;
import act.handler.CliHandlerBase;
import act.util.ProgressGauge;
import act.util.PropertySpec;
import org.osgl.$;
import org.osgl.logging.L;
import org.osgl.logging.Logger;
import org.osgl.util.S;
import java.util.List;
public final class CliHandlerProxy extends CliHandlerBase {
private static Logger logger = L.get(CliHandlerProxy.class);
private App app;
private CommandMethodMetaInfo methodMetaInfo;
private CommanderClassMetaInfo classMetaInfo;
private volatile CommandExecutor executor = null;
public CliHandlerProxy(CommanderClassMetaInfo classMetaInfo, CommandMethodMetaInfo metaInfo, App app) {
this.methodMetaInfo = $.requireNotNull(metaInfo);
this.classMetaInfo = $.requireNotNull(classMetaInfo);
this.app = $.requireNotNull(app);
}
@Override
protected void releaseResources() {
if (null != executor) {
executor.destroy();
executor = null;
}
}
public CommandMethodMetaInfo methodMetaInfo() {
return methodMetaInfo;
}
public CommanderClassMetaInfo classMetaInfo() {
return classMetaInfo;
}
@Override
public boolean appliedIn(Act.Mode mode) {
return mode == Act.Mode.DEV || mode == methodMetaInfo.mode();
}
@Override
public void handle(CliContext context) {
try {
ensureAgentsReady();
saveCommandPath(context);
Object result = _handle(context);
onResult(result, context);
} catch ($.Break b) {
throw b;
} catch (CliException error) {
context.println(error.getMessage());
} catch (Exception e) {
String msg = e.getMessage();
if (S.blank(msg)) {
msg = S.fmt("Error processing command: %s", e);
}
context.println(msg);
logger.error(e, "Error handling request");
} catch (Throwable t) {
logger.fatal(t, "Error handling request");
} finally {
PropertySpec.current.remove();
}
}
@Override
public $.T2<String, String> commandLine() {
return methodMetaInfo.commandLine(classMetaInfo, app.classLoader());
}
@Override
public List<$.T2<String, String>> options() {
return methodMetaInfo.options(classMetaInfo, app.classLoader());
}
@SuppressWarnings("unchecked")
private void onResult(Object result, CliContext context) {
if (null == result) {
return;
}
if (result instanceof ProgressGauge) {
context.print((ProgressGauge) result);
} else {
PropertySpec.MetaInfo filter = methodMetaInfo.propertySpec();
methodMetaInfo.view().print(result, filter, context);
}
}
private void ensureAgentsReady() {
if (null == executor) {
synchronized (this) {
if (null == executor) {
generateExecutor();
}
}
}
}
// could be used by View to resolve default path to template
private void saveCommandPath(CliContext context) {
context.commandPath(methodMetaInfo.fullName());
}
private void generateExecutor() {
executor = new ReflectedCommandExecutor(methodMetaInfo, app);
}
private Object _handle(CliContext context) {
return executor.execute(context);
}
@Override
public String toString() {
return methodMetaInfo.fullName();
}
@Override
public int hashCode() {
return $.hc(methodMetaInfo);
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof CliHandlerProxy) {
CliHandlerProxy that = (CliHandlerProxy)obj;
return $.eq(that.methodMetaInfo, this.methodMetaInfo);
}
return false;
}
}