Skip to content

Commit 861f286

Browse files
committed
fix param binding issue with Field: SObjectBinder not applied; fix cli iterator issue: last iterating context not cleared
1 parent 379d0c0 commit 861f286

14 files changed

Lines changed: 246 additions & 37 deletions

File tree

src/main/java/act/SysUtilAdmin.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public String pwd() {
4343
return pwd(context).getAbsolutePath();
4444
}
4545

46-
@Command(name = "act.ls, act.dir", help = "List files in the current working directory")
46+
@Command(name = "act.ls, act.dir, act.ll", help = "List files in the current working directory")
4747
@PropertySpec("path as name,size,timestamp")
4848
public List<FileInfo> ls(
4949
@Optional("specify the path to be listed") String path,
@@ -103,6 +103,7 @@ public void cat(
103103
@Optional(help = "specify the end line to be printed out") int end,
104104
@Optional(help = "specify begin end as range, e.g. 5-8") String range,
105105
@Optional(lead = "-n,--line-number", help = "print line number") boolean printLineNumber,
106+
@Optional(lead = "-q,--grep", help = "specify search criteria") String q,
106107
CliContext context
107108
) {
108109
if (S.notBlank(range)) {
@@ -116,18 +117,22 @@ public void cat(
116117
return;
117118
}
118119
} else {
120+
final int defLimit = S.blank(q) ? 20 : Integer.MAX_VALUE;
119121
if (begin <= 0) {
120122
begin = 1;
121123
}
122124
if (end <= 0) {
123-
end = begin + (limit <= 0 ? 20 : limit) - 1;
125+
end = begin + (limit <= 0 ? defLimit : limit) - 1;
124126
}
125127
}
126128
List<String> lines = IO.readLines(sobj.asInputStream(), end);
127129
int len = lines.size();
128130
context.println("");
129131
for (int i = begin - 1; i < len; ++i) {
130132
String line = lines.get(i);
133+
if (S.notBlank(q) && !line.contains(q)) {
134+
continue;
135+
}
131136
if (printLineNumber) {
132137
context.print("%5s | ", i + 1);
133138
}

src/main/java/act/cli/CliContext.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ void handle() throws IOException {
298298
Help.INSTANCE.showHelp(parser.command(), this);
299299
} else {
300300
try {
301+
session.handler(handler);
301302
handler.handle(this);
302303
} catch ($.Break b) {
303304
throw b;

src/main/java/act/cli/CliSession.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import act.app.ActionContext;
55
import act.app.App;
66
import act.app.CliServer;
7+
import act.cli.builtin.IterateCursor;
78
import act.cli.event.CliSessionStart;
89
import act.cli.event.CliSessionTerminate;
910
import act.cli.util.CliCursor;
11+
import act.handler.CliHandler;
1012
import act.util.Banner;
1113
import act.util.DestroyableBase;
1214
import jline.console.ConsoleReader;
@@ -30,7 +32,6 @@ public class CliSession extends DestroyableBase implements Runnable {
3032

3133
private String id;
3234
private CliServer server;
33-
private CliDispatcher dispatcher;
3435
protected App app;
3536
private Socket socket;
3637
private long ts;
@@ -39,6 +40,8 @@ public class CliSession extends DestroyableBase implements Runnable {
3940
private ConsoleReader console;
4041
private CliCursor cursor;
4142
private CommandNameCompleter commandNameCompleter;
43+
// the current handler
44+
private CliHandler handler;
4245
/**
4346
* Allow user command to attach data to the context and fetched for later use.
4447
* <p>
@@ -54,7 +57,6 @@ public class CliSession extends DestroyableBase implements Runnable {
5457
*/
5558
protected CliSession(ActionContext context) {
5659
this.app = context.app();
57-
this.dispatcher = app.cliDispatcher();
5860
this.id = context.session().id();
5961
this.ts = $.ms();
6062
}
@@ -63,7 +65,6 @@ public CliSession(Socket socket, CliServer server) {
6365
this.socket = $.NPE(socket);
6466
this.server = $.NPE(server);
6567
this.app = server.app();
66-
this.dispatcher = app.cliDispatcher();
6768
id = app.cuid();
6869
ts = $.ms();
6970
commandNameCompleter = new CommandNameCompleter(app);
@@ -197,6 +198,18 @@ public void stop(String message) {
197198
stop();
198199
}
199200

201+
void handler(CliHandler handler) {
202+
if (handler == IterateCursor.INSTANCE) {
203+
return;
204+
}
205+
if (null == this.handler || S.string(this.handler).equals(S.string(handler))) {
206+
this.handler = handler;
207+
return;
208+
}
209+
this.handler = handler;
210+
removeCursor();
211+
}
212+
200213
private static void printBanner(String banner, ConsoleReader console) throws IOException {
201214
String[] lines = banner.split("[\n\r]");
202215
for (String line : lines) {

src/main/java/act/inject/param/ParamValueLoaderService.java

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ private <T> Map<Field, ParamValueLoader> fieldLoaders(Class<T> beanClass) {
166166
Type type = field.getGenericType();
167167
Annotation[] annotations = field.getAnnotations();
168168
BeanSpec spec = BeanSpec.of(type, annotations, field.getName(), injector);
169-
ParamValueLoader loader = findLoader(spec, type, annotations);
170-
boolean loadedByGenie = (loader instanceof ProvidedValueLoader && field.isAnnotationPresent(Inject.class));
171-
if (null != loader && !loadedByGenie) {
169+
ParamValueLoader loader = paramValueLoaderOf(spec, null);
170+
boolean provided = (loader instanceof ProvidedValueLoader);
171+
if (null != loader && !provided) {
172172
fieldLoaders.put(field, loader);
173173
}
174174
}
@@ -188,24 +188,31 @@ protected ParamValueLoader[] findMethodParamLoaders(Method method) {
188188
for (int i = 0; i < sz; ++i) {
189189
String name = paramName(i);
190190
BeanSpec spec = BeanSpec.of(types[i], annotations[i], name, injector);
191-
ParamValueLoader loader = paramRegistry.get($.T2(types[i], annotations[i]));
192-
if (null == loader) {
193-
if (Result.class.isAssignableFrom(spec.rawType())) {
194-
loader = RESULT_LOADER;
195-
} else if (Exception.class.isAssignableFrom(spec.rawType())) {
196-
loader = EXCEPTION_LOADED;
197-
} else {
198-
loader = findLoader(spec, types[i], annotations[i]);
199-
}
200-
// Cannot use spec as the key here because
201-
// spec does not compare Scoped annotation
202-
paramRegistry.putIfAbsent($.T2(types[i], annotations[i]), loader);
203-
}
204-
loaders[i] = loader;
191+
loaders[i] = paramValueLoaderOf(spec, null);
205192
}
206193
return loaders;
207194
}
208195

196+
private ParamValueLoader paramValueLoaderOf(BeanSpec spec, String bindName) {
197+
Class<?> rawType = spec.rawType();
198+
if (Result.class.isAssignableFrom(rawType)) {
199+
return RESULT_LOADER;
200+
} else if (Exception.class.isAssignableFrom(rawType)) {
201+
return EXCEPTION_LOADED;
202+
}
203+
Type type = spec.type();
204+
Annotation[] annotations = spec.allAnnotations();
205+
$.T2<Type, Annotation[]> key = $.T2(type, annotations);
206+
ParamValueLoader loader = paramRegistry.get(key);
207+
if (null == loader) {
208+
loader = findLoader(spec, type, annotations, bindName);
209+
// Cannot use spec as the key here because
210+
// spec does not compare Scoped annotation
211+
paramRegistry.putIfAbsent(key, loader);
212+
}
213+
return loader;
214+
}
215+
209216
protected abstract ParamValueLoader findContextSpecificLoader(
210217
String bindName,
211218
Class rawType,
@@ -225,7 +232,8 @@ protected boolean supportJsonDecorator() {
225232
private ParamValueLoader findLoader(
226233
BeanSpec spec,
227234
Type type,
228-
Annotation[] annotations
235+
Annotation[] annotations,
236+
String bindName
229237
) {
230238
Class rawType = spec.rawType();
231239
if (provided(spec, injector)) {
@@ -234,7 +242,9 @@ private ParamValueLoader findLoader(
234242
if (null != filter(annotations, NoBind.class)) {
235243
return null;
236244
}
237-
String bindName = bindName(annotations, spec.name());
245+
if (null == bindName) {
246+
bindName = bindName(annotations, spec.name());
247+
}
238248
ParamValueLoader loader = findContextSpecificLoader(bindName, rawType, spec, type, annotations);
239249
return decorate(loader, spec, annotations, supportJsonDecorator());
240250
}
@@ -351,10 +361,9 @@ public Object create() {
351361
}
352362

353363
private ParamValueLoader findLoader(ParamKey paramKey, Field field) {
354-
BeanSpec spec = BeanSpec.of(field.getGenericType(), field.getDeclaredAnnotations(), App.instance().injector());
355-
Class fieldType = field.getType();
364+
BeanSpec spec = BeanSpec.of(field.getGenericType(), field.getDeclaredAnnotations(), injector);
356365
Annotation[] annotations = field.getDeclaredAnnotations();
357-
if (ActProviders.isProvided(fieldType) || null != filter(annotations, Inject.class)) {
366+
if (provided(spec, injector)) {
358367
return ProvidedValueLoader.get(spec, injector);
359368
}
360369
String name = null;
@@ -367,11 +376,17 @@ private ParamValueLoader findLoader(ParamKey paramKey, Field field) {
367376
}
368377
ParamKey key = paramKey.child(name);
369378

370-
StringValueResolver resolver = resolverManager.resolver(fieldType, spec);
371-
if (null != resolver) {
372-
return new StringValueResolverValueLoader(key, resolver, null, fieldType);
379+
ParamValueLoader loader = paramValueLoaderOf(spec, key.toString());
380+
if (null != loader) {
381+
return loader;
373382
}
374383

384+
// Class fieldType = field.getType();
385+
// StringValueResolver resolver = resolverManager.resolver(fieldType, spec);
386+
// if (null != resolver) {
387+
// return new StringValueResolverValueLoader(key, resolver, null, fieldType);
388+
// }
389+
375390
return buildLoader(key, field.getGenericType(), spec);
376391
}
377392

src/main/java/act/route/RouteInfo.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ public String handler() {
2525
return _3;
2626
}
2727
public String compactHandler() {
28-
String[] sa = _3.split("\\.");
28+
return compactHandler(_3);
29+
}
30+
31+
public static String compactHandler(String handler) {
32+
String[] sa = handler.split("\\.");
2933
int len = sa.length;
3034
if (len == 1) {
31-
return _3;
35+
return handler;
3236
}
3337
StringBuilder sb = new StringBuilder();
3438
for (int i = 0; i < len - 2; ++i) {

src/main/java/act/route/Router.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ public String id() {
552552
public String label() {
553553
StringBuilder sb = S.builder(name);
554554
if (null != handler) {
555-
sb.append(" -> ").append(handler);
555+
sb.append(" -> ").append(RouteInfo.compactHandler(handler.toString()));
556556
}
557557
return sb.toString();
558558
}

src/main/resources/act.scan.list

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ act.inject.genie.GenieInjector
88
act.db.DbBind
99
act.controller.builtin.CliOverHttp
1010
act.app.conf.AppConfigPlugin
11-
act.view.rythm.JodaTransformers

testapp/pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
~ under the License.
1616
-->
1717
<!--
18-
********************************************
18+
********************************************\
1919
* version history
2020
********************************************
2121
0.0.2
2222
- update to ActFramework 0.3
23+
- update to Act-Storage 0.3
2324
2425
0.0.1
2526
- baseline version
@@ -126,6 +127,12 @@
126127
<version>0.3.0-SNAPSHOT</version>
127128
</dependency>
128129

130+
<dependency>
131+
<groupId>org.actframework</groupId>
132+
<artifactId>act-storage</artifactId>
133+
<version>0.3.0-SNAPSHOT</version>
134+
</dependency>
135+
129136
<dependency>
130137
<groupId>org.yaml</groupId>
131138
<artifactId>snakeyaml</artifactId>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package testapp.endpoint;
2+
3+
import act.app.DbServiceManager;
4+
import act.controller.Controller;
5+
import act.db.morphia.MorphiaService;
6+
import act.util.PropertySpec;
7+
import org.osgl.mvc.annotation.DeleteAction;
8+
import org.osgl.mvc.annotation.GetAction;
9+
import org.osgl.mvc.annotation.PostAction;
10+
import org.osgl.util.E;
11+
import testapp.model.mongo.Person;
12+
13+
import javax.inject.Inject;
14+
15+
public class MorphiaTestBed extends Controller.Util {
16+
17+
@Inject
18+
Person.Dao personDao;
19+
20+
@DeleteAction("/morphia")
21+
public void cleanDb(DbServiceManager dbServiceManager) {
22+
MorphiaService svc = dbServiceManager.dbService(DbServiceManager.DEFAULT);
23+
svc.ds().getDB().dropDatabase();
24+
}
25+
26+
@PostAction("/morphia/person")
27+
@PropertySpec("-photo")
28+
public void createPerson(Person person) {
29+
E.illegalStateIf(personDao.exists(person.getName()));
30+
personDao.save(person);
31+
}
32+
33+
@GetAction("/morphia/person/{name}")
34+
public Person findPerson(String name) {
35+
return personDao.findOneBy("name", name);
36+
}
37+
38+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package testapp.model.mongo;
2+
3+
import act.db.morphia.MorphiaModel;
4+
import org.osgl.util.KVStore;
5+
6+
/**
7+
* A morphia model with KVStore
8+
*/
9+
public class KVStoreModel<T extends MorphiaModel> extends MorphiaModel<T> {
10+
11+
/**
12+
* A typesafe attribute storage
13+
*/
14+
protected KVStore kv = new KVStore();
15+
16+
17+
}

0 commit comments

Comments
 (0)