Skip to content

Commit aaf21ee

Browse files
committed
提交
1 parent 7bb7a7a commit aaf21ee

18 files changed

Lines changed: 194 additions & 114 deletions

.gitignore

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
*.class
2-
*.iml
3-
*.log
4-
.idea
52

63
# Mobile Tools for Java (J2ME)
74
.mtj.tmp/
@@ -13,4 +10,15 @@
1310

1411
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
1512
hs_err_pid*
16-
target/
13+
14+
# Build results
15+
target
16+
17+
# IDEA files
18+
.idea
19+
*.iml
20+
21+
# Eclipse files
22+
.settings
23+
.classpath
24+
.project

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@
7070
<dependency>
7171
<groupId>com.qiniu</groupId>
7272
<artifactId>qiniu-java-sdk</artifactId>
73-
<version>7.0.4.2</version>
73+
<version>[7.2.0, 7.2.99]</version>
74+
</dependency>
75+
<dependency>
76+
<groupId>com.javabaas</groupId>
77+
<artifactId>javasdk</artifactId>
78+
<version>1.0</version>
7479
</dependency>
7580

7681
</dependencies>

src/main/java/com/javabaas/shell/Main.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.javabaas.shell;
22

3+
import com.javabaas.javasdk.JBConfig;
34
import org.springframework.shell.Bootstrap;
45

56
import java.io.IOException;
@@ -10,6 +11,7 @@
1011
public class Main {
1112

1213
public static void main(String[] args) throws IOException {
14+
JBConfig.initAdmin("http://127.0.0.1:9000/api", "JavaBaas");
1315
Bootstrap.main(args);
1416
}
1517

src/main/java/com/javabaas/shell/commands/AppCommands.java

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.javabaas.shell.commands;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.javabaas.javasdk.JBApp;
5+
import com.javabaas.javasdk.JBException;
6+
import com.javabaas.javasdk.callback.JBAppListCallback;
37
import com.javabaas.shell.common.CommandContext;
4-
import com.javabaas.shell.entity.JBApp;
8+
import com.javabaas.shell.entity.JBSApp;
59
import com.javabaas.shell.util.PropertiesUtil;
610
import com.javabaas.shell.util.SignUtil;
711
import org.fusesource.jansi.Ansi;
@@ -16,7 +20,9 @@
1620

1721
import javax.annotation.Resource;
1822
import java.util.HashMap;
23+
import java.util.List;
1924
import java.util.Map;
25+
import java.util.UUID;
2026

2127
/**
2228
* Created by Codi on 15/9/22.
@@ -30,6 +36,8 @@ public class AppCommands implements CommandMarker {
3036
private CommandContext context;
3137
@Resource(name = "AdminRestTemplate")
3238
private RestTemplate rest;
39+
@Resource(name = "MasterRestTemplate")
40+
private RestTemplate masterRest;
3341
@Autowired
3442
private SignUtil signUtil;
3543
@Autowired
@@ -44,10 +52,18 @@ public boolean isAvailable() {
4452
public void list() {
4553
context.cancelDoubleCheck();
4654
try {
47-
JBApp[] result = rest.getForObject(properties.getHost() + "admin/app/", JBApp[].class);
48-
for (JBApp app : result) {
49-
System.out.println(app.getName());
50-
}
55+
List<JBApp> list = JBApp.list();
56+
list.forEach(o -> System.out.println(o.getName()));
57+
// JBApp.listInBackground(new JBAppListCallback() {
58+
// @Override
59+
// public void done(boolean success, List<JBApp> list, JBException e) {
60+
// if (success) {
61+
// list.forEach(o -> System.out.println(o.getName()));
62+
// } else {
63+
// System.out.println(e.getMessage());
64+
// }
65+
// }
66+
// });
5167
} catch (HttpClientErrorException e) {
5268
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a(e.getResponseBodyAsString()).reset());
5369
}
@@ -57,9 +73,9 @@ public void list() {
5773
public void add(@CliOption(key = {""}, mandatory = true) final String name) {
5874
context.cancelDoubleCheck();
5975
try {
60-
Map<String, Object> field = new HashMap<>();
61-
field.put("name", name);
62-
rest.postForObject(properties.getHost() + "admin/app/", field, String.class);
76+
JBApp app = new JBApp();
77+
app.setName(name);
78+
app.save();
6379
System.out.println(Ansi.ansi().fg(Ansi.Color.GREEN).a("App added.").reset());
6480
set(name);
6581
} catch (HttpClientErrorException e) {
@@ -72,28 +88,30 @@ public void delete(@CliOption(key = {""}, mandatory = true) final String name) {
7288
context.cancelDoubleCheck();
7389
//显示类信息
7490
try {
75-
JBApp[] apps = rest.getForObject(properties.getHost() + "admin/app/", JBApp[].class);
76-
for (JBApp app : apps) {
91+
List<JBApp> list = JBApp.list();
92+
final boolean[] flag = {false};
93+
list.forEach(app -> {
7794
if (app.getName().equals(name)) {
95+
flag[0] = true;
7896
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a("Do you really want to delete? (Y/N)"));
7997
context.setDoubleCheck(new DoubleCheckListener() {
8098
@Override
8199
public void confirm() {
82-
rest.delete(properties.getHost() + "admin/app/" + app.getId(), String.class);
100+
app.delete();
83101
context.setCurrentApp(null);
84102
System.out.println(Ansi.ansi().fg(Ansi.Color.GREEN).a("App deleted.").reset());
85103
}
86-
87104
@Override
88105
public void cancel() {
89106

90107
}
91108
});
92-
return;
93109
}
94-
}
110+
});
95111
//未找到应用
96-
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a("App not found!").reset());
112+
if (!flag[0]) {
113+
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a("App not found!").reset());
114+
}
97115
} catch (HttpClientErrorException e) {
98116
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a(e.getResponseBodyAsString()).reset());
99117
}
@@ -107,17 +125,21 @@ public void set(@CliOption(key = {""}, mandatory = false, help = "app name") fin
107125
context.setCurrentApp(null);
108126
} else {
109127
try {
110-
JBApp[] apps = rest.getForObject(properties.getHost() + "admin/app/", JBApp[].class);
111-
for (JBApp app : apps) {
128+
List<JBApp> list = JBApp.list();
129+
final boolean[] flag = {false};
130+
list.forEach(app -> {
112131
if (app.getName().equals(name)) {
113-
rest.getForObject(properties.getHost() + "admin/app/" + app.getId(), String.class);
114-
System.out.println("Set current app to " + Ansi.ansi().fg(Ansi.Color.GREEN).a(app.getName()).reset());
115-
context.setCurrentApp(app);
132+
JBApp jbApp = JBApp.get(app.getId());
133+
flag[0] = true;
134+
System.out.println("Set current app to " + Ansi.ansi().fg(Ansi.Color.GREEN).a(jbApp.getName()).reset());
135+
context.setCurrentApp(jbApp);
116136
return;
117137
}
118-
}
138+
});
119139
//未找到应用
120-
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a("App not found!").reset());
140+
if (!flag[0]) {
141+
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a("App not found!").reset());
142+
}
121143
} catch (HttpClientErrorException e) {
122144
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a(e.getResponseBodyAsString()).reset());
123145
}
@@ -136,8 +158,8 @@ public void export() {
136158
public void appInfo() {
137159
context.cancelDoubleCheck();
138160
JBApp app = context.getCurrentApp();
139-
String appInfo = rest.getForObject(properties.getHost() + "admin/app/" + app.getId(), String.class);
140-
System.out.println(appInfo);
161+
JBApp jbApp = JBApp.get(app.getId());
162+
System.out.println(jbApp);
141163
}
142164

143165
@CliCommand(value = "import", help = "Import tha app.")
@@ -156,14 +178,34 @@ public void token() {
156178
context.cancelDoubleCheck();
157179
//获取令牌
158180
String timestamp = signUtil.getTimestamp();
181+
String nonce = UUID.randomUUID().toString().replace("-", "");
159182
System.out.println("Timestamp: " + timestamp);
160-
System.out.println("AdminSign: " + signUtil.getAdminSign(timestamp));
183+
System.out.println("Nonce: " + nonce);
184+
System.out.println("AdminSign: " + signUtil.getAdminSign(timestamp, nonce));
161185
if (context.getCurrentApp() != null) {
162186
System.out.println("AppId: " + signUtil.getAppId());
163187
System.out.println("Key: " + context.getCurrentApp().getKey());
164188
System.out.println("MasterKey: " + context.getCurrentApp().getMasterKey());
165-
System.out.println("Sign: " + signUtil.getSign(timestamp));
166-
System.out.println("MasterSign: " + signUtil.getMasterSign(timestamp));
189+
System.out.println("Sign: " + signUtil.getSign(timestamp, nonce));
190+
System.out.println("MasterSign: " + signUtil.getMasterSign(timestamp, nonce));
191+
}
192+
}
193+
194+
@CliCommand(value = "account", help = "Set Account.")
195+
public void setAccount(@CliOption(key = {""}, mandatory = true, help = "Input") final String input) throws JsonProcessingException {
196+
context.cancelDoubleCheck();
197+
String[] inputs = input.split(" ");
198+
if (inputs.length < 2) {
199+
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a("No Account!").reset());
200+
return;
201+
}
202+
String type = inputs[0];
203+
String account = inputs[1];
204+
try {
205+
masterRest.put(properties.getHost() + "master/account/setAccount/" + type, account);
206+
System.out.println(Ansi.ansi().fg(Ansi.Color.GREEN).a("Object updated.").reset());
207+
} catch (HttpClientErrorException e) {
208+
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a(e.getResponseBodyAsString()).reset());
167209
}
168210
}
169211

src/main/java/com/javabaas/shell/commands/ClassAclCommands.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.javabaas.shell.commands;
22

33
import com.fasterxml.jackson.core.JsonProcessingException;
4-
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.javabaas.javasdk.JBClazz;
5+
import com.javabaas.javasdk.JBUtils;
56
import com.javabaas.shell.common.CommandContext;
6-
import com.javabaas.shell.entity.JBClass;
77
import com.javabaas.shell.util.PropertiesUtil;
88
import org.fusesource.jansi.Ansi;
99
import org.springframework.beans.factory.annotation.Autowired;
@@ -41,11 +41,10 @@ public boolean isAvailable() {
4141
public void getACL() throws JsonProcessingException {
4242
context.cancelDoubleCheck();
4343
String className = context.getCurrentClass();
44-
ObjectMapper mapper = new ObjectMapper();
4544
//显示类信息
4645
try {
47-
JBClass baasClass = rest.getForObject(properties.getHost() + "master/clazz/" + className, JBClass.class);
48-
System.out.println(mapper.writeValueAsString(baasClass.getAcl()));
46+
JBClazz clazz = JBClazz.get(className);
47+
System.out.println(clazz.getAcl());
4948
} catch (HttpClientErrorException e) {
5049
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a(e.getResponseBodyAsString()).reset());
5150
}
@@ -57,7 +56,10 @@ public void setACL(@CliOption(key = {""}, mandatory = true, help = "Object by js
5756
context.cancelDoubleCheck();
5857
String className = context.getCurrentClass();
5958
try {
60-
rest.postForLocation(properties.getHost() + "master/clazz/" + className + "/acl", acl);
59+
JBClazz clazz = new JBClazz(className);
60+
JBClazz.JBClazzAcl clazzAcl = JBUtils.readValue(acl, JBClazz.JBClazzAcl.class);
61+
clazz.setAcl(clazzAcl);
62+
clazz.updateClazzAcl();
6163
System.out.println(Ansi.ansi().fg(Ansi.Color.GREEN).a("ACL updated.").reset());
6264
} catch (HttpClientErrorException e) {
6365
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a(e.getResponseBodyAsString()).reset());

src/main/java/com/javabaas/shell/commands/ClassCommands.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.javabaas.shell.commands;
22

3+
import com.javabaas.javasdk.JBClazz;
34
import com.javabaas.shell.common.CommandContext;
4-
import com.javabaas.shell.entity.JBClass;
5+
import com.javabaas.shell.entity.JBSClass;
56
import com.javabaas.shell.util.PropertiesUtil;
67
import org.fusesource.jansi.Ansi;
78
import org.springframework.beans.factory.annotation.Autowired;
@@ -15,6 +16,7 @@
1516

1617
import javax.annotation.Resource;
1718
import java.util.HashMap;
19+
import java.util.List;
1820
import java.util.Map;
1921

2022
/**
@@ -41,10 +43,8 @@ public boolean isAvailable() {
4143
public void find() {
4244
context.cancelDoubleCheck();
4345
//显示列表
44-
JBClass[] result = rest.getForObject(properties.getHost() + "master/clazz/", JBClass[].class);
45-
for (JBClass baasClass : result) {
46-
System.out.println(baasClass.getName() + "(" + baasClass.getCount() + ")");
47-
}
46+
List<JBClazz> list = JBClazz.list();
47+
list.forEach(clazz -> System.out.println(clazz.getName() + "(" + clazz.getCount() + ")"));
4848
}
4949

5050
@CliCommand(value = "set", help = "Set current class.")
@@ -55,7 +55,7 @@ public void find(@CliOption(key = {""}, mandatory = false, help = "class name")
5555
context.setCurrentClass(null);
5656
} else {
5757
try {
58-
rest.getForObject(properties.getHost() + "master/clazz/" + name, String.class);
58+
JBClazz.get(name);
5959
System.out.println(Ansi.ansi().a("Set current class to ").fg(Ansi.Color.GREEN).a(name).reset());
6060
context.setCurrentClass(name);
6161
} catch (HttpClientErrorException e) {
@@ -73,7 +73,8 @@ public void delete(@CliOption(key = {""}, mandatory = true) final String name) {
7373
context.setDoubleCheck(new DoubleCheckListener() {
7474
@Override
7575
public void confirm() {
76-
rest.delete(properties.getHost() + "master/clazz/" + name, String.class);
76+
JBClazz clazz = new JBClazz(name);
77+
clazz.delete();
7778
context.setCurrentClass(null);
7879
System.out.println(Ansi.ansi().fg(Ansi.Color.GREEN).a("Class deleted.").reset());
7980
}
@@ -92,10 +93,8 @@ public void cancel() {
9293
public void add(@CliOption(key = {""}, mandatory = true) final String name) {
9394
context.cancelDoubleCheck();
9495
try {
95-
Map<String, Object> field;
96-
field = new HashMap<>();
97-
field.put("name", name);
98-
rest.postForObject(properties.getHost() + "master/clazz/", field, String.class);
96+
JBClazz clazz = new JBClazz(name);
97+
clazz.save();
9998
System.out.println(Ansi.ansi().fg(Ansi.Color.GREEN).a("Class added.").reset());
10099
} catch (HttpClientErrorException e) {
101100
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a(e.getResponseBodyAsString()).reset());

0 commit comments

Comments
 (0)