Skip to content

Commit 4e8efe7

Browse files
committed
fix issue in RythmError when java source is null; cliOverHttp - wip
1 parent de818ed commit 4e8efe7

14 files changed

Lines changed: 239 additions & 49 deletions

File tree

src/main/java/act/app/App.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,10 @@ private void initRouters() {
765765
for (NamedPort port : ports) {
766766
moreRouters.put(port, new Router(this, port.name()));
767767
}
768+
if (config.cliOverHttp()) {
769+
NamedPort cliOverHttp = new NamedPort(AppConfig.PORT_CLI_OVER_HTTP, config.cliOverHttpPort());
770+
moreRouters.put(cliOverHttp, new Router(this, AppConfig.PORT_CLI_OVER_HTTP));
771+
}
768772
}
769773

770774
private void initEventBus() {

src/main/java/act/cli/CliDispatcher.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ public CliHandler handler(String command) {
7474
return null;
7575
}
7676

77+
/**
78+
* Returns a list of system commands in alphabetic order
79+
*
80+
* @return the system command list
81+
*/
82+
public List<String> systemCommands() {
83+
return commands(true, false);
84+
}
85+
86+
/**
87+
* Returns a list of application commands in alphabetic order
88+
*
89+
* @return the application command list
90+
*/
91+
public List<String> applicationCommands() {
92+
return commands(false, true);
93+
}
94+
7795
/**
7896
* Returns all commands in alphabetic order
7997
*

src/main/java/act/conf/AppConfig.java

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class AppConfig<T extends AppConfigurator> extends Config<AppConfigKey> i
4141

4242
public static final String CONF_FILE_NAME = "app.conf";
4343

44+
public static final String PORT_CLI_OVER_HTTP = "__admin__";
45+
4446
private App app;
4547

4648
static {
@@ -434,7 +436,55 @@ private void _mergeCliJSONPageSz(AppConfig conf) {
434436
}
435437
}
436438

437-
private int cliPort = -1;
439+
Boolean cliOverHttp;
440+
441+
protected T cliOverHttp(boolean enabled) {
442+
this.cliOverHttp = enabled;
443+
return me();
444+
}
445+
446+
public boolean cliOverHttp() {
447+
if (null == cliOverHttp) {
448+
Boolean B = get(CLI_OVER_HTTP);
449+
if (null == B) {
450+
B = false;
451+
}
452+
cliOverHttp = B;
453+
}
454+
return cliOverHttp;
455+
}
456+
457+
private void _mergeCliOverHttp(AppConfig config) {
458+
if (null == get(CLI_OVER_HTTP)) {
459+
cliOverHttp = config.cliOverHttp;
460+
}
461+
}
462+
463+
Integer cliOverHttpPort;
464+
465+
protected T cliOverHttpPort(int port) {
466+
this.cliOverHttpPort = port;
467+
return me();
468+
}
469+
470+
public int cliOverHttpPort() {
471+
if (null == cliOverHttpPort) {
472+
Integer I = get(CLI_OVER_HTTP_PORT);
473+
if (null == I) {
474+
I = 5462;
475+
}
476+
cliOverHttpPort = I;
477+
}
478+
return cliOverHttpPort;
479+
}
480+
481+
private void _mergeCliOverHttpPort(AppConfig config) {
482+
if (null == get(CLI_OVER_HTTP_PORT)) {
483+
cliOverHttpPort = config.cliOverHttpPort;
484+
}
485+
}
486+
487+
private Integer cliPort;
438488

439489
protected T cliPort(int port) {
440490
E.illegalArgumentIf(port < 1, "port value not valid: %s", port);
@@ -443,10 +493,10 @@ protected T cliPort(int port) {
443493
}
444494

445495
public int cliPort() {
446-
if (-1 == cliPort) {
496+
if (null == cliPort) {
447497
Integer I = get(CLI_PORT);
448498
if (null == I) {
449-
I = 5460;
499+
I = 5461;
450500
}
451501
cliPort = I;
452502
}
@@ -1138,7 +1188,7 @@ public List<NamedPort> namedPorts() {
11381188
if (null == namedPorts) {
11391189
String s = get(NAMED_PORTS);
11401190
if (null == s) {
1141-
namedPorts = C.list();
1191+
namedPorts = cliOverHttp() ? C.list(new NamedPort(PORT_CLI_OVER_HTTP, cliOverHttpPort())) : C.<NamedPort>list();
11421192
} else {
11431193
String[] sa = (s.split("[,;]+"));
11441194
ListBuilder<NamedPort> builder = ListBuilder.create();
@@ -1154,6 +1204,9 @@ public List<NamedPort> namedPorts() {
11541204
throw E.invalidConfiguration("port[%s] already configured", name);
11551205
}
11561206
}
1207+
if (cliOverHttp()) {
1208+
builder.add(new NamedPort(PORT_CLI_OVER_HTTP, cliOverHttpPort()));
1209+
}
11571210
namedPorts = builder.toList();
11581211
}
11591212
}
@@ -1580,18 +1633,18 @@ private void _mergeFlashCookieName(AppConfig config) {
15801633
}
15811634
}
15821635

1583-
private Long sessionTtl = null;
1636+
private Integer sessionTtl = null;
15841637

1585-
protected T sessionTtl(long seconds) {
1638+
protected T sessionTtl(int seconds) {
15861639
sessionTtl = seconds;
15871640
return me();
15881641
}
15891642

1590-
public long sessionTtl() {
1643+
public int sessionTtl() {
15911644
if (null == sessionTtl) {
15921645
sessionTtl = get(AppConfigKey.SESSION_TTL);
15931646
if (null == sessionTtl) {
1594-
sessionTtl = (long) 60 * 30;
1647+
sessionTtl = 60 * 30;
15951648
}
15961649
}
15971650
return sessionTtl;
@@ -1870,6 +1923,8 @@ public void _merge(AppConfigurator conf) {
18701923
_mergeCorsMaxAge(conf);
18711924
_mergeCliJSONPageSz(conf);
18721925
_mergeCliTablePageSz(conf);
1926+
_mergeCliOverHttp(conf);
1927+
_mergeCliOverHttpPort(conf);
18731928
_mergeCliPort(conf);
18741929
_mergeCliSessionExpiration(conf);
18751930
_mergeCsrf(conf);

src/main/java/act/conf/AppConfigKey.java

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ public enum AppConfigKey implements ConfigKey {
9292
CORS_MAX_AGE("cors.max_age"),
9393

9494
/**
95-
* {@code act.csrf} turn on/off global CSRF protect
95+
* {@code act.csrf.enabled} turn on/off global CSRF protect
9696
*
9797
* Default value: `true`
9898
*/
99-
CSRF("csrf"),
99+
CSRF("csrf.enabled"),
100100

101101
/**
102102
* {@code act.csf.param_name} specifies the http request param name
@@ -140,17 +140,24 @@ public enum AppConfigKey implements ConfigKey {
140140
* listen to.
141141
* <p>Default value: {@code 5461}</p>
142142
*/
143-
CLI_PORT("cli.port") {
144-
@Override
145-
public <T> T val(Map<String, ?> configuration) {
146-
Object v = configuration.get(key());
147-
if (null == v) return (T) (Number) 5461;
148-
if (v instanceof Number) {
149-
return (T) v;
150-
}
151-
return (T) (Integer.valueOf(v.toString()));
152-
}
153-
},
143+
CLI_PORT("cli.port"),
144+
145+
/**
146+
* `act.cli_over_http.enabled` turn on/off CLI over http feature, which
147+
* allows ActFramework to handle http request sent through to the {@link #CLI_OVER_HTTP_PORT}
148+
* as a way to invoke CLI commands and inspect results
149+
*
150+
* Default value: `false`
151+
*/
152+
CLI_OVER_HTTP("cli_over_http.enabled"),
153+
154+
/**
155+
* `act.cli_over_http.port` specifies the default cli over http port the application
156+
* listen to.
157+
*
158+
* Default value: `5462`
159+
*/
160+
CLI_OVER_HTTP_PORT("cli_over_http.port"),
154161

155162
/**
156163
* {@code cli.session.expiration} specifies the number of seconds
@@ -311,17 +318,7 @@ public <T> T val(Map<String, ?> configuration) {
311318
* <p/>
312319
* <p>Default value: {@code 5460}</p>
313320
*/
314-
HTTP_PORT("http.port") {
315-
@Override
316-
public <T> T val(Map<String, ?> configuration) {
317-
Object v = configuration.get(key());
318-
if (null == v) return null;
319-
if (v instanceof Number) {
320-
return (T) v;
321-
}
322-
return (T) (Integer.valueOf(v.toString()));
323-
}
324-
},
321+
HTTP_PORT("http.port"),
325322

326323
/**
327324
* {@code act.http.secure} specifies whether the default http port is
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package act.controller.builtin;
2+
3+
import act.cli.CliDispatcher;
4+
import act.conf.AppConfig;
5+
import act.controller.Controller;
6+
import org.osgl.http.H;
7+
import org.osgl.mvc.annotation.GetAction;
8+
import org.osgl.mvc.result.Result;
9+
import org.osgl.util.C;
10+
11+
import javax.inject.Inject;
12+
import java.util.List;
13+
14+
import static act.controller.Controller.Util.render;
15+
16+
/**
17+
* Handles CLI over http requests
18+
*/
19+
@Controller(port = AppConfig.PORT_CLI_OVER_HTTP)
20+
public class CliOverHttp {
21+
22+
@Inject
23+
CliDispatcher dispatcher;
24+
25+
@Inject
26+
H.Session session;
27+
28+
@Inject
29+
AppConfig config;
30+
31+
@GetAction
32+
public Result home() {
33+
List<String> mru = mru();
34+
return render(dispatcher, mru);
35+
}
36+
37+
private List<String> mru() {
38+
List<String> mru = session.cached("cli_over_http_mru");
39+
if (null == mru) {
40+
mru = C.newList();
41+
}
42+
session.cache("cli_over_http_mru", mru, config.sessionTtl());
43+
return mru;
44+
}
45+
46+
}

src/main/java/act/controller/bytecode/ControllerByteCodeScanner.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import act.asm.*;
88
import act.asm.signature.SignatureReader;
99
import act.asm.signature.SignatureVisitor;
10+
import act.conf.AppConfig;
1011
import act.controller.Controller;
1112
import act.controller.meta.*;
1213
import act.route.RouteSource;
@@ -538,6 +539,7 @@ public void visitEnum(String name, String desc, String value) {
538539

539540
@Override
540541
public void visitEnd() {
542+
super.visitEnd();
541543
if (httpMethods.isEmpty()) {
542544
// start(*) match
543545
httpMethods.addAll(H.Method.actionMethods());
@@ -552,6 +554,10 @@ public void visitEnd() {
552554
for (String portName : ports) {
553555
Router r = app.router(portName);
554556
if (null == r) {
557+
if (S.eq(AppConfig.PORT_CLI_OVER_HTTP, portName)) {
558+
// cli over http is disabled
559+
return;
560+
}
555561
throw E.invalidConfiguration("Cannot find configuration for named port[%s]", portName);
556562
}
557563
routers.add(r);

src/main/java/act/handler/CliHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import org.osgl.$;
77

88
import java.util.List;
9-
import java.util.Map;
109

1110
/**
1211
* Defines a thread-save function object that can be applied

src/main/java/act/inject/genie/GenieInjector.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
import org.osgl.$;
1313
import org.osgl.Osgl;
1414
import org.osgl.exception.NotAppliedException;
15-
import org.osgl.inject.Genie;
16-
import org.osgl.inject.InjectListener;
17-
import org.osgl.inject.Module;
18-
import org.osgl.inject.ScopeCache;
15+
import org.osgl.inject.*;
1916
import org.osgl.inject.annotation.LoadValue;
2017
import org.osgl.inject.annotation.Provided;
2118
import org.osgl.mvc.annotation.Bind;
@@ -26,9 +23,7 @@
2623
import javax.inject.Inject;
2724
import javax.inject.Provider;
2825
import java.lang.annotation.Annotation;
29-
import java.util.List;
30-
import java.util.Map;
31-
import java.util.Set;
26+
import java.util.*;
3227

3328
public class GenieInjector extends DependencyInjectorBase<GenieInjector> {
3429

@@ -43,6 +38,7 @@ protected void configure() {
4338

4439
private volatile Genie genie;
4540
private List<Object> modules;
41+
private Set<Class<? extends Annotation>> injectTags = new HashSet<>();
4642

4743
public GenieInjector(App app) {
4844
super(app);
@@ -94,6 +90,18 @@ public void addModule(Object module) {
9490
modules.add(module);
9591
}
9692

93+
public boolean hasInjectTag(BeanSpec spec) {
94+
if(spec.hasAnnotation(Inject.class)) {
95+
return true;
96+
}
97+
for (Class<? extends Annotation> tag : injectTags) {
98+
if (spec.hasAnnotation(tag)) {
99+
return true;
100+
}
101+
}
102+
return false;
103+
}
104+
97105
private C.List<Object> factories() {
98106
Set<String> factories = GenieFactoryFinder.factories();
99107
int len = factories.size();
@@ -135,6 +143,9 @@ public Void apply(Class aClass, Provider provider) throws NotAppliedException, O
135143

136144
ActProviders.registerBuiltInProviders(ActProviders.class, register);
137145
ActProviders.registerBuiltInProviders(GenieProviders.class, register);
146+
for (Class<? extends Annotation> injectTag: injectTags) {
147+
genie.registerInjectTag(injectTag);
148+
}
138149
}
139150
}
140151
}
@@ -148,11 +159,11 @@ public static void foundModule(Class<? extends Module> moduleClass) {
148159
genieInjector.addModule($.newInstance(moduleClass));
149160
}
150161

151-
@AnnotatedClassFinder(value = LoadValue.class, noAbstract = false)
162+
@AnnotatedClassFinder(value = LoadValue.class, noAbstract = false, callOn = AppEventId.DEPENDENCY_INJECTOR_LOADED)
152163
public static void foundValueLoader(Class<? extends Annotation> valueLoader) {
153164
App app = App.instance();
154165
GenieInjector genieInjector = app.injector();
155-
genieInjector.genie().registerInjectTag(valueLoader);
166+
genieInjector.injectTags.add(valueLoader);
156167
}
157168

158169
}

0 commit comments

Comments
 (0)