Skip to content

Commit 3624236

Browse files
committed
Update some examples with the tinystruct version 1.2.3.
1 parent b23472c commit 3624236

10 files changed

Lines changed: 53 additions & 60 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ To execute it in CLI mode
1414
$ bin/dispatcher --version
1515

1616
_/ ' _ _/ _ _ _/
17-
/ / /) (/ _) / / (/ ( / 0.9.9
17+
/ / /) (/ _) / / (/ ( / 1.2.3
1818
/
1919
```
2020
```tcsh

src/main/java/ApplicationGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static void main(String[]arguments)
1515
for(String className:list)
1616
{
1717
Generator generator=new MySQLGenerator();
18-
generator.setFileName("src/custom/objects/");
18+
generator.setPath("src/custom/objects/");
1919
generator.setPackageName("custom.objects");
2020
generator.importPackages("java.util.Date");
2121
generator.create(className,className);

src/main/java/tinystruct/examples/bible.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@
99
import org.tinystruct.ApplicationRuntimeException;
1010
import org.tinystruct.system.ApplicationManager;
1111
import org.tinystruct.system.Dispatcher;
12+
import org.tinystruct.system.annotation.Action;
1213
import org.tinystruct.system.util.URLResourceLoader;
1314

1415
public class bible extends AbstractApplication {
1516

1617
@Override
1718
public void init() {
1819
// TODO Auto-generated method stub
19-
this.setAction("bible", "open");
2020
}
2121

2222
@Override
2323
public String version() {
2424
return "v1.0";
2525
}
2626

27+
@Action("bible")
2728
public String open(String pattern) throws ApplicationException {
2829
pattern = pattern.replace('.', '/'); // To avoid the conflict with the path query
2930
URL uri;

src/main/java/tinystruct/examples/dateConverter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
import java.text.SimpleDateFormat;
99

1010
import org.tinystruct.AbstractApplication;
11+
import org.tinystruct.system.annotation.Action;
1112

1213
public class dateConverter extends AbstractApplication {
1314

1415
@Override
1516
public void init() {
1617
// TODO Auto-generated method stub
17-
this.setAction("convert", "convert");
1818
}
1919

2020
@Override
@@ -23,6 +23,7 @@ public String version() {
2323
return null;
2424
}
2525

26+
@Action("convert")
2627
public String convert() throws IOException, ParseException {
2728
String from = this.context.getAttribute("from").toString();
2829
String to = this.context.getAttribute("to").toString();

src/main/java/tinystruct/examples/distributedLockApp.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.tinystruct.AbstractApplication;
44
import org.tinystruct.ApplicationException;
55
import org.tinystruct.system.ApplicationManager;
6+
import org.tinystruct.system.annotation.Action;
67
import org.tinystruct.valve.DistributedLock;
78
import org.tinystruct.valve.Lock;
89
import org.tinystruct.valve.Watcher;
@@ -14,9 +15,6 @@ public class distributedLockApp extends AbstractApplication {
1415
@Override
1516
public void init() {
1617
// TODO Auto-generated method stub
17-
this.setAction("lock", "lock");
18-
this.setAction("unlock", "unlock");
19-
this.setAction("monitor", "monitor");
2018
}
2119

2220
@Override
@@ -25,6 +23,7 @@ public String version() {
2523
return null;
2624
}
2725

26+
@Action("lock")
2827
public void lock() throws ApplicationException {
2928
Lock lock = Watcher.getInstance().acquire();
3029

@@ -34,12 +33,14 @@ public void lock() throws ApplicationException {
3433
}
3534
}
3635

36+
@Action("unlock")
3737
public void unlock(String lockId) throws ApplicationException {
3838
Lock lock = new DistributedLock(lockId.getBytes());
3939
System.out.println("UnLock Id:" + lock.id());
4040
lock.unlock();
4141
}
42-
42+
43+
@Action("unlock")
4344
public void unlock() throws ApplicationException {
4445
Lock lock = Watcher.getInstance().acquire();
4546
if (lock != null) {

src/main/java/tinystruct/examples/error.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.tinystruct.http.Request;
99
import org.tinystruct.http.Response;
1010
import org.tinystruct.http.Session;
11+
import org.tinystruct.system.annotation.Action;
1112

1213
public class error extends AbstractApplication {
1314

@@ -18,7 +19,6 @@ public class error extends AbstractApplication {
1819
@Override
1920
public void init() {
2021
// TODO Auto-generated method stub
21-
this.setAction("error", "process");
2222
}
2323

2424
@Override
@@ -27,6 +27,7 @@ public String version() {
2727
return null;
2828
}
2929

30+
@Action("error")
3031
public Object process() throws ApplicationException {
3132
this.request = (Request) this.context.getAttribute("HTTP_REQUEST");
3233
this.response = (Response) this.context.getAttribute("HTTP_RESPONSE");

src/main/java/tinystruct/examples/firstApplication.java

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,28 @@
1010
import org.tinystruct.system.ApplicationManager;
1111

1212
import custom.objects.User;
13+
import org.tinystruct.system.Dispatcher;
14+
import org.tinystruct.system.annotation.Action;
15+
import org.tinystruct.system.annotation.Argument;
1316

1417
public class firstApplication extends AbstractApplication {
1518

1619
@Override
1720
public void init() {
1821
// TODO Auto-generated method stub
19-
this.setAction("praise", "praise");
20-
this.setAction("say", "say");
21-
this.setAction("youhappy", "happy");
22-
23-
this.setAction("user", "findUser");
24-
this.setAction("users", "findUsers");
25-
26-
this.setAction("version", "version", "GET");
27-
this.setAction("version", "setVersion","POST");
28-
29-
this.setAction("read", "read");
30-
31-
this.setAction("generate", "generate_with_user_table");
3222
}
33-
23+
24+
@Action("praise")
3425
public String praise(){
3526
return "Praise to the Lord!";
3627
}
37-
28+
29+
@Action("youhappy")
3830
public boolean happy(){
3931
return true;
4032
}
41-
33+
34+
@Action("read")
4235
public Object read(String json,String name){
4336
Builder builder = new Builder();
4437
try {
@@ -51,35 +44,43 @@ public Object read(String json,String name){
5144
return builder.get(name);
5245
}
5346

47+
@Action("say")
5448
public String say() throws ApplicationException {
5549
if(null != this.context.getAttribute("words"))
5650
return this.context.getAttribute("words").toString();
5751

5852
throw new ApplicationException("Could not find the parameter <i>words</i>.");
5953
}
6054

55+
@Action("say")
6156
public String say(String words){
6257
return words;
6358
}
6459

60+
@Action("version")
6561
@Override
6662
public String version() {
6763
// TODO Auto-generated method stub
6864
return this.context.getAttribute("name") + this.context.getAttribute("number").toString();
6965
}
70-
66+
67+
@Action(value = "version", options = {
68+
@Argument(key = "POST", description = "POST method"),
69+
})
7170
public void setVersion(float number){
7271
this.context.setAttribute("name", "struct");
7372
this.context.setAttribute("number", number);
73+
Dispatcher d;
7474
}
75-
75+
76+
@Action("generate")
7677
public void generate_with_user_table(){
7778
try {
7879
String[] list=new String[]{"User"};
7980
Generator generator=new MySQLGenerator();
8081
for(String className:list)
8182
{
82-
generator.setFileName("src/custom/objects/");
83+
generator.setPath("src/custom/objects/");
8384
generator.setPackageName("custom.objects");
8485
generator.importPackages("java.util.Date");
8586
generator.create(className,className);
@@ -91,7 +92,8 @@ public void generate_with_user_table(){
9192
e.printStackTrace();
9293
}
9394
}
94-
95+
96+
@Action("user")
9597
public User findUser(Object userId) throws ApplicationException{
9698

9799
if(userId!=null){
@@ -104,7 +106,8 @@ public User findUser(Object userId) throws ApplicationException{
104106

105107
return null;
106108
}
107-
109+
110+
@Action("users")
108111
public Table findUsers() throws ApplicationException{
109112
return new User().findAll();
110113
}

src/main/java/tinystruct/examples/hello.java

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
import org.tinystruct.system.ClassFileLoader;
1010
import org.tinystruct.system.Configuration;
1111
import org.tinystruct.system.Settings;
12+
import org.tinystruct.system.annotation.Action;
1213

1314
import javax.servlet.ServletException;
1415
import javax.servlet.http.HttpServletRequest;
1516
import javax.servlet.http.HttpServletResponse;
17+
import java.sql.PreparedStatement;
1618

1719
public class hello extends AbstractApplication {
1820

@@ -23,13 +25,6 @@ public class hello extends AbstractApplication {
2325
@Override
2426
public void init() {
2527
// TODO Auto-generated method stub
26-
this.setAction("say", "say");
27-
this.setAction("smile", "smile");
28-
this.setAction("render", "render");
29-
30-
this.setAction("account", "createAccount");
31-
this.setAction("login", "login");
32-
3328
}
3429

3530
@Override
@@ -38,21 +33,7 @@ public String version() {
3833
return null;
3934
}
4035

41-
public String say() {
42-
if(null != this.context.getAttribute("words"))
43-
return this.context.getAttribute("words").toString();
44-
45-
return "Invalid parameters.";
46-
}
47-
48-
public String say(String words) {
49-
return words;
50-
}
51-
52-
public String smile() throws ApplicationException {
53-
return ":)";
54-
}
55-
36+
@Action("login")
5637
public String login() throws ServletException {
5738
HttpServletRequest request = (HttpServletRequest) this.context.getAttribute("HTTP_REQUEST");
5839
HttpServletResponse response = (HttpServletResponse) this.context.getAttribute("HTTP_RESPONSE");
@@ -70,6 +51,7 @@ public String login() throws ServletException {
7051
return bearer;
7152
}
7253

54+
@Action("render")
7355
public hello render() {
7456
return this;
7557
}
@@ -127,10 +109,12 @@ private String getBearerToken(HttpServletRequest request) {
127109
return null;
128110
}
129111

112+
@Action("account")
130113
public void createAccount() {
131114
try (DatabaseOperator operator = new DatabaseOperator()){
132-
operator.createStatement(false);
133-
operator.execute("DROP TABLE ACCOUNT IF EXISTS; CREATE TABLE ACCOUNT(ID VARCHAR, USERNAME VARCHAR, PASSWORD VARCHAR, EMAIL VARCHAR);");
115+
String sql = "DROP TABLE ACCOUNT IF EXISTS; CREATE TABLE ACCOUNT(ID VARCHAR, USERNAME VARCHAR, PASSWORD VARCHAR, EMAIL VARCHAR);";
116+
PreparedStatement ps = operator.preparedStatement(sql, new Object[]{});
117+
operator.executeUpdate(ps);
134118
} catch (ApplicationException e) {
135119
e.printStackTrace();
136120
}

src/main/java/tinystruct/examples/startup.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import javax.servlet.http.HttpServletResponse;
77

88
import org.tinystruct.AbstractApplication;
9+
import org.tinystruct.system.annotation.Action;
910

1011
public class startup extends AbstractApplication {
1112

@@ -14,9 +15,9 @@ public class startup extends AbstractApplication {
1415
@Override
1516
public void init() {
1617
// TODO Auto-generated method stub
17-
this.setAction("startup", "index");
1818
}
1919

20+
@Action("startup")
2021
public startup index() {
2122
return this;
2223
}

src/main/java/tinystruct/examples/time.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import javax.servlet.http.HttpServletResponse;
77

88
import org.tinystruct.AbstractApplication;
9+
import org.tinystruct.system.annotation.Action;
910

1011
public class time extends AbstractApplication {
1112

@@ -14,20 +15,19 @@ public class time extends AbstractApplication {
1415
@Override
1516
public void init() {
1617
// TODO Auto-generated method stub
17-
this.setAction("time", "index");
18-
this.setAction("time/update", "update");
19-
this.setAction("time/start", "start");
20-
this.setAction("time/stop", "stop");
2118
}
2219

20+
@Action("time")
2321
public time index() {
2422
return this;
2523
}
26-
24+
25+
@Action("time/start")
2726
public void start() {
2827
stop=false;
2928
}
3029

30+
@Action("time/update")
3131
public void update() throws IOException, InterruptedException {
3232

3333
HttpServletResponse response = (HttpServletResponse) this.context
@@ -45,6 +45,7 @@ public void update() throws IOException, InterruptedException {
4545
}
4646
}
4747

48+
@Action("time/stop")
4849
public String stop() {
4950
stop = true;
5051
return "stopped!";

0 commit comments

Comments
 (0)