Compare commits
621 Commits
2.0.0.beta
...
2.4.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2464c360c0 | ||
|
|
1917ccf35c | ||
|
|
4f9c14da97 | ||
|
|
58e23c1b8c | ||
|
|
34156fc092 | ||
|
|
86fde56129 | ||
|
|
d997d3a7bb | ||
|
|
4e689855f4 | ||
|
|
eace16d7fd | ||
|
|
53b7f9b26f | ||
|
|
983c667725 | ||
|
|
96fd660d46 | ||
|
|
7d1770da8a | ||
|
|
b83f6867f3 | ||
|
|
fa7db42f50 | ||
|
|
80c07a5c5b | ||
|
|
59a4c85aeb | ||
|
|
7fc01d38e8 | ||
|
|
3010422f4e | ||
|
|
1f78bb1a98 | ||
|
|
1d7f32efe4 | ||
|
|
1e3d980437 | ||
|
|
8654b5eef8 | ||
|
|
5781ad1de0 | ||
|
|
e2a1fa15d3 | ||
|
|
50da7413d8 | ||
|
|
e45b945024 | ||
|
|
f3668a77ef | ||
|
|
2aee6cab20 | ||
|
|
6233a0b960 | ||
|
|
0ae469d8e2 | ||
|
|
bcad47ebd5 | ||
|
|
71d179b8c3 | ||
|
|
af35e5100e | ||
|
|
96c97c7dd2 | ||
|
|
d5a41fc8b6 | ||
|
|
c3c3afb3c3 | ||
|
|
4f32dcda16 | ||
|
|
67717e73ad | ||
|
|
1753734581 | ||
|
|
3e408e535c | ||
|
|
f154292cb5 | ||
|
|
ac261533c3 | ||
|
|
1982c984ee | ||
|
|
fb5f82c44f | ||
|
|
840cea06db | ||
|
|
3237e2488e | ||
|
|
98e1e40e18 | ||
|
|
def3fc7b2e | ||
|
|
6057b8f90a | ||
|
|
2847f761bb | ||
|
|
b0a2d2ad80 | ||
|
|
564b814f77 | ||
|
|
433585a231 | ||
|
|
6a4750e302 | ||
|
|
748a6f11c1 | ||
|
|
bdd9a82682 | ||
|
|
c551c157d1 | ||
|
|
7e777229ba | ||
|
|
612bfcba37 | ||
|
|
c44e00a100 | ||
|
|
7f46b3194a | ||
|
|
50b7999dee | ||
|
|
871f84b1ff | ||
|
|
4018a95af0 | ||
|
|
ffe3ff5830 | ||
|
|
9b9b0ab10e | ||
|
|
b19e5fe839 | ||
|
|
f6caf41960 | ||
|
|
67d57de166 | ||
|
|
4d79f17a3a | ||
|
|
245d6d6fc7 | ||
|
|
66bd1feadf | ||
|
|
c02e3549eb | ||
|
|
210b8dcf9d | ||
|
|
bbefa788fd | ||
|
|
2f6eb0908e | ||
|
|
51e4d44fc1 | ||
|
|
ed502daeb9 | ||
|
|
d7da527bd5 | ||
|
|
7c5146bc97 | ||
|
|
91cdf9990b | ||
|
|
7d03609462 | ||
|
|
7be9a06e76 | ||
|
|
3eaecacac0 | ||
|
|
9c5e23090c | ||
|
|
c7b05e530d | ||
|
|
889fa25e58 | ||
|
|
da6945224f | ||
|
|
026a727480 | ||
|
|
2404f547ab | ||
|
|
a9dd82dcf4 | ||
|
|
95443be313 | ||
|
|
5d1805b10e | ||
|
|
72eb175dc3 | ||
|
|
d5cbcaaa15 | ||
|
|
bfa722f6da | ||
|
|
6b9d35983a | ||
|
|
6cf50c7cc9 | ||
|
|
cf05851752 | ||
|
|
1cbbf17392 | ||
|
|
96afeb19b2 | ||
|
|
56da72b16e | ||
|
|
b781868876 | ||
|
|
6a05f4b497 | ||
|
|
b9fa617374 | ||
|
|
c59baee1f5 | ||
|
|
98531d6b2f | ||
|
|
8df5b45525 | ||
|
|
ebba966d52 | ||
|
|
169f35d41f | ||
|
|
8bec9e88a7 | ||
|
|
854aa0153f | ||
|
|
77eaaae6ce | ||
|
|
fd7badec0a | ||
|
|
a834b6ea21 | ||
|
|
31fad72172 | ||
|
|
ef14195e71 | ||
|
|
a2fab32356 | ||
|
|
2dd0ecfa49 | ||
|
|
849fe15728 | ||
|
|
76afab3c78 | ||
|
|
21250794f5 | ||
|
|
9f85ceb2ed | ||
|
|
b6ecdef0c5 | ||
|
|
4dfd528533 | ||
|
|
3131601477 | ||
|
|
5921cf5f0d | ||
|
|
788f7d5eb1 | ||
|
|
79cb79481e | ||
|
|
41f0061dca | ||
|
|
f981e4f886 | ||
|
|
ebf30e488a | ||
|
|
7ab889baac | ||
|
|
8d9d893839 | ||
|
|
205233f8c9 | ||
|
|
6ee9527ed9 | ||
|
|
ee56ffa346 | ||
|
|
3fcc478356 | ||
|
|
26e4cd2f4a | ||
|
|
509524144d | ||
|
|
676285e20b | ||
|
|
43edb0f814 | ||
|
|
1c684a4e32 | ||
|
|
156af0e2a4 | ||
|
|
28d9c465fd | ||
|
|
a285510656 | ||
|
|
d6042d142e | ||
|
|
be8d368e72 | ||
|
|
75698d9390 | ||
|
|
9d343694cc | ||
|
|
fddb2a3be0 | ||
|
|
3b44eddbb9 | ||
|
|
ba4100e22d | ||
|
|
72155e31dd | ||
|
|
60903c844e | ||
|
|
5c810b5ff6 | ||
|
|
e3a8c2d392 | ||
|
|
1f27d5f3e3 | ||
|
|
abb790da54 | ||
|
|
f4a2f2af94 | ||
|
|
29a21d060b | ||
|
|
de19861974 | ||
|
|
e5766e31dc | ||
|
|
fca727ecaf | ||
|
|
d009c9b2ed | ||
|
|
fd2e84f781 | ||
|
|
2b7676449d | ||
|
|
4b2c6eba63 | ||
|
|
84147280cf | ||
|
|
e62d9f8201 | ||
|
|
4cec4e64f9 | ||
|
|
9eb647428c | ||
|
|
b68808b325 | ||
|
|
a9dce0efbf | ||
|
|
de63f81238 | ||
|
|
a05afe45c0 | ||
|
|
236996caa1 | ||
|
|
b808c93885 | ||
|
|
7865b74359 | ||
|
|
143e1880d4 | ||
|
|
1be467cfe9 | ||
|
|
7f8f157616 | ||
|
|
c140ad6828 | ||
|
|
50e41dba25 | ||
|
|
c73fc5f266 | ||
|
|
a5f3adc477 | ||
|
|
b77764c1cf | ||
|
|
714b188560 | ||
|
|
47315f188a | ||
|
|
384a8a3991 | ||
|
|
7a80c00439 | ||
|
|
43486ce1e2 | ||
|
|
627c1e953e | ||
|
|
da141cc6bd | ||
|
|
8f3a9a6297 | ||
|
|
e8d496b0ad | ||
|
|
f5accfbe8f | ||
|
|
16e291036e | ||
|
|
a79b8b77b5 | ||
|
|
8e2d6acdf8 | ||
|
|
70c5123f7a | ||
|
|
7cfbaee199 | ||
|
|
fa8d78e18f | ||
|
|
4f5e72a31a | ||
|
|
7c5822fb7c | ||
|
|
60df140bfd | ||
|
|
f41e905842 | ||
|
|
9824fe6da3 | ||
|
|
1fb37f4b67 | ||
|
|
a8a6861a21 | ||
|
|
614222bdc3 | ||
|
|
8e98dda795 | ||
|
|
3686306505 | ||
|
|
d29aa78022 | ||
|
|
85fa406e1d | ||
|
|
d501e2016f | ||
|
|
cf0cd66ac9 | ||
|
|
c921f657b1 | ||
|
|
c93c1d84c5 | ||
|
|
2e66b1a546 | ||
|
|
48629146ed | ||
|
|
a6c8502416 | ||
|
|
fc74dc33c1 | ||
|
|
73de9dfc33 | ||
|
|
68d78a4aa4 | ||
|
|
b618e0e884 | ||
|
|
2d1a6e0edc | ||
|
|
76a7e92787 | ||
|
|
a2178b9a5f | ||
|
|
dcf0fecdb0 | ||
|
|
f88db8abf9 | ||
|
|
0c8b0f5e19 | ||
|
|
b794872147 | ||
|
|
86614f035b | ||
|
|
5f3a472c5e | ||
|
|
7eb2a405d3 | ||
|
|
1d030183bb | ||
|
|
d211692306 | ||
|
|
86c7d95c80 | ||
|
|
e99d43b25c | ||
|
|
730fc0a911 | ||
|
|
1ea92d7165 | ||
|
|
0dfc0ca853 | ||
|
|
474f82b3cf | ||
|
|
ae80109c77 | ||
|
|
807fe63cbe | ||
|
|
44ed3259f9 | ||
|
|
93b2b5fab8 | ||
|
|
58e807439c | ||
|
|
ca2f34bfac | ||
|
|
b8719d7f76 | ||
|
|
be0fdd9045 | ||
|
|
1d2b583c0c | ||
|
|
9def35e2e1 | ||
|
|
a11127ea58 | ||
|
|
af80864e08 | ||
|
|
f6a4fbdf87 | ||
|
|
61f48be2a6 | ||
|
|
8516925537 | ||
|
|
f828ec3d08 | ||
|
|
1eb1706166 | ||
|
|
ffd3b6daf6 | ||
|
|
76d538264a | ||
|
|
02d0a06195 | ||
|
|
c43325e402 | ||
|
|
63bc4b2d00 | ||
|
|
6e5da92263 | ||
|
|
6358719255 | ||
|
|
bdfd92299a | ||
|
|
aa4c9896ba | ||
|
|
1549f1feed | ||
|
|
2a9a713f3e | ||
|
|
0377dac71f | ||
|
|
13217a11c0 | ||
|
|
146b81ee5e | ||
|
|
8502792aad | ||
|
|
3d9447327a | ||
|
|
288fc67621 | ||
|
|
d4bc751a20 | ||
|
|
4cf160e5d3 | ||
|
|
98f29d6a6e | ||
|
|
92bb0a561b | ||
|
|
1329f6f0e1 | ||
|
|
a3dbfaba88 | ||
|
|
2032f39bf9 | ||
|
|
8840415739 | ||
|
|
bdf2fd21c3 | ||
|
|
bd1e326404 | ||
|
|
ebd31bc3d3 | ||
|
|
7185191d91 | ||
|
|
1a1dd44f34 | ||
|
|
95fd147268 | ||
|
|
88657fdf63 | ||
|
|
03de684940 | ||
|
|
b445b99572 | ||
|
|
bd4343c5d8 | ||
|
|
a58c13cd9f | ||
|
|
cf23ecc12c | ||
|
|
86754c2324 | ||
|
|
e6f0a8fdf3 | ||
|
|
fa9fc531d0 | ||
|
|
30bf2c1ad3 | ||
|
|
d326da9344 | ||
|
|
8f6494163d | ||
|
|
490cabefb2 | ||
|
|
66ff86d6fb | ||
|
|
0e5479d55e | ||
|
|
3eb3e0104d | ||
|
|
079e6116b2 | ||
|
|
b6a3adb21c | ||
|
|
ecf831c0f5 | ||
|
|
adfa0be79e | ||
|
|
7ec91802b2 | ||
|
|
1e15eb31c7 | ||
|
|
8d5e61a9a2 | ||
|
|
64500b113a | ||
|
|
a7e5fad571 | ||
|
|
531b00b6fd | ||
|
|
ce3fc5792b | ||
|
|
a477b2fb73 | ||
|
|
6a5d121615 | ||
|
|
90960e0574 | ||
|
|
b6980f7cf8 | ||
|
|
2fb3472de0 | ||
|
|
6dacbafc29 | ||
|
|
f1fcbd7396 | ||
|
|
681e389f19 | ||
|
|
06b7274364 | ||
|
|
b4f7de4858 | ||
|
|
6261232ce6 | ||
|
|
eb35b90950 | ||
|
|
958cea4e0d | ||
|
|
7f184bae50 | ||
|
|
3d68103a19 | ||
|
|
612851bce0 | ||
|
|
e365dae9e4 | ||
|
|
b6d5fc02dc | ||
|
|
59d139ace2 | ||
|
|
1d7a72f992 | ||
|
|
45f2ce261e | ||
|
|
f79e49db5a | ||
|
|
218aebaa0f | ||
|
|
f11870d6bc | ||
|
|
ebea6bb92c | ||
|
|
4806d6ada0 | ||
|
|
6909748bda | ||
|
|
982fc8440c | ||
|
|
e4df07a00c | ||
|
|
0ee30aca7e | ||
|
|
b994bcbcfb | ||
|
|
cc23b44409 | ||
|
|
de36ef697d | ||
|
|
e0d224a330 | ||
|
|
adc2106bec | ||
|
|
6cdbe957a7 | ||
|
|
14808cb01c | ||
|
|
1edadbfee8 | ||
|
|
043a23ecdf | ||
|
|
383ef37989 | ||
|
|
9cd9a8d3ea | ||
|
|
db8c94f433 | ||
|
|
22fb3e5bef | ||
|
|
6329722f17 | ||
|
|
2dee61223d | ||
|
|
f83fc52e9c | ||
|
|
2f8a04c15d | ||
|
|
a1501af7a7 | ||
|
|
a1fdfc9cc9 | ||
|
|
2dddf3c2a5 | ||
|
|
84061cf60f | ||
|
|
557c2c7858 | ||
|
|
cb2d355bc9 | ||
|
|
ed1b642d5b | ||
|
|
411f5e1951 | ||
|
|
450506ca96 | ||
|
|
bf700aa88f | ||
|
|
c108ab196c | ||
|
|
cac8662c01 | ||
|
|
197c58ef98 | ||
|
|
d4c2723759 | ||
|
|
c34a6d8f49 | ||
|
|
5de580c00b | ||
|
|
ed1fb151d7 | ||
|
|
4c4913c5d0 | ||
|
|
adb0dc5963 | ||
|
|
740bc8ae31 | ||
|
|
30a0b12020 | ||
|
|
b0b8b0db3b | ||
|
|
71244732f2 | ||
|
|
820af60c19 | ||
|
|
b3252359bd | ||
|
|
9ade568597 | ||
|
|
cf5224a5f6 | ||
|
|
6c4a83d14d | ||
|
|
0439b95139 | ||
|
|
72a618a0b7 | ||
|
|
0f7520e67b | ||
|
|
fcd69474a2 | ||
|
|
3130e00bab | ||
|
|
1c063b57ec | ||
|
|
efeff3b720 | ||
|
|
ea15634f47 | ||
|
|
e744d001c6 | ||
|
|
06e486e5e2 | ||
|
|
95180ffdef | ||
|
|
e5bd85e9aa | ||
|
|
ec4ec2ff77 | ||
|
|
5a1c9215bc | ||
|
|
53457b73ad | ||
|
|
8cb9fcf01a | ||
|
|
2c88b0e75e | ||
|
|
4daffdc31f | ||
|
|
16dec2cde5 | ||
|
|
82e5fb593e | ||
|
|
4a60ecb3ff | ||
|
|
38307405ed | ||
|
|
fe332d0cbd | ||
|
|
e45f75c1d6 | ||
|
|
a77f450757 | ||
|
|
9f8e946ea3 | ||
|
|
7633687665 | ||
|
|
a1e37643d0 | ||
|
|
8440b58d6c | ||
|
|
a97f8efe21 | ||
|
|
d83e7c22ac | ||
|
|
01a5b32b22 | ||
|
|
8834f57582 | ||
|
|
57fd7b96b4 | ||
|
|
14fe2fbc84 | ||
|
|
0b86edb654 | ||
|
|
07ab74902c | ||
|
|
e4c6e860c1 | ||
|
|
02cf587fcf | ||
|
|
081773163b | ||
|
|
292ff63699 | ||
|
|
f1a97c0219 | ||
|
|
e545010034 | ||
|
|
ef57fa9a25 | ||
|
|
1bd0d891a4 | ||
|
|
9a51c1de1d | ||
|
|
e4bb75dd8e | ||
|
|
66e1e08ae2 | ||
|
|
708cd5a644 | ||
|
|
138c9aba97 | ||
|
|
e5e17b4496 | ||
|
|
cadaebefb9 | ||
|
|
e2fb7935f6 | ||
|
|
16b3dab600 | ||
|
|
758972890e | ||
|
|
a88285d935 | ||
|
|
d7832fb931 | ||
|
|
f073c9a0bc | ||
|
|
63ef83ec62 | ||
|
|
51c50415e1 | ||
|
|
2f08fa6476 | ||
|
|
3091972fc5 | ||
|
|
8e14df9a95 | ||
|
|
683cec6f4d | ||
|
|
ec6e5aa3b1 | ||
|
|
f4c00a9b6f | ||
|
|
c071ec5d6c | ||
|
|
29148c4b42 | ||
|
|
7340caa4a9 | ||
|
|
7d23dfa73d | ||
|
|
19932820a9 | ||
|
|
6b59c54087 | ||
|
|
58f5ee999a | ||
|
|
32e8c033ea | ||
|
|
84a96f750f | ||
|
|
6560e71e48 | ||
|
|
ee8a18a528 | ||
|
|
c36fc36964 | ||
|
|
a34f85bfc9 | ||
|
|
6f00efa077 | ||
|
|
469dff8478 | ||
|
|
c50eb79b1d | ||
|
|
985bf6ed25 | ||
|
|
bd51696e08 | ||
|
|
2764d59a4f | ||
|
|
c1509bb712 | ||
|
|
b14f32deb3 | ||
|
|
62145a2aad | ||
|
|
8fb88a57b2 | ||
|
|
44f12ae003 | ||
|
|
bf97ef3a08 | ||
|
|
aeefc3b8a2 | ||
|
|
5763718816 | ||
|
|
3b142b7504 | ||
|
|
0f52d32424 | ||
|
|
f5f3c48f38 | ||
|
|
9f9f5aa000 | ||
|
|
be4ca0287b | ||
|
|
a84ed72f28 | ||
|
|
8eb5f56f42 | ||
|
|
df1aa2b379 | ||
|
|
da2befcb97 | ||
|
|
92d0f7e796 | ||
|
|
6aa3949d05 | ||
|
|
fa833d9224 | ||
|
|
c9261f8475 | ||
|
|
a1df62af08 | ||
|
|
aa12413f4e | ||
|
|
2ccd9ba10f | ||
|
|
81ae68c571 | ||
|
|
51b45f4713 | ||
|
|
66e1f58879 | ||
|
|
06bb5180cf | ||
|
|
d83d7f879c | ||
|
|
b8e92c949e | ||
|
|
91548a0ca9 | ||
|
|
dbca25cd54 | ||
|
|
539ea15ae5 | ||
|
|
ad1d9f33d4 | ||
|
|
df98c1a58e | ||
|
|
56d1969c96 | ||
|
|
4b341436af | ||
|
|
4d3d73b4c1 | ||
|
|
55ab279e7f | ||
|
|
82ab994608 | ||
|
|
1c4035e677 | ||
|
|
afdc9e7207 | ||
|
|
9b83abb06a | ||
|
|
26d1a10bd0 | ||
|
|
7e55dcc46d | ||
|
|
45802d2403 | ||
|
|
3660a2a4e5 | ||
|
|
bed81bd93d | ||
|
|
131855cdc5 | ||
|
|
8b69e7d02b | ||
|
|
02a10bf014 | ||
|
|
004b83172e | ||
|
|
fee4555cef | ||
|
|
758bd7de72 | ||
|
|
b2dd366640 | ||
|
|
934c82eadd | ||
|
|
c7ed6574cc | ||
|
|
2ea2667fa7 | ||
|
|
34ae2d38c5 | ||
|
|
a1c95544cb | ||
|
|
c6dc38c35c | ||
|
|
39203ab598 | ||
|
|
51a95a84aa | ||
|
|
8a8d45e642 | ||
|
|
52eb7dbc0c | ||
|
|
0e14b60f12 | ||
|
|
d373ab7204 | ||
|
|
4f9a563ba7 | ||
|
|
852da19b1e | ||
|
|
ddfc040a53 | ||
|
|
df3ccb763a | ||
|
|
f42561ca93 | ||
|
|
580e28519a | ||
|
|
9ecc1d8f19 | ||
|
|
40629ed7b9 | ||
|
|
5790135add | ||
|
|
fd862ed6c6 | ||
|
|
33763af96c | ||
|
|
7c05df3cfb | ||
|
|
f471e2d4c5 | ||
|
|
d4fd093521 | ||
|
|
40003c7789 | ||
|
|
b94f99f338 | ||
|
|
bd21644571 | ||
|
|
5f3599d9b8 | ||
|
|
1e4a30bd70 | ||
|
|
e7dc5de9f2 | ||
|
|
ccb9cb28f5 | ||
|
|
4d9b72c922 | ||
|
|
a51ae13a39 | ||
|
|
dfca186688 | ||
|
|
fadd229a89 | ||
|
|
7acc69adc4 | ||
|
|
d88e4120a1 | ||
|
|
ef98edd91a | ||
|
|
f4548bbe34 | ||
|
|
11a5faca1d | ||
|
|
c37b0e8cb5 | ||
|
|
a20570a6eb | ||
|
|
5e3edb7e1d | ||
|
|
ad8f1d2da6 | ||
|
|
24b23c894f | ||
|
|
c551d5fb81 | ||
|
|
fba43894c1 | ||
|
|
22cc7e086c | ||
|
|
1791008729 | ||
|
|
90e15dd253 | ||
|
|
7db73c076c | ||
|
|
95ad6e99d9 | ||
|
|
0b2a5d0f61 | ||
|
|
b7acce0814 | ||
|
|
8744e76cad | ||
|
|
446b3c13dc | ||
|
|
3951e28148 | ||
|
|
b74d679608 | ||
|
|
edbc878b73 | ||
|
|
f706209ec1 | ||
|
|
bf000b188f | ||
|
|
def1736a9b | ||
|
|
0242f4c0c3 | ||
|
|
5cd399b2df | ||
|
|
9ddb662016 | ||
|
|
2947275d54 | ||
|
|
e43f814872 | ||
|
|
6e16f52e28 | ||
|
|
fca13557df | ||
|
|
824a6df55a | ||
|
|
b98b526c50 | ||
|
|
8f6aa4f4a5 | ||
|
|
00a07a79b2 | ||
|
|
264dfbef2e | ||
|
|
2e27814809 | ||
|
|
f767f40e56 | ||
|
|
543ecc071a | ||
|
|
2a14f39495 | ||
|
|
95c8ae2334 | ||
|
|
338ea13828 | ||
|
|
4a8b9e5fec | ||
|
|
e281cac3d3 | ||
|
|
a495829a3c | ||
|
|
b0deed2a89 |
7
bin/command.bat
Normal file
7
bin/command.bat
Normal file
@@ -0,0 +1,7 @@
|
||||
@ECHO OFF
|
||||
|
||||
SET APP_HOME=%~dp0
|
||||
|
||||
IF NOT EXIST "%APP_HOME%\conf\application.xml" SET APP_HOME=%~dp0..
|
||||
|
||||
java -DCMD=%1 -DAPP_HOME="%APP_HOME%" -classpath "%APP_HOME%"\lib\* org.redkale.boot.Application
|
||||
23
bin/command.sh
Normal file
23
bin/command.sh
Normal file
@@ -0,0 +1,23 @@
|
||||
#!/bin/sh
|
||||
|
||||
export LC_ALL="zh_CN.UTF-8"
|
||||
|
||||
APP_HOME=`dirname "$0"`
|
||||
|
||||
cd "$APP_HOME"/..
|
||||
|
||||
APP_HOME=`pwd`
|
||||
|
||||
if [ ! -f "$APP_HOME"/conf/application.xml ]; then
|
||||
APP_HOME="$APP_HOME"/..
|
||||
fi
|
||||
|
||||
lib="$APP_HOME"/lib
|
||||
for jar in `ls $APP_HOME/lib/*.jar`
|
||||
do
|
||||
lib=$lib:$jar
|
||||
done
|
||||
export CLASSPATH=$CLASSPATH:$lib
|
||||
|
||||
echo "$APP_HOME"
|
||||
java -DCMD=$1 -DAPP_HOME="$APP_HOME" org.redkale.boot.Application
|
||||
@@ -4,5 +4,5 @@ SET APP_HOME=%~dp0
|
||||
|
||||
IF NOT EXIST "%APP_HOME%\conf\application.xml" SET APP_HOME=%~dp0..
|
||||
|
||||
java -DAPP_HOME="%APP_HOME%" -classpath "%APP_HOME%"\lib\* org.redkale.boot.Application
|
||||
java -server -DAPP_HOME="%APP_HOME%" -classpath "%APP_HOME%"\lib\* org.redkale.boot.Application
|
||||
|
||||
|
||||
@@ -24,5 +24,5 @@ done
|
||||
export CLASSPATH=$CLASSPATH:$lib
|
||||
|
||||
echo "$APP_HOME"
|
||||
nohup java -DAPP_HOME="$APP_HOME" org.redkale.boot.Application > "$APP_HOME"/logs.out &
|
||||
nohup java -server -DAPP_HOME="$APP_HOME" org.redkale.boot.Application > "$APP_HOME"/logs.out &
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<application port="2121">
|
||||
<application nodeid="10000" port="2121">
|
||||
|
||||
<!-- 详细配置说明见: http://redkale.org/redkale.html#redkale_confxml -->
|
||||
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
<EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD>jarĬ<EFBFBD>Ϸ<EFBFBD><EFBFBD>ڴ˴<EFBFBD>
|
||||
@@ -16,10 +16,6 @@
|
||||
<directory>${project.basedir}/conf</directory>
|
||||
<outputDirectory>conf</outputDirectory>
|
||||
</fileSet>
|
||||
<fileSet>
|
||||
<directory>${project.basedir}/libs</directory>
|
||||
<outputDirectory>libs</outputDirectory>
|
||||
</fileSet>
|
||||
<fileSet>
|
||||
<directory>${project.basedir}/logs</directory>
|
||||
<outputDirectory>logs</outputDirectory>
|
||||
|
||||
17
my/pom.xml
17
my/pom.xml
@@ -6,7 +6,7 @@
|
||||
<packaging>jar</packaging>
|
||||
<url>http://redkale.org</url>
|
||||
<description>redkale -- java framework</description>
|
||||
<version>1.6.2</version>
|
||||
<version>2.2.0</version>
|
||||
<licenses>
|
||||
<license>
|
||||
<name>Apache 2</name>
|
||||
@@ -37,8 +37,8 @@
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
<name>Redkale</name>
|
||||
<distributionManagement>
|
||||
@@ -61,7 +61,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<version>3.8.0</version>
|
||||
<configuration>
|
||||
<compilerArgument>-parameters</compilerArgument>
|
||||
<encoding>UTF-8</encoding>
|
||||
@@ -74,9 +74,10 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.6</version>
|
||||
<version>3.2.0</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<addMavenDescriptor>false</addMavenDescriptor>
|
||||
<manifest>
|
||||
<mainClass>org.redkale.boot.Application</mainClass>
|
||||
</manifest>
|
||||
@@ -101,7 +102,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<version>3.2.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
@@ -113,7 +114,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<version>3.2.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
@@ -126,7 +127,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<version>3.2.0</version>
|
||||
<configuration>
|
||||
<appendAssemblyId>false</appendAssemblyId>
|
||||
<descriptors>
|
||||
|
||||
@@ -19,11 +19,13 @@
|
||||
serviceid1_name1 serviceid1_name2 serviceid2_name1 serviceid2_name2
|
||||
-->
|
||||
<!--
|
||||
address: 本地局域网的IP地址, 默认值为默认网卡的ip,当不使用默认值需要指定值,如192.168.1.22
|
||||
nodeid: int 进程的节点ID,用于分布式环境,一个系统中节点ID必须全局唯一,使用cluster时框架会进行唯一性校验
|
||||
name: 进程的名称,用于监控识别,命名规则: 字母、数字、下划线
|
||||
address: 本地局域网的IP地址, 默认值为默认网卡的ip,当不使用默认值需要指定值,如192.168.1.22
|
||||
port: required 程序的管理Server的端口,用于关闭或者与监管系统进行数据交互
|
||||
lib: 加上额外的lib路径,多个路径用分号;隔开; 默认为空。 例如: ${APP_HOME}/lib/a.jar;${APP_HOME}/lib2/b.jar;
|
||||
lib: 加上额外的lib路径,多个路径用分号;隔开; 默认为空。 例如: ${APP_HOME}/lib/a.jar;${APP_HOME}/lib2/b.jar;
|
||||
-->
|
||||
<application port="6560" lib="">
|
||||
<application nodeid="1000" port="6560" lib="">
|
||||
|
||||
<!--
|
||||
【节点全局唯一】
|
||||
@@ -31,24 +33,73 @@
|
||||
-->
|
||||
<resources>
|
||||
|
||||
<!--
|
||||
【节点全局唯一】 @since 2.3.0
|
||||
全局Serivce执行的线程池, Application.workExecutor, 没配置该节点将自动创建一个。
|
||||
threads: 线程数,为0表示不启用workExecutor,只用IO线程。默认: CPU核数, 核数=1的情况下默认值为2
|
||||
hash: 是否使用ThreadHashExecutor作为线程池,默认值为:false
|
||||
-->
|
||||
<executor threads="4" hash="false"/>
|
||||
|
||||
<!--
|
||||
【节点全局唯一】
|
||||
transport节点只能有一个,用于配置所有Transport的池参数,没配置该节点将自动创建一个。
|
||||
threads: 线程总数, 默认: <group>节点数*CPU核数*2
|
||||
bufferCapacity: ByteBuffer的初始化大小, 默认: 32K;
|
||||
bufferPoolSize: ByteBuffer池的大小,默认: 线程总数*4
|
||||
readTimeoutSeconds: TCP读取超时秒数, 默认为6秒, 为0表示无超时限制
|
||||
threads: 线程总数, 默认: <group>节点数*CPU核数*2
|
||||
bufferCapacity: ByteBuffer的初始化大小, 默认: 32K;
|
||||
bufferPoolSize: ByteBuffer池的大小,默认: 线程总数*4
|
||||
readTimeoutSeconds: TCP读取超时秒数, 默认为6秒, 为0表示无超时限制
|
||||
writeTimeoutSeconds: TCP写入超时秒数, 默认为6秒, 为0表示无超时限制
|
||||
strategy: 远程请求的负载均衡策略, 必须是org.redkale.net.TransportStrategy的实现类
|
||||
strategy: 远程请求的负载均衡策略, 必须是org.redkale.net.TransportStrategy的实现类
|
||||
-->
|
||||
<transport bufferCapacity="32K" bufferPoolSize="32" threads="32" readTimeoutSeconds="6" writeTimeoutSeconds="6"/>
|
||||
|
||||
<!--
|
||||
【节点全局唯一】
|
||||
自动扫描时排除部分包路径
|
||||
value: 排除lib.path与excludes中的正则表达式匹配的路径, 多个正则表达式用分号;隔开
|
||||
-->
|
||||
<excludelibs value="^.*mysql.*$;^.*kafka.*$"/>
|
||||
|
||||
<!--
|
||||
【节点全局唯一】
|
||||
第三方服务发现管理接口
|
||||
value: 类名,必须是org.redkale.cluster.ClusterAgent的子类
|
||||
waits: 注销服务后是否需要等待检查周期时间后再进行Service销毁,默认值为:false
|
||||
当一个Service进行服务注销后,不能立刻销毁Service,因为健康检测是有间隔时间差的,
|
||||
需要等待一个健康检测周期时间,让其他进程都更新完服务列表。
|
||||
如果使用MQ,可以设置为false,如果对服务健壮性要求高,建议设置为true
|
||||
protocols: 服务发现可以处理的协议, 默认值为: SNCP, 多个协议用分号;隔开
|
||||
ports: 服务发现可以处理的端口, 多个端口用分号;隔开
|
||||
-->
|
||||
<!--
|
||||
<cluster value="org.redkalex.cluster.consul.ConsulClusterAgent" waits="false" protocols="SNCP" ports="7070;7071">
|
||||
<property name="xxxxxx" value="XXXXXXXX"/>
|
||||
</cluster>
|
||||
-->
|
||||
<!--
|
||||
MQ管理接口配置
|
||||
不同MQ节点所配置的MQ集群不能重复。
|
||||
MQ跟着协议走,所以mq的属性值需要被赋值在rest节点上, 由于SncpServlet是自动生成的,故SNCP协议下,mq属性值被赋值在service/services节点上
|
||||
name: 服务的名称,用于监控识别,多个mq节点时只能有一个name为空的节点,mq.name不能重复,命名规则: 字母、数字、下划线
|
||||
value: 实现类名,必须是org.redkale.mq.MessageAgent的子类
|
||||
MQ节点下的子节点配置没有固定格式, 根据MessageAgent实现方的定义来配置
|
||||
-->
|
||||
<!--
|
||||
<mq name="" value="org.redkalex.mq.kafka.KafkaMessageAgent">
|
||||
<servers value="127.0.0.1:9101"/>
|
||||
<consumer>
|
||||
<property name="xxxxxx" value="XXXXXXXX"/>
|
||||
</consumer>
|
||||
<producer>
|
||||
<property name="xxxxxx" value="XXXXXXXX"/>
|
||||
</producer>
|
||||
</mq>
|
||||
-->
|
||||
<!--
|
||||
一个组包含多个node, 同一Service服务可以由多个进程提供,这些进程称为一个GROUP,且同一GROUP内的进程必须在同一机房或局域网内
|
||||
一个group节点对应一个 Transport 对象。
|
||||
name: 服务组ID,长度不能超过11个字节. 默认为空字符串。 注意: name不能包含$符号。
|
||||
protocol:值范围:UDP TCP, 默认TCP
|
||||
subprotocol: 子协议,预留字段。默认值为空
|
||||
name: 服务组ID,长度不能超过11个字节. 默认为空字符串。 注意: name不能包含$符号。
|
||||
protocol: 值范围:UDP TCP, 默认TCP
|
||||
注意: 一个node只能所属一个group。只要存在protocol=SNCP的Server节点信息, 就必须有group节点信息。
|
||||
-->
|
||||
<group name="" protocol="TCP">
|
||||
@@ -63,10 +114,10 @@
|
||||
|
||||
<!--
|
||||
全局的数据源设置, 可以是CacheSource、DataSource, JDBC的DataSource通常通过persistence.xml配置,此处多用于CacheSource的配置
|
||||
name: 资源名,用于依赖注入。
|
||||
value:类名,必须是CacheSource或DataSource的子类,且必须实现Service接口。如果是DataSource.class,系统自动映射成DataJdbcSource.class
|
||||
groups: 指定groups。
|
||||
xxx: 其他属性与子节点通过Service.init方法传入的AnyValue获取。
|
||||
name: 资源名,用于依赖注入。
|
||||
value: 类名,必须是CacheSource或DataSource的子类,且必须实现Service接口。如果是DataSource.class,系统自动映射成DataJdbcSource.class
|
||||
groups: 指定groups。
|
||||
xxx: 其他属性与子节点通过Service.init方法传入的AnyValue获取。
|
||||
-->
|
||||
<source name="redis" value="org.redkalex.cache.RedisCacheSource" xxx="16">
|
||||
<node addr="127.0.0.1" port="7070"/>
|
||||
@@ -88,12 +139,9 @@
|
||||
System.setProperty("net.transport.poolmaxconns", "100");
|
||||
System.setProperty("net.transport.pinginterval", "30");
|
||||
System.setProperty("net.transport.checkinterval", "30");
|
||||
System.setProperty("convert.json.tiny", "true");
|
||||
System.setProperty("convert.bson.tiny", "true");
|
||||
System.setProperty("convert.json.pool.size", "128");
|
||||
System.setProperty("convert.bson.pool.size", "128");
|
||||
System.setProperty("convert.json.writer.buffer.defsize", "4096");
|
||||
System.setProperty("convert.bson.writer.buffer.defsize", "4096");
|
||||
System.setProperty("convert.tiny", "true");
|
||||
System.setProperty("convert.pool.size", "128");
|
||||
System.setProperty("convert.writer.buffer.defsize", "4096");
|
||||
|
||||
<properties>节点下也可包含非<property>节点.
|
||||
非<property>其节点可以通过@Resource(name="properties.xxxxxx")进行注入, 被注解的字段类型只能是AnyValue、AnyValue[]
|
||||
@@ -107,35 +155,35 @@
|
||||
|
||||
</resources>
|
||||
<!--
|
||||
protocol: required server所启动的协议,Redkale内置的有HTTP、SNCP、WATCH。协议均使用TCP实现; WATCH服务只能存在一个。
|
||||
name: 服务的名称,用于监控识别,一个配置文件中的server.name不能重复,命名规则: 字母、数字、下划线
|
||||
host: 服务所占address , 默认: 0.0.0.0
|
||||
port: required 服务所占端口
|
||||
root: 如果是web类型服务,则包含页面 默认:{APP_HOME}/root
|
||||
lib: server额外的class目录, 默认为${APP_HOME}/libs/*;
|
||||
excludelibs: 排除lib.path与excludes中的正则表达式匹配的路径, 多个正则表达式用分号;隔开
|
||||
charset: 文本编码, 默认: UTF-8
|
||||
backlog: 默认10K
|
||||
threads: 线程数, 默认: CPU核数*32
|
||||
maxconns:最大连接数, 小于1表示无限制, 默认: 0
|
||||
maxbody: request.body最大值, 默认: 64K
|
||||
bufferCapacity: ByteBuffer的初始化大小, TCP默认: 32K; (HTTP 2.0、WebSocket,必须要16k以上); UDP默认: 1350B
|
||||
bufferPoolSize: ByteBuffer池的大小,默认: 线程数*4
|
||||
responsePoolSize: Response池的大小,默认: 线程数*2
|
||||
protocol: required server所启动的协议,Redkale内置的有HTTP、SNCP、WATCH。协议均使用TCP实现; WATCH服务只能存在一个。
|
||||
name: 服务的名称,用于监控识别,一个配置文件中的server.name不能重复,命名规则: 字母、数字、下划线
|
||||
host: 服务所占address , 默认: 0.0.0.0
|
||||
port: required 服务所占端口
|
||||
root: 如果是web类型服务,则包含页面 默认:{APP_HOME}/root
|
||||
lib: server额外的class目录, 默认为${APP_HOME}/libs/*;
|
||||
excludelibs: 排除lib.path与excludes中的正则表达式匹配的路径, 多个正则表达式用分号;隔开
|
||||
charset: 文本编码, 默认: UTF-8
|
||||
backlog: 默认10K
|
||||
threads: 线程数, 默认: CPU核数*2,最小8个【已废弃 @since 2.3.0】
|
||||
maxconns: 最大连接数, 小于1表示无限制, 默认: 0
|
||||
maxbody: request.body最大值, 默认: 64K
|
||||
bufferCapacity: ByteBuffer的初始化大小, TCP默认: 32K; (HTTP 2.0、WebSocket,必须要16k以上); UDP默认: 1350B
|
||||
bufferPoolSize: ByteBuffer池的大小,默认: 线程数*4
|
||||
responsePoolSize: Response池的大小,默认: 线程数*2
|
||||
aliveTimeoutSeconds: KeepAlive读操作超时秒数, 默认30, 0表示永久不超时; -1表示禁止KeepAlive
|
||||
readTimeoutSeconds: 读操作超时秒数, 默认0, 表示永久不超时
|
||||
writeTimeoutSeconds: 写操作超时秒数, 默认0, 表示永久不超时
|
||||
netimpl: ProtocolServer的实现类。TCP情况下值可以是aio或nio,默认值为aio;UDP情况下值可以是bio,默认值为bio;
|
||||
interceptor: 启动/关闭NodeServer时被调用的拦截器实现类,必须是org.redkale.boot.NodeInterceptor的子类,默认为null
|
||||
readTimeoutSeconds: 读操作超时秒数, 默认0, 表示永久不超时
|
||||
writeTimeoutSeconds: 写操作超时秒数, 默认0, 表示永久不超时
|
||||
iogroup: 流线程组AsyncGroup对象,如果值为client,表示和Application.asyncGroup对象共用
|
||||
interceptor: 启动/关闭NodeServer时被调用的拦截器实现类,必须是org.redkale.boot.NodeInterceptor的子类,默认为null
|
||||
-->
|
||||
<server protocol="HTTP" host="127.0.0.1" port="6060" root="root" lib="">
|
||||
|
||||
<!--
|
||||
【节点在<server>中唯一】
|
||||
value: 创建SSLContext的实现类, 可自定义,必须是org.redkale.net.SSLCreator的子类
|
||||
clientauth: true/false/want
|
||||
keystorepass: KEY密码
|
||||
keystorefile: KEY文件
|
||||
clientauth: true/false/want
|
||||
keystorepass: KEY密码
|
||||
keystorefile: KEY文件
|
||||
truststorepass: TRUST密码
|
||||
truststorefile: TRUST文件
|
||||
-->
|
||||
@@ -146,11 +194,13 @@
|
||||
在同一个进程中同一个name同一类型的Service将共用同一个实例
|
||||
autoload="true" 默认值. 自动加载classpath下所有的Service类
|
||||
autoload="false" 需要显著的指定Service类
|
||||
mq: 所属的MQ管理器,当 protocol == SNCP 时该值才有效, 存在该属性表示Service的SNCP协议采用消息总线代理模式
|
||||
includes: 当autoload="true", 拉取类名与includes中的正则表达式匹配的类, 多个正则表达式用分号;隔开
|
||||
excludes: 当autoload="true", 排除类名与excludes中的正则表达式匹配的类, 多个正则表达式用分号;隔开
|
||||
groups: 所属组的节点,多个节点值用;隔开,如果配置文件中存在多个SNCP协议的Server节点,需要显式指定group属性.
|
||||
当 protocol == SNCP 时 group表示当前Server与哪些节点组关联。
|
||||
当 protocol != SNCP 时 group只能是空或者一个group的节点值,不能为多个节点值。
|
||||
groups: 所属组的节点,多个节点值用;隔开,如果配置文件中存在多个SNCP协议的Server节点,需要显式指定group属性.
|
||||
当 protocol == SNCP 时 group表示当前Server与哪些节点组关联。
|
||||
当 protocol != SNCP 时 group只能是空或者一个group的节点值,不能为多个节点值。
|
||||
特殊值"$cluster", 视为通过第三方服务注册发现管理工具来获取远程模式的ip端口信息
|
||||
-->
|
||||
<services autoload="true" includes="" excludes="">
|
||||
|
||||
@@ -158,6 +208,7 @@
|
||||
<service value="com.xxx.XXX1Service"/>
|
||||
<!--
|
||||
name: 显式指定name,覆盖默认的空字符串值。 注意: name不能包含$符号。
|
||||
mq: 所属的MQ管理器,当 protocol == SNCP 时该值才有效, 存在该属性表示Service的SNCP协议采用消息总线代理模式
|
||||
groups: 显式指定groups,覆盖<services>节点的groups默认值。
|
||||
ignore: 是否禁用, 默认为false。
|
||||
-->
|
||||
@@ -174,8 +225,8 @@
|
||||
加载所有的Filter服务;
|
||||
autoload="true" 默认值.
|
||||
autoload="false" 需要显著的指定Filter类
|
||||
includes: 当autoload="true", 拉取类名与includes中的正则表达式匹配的类, 多个正则表达式用分号;隔开
|
||||
excludes: 当autoload="true", 排除类名与excludes中的正则表达式匹配的类, 多个正则表达式用分号;隔开
|
||||
includes: 当autoload="true", 拉取类名与includes中的正则表达式匹配的类, 多个正则表达式用分号;隔开
|
||||
excludes: 当autoload="true", 排除类名与excludes中的正则表达式匹配的类, 多个正则表达式用分号;隔开
|
||||
-->
|
||||
<filters autoload="true" includes="" excludes="">
|
||||
|
||||
@@ -198,8 +249,9 @@
|
||||
<!--
|
||||
REST的核心配置项
|
||||
当Server为HTTP协议时, rest节点才有效。存在[rest]节点则Server启动时会加载REST服务, 节点可以多个,(WATCH协议不需要设置,系统会自动生成)
|
||||
path: servlet的ContextPath前缀 默认为空
|
||||
path: servlet的ContextPath前缀 默认为空 【注: 开启MQ时,该字段失效】
|
||||
base: REST服务的BaseServlet,必须是 org.redkale.net.http.HttpServlet 的子类,且子类必须标记@HttpUserType。
|
||||
mq: 所属的MQ管理器, 存在该属性表示RestService的请求来自于消息总线 【注: 开启MQ时,path字段失效】
|
||||
autoload:默认值"true" 默认值. 加载当前server所能使用的Servce对象;
|
||||
includes:当autoload="true", 拉取类名与includes中的正则表达式匹配的类, 多个正则表达式用分号;隔开
|
||||
excludes:当autoload="true", 排除类名与excludes中的正则表达式匹配的类, 多个正则表达式用分号;隔开
|
||||
@@ -256,28 +308,29 @@
|
||||
【节点在<server>中唯一】
|
||||
当Server为HTTP协议时,render才有效. 指定输出引擎的实现类
|
||||
value: 输出引擎的实现类, 必须是org.redkale.net.http.HttpRender的子类
|
||||
suffixs: 引擎文件名后缀,多个用;隔开,默认值为: .htel
|
||||
-->
|
||||
<render value="org.redkalex.htel.HttpTemplateRender"/>
|
||||
<render value="org.redkalex.htel.HttpTemplateRender" suffixs=".htel"/>
|
||||
<!--
|
||||
【节点在<server>中唯一】
|
||||
当Server为HTTP协议时,ResourceServlet才有效. 默认存在一个有默认属性的resource-servlet节点
|
||||
webroot: web资源的根目录, 默认取server节点中的root值
|
||||
servlet: 静态资源HttpServlet的实现,默认使用HttpResourceServlet
|
||||
index : 启始页,默认值:index.html
|
||||
index : 启始页,默认值:index.html
|
||||
-->
|
||||
<resource-servlet webroot="root" index="index.html">
|
||||
<!--
|
||||
【节点在<resource-servlet>中唯一】
|
||||
资源缓存的配置, 默认存在一个含默认属性的caches节点
|
||||
limit: 资源缓存最大容量, 默认: 0, 为0表示不缓存, 单位可以是B、K、M、G,不区分大小写
|
||||
limit: 资源缓存最大容量, 默认: 0, 为0表示不缓存, 单位可以是B、K、M、G,不区分大小写
|
||||
lengthmax: 可缓存的文件大小上限, 默认: 1M(超过1M的文件不会被缓存)
|
||||
watch: 是否监控缓存文件的变化, 默认为false,不监控
|
||||
watch: 是否监控缓存文件的变化, 默认为false,不监控
|
||||
-->
|
||||
<cache limit="0M" lengthmax="1M" watch="false"/>
|
||||
<!--
|
||||
支持类似nginx中的rewrite, 目前只支持静态资源对静态资源的跳转。
|
||||
type: 匹配的类型, 目前只支持location(匹配requestURI), 默认: location
|
||||
match: 匹配的正则表达式
|
||||
type: 匹配的类型, 目前只支持location(匹配requestURI), 默认: location
|
||||
match: 匹配的正则表达式
|
||||
forward: 需跳转后的资源链接
|
||||
例如下面例子是将/xxx-yyy.html的页面全部跳转到/xxx.html
|
||||
-->
|
||||
@@ -285,11 +338,11 @@
|
||||
</resource-servlet>
|
||||
<!--
|
||||
加载所有的Servlet服务;
|
||||
path: servlet的ContextPath前缀 默认为空
|
||||
path: servlet的ContextPath前缀 默认为空
|
||||
autoload="true" 默认值. 自动加载classpath下所有的Servlet类
|
||||
autoload="false" 需要显著的指定Service类
|
||||
includes: 当autoload="true", 拉取类名与includes中的正则表达式匹配的类, 多个正则表达式用分号;隔开
|
||||
excludes: 当autoload="true", 排除类名与excludes中的正则表达式匹配的类, 多个正则表达式用分号;隔开
|
||||
includes: 当autoload="true", 拉取类名与includes中的正则表达式匹配的类, 多个正则表达式用分号;隔开
|
||||
excludes: 当autoload="true", 排除类名与excludes中的正则表达式匹配的类, 多个正则表达式用分号;隔开
|
||||
-->
|
||||
<servlets path="/pipes" autoload="true" includes="" excludes="">
|
||||
<!--
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
-->
|
||||
<property name="javax.persistence.cachemode" value="ALL"/>
|
||||
|
||||
<!-- 多个URL用;隔开,如分布式SearchSource需要配多个URL -->
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/dbuser?characterEncoding=utf8"/>
|
||||
<!--
|
||||
javax.persistence.jdbc.driver在JPA的值是JDBC驱动,Redkale有所不同,值应该是javax.sql.DataSource的子类。
|
||||
|
||||
@@ -51,4 +51,12 @@ public @interface Cacheable {
|
||||
* @return int
|
||||
*/
|
||||
int interval() default 0;
|
||||
|
||||
/**
|
||||
* DataSource是否直接返回对象的真实引用, 而不是copy一份
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
boolean direct() default false;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
/** *****************************************************************************
|
||||
* Copyright (c) 2008 - 2013 Oracle Corporation. All rights reserved.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
|
||||
* which accompanies this distribution.
|
||||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Linda DeMichiel - Java Persistence 2.1
|
||||
* Linda DeMichiel - Java Persistence 2.0
|
||||
*
|
||||
***************************************************************************** */
|
||||
package javax.persistence;
|
||||
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.annotation.Retention;
|
||||
import static java.lang.annotation.ElementType.FIELD;
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* Provides for the specification of generation strategies for the
|
||||
* values of primary keys.
|
||||
*
|
||||
* <p>
|
||||
* The <code>GeneratedValue</code> annotation
|
||||
* may be applied to a primary key property or field of an entity or
|
||||
* mapped superclass in conjunction with the {@link Id} annotation.
|
||||
* The use of the <code>GeneratedValue</code> annotation is only
|
||||
* required to be supported for simple primary keys. Use of the
|
||||
* <code>GeneratedValue</code> annotation is not supported for derived
|
||||
* primary keys.
|
||||
*
|
||||
* <pre>
|
||||
*
|
||||
* Example 1:
|
||||
*
|
||||
* @Id
|
||||
* @GeneratedValue(strategy=SEQUENCE, generator="CUST_SEQ")
|
||||
* @Column(name="CUST_ID")
|
||||
* public Long getId() { return id; }
|
||||
*
|
||||
* Example 2:
|
||||
*
|
||||
* @Id
|
||||
* @GeneratedValue(strategy=TABLE, generator="CUST_GEN")
|
||||
* @Column(name="CUST_ID")
|
||||
* Long id;
|
||||
* </pre>
|
||||
*
|
||||
* @see Id
|
||||
*
|
||||
* @since Java Persistence 1.0
|
||||
*/
|
||||
@Target({METHOD, FIELD})
|
||||
@Retention(RUNTIME)
|
||||
|
||||
public @interface GeneratedValue {
|
||||
|
||||
}
|
||||
@@ -45,7 +45,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
* </pre>
|
||||
*
|
||||
* @see Column
|
||||
* @see GeneratedValue
|
||||
* see GeneratedValue
|
||||
*
|
||||
* @since Java Persistence 1.0
|
||||
*/
|
||||
|
||||
@@ -22,7 +22,9 @@ module org.redkale {
|
||||
exports org.redkale.convert.bson;
|
||||
exports org.redkale.convert.ext;
|
||||
exports org.redkale.convert.json;
|
||||
exports org.redkale.mq;
|
||||
exports org.redkale.net;
|
||||
exports org.redkale.net.client;
|
||||
exports org.redkale.net.http;
|
||||
exports org.redkale.net.sncp;
|
||||
exports org.redkale.service;
|
||||
@@ -30,5 +32,10 @@ module org.redkale {
|
||||
exports org.redkale.util;
|
||||
exports org.redkale.watch;
|
||||
|
||||
uses org.redkale.cluster.ClusterAgent;
|
||||
uses org.redkale.mq.MessageAgent;
|
||||
uses org.redkale.source.CacheSource;
|
||||
uses org.redkale.source.SourceLoader;
|
||||
uses org.redkale.util.ResourceInjectLoader;
|
||||
}
|
||||
*/
|
||||
@@ -60,8 +60,8 @@ package org.redkale.asm;
|
||||
|
||||
/**
|
||||
* A visitor to visit a Java annotation. The methods of this class must be
|
||||
* called in the following order: ( <tt>visit</tt> | <tt>visitEnum</tt> |
|
||||
* <tt>visitAnnotation</tt> | <tt>visitArray</tt> )* <tt>visitEnd</tt>.
|
||||
* called in the following order: ( <tt>visit</tt> | <tt>visitEnum</tt> |
|
||||
* <tt>visitAnnotation</tt> | <tt>visitArray</tt> )* <tt>visitEnd</tt>.
|
||||
*
|
||||
* @author Eric Bruneton
|
||||
* @author Eugene Kuleshov
|
||||
@@ -102,6 +102,9 @@ public abstract class AnnotationVisitor {
|
||||
* method calls. May be null.
|
||||
*/
|
||||
public AnnotationVisitor(final int api, final AnnotationVisitor av) {
|
||||
if (api < Opcodes.ASM4 || api > Opcodes.ASM6) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
this.api = api;
|
||||
this.av = av;
|
||||
}
|
||||
@@ -151,7 +154,7 @@ public abstract class AnnotationVisitor {
|
||||
* @param desc
|
||||
* the class descriptor of the nested annotation class.
|
||||
* @return a visitor to visit the actual nested annotation value, or
|
||||
* <tt>null</tt> if this visitor is not interested in visiting this
|
||||
* <tt>null</tt> if this visitor is not interested in visiting this
|
||||
* nested annotation. <i>The nested annotation value must be fully
|
||||
* visited before calling other methods on this annotation
|
||||
* visitor</i>.
|
||||
@@ -172,7 +175,7 @@ public abstract class AnnotationVisitor {
|
||||
* @param name
|
||||
* the value name.
|
||||
* @return a visitor to visit the actual array value elements, or
|
||||
* <tt>null</tt> if this visitor is not interested in visiting these
|
||||
* <tt>null</tt> if this visitor is not interested in visiting these
|
||||
* values. The 'name' parameters passed to the methods of this
|
||||
* visitor are ignored. <i>All the array values must be visited
|
||||
* before calling other methods on this annotation visitor</i>.
|
||||
|
||||
@@ -77,7 +77,7 @@ final class AnnotationWriter extends AnnotationVisitor {
|
||||
private int size;
|
||||
|
||||
/**
|
||||
* <tt>true<tt> if values are named, <tt>false</tt> otherwise. Annotation
|
||||
* <tt>true<tt> if values are named, <tt>false</tt> otherwise. Annotation
|
||||
* writers used for annotation default and annotation arrays use unnamed
|
||||
* values.
|
||||
*/
|
||||
@@ -122,13 +122,13 @@ final class AnnotationWriter extends AnnotationVisitor {
|
||||
* @param cw
|
||||
* the class writer to which this annotation must be added.
|
||||
* @param named
|
||||
* <tt>true<tt> if values are named, <tt>false</tt> otherwise.
|
||||
* <tt>true<tt> if values are named, <tt>false</tt> otherwise.
|
||||
* @param bv
|
||||
* where the annotation values must be stored.
|
||||
* @param parent
|
||||
* where the number of annotation values must be stored.
|
||||
* @param offset
|
||||
* where in <tt>parent</tt> the number of annotation values must
|
||||
* where in <tt>parent</tt> the number of annotation values must
|
||||
* be stored.
|
||||
*/
|
||||
AnnotationWriter(final ClassWriter cw, final boolean named,
|
||||
@@ -354,7 +354,7 @@ final class AnnotationWriter extends AnnotationVisitor {
|
||||
* @param typePath
|
||||
* the path to the annotated type argument, wildcard bound, array
|
||||
* element type, or static inner type within 'typeRef'. May be
|
||||
* <tt>null</tt> if the annotation targets 'typeRef' as a whole.
|
||||
* <tt>null</tt> if the annotation targets 'typeRef' as a whole.
|
||||
* @param out
|
||||
* where the type reference and type path must be put.
|
||||
*/
|
||||
|
||||
@@ -58,13 +58,15 @@
|
||||
*/
|
||||
package org.redkale.asm;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* A non standard class, field, method or code attribute.
|
||||
*
|
||||
* @author Eric Bruneton
|
||||
* @author Eugene Kuleshov
|
||||
*/
|
||||
class Attribute {
|
||||
public class Attribute {
|
||||
|
||||
/**
|
||||
* The type of this attribute.
|
||||
@@ -77,7 +79,7 @@ class Attribute {
|
||||
byte[] value;
|
||||
|
||||
/**
|
||||
* The next attribute in this attribute list. May be <tt>null</tt>.
|
||||
* The next attribute in this attribute list. May be <tt>null</tt>.
|
||||
*/
|
||||
Attribute next;
|
||||
|
||||
@@ -92,19 +94,19 @@ class Attribute {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <tt>true</tt> if this type of attribute is unknown. The default
|
||||
* implementation of this method always returns <tt>true</tt>.
|
||||
* Returns <tt>true</tt> if this type of attribute is unknown. The default
|
||||
* implementation of this method always returns <tt>true</tt>.
|
||||
*
|
||||
* @return <tt>true</tt> if this type of attribute is unknown.
|
||||
* @return <tt>true</tt> if this type of attribute is unknown.
|
||||
*/
|
||||
public boolean isUnknown() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <tt>true</tt> if this type of attribute is a code attribute.
|
||||
* Returns <tt>true</tt> if this type of attribute is a code attribute.
|
||||
*
|
||||
* @return <tt>true</tt> if this type of attribute is a code attribute.
|
||||
* @return <tt>true</tt> if this type of attribute is a code attribute.
|
||||
*/
|
||||
public boolean isCodeAttribute() {
|
||||
return false;
|
||||
@@ -113,7 +115,7 @@ class Attribute {
|
||||
/**
|
||||
* Returns the labels corresponding to this attribute.
|
||||
*
|
||||
* @return the labels corresponding to this attribute, or <tt>null</tt> if
|
||||
* @return the labels corresponding to this attribute, or <tt>null</tt> if
|
||||
* this attribute is not a code attribute that contains labels.
|
||||
*/
|
||||
protected Label[] getLabels() {
|
||||
@@ -123,7 +125,7 @@ class Attribute {
|
||||
/**
|
||||
* Reads a {@link #type type} attribute. This method must return a
|
||||
* <i>new</i> {@link Attribute} object, of type {@link #type type},
|
||||
* corresponding to the <tt>len</tt> bytes starting at the given offset, in
|
||||
* corresponding to the <tt>len</tt> bytes starting at the given offset, in
|
||||
* the given class reader.
|
||||
*
|
||||
* @param cr
|
||||
@@ -146,7 +148,7 @@ class Attribute {
|
||||
* containing the type and the length of the attribute, are not
|
||||
* taken into account here.
|
||||
* @param labels
|
||||
* the labels of the method's code, or <tt>null</tt> if the
|
||||
* the labels of the method's code, or <tt>null</tt> if the
|
||||
* attribute to be read is not a code attribute.
|
||||
* @return a <i>new</i> {@link Attribute} object corresponding to the given
|
||||
* bytes.
|
||||
@@ -169,11 +171,11 @@ class Attribute {
|
||||
* class the items that corresponds to this attribute.
|
||||
* @param code
|
||||
* the bytecode of the method corresponding to this code
|
||||
* attribute, or <tt>null</tt> if this attribute is not a code
|
||||
* attribute, or <tt>null</tt> if this attribute is not a code
|
||||
* attributes.
|
||||
* @param len
|
||||
* the length of the bytecode of the method corresponding to this
|
||||
* code attribute, or <tt>null</tt> if this attribute is not a
|
||||
* code attribute, or <tt>null</tt> if this attribute is not a
|
||||
* code attribute.
|
||||
* @param maxStack
|
||||
* the maximum stack size of the method corresponding to this
|
||||
@@ -216,11 +218,11 @@ class Attribute {
|
||||
* byte arrays, with the {@link #write write} method.
|
||||
* @param code
|
||||
* the bytecode of the method corresponding to these code
|
||||
* attributes, or <tt>null</tt> if these attributes are not code
|
||||
* attributes, or <tt>null</tt> if these attributes are not code
|
||||
* attributes.
|
||||
* @param len
|
||||
* the length of the bytecode of the method corresponding to
|
||||
* these code attributes, or <tt>null</tt> if these attributes
|
||||
* these code attributes, or <tt>null</tt> if these attributes
|
||||
* are not code attributes.
|
||||
* @param maxStack
|
||||
* the maximum stack size of the method corresponding to these
|
||||
@@ -254,11 +256,11 @@ class Attribute {
|
||||
* byte arrays, with the {@link #write write} method.
|
||||
* @param code
|
||||
* the bytecode of the method corresponding to these code
|
||||
* attributes, or <tt>null</tt> if these attributes are not code
|
||||
* attributes, or <tt>null</tt> if these attributes are not code
|
||||
* attributes.
|
||||
* @param len
|
||||
* the length of the bytecode of the method corresponding to
|
||||
* these code attributes, or <tt>null</tt> if these attributes
|
||||
* these code attributes, or <tt>null</tt> if these attributes
|
||||
* are not code attributes.
|
||||
* @param maxStack
|
||||
* the maximum stack size of the method corresponding to these
|
||||
@@ -281,4 +283,72 @@ class Attribute {
|
||||
attr = attr.next;
|
||||
}
|
||||
}
|
||||
|
||||
//The stuff below is temporary - once proper support for nestmate attribute has been added, it can be safely removed.
|
||||
//see also changes in ClassReader.accept.
|
||||
|
||||
public static class NestMembers extends Attribute {
|
||||
public NestMembers() {
|
||||
super("NestMembers");
|
||||
}
|
||||
|
||||
byte[] bytes;
|
||||
String[] classes;
|
||||
|
||||
@Override
|
||||
protected Attribute read(ClassReader cr, int off, int len, char[] buf, int codeOff, Label[] labels) {
|
||||
int offset = off;
|
||||
NestMembers a = new NestMembers();
|
||||
int size = cr.readShort(off);
|
||||
a.classes = new String[size];
|
||||
off += 2;
|
||||
for (int i = 0; i < size ; i++) {
|
||||
a.classes[i] = cr.readClass(off, buf);
|
||||
off += 2;
|
||||
}
|
||||
a.bytes = Arrays.copyOfRange(cr.b, offset, offset + len);
|
||||
return a;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ByteVector write(ClassWriter cw, byte[] code, int len, int maxStack, int maxLocals) {
|
||||
ByteVector v = new ByteVector(bytes.length);
|
||||
v.putShort(classes.length);
|
||||
for (String s : classes) {
|
||||
v.putShort(cw.newClass(s));
|
||||
}
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
public static class NestHost extends Attribute {
|
||||
|
||||
byte[] bytes;
|
||||
String clazz;
|
||||
|
||||
public NestHost() {
|
||||
super("NestHost");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Attribute read(ClassReader cr, int off, int len, char[] buf, int codeOff, Label[] labels) {
|
||||
int offset = off;
|
||||
NestHost a = new NestHost();
|
||||
a.clazz = cr.readClass(off, buf);
|
||||
a.bytes = Arrays.copyOfRange(cr.b, offset, offset + len);
|
||||
return a;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ByteVector write(ClassWriter cw, byte[] code, int len, int maxStack, int maxLocals) {
|
||||
ByteVector v = new ByteVector(bytes.length);
|
||||
v.putShort(cw.newClass(clazz));
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
static final Attribute[] DEFAULT_ATTRIBUTE_PROTOS = new Attribute[] {
|
||||
new NestMembers(),
|
||||
new NestHost()
|
||||
};
|
||||
}
|
||||
|
||||
@@ -332,7 +332,7 @@ public class ByteVector {
|
||||
* automatically enlarged if necessary.
|
||||
*
|
||||
* @param b
|
||||
* an array of bytes. May be <tt>null</tt> to put <tt>len</tt>
|
||||
* an array of bytes. May be <tt>null</tt> to put <tt>len</tt>
|
||||
* null bytes into this byte vector.
|
||||
* @param off
|
||||
* index of the fist byte of b that must be copied.
|
||||
|
||||
@@ -185,9 +185,9 @@ public class ClassReader {
|
||||
public ClassReader(final byte[] b, final int off, final int len) {
|
||||
this.b = b;
|
||||
// checks the class version
|
||||
if (readShort(off + 6) > Opcodes.V10) {
|
||||
//throw new IllegalArgumentException();
|
||||
}
|
||||
//if (readShort(off + 6) > Opcodes.V11) {
|
||||
// throw new IllegalArgumentException();
|
||||
//}
|
||||
// parses the constant pool
|
||||
items = new int[readUnsignedShort(off + 8)];
|
||||
int n = items.length;
|
||||
@@ -205,6 +205,10 @@ public class ClassReader {
|
||||
case ClassWriter.FLOAT:
|
||||
case ClassWriter.NAME_TYPE:
|
||||
case ClassWriter.INDY:
|
||||
// @@@ ClassWriter.CONDY
|
||||
// Enables MethodHandles.lookup().defineClass to function correctly
|
||||
// when it reads the class name
|
||||
case 17:
|
||||
size = 5;
|
||||
break;
|
||||
case ClassWriter.LONG:
|
||||
@@ -267,7 +271,7 @@ public class ClassReader {
|
||||
* {@link Type#getInternalName() getInternalName}). For interfaces, the
|
||||
* super class is {@link Object}.
|
||||
*
|
||||
* @return the internal name of super class, or <tt>null</tt> for
|
||||
* @return the internal name of super class, or <tt>null</tt> for
|
||||
* {@link Object} class.
|
||||
*
|
||||
* @see ClassVisitor#visit(int, int, String, String, String, String[])
|
||||
@@ -281,7 +285,7 @@ public class ClassReader {
|
||||
* {@link Type#getInternalName() getInternalName}).
|
||||
*
|
||||
* @return the array of internal names for all implemented interfaces or
|
||||
* <tt>null</tt>.
|
||||
* <tt>null</tt>.
|
||||
*
|
||||
* @see ClassVisitor#visit(int, int, String, String, String, String[])
|
||||
*/
|
||||
@@ -526,7 +530,7 @@ public class ClassReader {
|
||||
* , {@link #SKIP_FRAMES}, {@link #SKIP_CODE}.
|
||||
*/
|
||||
public void accept(final ClassVisitor classVisitor, final int flags) {
|
||||
accept(classVisitor, new Attribute[0], flags);
|
||||
accept(classVisitor, Attribute.DEFAULT_ATTRIBUTE_PROTOS, flags);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1932,7 +1936,7 @@ public class ClassReader {
|
||||
* @param v
|
||||
* start offset in {@link #b b} of the annotations to be read.
|
||||
* @param visible
|
||||
* <tt>true</tt> if the annotations to be read are visible at
|
||||
* <tt>true</tt> if the annotations to be read are visible at
|
||||
* runtime.
|
||||
*/
|
||||
private void readParameterAnnotations(final MethodVisitor mv,
|
||||
@@ -2474,9 +2478,9 @@ public class ClassReader {
|
||||
* and the length of the attribute, are not taken into account
|
||||
* here.
|
||||
* @param labels
|
||||
* the labels of the method's code, or <tt>null</tt> if the
|
||||
* the labels of the method's code, or <tt>null</tt> if the
|
||||
* attribute to be read is not a code attribute.
|
||||
* @return the attribute that has been read, or <tt>null</tt> to skip this
|
||||
* @return the attribute that has been read, or <tt>null</tt> to skip this
|
||||
* attribute.
|
||||
*/
|
||||
private Attribute readAttribute(final Attribute[] attrs, final String type,
|
||||
|
||||
@@ -60,11 +60,11 @@ package org.redkale.asm;
|
||||
|
||||
/**
|
||||
* A visitor to visit a Java class. The methods of this class must be called in
|
||||
* the following order: <tt>visit</tt> [ <tt>visitSource</tt> ] [
|
||||
* <tt>visitModule</tt> ][ <tt>visitOuterClass</tt> ] ( <tt>visitAnnotation</tt> |
|
||||
* <tt>visitTypeAnnotation</tt> | <tt>visitAttribute</tt> )* (
|
||||
* <tt>visitInnerClass</tt> | <tt>visitField</tt> | <tt>visitMethod</tt> )*
|
||||
* <tt>visitEnd</tt>.
|
||||
* the following order: <tt>visit</tt> [ <tt>visitSource</tt> ] [
|
||||
* <tt>visitModule</tt> ][ <tt>visitOuterClass</tt> ] ( <tt>visitAnnotation</tt> |
|
||||
* <tt>visitTypeAnnotation</tt> | <tt>visitAttribute</tt> )* (
|
||||
* <tt>visitInnerClass</tt> | <tt>visitField</tt> | <tt>visitMethod</tt> )*
|
||||
* <tt>visitEnd</tt>.
|
||||
*
|
||||
* @author Eric Bruneton
|
||||
*/
|
||||
@@ -104,6 +104,9 @@ public abstract class ClassVisitor {
|
||||
* calls. May be null.
|
||||
*/
|
||||
public ClassVisitor(final int api, final ClassVisitor cv) {
|
||||
if (api < Opcodes.ASM4 || api > Opcodes.ASM6) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
this.api = api;
|
||||
this.cv = cv;
|
||||
}
|
||||
@@ -120,18 +123,18 @@ public abstract class ClassVisitor {
|
||||
* the internal name of the class (see
|
||||
* {@link Type#getInternalName() getInternalName}).
|
||||
* @param signature
|
||||
* the signature of this class. May be <tt>null</tt> if the class
|
||||
* the signature of this class. May be <tt>null</tt> if the class
|
||||
* is not a generic one, and does not extend or implement generic
|
||||
* classes or interfaces.
|
||||
* @param superName
|
||||
* the internal of name of the super class (see
|
||||
* {@link Type#getInternalName() getInternalName}). For
|
||||
* interfaces, the super class is {@link Object}. May be
|
||||
* <tt>null</tt>, but only for the {@link Object} class.
|
||||
* <tt>null</tt>, but only for the {@link Object} class.
|
||||
* @param interfaces
|
||||
* the internal names of the class's interfaces (see
|
||||
* {@link Type#getInternalName() getInternalName}). May be
|
||||
* <tt>null</tt>.
|
||||
* <tt>null</tt>.
|
||||
*/
|
||||
public void visit(int version, int access, String name, String signature,
|
||||
String superName, String[] interfaces) {
|
||||
@@ -145,11 +148,11 @@ public abstract class ClassVisitor {
|
||||
*
|
||||
* @param source
|
||||
* the name of the source file from which the class was compiled.
|
||||
* May be <tt>null</tt>.
|
||||
* May be <tt>null</tt>.
|
||||
* @param debug
|
||||
* additional debug information to compute the correspondance
|
||||
* between source and compiled elements of the class. May be
|
||||
* <tt>null</tt>.
|
||||
* <tt>null</tt>.
|
||||
*/
|
||||
public void visitSource(String source, String debug) {
|
||||
if (cv != null) {
|
||||
@@ -166,7 +169,7 @@ public abstract class ClassVisitor {
|
||||
* and {@code ACC_MANDATED}.
|
||||
* @param version
|
||||
* module version or null.
|
||||
* @return a visitor to visit the module values, or <tt>null</tt> if
|
||||
* @return a visitor to visit the module values, or <tt>null</tt> if
|
||||
* this visitor is not interested in visiting this module.
|
||||
*/
|
||||
public ModuleVisitor visitModule(String name, int access, String version) {
|
||||
@@ -187,11 +190,11 @@ public abstract class ClassVisitor {
|
||||
* internal name of the enclosing class of the class.
|
||||
* @param name
|
||||
* the name of the method that contains the class, or
|
||||
* <tt>null</tt> if the class is not enclosed in a method of its
|
||||
* <tt>null</tt> if the class is not enclosed in a method of its
|
||||
* enclosing class.
|
||||
* @param desc
|
||||
* the descriptor of the method that contains the class, or
|
||||
* <tt>null</tt> if the class is not enclosed in a method of its
|
||||
* <tt>null</tt> if the class is not enclosed in a method of its
|
||||
* enclosing class.
|
||||
*/
|
||||
public void visitOuterClass(String owner, String name, String desc) {
|
||||
@@ -206,8 +209,8 @@ public abstract class ClassVisitor {
|
||||
* @param desc
|
||||
* the class descriptor of the annotation class.
|
||||
* @param visible
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* this visitor is not interested in visiting this annotation.
|
||||
*/
|
||||
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
|
||||
@@ -231,16 +234,19 @@ public abstract class ClassVisitor {
|
||||
* @param typePath
|
||||
* the path to the annotated type argument, wildcard bound, array
|
||||
* element type, or static inner type within 'typeRef'. May be
|
||||
* <tt>null</tt> if the annotation targets 'typeRef' as a whole.
|
||||
* <tt>null</tt> if the annotation targets 'typeRef' as a whole.
|
||||
* @param desc
|
||||
* the class descriptor of the annotation class.
|
||||
* @param visible
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* this visitor is not interested in visiting this annotation.
|
||||
*/
|
||||
public AnnotationVisitor visitTypeAnnotation(int typeRef,
|
||||
TypePath typePath, String desc, boolean visible) {
|
||||
if (api < Opcodes.ASM5) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
if (cv != null) {
|
||||
return cv.visitTypeAnnotation(typeRef, typePath, desc, visible);
|
||||
}
|
||||
@@ -269,10 +275,10 @@ public abstract class ClassVisitor {
|
||||
* @param outerName
|
||||
* the internal name of the class to which the inner class
|
||||
* belongs (see {@link Type#getInternalName() getInternalName}).
|
||||
* May be <tt>null</tt> for not member classes.
|
||||
* May be <tt>null</tt> for not member classes.
|
||||
* @param innerName
|
||||
* the (simple) name of the inner class inside its enclosing
|
||||
* class. May be <tt>null</tt> for anonymous inner classes.
|
||||
* class. May be <tt>null</tt> for anonymous inner classes.
|
||||
* @param access
|
||||
* the access flags of the inner class as originally declared in
|
||||
* the enclosing class.
|
||||
@@ -295,20 +301,20 @@ public abstract class ClassVisitor {
|
||||
* @param desc
|
||||
* the field's descriptor (see {@link Type Type}).
|
||||
* @param signature
|
||||
* the field's signature. May be <tt>null</tt> if the field's
|
||||
* the field's signature. May be <tt>null</tt> if the field's
|
||||
* type does not use generic types.
|
||||
* @param value
|
||||
* the field's initial value. This parameter, which may be
|
||||
* <tt>null</tt> if the field does not have an initial value,
|
||||
* <tt>null</tt> if the field does not have an initial value,
|
||||
* must be an {@link Integer}, a {@link Float}, a {@link Long}, a
|
||||
* {@link Double} or a {@link String} (for <tt>int</tt>,
|
||||
* <tt>float</tt>, <tt>long</tt> or <tt>String</tt> fields
|
||||
* {@link Double} or a {@link String} (for <tt>int</tt>,
|
||||
* <tt>float</tt>, <tt>long</tt> or <tt>String</tt> fields
|
||||
* respectively). <i>This parameter is only used for static
|
||||
* fields</i>. Its value is ignored for non static fields, which
|
||||
* must be initialized through bytecode instructions in
|
||||
* constructors or methods.
|
||||
* @return a visitor to visit field annotations and attributes, or
|
||||
* <tt>null</tt> if this class visitor is not interested in visiting
|
||||
* <tt>null</tt> if this class visitor is not interested in visiting
|
||||
* these annotations and attributes.
|
||||
*/
|
||||
public FieldVisitor visitField(int access, String name, String desc,
|
||||
@@ -321,7 +327,7 @@ public abstract class ClassVisitor {
|
||||
|
||||
/**
|
||||
* Visits a method of the class. This method <i>must</i> return a new
|
||||
* {@link MethodVisitor} instance (or <tt>null</tt>) each time it is called,
|
||||
* {@link MethodVisitor} instance (or <tt>null</tt>) each time it is called,
|
||||
* i.e., it should not return a previously returned visitor.
|
||||
*
|
||||
* @param access
|
||||
@@ -333,14 +339,14 @@ public abstract class ClassVisitor {
|
||||
* @param desc
|
||||
* the method's descriptor (see {@link Type Type}).
|
||||
* @param signature
|
||||
* the method's signature. May be <tt>null</tt> if the method
|
||||
* the method's signature. May be <tt>null</tt> if the method
|
||||
* parameters, return type and exceptions do not use generic
|
||||
* types.
|
||||
* @param exceptions
|
||||
* the internal names of the method's exception classes (see
|
||||
* {@link Type#getInternalName() getInternalName}). May be
|
||||
* <tt>null</tt>.
|
||||
* @return an object to visit the byte code of the method, or <tt>null</tt>
|
||||
* <tt>null</tt>.
|
||||
* @return an object to visit the byte code of the method, or <tt>null</tt>
|
||||
* if this class visitor is not interested in visiting the code of
|
||||
* this method.
|
||||
*/
|
||||
|
||||
@@ -391,8 +391,8 @@ public class ClassWriter extends ClassVisitor {
|
||||
* A type table used to temporarily store internal names that will not
|
||||
* necessarily be stored in the constant pool. This type table is used by
|
||||
* the control flow and data flow analysis algorithm used to compute stack
|
||||
* map frames from scratch. This array associates to each index <tt>i</tt>
|
||||
* the Item whose index is <tt>i</tt>. All Item objects stored in this array
|
||||
* map frames from scratch. This array associates to each index <tt>i</tt>
|
||||
* the Item whose index is <tt>i</tt>. All Item objects stored in this array
|
||||
* are also stored in the {@link #items} hash table. These two arrays allow
|
||||
* to retrieve an Item from its index or, conversely, to get the index of an
|
||||
* Item from its value. Each Item stores an internal name in its
|
||||
@@ -556,7 +556,7 @@ public class ClassWriter extends ClassVisitor {
|
||||
private int compute;
|
||||
|
||||
/**
|
||||
* <tt>true</tt> if some methods have wide forward jumps using ASM pseudo
|
||||
* <tt>true</tt> if some methods have wide forward jumps using ASM pseudo
|
||||
* instructions, which need to be expanded into sequences of standard
|
||||
* bytecode instructions. In this case the class is re-read and re-written
|
||||
* with a ClassReader -> ClassWriter chain to perform this transformation.
|
||||
@@ -1297,6 +1297,38 @@ public class ClassWriter extends ClassVisitor {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a handle to the constant pool of the class being build. Does nothing
|
||||
* if the constant pool already contains a similar item. <i>This method is
|
||||
* intended for {@link Attribute} sub classes, and is normally not needed by
|
||||
* class generators or adapters.</i>
|
||||
*
|
||||
* @param tag
|
||||
* the kind of this handle. Must be {@link Opcodes#H_GETFIELD},
|
||||
* {@link Opcodes#H_GETSTATIC}, {@link Opcodes#H_PUTFIELD},
|
||||
* {@link Opcodes#H_PUTSTATIC}, {@link Opcodes#H_INVOKEVIRTUAL},
|
||||
* {@link Opcodes#H_INVOKESTATIC},
|
||||
* {@link Opcodes#H_INVOKESPECIAL},
|
||||
* {@link Opcodes#H_NEWINVOKESPECIAL} or
|
||||
* {@link Opcodes#H_INVOKEINTERFACE}.
|
||||
* @param owner
|
||||
* the internal name of the field or method owner class.
|
||||
* @param name
|
||||
* the name of the field or method.
|
||||
* @param desc
|
||||
* the descriptor of the field or method.
|
||||
* @return the index of a new or already existing method type reference
|
||||
* item.
|
||||
*
|
||||
* @deprecated this method is superseded by
|
||||
* {@link #newHandle(int, String, String, String, boolean)}.
|
||||
*/
|
||||
@Deprecated
|
||||
public int newHandle(final int tag, final String owner, final String name,
|
||||
final String desc) {
|
||||
return newHandle(tag, owner, name, desc, tag == Opcodes.H_INVOKEINTERFACE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a handle to the constant pool of the class being build. Does nothing
|
||||
* if the constant pool already contains a similar item. <i>This method is
|
||||
@@ -1486,7 +1518,7 @@ public class ClassWriter extends ClassVisitor {
|
||||
* @param desc
|
||||
* the method's descriptor.
|
||||
* @param itf
|
||||
* <tt>true</tt> if <tt>owner</tt> is an interface.
|
||||
* <tt>true</tt> if <tt>owner</tt> is an interface.
|
||||
* @return a new or already existing method reference item.
|
||||
*/
|
||||
Item newMethodItem(final String owner, final String name,
|
||||
@@ -1515,7 +1547,7 @@ public class ClassWriter extends ClassVisitor {
|
||||
* @param desc
|
||||
* the method's descriptor.
|
||||
* @param itf
|
||||
* <tt>true</tt> if <tt>owner</tt> is an interface.
|
||||
* <tt>true</tt> if <tt>owner</tt> is an interface.
|
||||
* @return the index of a new or already existing method reference item.
|
||||
*/
|
||||
public int newMethod(final String owner, final String name,
|
||||
@@ -1778,7 +1810,7 @@ public class ClassWriter extends ClassVisitor {
|
||||
* @param key
|
||||
* a constant pool item.
|
||||
* @return the constant pool's hash table item which is equal to the given
|
||||
* item, or <tt>null</tt> if there is no such item.
|
||||
* item, or <tt>null</tt> if there is no such item.
|
||||
*/
|
||||
private Item get(final Item key) {
|
||||
Item i = items[key.hashCode % items.length];
|
||||
|
||||
@@ -60,8 +60,8 @@ package org.redkale.asm;
|
||||
|
||||
/**
|
||||
* A visitor to visit a Java field. The methods of this class must be called in
|
||||
* the following order: ( <tt>visitAnnotation</tt> |
|
||||
* <tt>visitTypeAnnotation</tt> | <tt>visitAttribute</tt> )* <tt>visitEnd</tt>.
|
||||
* the following order: ( <tt>visitAnnotation</tt> |
|
||||
* <tt>visitTypeAnnotation</tt> | <tt>visitAttribute</tt> )* <tt>visitEnd</tt>.
|
||||
*
|
||||
* @author Eric Bruneton
|
||||
*/
|
||||
@@ -101,6 +101,9 @@ public abstract class FieldVisitor {
|
||||
* calls. May be null.
|
||||
*/
|
||||
public FieldVisitor(final int api, final FieldVisitor fv) {
|
||||
if (api < Opcodes.ASM4 || api > Opcodes.ASM6) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
this.api = api;
|
||||
this.fv = fv;
|
||||
}
|
||||
@@ -111,8 +114,8 @@ public abstract class FieldVisitor {
|
||||
* @param desc
|
||||
* the class descriptor of the annotation class.
|
||||
* @param visible
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* this visitor is not interested in visiting this annotation.
|
||||
*/
|
||||
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
|
||||
@@ -132,16 +135,19 @@ public abstract class FieldVisitor {
|
||||
* @param typePath
|
||||
* the path to the annotated type argument, wildcard bound, array
|
||||
* element type, or static inner type within 'typeRef'. May be
|
||||
* <tt>null</tt> if the annotation targets 'typeRef' as a whole.
|
||||
* <tt>null</tt> if the annotation targets 'typeRef' as a whole.
|
||||
* @param desc
|
||||
* the class descriptor of the annotation class.
|
||||
* @param visible
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* this visitor is not interested in visiting this annotation.
|
||||
*/
|
||||
public AnnotationVisitor visitTypeAnnotation(int typeRef,
|
||||
TypePath typePath, String desc, boolean visible) {
|
||||
if (api < Opcodes.ASM5) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
if (fv != null) {
|
||||
return fv.visitTypeAnnotation(typeRef, typePath, desc, visible);
|
||||
}
|
||||
|
||||
@@ -100,28 +100,28 @@ final class FieldWriter extends FieldVisitor {
|
||||
private int value;
|
||||
|
||||
/**
|
||||
* The runtime visible annotations of this field. May be <tt>null</tt>.
|
||||
* The runtime visible annotations of this field. May be <tt>null</tt>.
|
||||
*/
|
||||
private AnnotationWriter anns;
|
||||
|
||||
/**
|
||||
* The runtime invisible annotations of this field. May be <tt>null</tt>.
|
||||
* The runtime invisible annotations of this field. May be <tt>null</tt>.
|
||||
*/
|
||||
private AnnotationWriter ianns;
|
||||
|
||||
/**
|
||||
* The runtime visible type annotations of this field. May be <tt>null</tt>.
|
||||
* The runtime visible type annotations of this field. May be <tt>null</tt>.
|
||||
*/
|
||||
private AnnotationWriter tanns;
|
||||
|
||||
/**
|
||||
* The runtime invisible type annotations of this field. May be
|
||||
* <tt>null</tt>.
|
||||
* <tt>null</tt>.
|
||||
*/
|
||||
private AnnotationWriter itanns;
|
||||
|
||||
/**
|
||||
* The non standard attributes of this field. May be <tt>null</tt>.
|
||||
* The non standard attributes of this field. May be <tt>null</tt>.
|
||||
*/
|
||||
private Attribute attrs;
|
||||
|
||||
@@ -141,9 +141,9 @@ final class FieldWriter extends FieldVisitor {
|
||||
* @param desc
|
||||
* the field's descriptor (see {@link Type}).
|
||||
* @param signature
|
||||
* the field's signature. May be <tt>null</tt>.
|
||||
* the field's signature. May be <tt>null</tt>.
|
||||
* @param value
|
||||
* the field's constant value. May be <tt>null</tt>.
|
||||
* the field's constant value. May be <tt>null</tt>.
|
||||
*/
|
||||
FieldWriter(final ClassWriter cw, final int access, final String name,
|
||||
final String desc, final String signature, final Object value) {
|
||||
|
||||
@@ -1403,7 +1403,7 @@ class Frame {
|
||||
|
||||
/**
|
||||
* Merges the input frame of the given basic block with the input and output
|
||||
* frames of this basic block. Returns <tt>true</tt> if the input frame of
|
||||
* frames of this basic block. Returns <tt>true</tt> if the input frame of
|
||||
* the given label has been changed by this operation.
|
||||
*
|
||||
* @param cw
|
||||
@@ -1413,7 +1413,7 @@ class Frame {
|
||||
* @param edge
|
||||
* the kind of the {@link Edge} between this label and 'label'.
|
||||
* See {@link Edge#info}.
|
||||
* @return <tt>true</tt> if the input frame of the given label has been
|
||||
* @return <tt>true</tt> if the input frame of the given label has been
|
||||
* changed by this operation.
|
||||
*/
|
||||
final boolean merge(final ClassWriter cw, final Frame frame, final int edge) {
|
||||
@@ -1511,7 +1511,7 @@ class Frame {
|
||||
|
||||
/**
|
||||
* Merges the type at the given index in the given type array with the given
|
||||
* type. Returns <tt>true</tt> if the type array has been modified by this
|
||||
* type. Returns <tt>true</tt> if the type array has been modified by this
|
||||
* operation.
|
||||
*
|
||||
* @param cw
|
||||
@@ -1522,7 +1522,7 @@ class Frame {
|
||||
* an array of types.
|
||||
* @param index
|
||||
* the index of the type that must be merged in 'types'.
|
||||
* @return <tt>true</tt> if the type array has been modified by this
|
||||
* @return <tt>true</tt> if the type array has been modified by this
|
||||
* operation.
|
||||
*/
|
||||
private static boolean merge(final ClassWriter cw, int t,
|
||||
|
||||
@@ -99,6 +99,34 @@ public final class Handle {
|
||||
*/
|
||||
final boolean itf;
|
||||
|
||||
/**
|
||||
* Constructs a new field or method handle.
|
||||
*
|
||||
* @param tag
|
||||
* the kind of field or method designated by this Handle. Must be
|
||||
* {@link Opcodes#H_GETFIELD}, {@link Opcodes#H_GETSTATIC},
|
||||
* {@link Opcodes#H_PUTFIELD}, {@link Opcodes#H_PUTSTATIC},
|
||||
* {@link Opcodes#H_INVOKEVIRTUAL},
|
||||
* {@link Opcodes#H_INVOKESTATIC},
|
||||
* {@link Opcodes#H_INVOKESPECIAL},
|
||||
* {@link Opcodes#H_NEWINVOKESPECIAL} or
|
||||
* {@link Opcodes#H_INVOKEINTERFACE}.
|
||||
* @param owner
|
||||
* the internal name of the class that owns the field or method
|
||||
* designated by this handle.
|
||||
* @param name
|
||||
* the name of the field or method designated by this handle.
|
||||
* @param desc
|
||||
* the descriptor of the field or method designated by this
|
||||
* handle.
|
||||
*
|
||||
* @deprecated this constructor has been superseded
|
||||
* by {@link #Handle(int, String, String, String, boolean)}.
|
||||
*/
|
||||
@Deprecated
|
||||
public Handle(int tag, String owner, String name, String desc) {
|
||||
this(tag, owner, name, desc, tag == Opcodes.H_INVOKEINTERFACE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new field or method handle.
|
||||
|
||||
@@ -82,7 +82,7 @@ class Handler {
|
||||
|
||||
/**
|
||||
* Internal name of the type of exceptions handled by this handler, or
|
||||
* <tt>null</tt> to catch any exceptions.
|
||||
* <tt>null</tt> to catch any exceptions.
|
||||
*/
|
||||
String desc;
|
||||
|
||||
|
||||
@@ -306,8 +306,8 @@ final class Item {
|
||||
* @param i
|
||||
* the item to be compared to this one. Both items must have the
|
||||
* same {@link #type}.
|
||||
* @return <tt>true</tt> if the given item if equal to this one,
|
||||
* <tt>false</tt> otherwise.
|
||||
* @return <tt>true</tt> if the given item if equal to this one,
|
||||
* <tt>false</tt> otherwise.
|
||||
*/
|
||||
boolean isEqualTo(final Item i) {
|
||||
switch (type) {
|
||||
|
||||
@@ -325,8 +325,8 @@ public class Label {
|
||||
* the position of first byte of the bytecode instruction that
|
||||
* contains this label.
|
||||
* @param wideOffset
|
||||
* <tt>true</tt> if the reference must be stored in 4 bytes, or
|
||||
* <tt>false</tt> if it must be stored with 2 bytes.
|
||||
* <tt>true</tt> if the reference must be stored in 4 bytes, or
|
||||
* <tt>false</tt> if it must be stored with 2 bytes.
|
||||
* @throws IllegalArgumentException
|
||||
* if this label has not been created by the given code writer.
|
||||
*/
|
||||
@@ -389,7 +389,7 @@ public class Label {
|
||||
* the position of this label in the bytecode.
|
||||
* @param data
|
||||
* the bytecode of the method.
|
||||
* @return <tt>true</tt> if a blank that was left for this label was too
|
||||
* @return <tt>true</tt> if a blank that was left for this label was too
|
||||
* small to store the offset. In such a case the corresponding jump
|
||||
* instruction is replaced with a pseudo instruction (using unused
|
||||
* opcodes) using an unsigned two bytes offset. These pseudo
|
||||
|
||||
@@ -60,24 +60,24 @@ package org.redkale.asm;
|
||||
|
||||
/**
|
||||
* A visitor to visit a Java method. The methods of this class must be called in
|
||||
* the following order: ( <tt>visitParameter</tt> )* [
|
||||
* <tt>visitAnnotationDefault</tt> ] ( <tt>visitAnnotation</tt> |
|
||||
* <tt>visitParameterAnnotation</tt> <tt>visitTypeAnnotation</tt> |
|
||||
* <tt>visitAttribute</tt> )* [ <tt>visitCode</tt> ( <tt>visitFrame</tt> |
|
||||
* <tt>visit<i>X</i>Insn</tt> | <tt>visitLabel</tt> |
|
||||
* <tt>visitInsnAnnotation</tt> | <tt>visitTryCatchBlock</tt> |
|
||||
* <tt>visitTryCatchAnnotation</tt> | <tt>visitLocalVariable</tt> |
|
||||
* <tt>visitLocalVariableAnnotation</tt> | <tt>visitLineNumber</tt> )*
|
||||
* <tt>visitMaxs</tt> ] <tt>visitEnd</tt>. In addition, the
|
||||
* <tt>visit<i>X</i>Insn</tt> and <tt>visitLabel</tt> methods must be called in
|
||||
* the following order: ( <tt>visitParameter</tt> )* [
|
||||
* <tt>visitAnnotationDefault</tt> ] ( <tt>visitAnnotation</tt> |
|
||||
* <tt>visitParameterAnnotation</tt> <tt>visitTypeAnnotation</tt> |
|
||||
* <tt>visitAttribute</tt> )* [ <tt>visitCode</tt> ( <tt>visitFrame</tt> |
|
||||
* <tt>visit<i>X</i>Insn</tt> | <tt>visitLabel</tt> |
|
||||
* <tt>visitInsnAnnotation</tt> | <tt>visitTryCatchBlock</tt> |
|
||||
* <tt>visitTryCatchAnnotation</tt> | <tt>visitLocalVariable</tt> |
|
||||
* <tt>visitLocalVariableAnnotation</tt> | <tt>visitLineNumber</tt> )*
|
||||
* <tt>visitMaxs</tt> ] <tt>visitEnd</tt>. In addition, the
|
||||
* <tt>visit<i>X</i>Insn</tt> and <tt>visitLabel</tt> methods must be called in
|
||||
* the sequential order of the bytecode instructions of the visited code,
|
||||
* <tt>visitInsnAnnotation</tt> must be called <i>after</i> the annotated
|
||||
* instruction, <tt>visitTryCatchBlock</tt> must be called <i>before</i> the
|
||||
* <tt>visitInsnAnnotation</tt> must be called <i>after</i> the annotated
|
||||
* instruction, <tt>visitTryCatchBlock</tt> must be called <i>before</i> the
|
||||
* labels passed as arguments have been visited,
|
||||
* <tt>visitTryCatchBlockAnnotation</tt> must be called <i>after</i> the
|
||||
* <tt>visitTryCatchBlockAnnotation</tt> must be called <i>after</i> the
|
||||
* corresponding try catch block has been visited, and the
|
||||
* <tt>visitLocalVariable</tt>, <tt>visitLocalVariableAnnotation</tt> and
|
||||
* <tt>visitLineNumber</tt> methods must be called <i>after</i> the labels
|
||||
* <tt>visitLocalVariable</tt>, <tt>visitLocalVariableAnnotation</tt> and
|
||||
* <tt>visitLineNumber</tt> methods must be called <i>after</i> the labels
|
||||
* passed as arguments have been visited.
|
||||
*
|
||||
* @author Eric Bruneton
|
||||
@@ -118,6 +118,9 @@ public abstract class MethodVisitor {
|
||||
* calls. May be null.
|
||||
*/
|
||||
public MethodVisitor(final int api, final MethodVisitor mv) {
|
||||
if (api < Opcodes.ASM4 || api > Opcodes.ASM6) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
this.api = api;
|
||||
this.mv = mv;
|
||||
}
|
||||
@@ -132,11 +135,14 @@ public abstract class MethodVisitor {
|
||||
* @param name
|
||||
* parameter name or null if none is provided.
|
||||
* @param access
|
||||
* the parameter's access flags, only <tt>ACC_FINAL</tt>,
|
||||
* <tt>ACC_SYNTHETIC</tt> or/and <tt>ACC_MANDATED</tt> are
|
||||
* the parameter's access flags, only <tt>ACC_FINAL</tt>,
|
||||
* <tt>ACC_SYNTHETIC</tt> or/and <tt>ACC_MANDATED</tt> are
|
||||
* allowed (see {@link Opcodes}).
|
||||
*/
|
||||
public void visitParameter(String name, int access) {
|
||||
if (api < Opcodes.ASM5) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
if (mv != null) {
|
||||
mv.visitParameter(name, access);
|
||||
}
|
||||
@@ -146,7 +152,7 @@ public abstract class MethodVisitor {
|
||||
* Visits the default value of this annotation interface method.
|
||||
*
|
||||
* @return a visitor to the visit the actual default value of this
|
||||
* annotation interface method, or <tt>null</tt> if this visitor is
|
||||
* annotation interface method, or <tt>null</tt> if this visitor is
|
||||
* not interested in visiting this default value. The 'name'
|
||||
* parameters passed to the methods of this annotation visitor are
|
||||
* ignored. Moreover, exacly one visit method must be called on this
|
||||
@@ -165,8 +171,8 @@ public abstract class MethodVisitor {
|
||||
* @param desc
|
||||
* the class descriptor of the annotation class.
|
||||
* @param visible
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* this visitor is not interested in visiting this annotation.
|
||||
*/
|
||||
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
|
||||
@@ -193,16 +199,19 @@ public abstract class MethodVisitor {
|
||||
* @param typePath
|
||||
* the path to the annotated type argument, wildcard bound, array
|
||||
* element type, or static inner type within 'typeRef'. May be
|
||||
* <tt>null</tt> if the annotation targets 'typeRef' as a whole.
|
||||
* <tt>null</tt> if the annotation targets 'typeRef' as a whole.
|
||||
* @param desc
|
||||
* the class descriptor of the annotation class.
|
||||
* @param visible
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* this visitor is not interested in visiting this annotation.
|
||||
*/
|
||||
public AnnotationVisitor visitTypeAnnotation(int typeRef,
|
||||
TypePath typePath, String desc, boolean visible) {
|
||||
if (api < Opcodes.ASM5) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
if (mv != null) {
|
||||
return mv.visitTypeAnnotation(typeRef, typePath, desc, visible);
|
||||
}
|
||||
@@ -217,8 +226,8 @@ public abstract class MethodVisitor {
|
||||
* @param desc
|
||||
* the class descriptor of the annotation class.
|
||||
* @param visible
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* this visitor is not interested in visiting this annotation.
|
||||
*/
|
||||
public AnnotationVisitor visitParameterAnnotation(int parameter,
|
||||
@@ -444,6 +453,34 @@ public abstract class MethodVisitor {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Visits a method instruction. A method instruction is an instruction that
|
||||
* invokes a method.
|
||||
*
|
||||
* @param opcode
|
||||
* the opcode of the type instruction to be visited. This opcode
|
||||
* is either INVOKEVIRTUAL, INVOKESPECIAL, INVOKESTATIC or
|
||||
* INVOKEINTERFACE.
|
||||
* @param owner
|
||||
* the internal name of the method's owner class (see
|
||||
* {@link Type#getInternalName() getInternalName}).
|
||||
* @param name
|
||||
* the method's name.
|
||||
* @param desc
|
||||
* the method's descriptor (see {@link Type Type}).
|
||||
*/
|
||||
@Deprecated
|
||||
public void visitMethodInsn(int opcode, String owner, String name,
|
||||
String desc) {
|
||||
if (api >= Opcodes.ASM5) {
|
||||
boolean itf = opcode == Opcodes.INVOKEINTERFACE;
|
||||
visitMethodInsn(opcode, owner, name, desc, itf);
|
||||
return;
|
||||
}
|
||||
if (mv != null) {
|
||||
mv.visitMethodInsn(opcode, owner, name, desc);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Visits a method instruction. A method instruction is an instruction that
|
||||
@@ -465,6 +502,14 @@ public abstract class MethodVisitor {
|
||||
*/
|
||||
public void visitMethodInsn(int opcode, String owner, String name,
|
||||
String desc, boolean itf) {
|
||||
if (api < Opcodes.ASM5) {
|
||||
if (itf != (opcode == Opcodes.INVOKEINTERFACE)) {
|
||||
throw new IllegalArgumentException(
|
||||
"INVOKESPECIAL/STATIC on interfaces require ASM 5");
|
||||
}
|
||||
visitMethodInsn(opcode, owner, name, desc);
|
||||
return;
|
||||
}
|
||||
if (mv != null) {
|
||||
mv.visitMethodInsn(opcode, owner, name, desc, itf);
|
||||
}
|
||||
@@ -569,7 +614,7 @@ public abstract class MethodVisitor {
|
||||
* the constant to be loaded on the stack. This parameter must be
|
||||
* a non null {@link Integer}, a {@link Float}, a {@link Long}, a
|
||||
* {@link Double}, a {@link String}, a {@link Type} of OBJECT or
|
||||
* ARRAY sort for <tt>.class</tt> constants, for classes whose
|
||||
* ARRAY sort for <tt>.class</tt> constants, for classes whose
|
||||
* version is 49.0, a {@link Type} of METHOD sort or a
|
||||
* {@link Handle} for MethodType and MethodHandle constants, for
|
||||
* classes whose version is 51.0.
|
||||
@@ -604,8 +649,8 @@ public abstract class MethodVisitor {
|
||||
* @param dflt
|
||||
* beginning of the default handler block.
|
||||
* @param labels
|
||||
* beginnings of the handler blocks. <tt>labels[i]</tt> is the
|
||||
* beginning of the handler block for the <tt>min + i</tt> key.
|
||||
* beginnings of the handler blocks. <tt>labels[i]</tt> is the
|
||||
* beginning of the handler block for the <tt>min + i</tt> key.
|
||||
*/
|
||||
public void visitTableSwitchInsn(int min, int max, Label dflt,
|
||||
Label... labels) {
|
||||
@@ -622,8 +667,8 @@ public abstract class MethodVisitor {
|
||||
* @param keys
|
||||
* the values of the keys.
|
||||
* @param labels
|
||||
* beginnings of the handler blocks. <tt>labels[i]</tt> is the
|
||||
* beginning of the handler block for the <tt>keys[i]</tt> key.
|
||||
* beginnings of the handler blocks. <tt>labels[i]</tt> is the
|
||||
* beginning of the handler block for the <tt>keys[i]</tt> key.
|
||||
*/
|
||||
public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {
|
||||
if (mv != null) {
|
||||
@@ -668,16 +713,19 @@ public abstract class MethodVisitor {
|
||||
* @param typePath
|
||||
* the path to the annotated type argument, wildcard bound, array
|
||||
* element type, or static inner type within 'typeRef'. May be
|
||||
* <tt>null</tt> if the annotation targets 'typeRef' as a whole.
|
||||
* <tt>null</tt> if the annotation targets 'typeRef' as a whole.
|
||||
* @param desc
|
||||
* the class descriptor of the annotation class.
|
||||
* @param visible
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* this visitor is not interested in visiting this annotation.
|
||||
*/
|
||||
public AnnotationVisitor visitInsnAnnotation(int typeRef,
|
||||
TypePath typePath, String desc, boolean visible) {
|
||||
if (api < Opcodes.ASM5) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
if (mv != null) {
|
||||
return mv.visitInsnAnnotation(typeRef, typePath, desc, visible);
|
||||
}
|
||||
@@ -699,7 +747,7 @@ public abstract class MethodVisitor {
|
||||
* beginning of the exception handler's code.
|
||||
* @param type
|
||||
* internal name of the type of exceptions handled by the
|
||||
* handler, or <tt>null</tt> to catch any exceptions (for
|
||||
* handler, or <tt>null</tt> to catch any exceptions (for
|
||||
* "finally" blocks).
|
||||
* @throws IllegalArgumentException
|
||||
* if one of the labels has already been visited by this visitor
|
||||
@@ -725,16 +773,19 @@ public abstract class MethodVisitor {
|
||||
* @param typePath
|
||||
* the path to the annotated type argument, wildcard bound, array
|
||||
* element type, or static inner type within 'typeRef'. May be
|
||||
* <tt>null</tt> if the annotation targets 'typeRef' as a whole.
|
||||
* <tt>null</tt> if the annotation targets 'typeRef' as a whole.
|
||||
* @param desc
|
||||
* the class descriptor of the annotation class.
|
||||
* @param visible
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* this visitor is not interested in visiting this annotation.
|
||||
*/
|
||||
public AnnotationVisitor visitTryCatchAnnotation(int typeRef,
|
||||
TypePath typePath, String desc, boolean visible) {
|
||||
if (api < Opcodes.ASM5) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
if (mv != null) {
|
||||
return mv.visitTryCatchAnnotation(typeRef, typePath, desc, visible);
|
||||
}
|
||||
@@ -750,7 +801,7 @@ public abstract class MethodVisitor {
|
||||
* the type descriptor of this local variable.
|
||||
* @param signature
|
||||
* the type signature of this local variable. May be
|
||||
* <tt>null</tt> if the local variable type does not use generic
|
||||
* <tt>null</tt> if the local variable type does not use generic
|
||||
* types.
|
||||
* @param start
|
||||
* the first instruction corresponding to the scope of this local
|
||||
@@ -782,7 +833,7 @@ public abstract class MethodVisitor {
|
||||
* @param typePath
|
||||
* the path to the annotated type argument, wildcard bound, array
|
||||
* element type, or static inner type within 'typeRef'. May be
|
||||
* <tt>null</tt> if the annotation targets 'typeRef' as a whole.
|
||||
* <tt>null</tt> if the annotation targets 'typeRef' as a whole.
|
||||
* @param start
|
||||
* the fist instructions corresponding to the continuous ranges
|
||||
* that make the scope of this local variable (inclusive).
|
||||
@@ -796,13 +847,16 @@ public abstract class MethodVisitor {
|
||||
* @param desc
|
||||
* the class descriptor of the annotation class.
|
||||
* @param visible
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* this visitor is not interested in visiting this annotation.
|
||||
*/
|
||||
public AnnotationVisitor visitLocalVariableAnnotation(int typeRef,
|
||||
TypePath typePath, Label[] start, Label[] end, int[] index,
|
||||
String desc, boolean visible) {
|
||||
if (api < Opcodes.ASM5) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
if (mv != null) {
|
||||
return mv.visitLocalVariableAnnotation(typeRef, typePath, start,
|
||||
end, index, desc, visible);
|
||||
@@ -819,7 +873,7 @@ public abstract class MethodVisitor {
|
||||
* @param start
|
||||
* the first instruction corresponding to this line number.
|
||||
* @throws IllegalArgumentException
|
||||
* if <tt>start</tt> has not already been visited by this
|
||||
* if <tt>start</tt> has not already been visited by this
|
||||
* visitor (by the {@link #visitLabel visitLabel} method).
|
||||
*/
|
||||
public void visitLineNumber(int line, Label start) {
|
||||
|
||||
@@ -218,41 +218,41 @@ class MethodWriter extends MethodVisitor {
|
||||
int[] exceptions;
|
||||
|
||||
/**
|
||||
* The annotation default attribute of this method. May be <tt>null</tt>.
|
||||
* The annotation default attribute of this method. May be <tt>null</tt>.
|
||||
*/
|
||||
private ByteVector annd;
|
||||
|
||||
/**
|
||||
* The runtime visible annotations of this method. May be <tt>null</tt>.
|
||||
* The runtime visible annotations of this method. May be <tt>null</tt>.
|
||||
*/
|
||||
private AnnotationWriter anns;
|
||||
|
||||
/**
|
||||
* The runtime invisible annotations of this method. May be <tt>null</tt>.
|
||||
* The runtime invisible annotations of this method. May be <tt>null</tt>.
|
||||
*/
|
||||
private AnnotationWriter ianns;
|
||||
|
||||
/**
|
||||
* The runtime visible type annotations of this method. May be <tt>null</tt>
|
||||
* The runtime visible type annotations of this method. May be <tt>null</tt>
|
||||
* .
|
||||
*/
|
||||
private AnnotationWriter tanns;
|
||||
|
||||
/**
|
||||
* The runtime invisible type annotations of this method. May be
|
||||
* <tt>null</tt>.
|
||||
* <tt>null</tt>.
|
||||
*/
|
||||
private AnnotationWriter itanns;
|
||||
|
||||
/**
|
||||
* The runtime visible parameter annotations of this method. May be
|
||||
* <tt>null</tt>.
|
||||
* <tt>null</tt>.
|
||||
*/
|
||||
private AnnotationWriter[] panns;
|
||||
|
||||
/**
|
||||
* The runtime invisible parameter annotations of this method. May be
|
||||
* <tt>null</tt>.
|
||||
* <tt>null</tt>.
|
||||
*/
|
||||
private AnnotationWriter[] ipanns;
|
||||
|
||||
@@ -381,12 +381,12 @@ class MethodWriter extends MethodVisitor {
|
||||
private int lastCodeOffset;
|
||||
|
||||
/**
|
||||
* The runtime visible type annotations of the code. May be <tt>null</tt>.
|
||||
* The runtime visible type annotations of the code. May be <tt>null</tt>.
|
||||
*/
|
||||
private AnnotationWriter ctanns;
|
||||
|
||||
/**
|
||||
* The runtime invisible type annotations of the code. May be <tt>null</tt>.
|
||||
* The runtime invisible type annotations of the code. May be <tt>null</tt>.
|
||||
*/
|
||||
private AnnotationWriter ictanns;
|
||||
|
||||
@@ -446,7 +446,7 @@ class MethodWriter extends MethodVisitor {
|
||||
* is relative to the beginning of the current basic block, i.e., the true
|
||||
* stack size after the last visited instruction is equal to the
|
||||
* {@link Label#inputStackTop beginStackSize} of the current basic block
|
||||
* plus <tt>stackSize</tt>.
|
||||
* plus <tt>stackSize</tt>.
|
||||
*/
|
||||
private int stackSize;
|
||||
|
||||
@@ -455,7 +455,7 @@ class MethodWriter extends MethodVisitor {
|
||||
* This size is relative to the beginning of the current basic block, i.e.,
|
||||
* the true maximum stack size after the last visited instruction is equal
|
||||
* to the {@link Label#inputStackTop beginStackSize} of the current basic
|
||||
* block plus <tt>stackSize</tt>.
|
||||
* block plus <tt>stackSize</tt>.
|
||||
*/
|
||||
private int maxStackSize;
|
||||
|
||||
@@ -475,10 +475,10 @@ class MethodWriter extends MethodVisitor {
|
||||
* @param desc
|
||||
* the method's descriptor (see {@link Type}).
|
||||
* @param signature
|
||||
* the method's signature. May be <tt>null</tt>.
|
||||
* the method's signature. May be <tt>null</tt>.
|
||||
* @param exceptions
|
||||
* the internal names of the method's exceptions. May be
|
||||
* <tt>null</tt>.
|
||||
* <tt>null</tt>.
|
||||
* @param compute
|
||||
* Indicates what must be automatically computed (see #compute).
|
||||
*/
|
||||
|
||||
@@ -60,9 +60,9 @@ package org.redkale.asm;
|
||||
|
||||
/**
|
||||
* A visitor to visit a Java module. The methods of this class must be called in
|
||||
* the following order: <tt>visitMainClass</tt> | ( <tt>visitPackage</tt> |
|
||||
* <tt>visitRequire</tt> | <tt>visitExport</tt> | <tt>visitOpen</tt> |
|
||||
* <tt>visitUse</tt> | <tt>visitProvide</tt> )* <tt>visitEnd</tt>.
|
||||
* the following order: <tt>visitMainClass</tt> | ( <tt>visitPackage</tt> |
|
||||
* <tt>visitRequire</tt> | <tt>visitExport</tt> | <tt>visitOpen</tt> |
|
||||
* <tt>visitUse</tt> | <tt>visitProvide</tt> )* <tt>visitEnd</tt>.
|
||||
*
|
||||
* The methods {@link #visitRequire(String, int, String)}, {@link #visitExport(String, int, String...)},
|
||||
* {@link #visitOpen(String, int, String...)} and {@link #visitPackage(String)}
|
||||
@@ -157,7 +157,7 @@ public abstract class ModuleVisitor {
|
||||
* {@code ACC_MANDATED}.
|
||||
* @param modules the qualified names of the modules that can access to
|
||||
* the public classes of the exported package or
|
||||
* <tt>null</tt>.
|
||||
* <tt>null</tt>.
|
||||
*/
|
||||
public void visitExport(String packaze, int access, String... modules) {
|
||||
if (mv != null) {
|
||||
@@ -174,7 +174,7 @@ public abstract class ModuleVisitor {
|
||||
* {@code ACC_MANDATED}.
|
||||
* @param modules the qualified names of the modules that can use deep
|
||||
* reflection to the classes of the open package or
|
||||
* <tt>null</tt>.
|
||||
* <tt>null</tt>.
|
||||
*/
|
||||
public void visitOpen(String packaze, int access, String... modules) {
|
||||
if (mv != null) {
|
||||
|
||||
@@ -80,12 +80,17 @@ public interface Opcodes {
|
||||
|
||||
// versions
|
||||
|
||||
int V1_1 = 3 << 16 | 45;
|
||||
int V1_2 = 0 << 16 | 46;
|
||||
int V1_3 = 0 << 16 | 47;
|
||||
int V1_4 = 0 << 16 | 48;
|
||||
int V1_5 = 0 << 16 | 49;
|
||||
int V1_6 = 0 << 16 | 50;
|
||||
int V1_7 = 0 << 16 | 51;
|
||||
int V1_8 = 0 << 16 | 52;
|
||||
int V9 = 0 << 16 | 53;
|
||||
int V10 = 0 << 16 | 54;
|
||||
int V11 = 0 << 16 | 55;
|
||||
|
||||
// access flags
|
||||
|
||||
|
||||
@@ -71,47 +71,47 @@ import java.lang.reflect.Method;
|
||||
public class Type {
|
||||
|
||||
/**
|
||||
* The sort of the <tt>void</tt> type. See {@link #getSort getSort}.
|
||||
* The sort of the <tt>void</tt> type. See {@link #getSort getSort}.
|
||||
*/
|
||||
public static final int VOID = 0;
|
||||
|
||||
/**
|
||||
* The sort of the <tt>boolean</tt> type. See {@link #getSort getSort}.
|
||||
* The sort of the <tt>boolean</tt> type. See {@link #getSort getSort}.
|
||||
*/
|
||||
public static final int BOOLEAN = 1;
|
||||
|
||||
/**
|
||||
* The sort of the <tt>char</tt> type. See {@link #getSort getSort}.
|
||||
* The sort of the <tt>char</tt> type. See {@link #getSort getSort}.
|
||||
*/
|
||||
public static final int CHAR = 2;
|
||||
|
||||
/**
|
||||
* The sort of the <tt>byte</tt> type. See {@link #getSort getSort}.
|
||||
* The sort of the <tt>byte</tt> type. See {@link #getSort getSort}.
|
||||
*/
|
||||
public static final int BYTE = 3;
|
||||
|
||||
/**
|
||||
* The sort of the <tt>short</tt> type. See {@link #getSort getSort}.
|
||||
* The sort of the <tt>short</tt> type. See {@link #getSort getSort}.
|
||||
*/
|
||||
public static final int SHORT = 4;
|
||||
|
||||
/**
|
||||
* The sort of the <tt>int</tt> type. See {@link #getSort getSort}.
|
||||
* The sort of the <tt>int</tt> type. See {@link #getSort getSort}.
|
||||
*/
|
||||
public static final int INT = 5;
|
||||
|
||||
/**
|
||||
* The sort of the <tt>float</tt> type. See {@link #getSort getSort}.
|
||||
* The sort of the <tt>float</tt> type. See {@link #getSort getSort}.
|
||||
*/
|
||||
public static final int FLOAT = 6;
|
||||
|
||||
/**
|
||||
* The sort of the <tt>long</tt> type. See {@link #getSort getSort}.
|
||||
* The sort of the <tt>long</tt> type. See {@link #getSort getSort}.
|
||||
*/
|
||||
public static final int LONG = 7;
|
||||
|
||||
/**
|
||||
* The sort of the <tt>double</tt> type. See {@link #getSort getSort}.
|
||||
* The sort of the <tt>double</tt> type. See {@link #getSort getSort}.
|
||||
*/
|
||||
public static final int DOUBLE = 8;
|
||||
|
||||
@@ -131,55 +131,55 @@ public class Type {
|
||||
public static final int METHOD = 11;
|
||||
|
||||
/**
|
||||
* The <tt>void</tt> type.
|
||||
* The <tt>void</tt> type.
|
||||
*/
|
||||
public static final Type VOID_TYPE = new Type(VOID, null, ('V' << 24)
|
||||
| (5 << 16) | (0 << 8) | 0, 1);
|
||||
|
||||
/**
|
||||
* The <tt>boolean</tt> type.
|
||||
* The <tt>boolean</tt> type.
|
||||
*/
|
||||
public static final Type BOOLEAN_TYPE = new Type(BOOLEAN, null, ('Z' << 24)
|
||||
| (0 << 16) | (5 << 8) | 1, 1);
|
||||
|
||||
/**
|
||||
* The <tt>char</tt> type.
|
||||
* The <tt>char</tt> type.
|
||||
*/
|
||||
public static final Type CHAR_TYPE = new Type(CHAR, null, ('C' << 24)
|
||||
| (0 << 16) | (6 << 8) | 1, 1);
|
||||
|
||||
/**
|
||||
* The <tt>byte</tt> type.
|
||||
* The <tt>byte</tt> type.
|
||||
*/
|
||||
public static final Type BYTE_TYPE = new Type(BYTE, null, ('B' << 24)
|
||||
| (0 << 16) | (5 << 8) | 1, 1);
|
||||
|
||||
/**
|
||||
* The <tt>short</tt> type.
|
||||
* The <tt>short</tt> type.
|
||||
*/
|
||||
public static final Type SHORT_TYPE = new Type(SHORT, null, ('S' << 24)
|
||||
| (0 << 16) | (7 << 8) | 1, 1);
|
||||
|
||||
/**
|
||||
* The <tt>int</tt> type.
|
||||
* The <tt>int</tt> type.
|
||||
*/
|
||||
public static final Type INT_TYPE = new Type(INT, null, ('I' << 24)
|
||||
| (0 << 16) | (0 << 8) | 1, 1);
|
||||
|
||||
/**
|
||||
* The <tt>float</tt> type.
|
||||
* The <tt>float</tt> type.
|
||||
*/
|
||||
public static final Type FLOAT_TYPE = new Type(FLOAT, null, ('F' << 24)
|
||||
| (2 << 16) | (2 << 8) | 1, 1);
|
||||
|
||||
/**
|
||||
* The <tt>long</tt> type.
|
||||
* The <tt>long</tt> type.
|
||||
*/
|
||||
public static final Type LONG_TYPE = new Type(LONG, null, ('J' << 24)
|
||||
| (1 << 16) | (1 << 8) | 2, 1);
|
||||
|
||||
/**
|
||||
* The <tt>double</tt> type.
|
||||
* The <tt>double</tt> type.
|
||||
*/
|
||||
public static final Type DOUBLE_TYPE = new Type(DOUBLE, null, ('D' << 24)
|
||||
| (3 << 16) | (3 << 8) | 2, 1);
|
||||
@@ -439,8 +439,8 @@ public class Type {
|
||||
* @return the size of the arguments of the method (plus one for the
|
||||
* implicit this argument), argSize, and the size of its return
|
||||
* value, retSize, packed into a single int i =
|
||||
* <tt>(argSize << 2) | retSize</tt> (argSize is therefore equal to
|
||||
* <tt>i >> 2</tt>, and retSize to <tt>i & 0x03</tt>).
|
||||
* <tt>(argSize << 2) | retSize</tt> (argSize is therefore equal to
|
||||
* <tt>i >> 2</tt>, and retSize to <tt>i & 0x03</tt>).
|
||||
*/
|
||||
public static int getArgumentsAndReturnSizes(final String desc) {
|
||||
int n = 1;
|
||||
@@ -645,9 +645,9 @@ public class Type {
|
||||
* @return the size of the arguments (plus one for the implicit this
|
||||
* argument), argSize, and the size of the return value, retSize,
|
||||
* packed into a single
|
||||
* int i = <tt>(argSize << 2) | retSize</tt>
|
||||
* (argSize is therefore equal to <tt>i >> 2</tt>,
|
||||
* and retSize to <tt>i & 0x03</tt>).
|
||||
* int i = <tt>(argSize << 2) | retSize</tt>
|
||||
* (argSize is therefore equal to <tt>i >> 2</tt>,
|
||||
* and retSize to <tt>i & 0x03</tt>).
|
||||
*/
|
||||
public int getArgumentsAndReturnSizes() {
|
||||
return getArgumentsAndReturnSizes(getDescriptor());
|
||||
@@ -838,8 +838,8 @@ public class Type {
|
||||
* Returns the size of values of this type. This method must not be used for
|
||||
* method types.
|
||||
*
|
||||
* @return the size of values of this type, i.e., 2 for <tt>long</tt> and
|
||||
* <tt>double</tt>, 0 for <tt>void</tt> and 1 otherwise.
|
||||
* @return the size of values of this type, i.e., 2 for <tt>long</tt> and
|
||||
* <tt>double</tt>, 0 for <tt>void</tt> and 1 otherwise.
|
||||
*/
|
||||
public int getSize() {
|
||||
// the size is in byte 0 of 'off' for primitive types (buf == null)
|
||||
@@ -855,8 +855,8 @@ public class Type {
|
||||
* ISTORE, IALOAD, IASTORE, IADD, ISUB, IMUL, IDIV, IREM, INEG,
|
||||
* ISHL, ISHR, IUSHR, IAND, IOR, IXOR and IRETURN.
|
||||
* @return an opcode that is similar to the given opcode, but adapted to
|
||||
* this Java type. For example, if this type is <tt>float</tt> and
|
||||
* <tt>opcode</tt> is IRETURN, this method returns FRETURN.
|
||||
* this Java type. For example, if this type is <tt>float</tt> and
|
||||
* <tt>opcode</tt> is IRETURN, this method returns FRETURN.
|
||||
*/
|
||||
public int getOpcode(final int opcode) {
|
||||
if (opcode == Opcodes.IALOAD || opcode == Opcodes.IASTORE) {
|
||||
@@ -879,7 +879,7 @@ public class Type {
|
||||
*
|
||||
* @param o
|
||||
* the object to be compared to this type.
|
||||
* @return <tt>true</tt> if the given object is equal to this type.
|
||||
* @return <tt>true</tt> if the given object is equal to this type.
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(final Object o) {
|
||||
|
||||
@@ -196,7 +196,7 @@ public final class ApiDocsService {
|
||||
final FileOutputStream out = new FileOutputStream(new File(app.getHome(), "apidoc.json"));
|
||||
out.write(json.getBytes("UTF-8"));
|
||||
out.close();
|
||||
File doctemplate = new File(app.getConfPath(), "apidoc-template.html");
|
||||
File doctemplate = new File(app.getConfPath().toString(), "apidoc-template.html");
|
||||
InputStream in = null;
|
||||
if (doctemplate.isFile() && doctemplate.canRead()) {
|
||||
in = new FileInputStream(doctemplate);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -35,6 +35,14 @@ public interface ApplicationListener {
|
||||
default void preStart(Application application) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Application 在运行start后调用
|
||||
*
|
||||
* @param application Application
|
||||
*/
|
||||
default void postStart(Application application) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Application 在运行shutdown前调用
|
||||
*
|
||||
|
||||
@@ -175,8 +175,9 @@ public final class ClassFilter<T> {
|
||||
}
|
||||
if (!r && ors != null) {
|
||||
for (ClassFilter filter : ors) {
|
||||
if (filter.accept(property, clazzname)) {
|
||||
if (filter.accept(filter.conf, clazzname)) {
|
||||
cf = filter;
|
||||
property = cf.conf;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -189,11 +190,11 @@ public final class ClassFilter<T> {
|
||||
if (property == null) {
|
||||
property = cf.conf;
|
||||
} else if (property instanceof DefaultAnyValue) {
|
||||
((DefaultAnyValue) property).addAll(cf.conf);
|
||||
((DefaultAnyValue) property).addAllStringSet(cf.conf);
|
||||
} else {
|
||||
DefaultAnyValue dav = new DefaultAnyValue();
|
||||
dav.addAll(property);
|
||||
dav.addAll(cf.conf);
|
||||
dav.addAllStringSet(property);
|
||||
dav.addAllStringSet(cf.conf);
|
||||
property = dav;
|
||||
}
|
||||
}
|
||||
@@ -207,7 +208,9 @@ public final class ClassFilter<T> {
|
||||
} catch (Throwable cfe) {
|
||||
if (finest && !clazzname.startsWith("sun.") && !clazzname.startsWith("javax.")
|
||||
&& !clazzname.startsWith("com.sun.") && !clazzname.startsWith("jdk.") && !clazzname.startsWith("META-INF")
|
||||
&& (!(cfe instanceof NoClassDefFoundError) || (cfe instanceof UnsupportedClassVersionError) || ((NoClassDefFoundError) cfe).getMessage().startsWith("java.lang.NoClassDefFoundError: java"))) {
|
||||
&& !clazzname.startsWith("com.mysql.") && !clazzname.startsWith("com.microsoft.") && !clazzname.startsWith("freemarker.")
|
||||
&& !clazzname.startsWith("org.redkale") && (clazzname.contains("Service") || clazzname.contains("Servlet"))) {
|
||||
//&& (!(cfe instanceof NoClassDefFoundError) || (cfe instanceof UnsupportedClassVersionError) || ((NoClassDefFoundError) cfe).getMessage().startsWith("java.lang.NoClassDefFoundError: java"))) {
|
||||
logger.log(Level.FINEST, ClassFilter.class.getSimpleName() + " filter error for class: " + clazzname + (url == null ? "" : (" in " + url)), cfe);
|
||||
}
|
||||
}
|
||||
@@ -241,7 +244,7 @@ public final class ClassFilter<T> {
|
||||
}
|
||||
if (!r && ors != null) {
|
||||
for (ClassFilter filter : ors) {
|
||||
if (filter.accept(property, classname)) return true;
|
||||
if (filter.accept(filter.conf, classname)) return true;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
@@ -405,7 +408,7 @@ public final class ClassFilter<T> {
|
||||
str = str.trim();
|
||||
if (str.endsWith(";")) str = str.substring(0, str.length() - 1);
|
||||
}
|
||||
if (str != null) groups.addAll(Arrays.asList(str.split(";")));
|
||||
if (str != null) this.groups.addAll(Arrays.asList(str.split(";")));
|
||||
this.property = property;
|
||||
this.autoload = autoload;
|
||||
this.expect = expect;
|
||||
@@ -415,7 +418,7 @@ public final class ClassFilter<T> {
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.getClass().getSimpleName() + "[thread=" + Thread.currentThread().getName()
|
||||
+ ", type=" + this.type.getSimpleName() + ", name=" + name + ", groups=" + groups + "]";
|
||||
+ ", type=" + this.type.getSimpleName() + ", name=" + name + ", groups=" + this.groups + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -442,6 +445,10 @@ public final class ClassFilter<T> {
|
||||
return property;
|
||||
}
|
||||
|
||||
public boolean containsGroup(String group) {
|
||||
return groups != null && groups.contains(group);
|
||||
}
|
||||
|
||||
public boolean isEmptyGroups() {
|
||||
return groups == null || groups.isEmpty();
|
||||
}
|
||||
@@ -493,7 +500,7 @@ public final class ClassFilter<T> {
|
||||
boolean skip = false;
|
||||
for (Pattern p : excludePatterns) {
|
||||
if (p.matcher(url.toString()).matches()) {
|
||||
skip = false;
|
||||
skip = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,19 +46,19 @@ public class LogFileHandler extends Handler {
|
||||
private static final String format = "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%tL %4$s %2$s\r\n%5$s%6$s\r\n";
|
||||
|
||||
@Override
|
||||
public String format(LogRecord record) {
|
||||
public String format(LogRecord log) {
|
||||
String source;
|
||||
if (record.getSourceClassName() != null) {
|
||||
source = record.getSourceClassName();
|
||||
if (record.getSourceMethodName() != null) {
|
||||
source += " " + record.getSourceMethodName();
|
||||
if (log.getSourceClassName() != null) {
|
||||
source = log.getSourceClassName();
|
||||
if (log.getSourceMethodName() != null) {
|
||||
source += " " + log.getSourceMethodName();
|
||||
}
|
||||
} else {
|
||||
source = record.getLoggerName();
|
||||
source = log.getLoggerName();
|
||||
}
|
||||
String message = formatMessage(record);
|
||||
String message = formatMessage(log);
|
||||
String throwable = "";
|
||||
if (record.getThrown() != null) {
|
||||
if (log.getThrown() != null) {
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw) {
|
||||
@Override
|
||||
@@ -67,22 +67,38 @@ public class LogFileHandler extends Handler {
|
||||
}
|
||||
};
|
||||
pw.println();
|
||||
record.getThrown().printStackTrace(pw);
|
||||
log.getThrown().printStackTrace(pw);
|
||||
pw.close();
|
||||
throwable = sw.toString();
|
||||
}
|
||||
return String.format(format,
|
||||
System.currentTimeMillis(),
|
||||
source,
|
||||
record.getLoggerName(),
|
||||
record.getLevel().getName(),
|
||||
log.getLoggerName(),
|
||||
log.getLevel().getName(),
|
||||
message,
|
||||
throwable);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected final LinkedBlockingQueue<LogRecord> records = new LinkedBlockingQueue();
|
||||
public static void initDebugLogConfig() {
|
||||
try {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
final PrintStream ps = new PrintStream(out);
|
||||
ps.println("handlers = java.util.logging.ConsoleHandler");
|
||||
ps.println(".level = FINEST");
|
||||
ps.println("jdk.level = INFO");
|
||||
ps.println("sun.level = INFO");
|
||||
ps.println("com.sun.level = INFO");
|
||||
ps.println("javax.level = INFO");
|
||||
ps.println("java.util.logging.ConsoleHandler.level = FINEST");
|
||||
ps.println("java.util.logging.ConsoleHandler.formatter = " + LogFileHandler.LoggingFormater.class.getName());
|
||||
LogManager.getLogManager().readConfiguration(new ByteArrayInputStream(out.toByteArray()));
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
protected final LinkedBlockingQueue<LogRecord> logqueue = new LinkedBlockingQueue();
|
||||
|
||||
private String pattern;
|
||||
|
||||
@@ -133,7 +149,7 @@ public class LogFileHandler extends Handler {
|
||||
}
|
||||
|
||||
private void open() {
|
||||
final String name = "Logging-" + getClass().getSimpleName() + "-Thread";
|
||||
final String name = "Redkale-Logging-" + getClass().getSimpleName() + "-Thread";
|
||||
new Thread() {
|
||||
{
|
||||
setName(name);
|
||||
@@ -144,9 +160,9 @@ public class LogFileHandler extends Handler {
|
||||
public void run() {
|
||||
while (true) {
|
||||
try {
|
||||
LogRecord record = records.take();
|
||||
LogRecord log = logqueue.take();
|
||||
final boolean bigger = (limit > 0 && limit <= loglength.get());
|
||||
final boolean changeday = tomorrow <= record.getMillis();
|
||||
final boolean changeday = tomorrow <= log.getMillis();
|
||||
if (bigger || changeday) {
|
||||
updateTomorrow();
|
||||
if (logstream != null) {
|
||||
@@ -193,12 +209,12 @@ public class LogFileHandler extends Handler {
|
||||
logunusualstream = new FileOutputStream(logunusualfile, append);
|
||||
}
|
||||
//----------------------写日志-------------------------
|
||||
String message = getFormatter().format(record);
|
||||
String message = getFormatter().format(log);
|
||||
String encoding = getEncoding();
|
||||
byte[] bytes = encoding == null ? message.getBytes() : message.getBytes(encoding);
|
||||
logstream.write(bytes);
|
||||
loglength.addAndGet(bytes.length);
|
||||
if (unusual != null && (record.getLevel() == Level.WARNING || record.getLevel() == Level.SEVERE)) {
|
||||
if (unusual != null && (log.getLevel() == Level.WARNING || log.getLevel() == Level.SEVERE)) {
|
||||
logunusualstream.write(bytes);
|
||||
logunusuallength.addAndGet(bytes.length);
|
||||
}
|
||||
@@ -310,21 +326,21 @@ public class LogFileHandler extends Handler {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void publish(LogRecord record) {
|
||||
final String sourceClassName = record.getSourceClassName();
|
||||
public void publish(LogRecord log) {
|
||||
final String sourceClassName = log.getSourceClassName();
|
||||
if (sourceClassName == null || true) {
|
||||
StackTraceElement[] ses = new Throwable().getStackTrace();
|
||||
for (int i = 2; i < ses.length; i++) {
|
||||
if (ses[i].getClassName().startsWith("java.util.logging")) continue;
|
||||
record.setSourceClassName('[' + Thread.currentThread().getName() + "] " + ses[i].getClassName());
|
||||
record.setSourceMethodName(ses[i].getMethodName());
|
||||
log.setSourceClassName('[' + Thread.currentThread().getName() + "] " + ses[i].getClassName());
|
||||
log.setSourceMethodName(ses[i].getMethodName());
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
record.setSourceClassName('[' + Thread.currentThread().getName() + "] " + sourceClassName);
|
||||
log.setSourceClassName('[' + Thread.currentThread().getName() + "] " + sourceClassName);
|
||||
}
|
||||
if (denyreg != null && denyreg.matcher(record.getMessage()).find()) return;
|
||||
records.offer(record);
|
||||
if (denyreg != null && denyreg.matcher(log.getMessage()).find()) return;
|
||||
logqueue.offer(log);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -14,6 +14,8 @@ import java.util.logging.Level;
|
||||
import javax.annotation.*;
|
||||
import static org.redkale.boot.Application.RESNAME_SNCP_ADDR;
|
||||
import org.redkale.boot.ClassFilter.FilterEntry;
|
||||
import org.redkale.cluster.ClusterAgent;
|
||||
import org.redkale.mq.MessageAgent;
|
||||
import org.redkale.net.*;
|
||||
import org.redkale.net.http.*;
|
||||
import org.redkale.net.sncp.Sncp;
|
||||
@@ -30,7 +32,7 @@ import org.redkale.watch.*;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
@NodeProtocol({"HTTP"})
|
||||
@NodeProtocol("HTTP")
|
||||
public class NodeHttpServer extends NodeServer {
|
||||
|
||||
protected final boolean rest; //是否加载REST服务, 为true加载rest节点信息并将所有可REST化的Service生成RestServlet
|
||||
@@ -44,7 +46,7 @@ public class NodeHttpServer extends NodeServer {
|
||||
}
|
||||
|
||||
private static Server createServer(Application application, AnyValue serconf) {
|
||||
return new HttpServer(application.getStartTime(), application.getResourceFactory().createChild());
|
||||
return new HttpServer(application, application.getStartTime(), application.getResourceFactory().createChild());
|
||||
}
|
||||
|
||||
public HttpServer getHttpServer() {
|
||||
@@ -120,7 +122,15 @@ public class NodeHttpServer extends NodeServer {
|
||||
resourceFactory.register(RESNAME_SNCP_ADDR, String.class, sncpResFactory.find(RESNAME_SNCP_ADDR, String.class));
|
||||
}
|
||||
if (nodeService == null) {
|
||||
nodeService = Sncp.createLocalService(serverClassLoader, resourceName, WebSocketNodeService.class, application.getResourceFactory(), application.getSncpTransportFactory(), (InetSocketAddress) null, (Set<String>) null, (AnyValue) null);
|
||||
MessageAgent messageAgent = null;
|
||||
try {
|
||||
Field c = WebSocketServlet.class.getDeclaredField("messageAgent");
|
||||
c.setAccessible(true);
|
||||
messageAgent = (MessageAgent) c.get(src);
|
||||
} catch (Exception ex) {
|
||||
logger.log(Level.WARNING, "WebSocketServlet getMessageAgent error", ex);
|
||||
}
|
||||
nodeService = Sncp.createLocalService(serverClassLoader, resourceName, WebSocketNodeService.class, messageAgent, application.getResourceFactory(), application.getSncpTransportFactory(), (InetSocketAddress) null, (Set<String>) null, (AnyValue) null);
|
||||
regFactory.register(resourceName, WebSocketNode.class, nodeService);
|
||||
}
|
||||
resourceFactory.inject(nodeService, self);
|
||||
@@ -136,7 +146,7 @@ public class NodeHttpServer extends NodeServer {
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void loadHttpFilter(final AnyValue filtersConf, final ClassFilter<? extends Filter> classFilter) throws Exception {
|
||||
final StringBuilder sb = logger.isLoggable(Level.INFO) ? new StringBuilder() : null;
|
||||
final String threadName = "[" + Thread.currentThread().getName() + "] ";
|
||||
final String localThreadName = "[" + Thread.currentThread().getName() + "] ";
|
||||
List<FilterEntry<? extends Filter>> list = new ArrayList(classFilter.getFilterEntrys());
|
||||
for (FilterEntry<? extends Filter> en : list) {
|
||||
Class<HttpFilter> clazz = (Class<HttpFilter>) en.getType();
|
||||
@@ -145,7 +155,7 @@ public class NodeHttpServer extends NodeServer {
|
||||
resourceFactory.inject(filter, this);
|
||||
DefaultAnyValue filterConf = (DefaultAnyValue) en.getProperty();
|
||||
this.httpServer.addHttpFilter(filter, filterConf);
|
||||
if (sb != null) sb.append(threadName).append(" Load ").append(clazz.getName()).append(LINE_SEPARATOR);
|
||||
if (sb != null) sb.append(localThreadName).append(" Load ").append(clazz.getName()).append(LINE_SEPARATOR);
|
||||
}
|
||||
if (sb != null && sb.length() > 0) logger.log(Level.INFO, sb.toString());
|
||||
}
|
||||
@@ -158,7 +168,7 @@ public class NodeHttpServer extends NodeServer {
|
||||
if (!prefix0.isEmpty() && prefix0.charAt(prefix0.length() - 1) == '/') prefix0 = prefix0.substring(0, prefix0.length() - 1);
|
||||
if (!prefix0.isEmpty() && prefix0.charAt(0) != '/') prefix0 = '/' + prefix0;
|
||||
final String prefix = prefix0;
|
||||
final String threadName = "[" + Thread.currentThread().getName() + "] ";
|
||||
final String localThreadName = "[" + Thread.currentThread().getName() + "] ";
|
||||
List<FilterEntry<? extends Servlet>> list = new ArrayList(servletFilter.getFilterEntrys());
|
||||
list.sort((FilterEntry<? extends Servlet> o1, FilterEntry<? extends Servlet> o2) -> { //必须保证WebSocketServlet优先加载, 因为要确保其他的HttpServlet可以注入本地模式的WebSocketNode
|
||||
boolean ws1 = WebSocketServlet.class.isAssignableFrom(o1.getType());
|
||||
@@ -176,7 +186,11 @@ public class NodeHttpServer extends NodeServer {
|
||||
Class<HttpServlet> clazz = (Class<HttpServlet>) en.getType();
|
||||
if (Modifier.isAbstract(clazz.getModifiers())) continue;
|
||||
WebServlet ws = clazz.getAnnotation(WebServlet.class);
|
||||
if (ws == null || ws.value().length == 0) continue;
|
||||
if (ws == null) continue;
|
||||
if (ws.value().length == 0) {
|
||||
logger.log(Level.INFO, "not found @WebServlet.value in " + clazz.getName());
|
||||
continue;
|
||||
}
|
||||
final HttpServlet servlet = clazz.getDeclaredConstructor().newInstance();
|
||||
resourceFactory.inject(servlet, this);
|
||||
final String[] mappings = ws.value();
|
||||
@@ -197,7 +211,7 @@ public class NodeHttpServer extends NodeServer {
|
||||
if (as.getKey().length() > max) max = as.getKey().length();
|
||||
}
|
||||
for (AbstractMap.SimpleEntry<String, String[]> as : ss) {
|
||||
sb.append(threadName).append(" Load ").append(as.getKey());
|
||||
sb.append(localThreadName).append(" Load ").append(as.getKey());
|
||||
for (int i = 0; i < max - as.getKey().length(); i++) {
|
||||
sb.append(' ');
|
||||
}
|
||||
@@ -222,11 +236,18 @@ public class NodeHttpServer extends NodeServer {
|
||||
String prefix0 = restConf.getValue("path", "");
|
||||
if (!prefix0.isEmpty() && prefix0.charAt(prefix0.length() - 1) == '/') prefix0 = prefix0.substring(0, prefix0.length() - 1);
|
||||
if (!prefix0.isEmpty() && prefix0.charAt(0) != '/') prefix0 = '/' + prefix0;
|
||||
final String prefix = prefix0;
|
||||
|
||||
final String threadName = "[" + Thread.currentThread().getName() + "] ";
|
||||
final String localThreadName = "[" + Thread.currentThread().getName() + "] ";
|
||||
final List<AbstractMap.SimpleEntry<String, String[]>> ss = sb == null ? null : new ArrayList<>();
|
||||
|
||||
String mqname = restConf.getValue("mq");
|
||||
MessageAgent agent0 = null;
|
||||
if (mqname != null) {
|
||||
agent0 = application.getMessageAgent(mqname);
|
||||
if (agent0 == null) throw new RuntimeException("not found " + MessageAgent.class.getSimpleName() + " config for (name=" + mqname + ")");
|
||||
}
|
||||
final MessageAgent messageAgent = agent0;
|
||||
if (messageAgent != null) prefix0 = ""; //开启MQ时,prefix字段失效
|
||||
final String prefix = prefix0;
|
||||
final boolean autoload = restConf.getBoolValue("autoload", true);
|
||||
{ //加载RestService
|
||||
String userTypeStr = restConf.getValue("usertype");
|
||||
@@ -269,7 +290,9 @@ public class NodeHttpServer extends NodeServer {
|
||||
WebServlet ws = servlet.getClass().getAnnotation(WebServlet.class);
|
||||
if (ws != null && !ws.repair()) prefix2 = "";
|
||||
resourceFactory.inject(servlet, NodeHttpServer.this);
|
||||
//if (finest) logger.finest(threadName + " Create RestServlet(resource.name='" + name + "') = " + servlet);
|
||||
dynServletMap.put(service, servlet);
|
||||
if (messageAgent != null) messageAgent.putService(this, service, servlet);
|
||||
//if (finest) logger.finest(localThreadName + " Create RestServlet(resource.name='" + name + "') = " + servlet);
|
||||
if (ss != null) {
|
||||
String[] mappings = servlet.getClass().getAnnotation(WebServlet.class).value();
|
||||
for (int i = 0; i < mappings.length; i++) {
|
||||
@@ -322,13 +345,13 @@ public class NodeHttpServer extends NodeServer {
|
||||
return;
|
||||
}
|
||||
restedObjects.add(stype); //避免重复创建Rest对象
|
||||
HttpServlet servlet = httpServer.addRestWebSocketServlet(serverClassLoader, stype, prefix, en.getProperty());
|
||||
WebSocketServlet servlet = httpServer.addRestWebSocketServlet(serverClassLoader, stype, messageAgent, prefix, en.getProperty());
|
||||
if (servlet == null) return; //没有RestOnMessage方法的HttpServlet调用Rest.createRestWebSocketServlet就会返回null
|
||||
String prefix2 = prefix;
|
||||
WebServlet ws = servlet.getClass().getAnnotation(WebServlet.class);
|
||||
if (ws != null && !ws.repair()) prefix2 = "";
|
||||
resourceFactory.inject(servlet, NodeHttpServer.this);
|
||||
if (finest) logger.finest(threadName + " " + stype.getName() + " create a RestWebSocketServlet");
|
||||
if (finest) logger.finest(localThreadName + " " + stype.getName() + " create a RestWebSocketServlet");
|
||||
if (ss != null) {
|
||||
String[] mappings = servlet.getClass().getAnnotation(WebServlet.class).value();
|
||||
for (int i = 0; i < mappings.length; i++) {
|
||||
@@ -338,6 +361,7 @@ public class NodeHttpServer extends NodeServer {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (messageAgent != null) this.messageAgents.put(messageAgent.getName(), messageAgent);
|
||||
//输出信息
|
||||
if (ss != null && !ss.isEmpty() && sb != null) {
|
||||
ss.sort((AbstractMap.SimpleEntry<String, String[]> o1, AbstractMap.SimpleEntry<String, String[]> o2) -> o1.getKey().compareTo(o2.getKey()));
|
||||
@@ -346,13 +370,30 @@ public class NodeHttpServer extends NodeServer {
|
||||
if (as.getKey().length() > max) max = as.getKey().length();
|
||||
}
|
||||
for (AbstractMap.SimpleEntry<String, String[]> as : ss) {
|
||||
sb.append(threadName).append(" Load ").append(as.getKey());
|
||||
sb.append(localThreadName).append(" Load ").append(as.getKey());
|
||||
for (int i = 0; i < max - as.getKey().length(); i++) {
|
||||
sb.append(' ');
|
||||
}
|
||||
sb.append(" mapping to ").append(Arrays.toString(as.getValue())).append(LINE_SEPARATOR);
|
||||
}
|
||||
sb.append(threadName).append(" All HttpServlets load cost " + (System.currentTimeMillis() - starts) + " ms" + LINE_SEPARATOR);
|
||||
sb.append(localThreadName).append(" All HttpServlets load cost ").append(System.currentTimeMillis() - starts).append(" ms").append(LINE_SEPARATOR);
|
||||
}
|
||||
}
|
||||
|
||||
@Override //loadServlet执行之后调用
|
||||
protected void postLoadServlets() {
|
||||
final ClusterAgent cluster = application.clusterAgent;
|
||||
if (cluster != null) {
|
||||
NodeProtocol pros = getClass().getAnnotation(NodeProtocol.class);
|
||||
String protocol = pros.value().toUpperCase();
|
||||
if (!cluster.containsProtocol(protocol)) return;
|
||||
if (!cluster.containsPort(server.getSocketAddress().getPort())) return;
|
||||
cluster.register(this, protocol, dynServletMap.keySet(), new HashSet<>());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void afterClusterDeregisterOnPreDestroyServices(ClusterAgent cluster, String protocol) {
|
||||
cluster.deregister(this, protocol, dynServletMap.keySet(), new HashSet<>());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,5 +20,5 @@ import java.lang.annotation.*;
|
||||
@Documented
|
||||
public @interface NodeProtocol {
|
||||
|
||||
String[] value();
|
||||
String value();
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
*/
|
||||
package org.redkale.boot;
|
||||
|
||||
import org.redkale.cluster.ClusterAgent;
|
||||
import org.redkale.mq.MessageAgent;
|
||||
import org.redkale.util.RedkaleClassLoader;
|
||||
import java.io.*;
|
||||
import java.lang.annotation.Annotation;
|
||||
@@ -17,7 +19,6 @@ import java.util.concurrent.*;
|
||||
import java.util.function.*;
|
||||
import java.util.logging.*;
|
||||
import javax.annotation.*;
|
||||
import javax.persistence.Transient;
|
||||
import static org.redkale.boot.Application.*;
|
||||
import org.redkale.boot.ClassFilter.FilterEntry;
|
||||
import org.redkale.net.Filter;
|
||||
@@ -27,7 +28,6 @@ import org.redkale.net.sncp.*;
|
||||
import org.redkale.service.*;
|
||||
import org.redkale.source.*;
|
||||
import org.redkale.util.*;
|
||||
import org.redkale.util.AnyValue.DefaultAnyValue;
|
||||
|
||||
/**
|
||||
* Server节点的初始化配置类
|
||||
@@ -67,11 +67,13 @@ public abstract class NodeServer {
|
||||
private InetSocketAddress sncpAddress;
|
||||
|
||||
//加载Service时的处理函数
|
||||
protected Consumer<Service> consumer;
|
||||
protected BiConsumer<MessageAgent, Service> consumer;
|
||||
|
||||
//server节点的配置
|
||||
protected AnyValue serverConf;
|
||||
|
||||
protected final String threadName;
|
||||
|
||||
//加载server节点后的拦截器
|
||||
protected NodeInterceptor interceptor;
|
||||
|
||||
@@ -84,11 +86,20 @@ public abstract class NodeServer {
|
||||
//远程模式的Service对象集合
|
||||
protected final Set<Service> remoteServices = new LinkedHashSet<>();
|
||||
|
||||
protected final Map<Service, Servlet> dynServletMap = new LinkedHashMap<>();
|
||||
|
||||
//MessageAgent对象集合
|
||||
protected final Map<String, MessageAgent> messageAgents = new HashMap<>();
|
||||
|
||||
//需要远程模式Service的MessageAgent对象集合
|
||||
protected final Map<String, MessageAgent> sncpRemoteAgents = new HashMap<>();
|
||||
|
||||
private volatile int maxClassNameLength = 0;
|
||||
|
||||
private volatile int maxNameLength = 0;
|
||||
|
||||
public NodeServer(Application application, Server server) {
|
||||
this.threadName = Thread.currentThread().getName();
|
||||
this.application = application;
|
||||
this.server = server;
|
||||
this.resourceFactory = server.getResourceFactory();
|
||||
@@ -111,7 +122,7 @@ public abstract class NodeServer {
|
||||
this.serverConf = config == null ? AnyValue.create() : config;
|
||||
if (isSNCP()) { // SNCP协议
|
||||
String host = this.serverConf.getValue("host", isWATCH() ? "127.0.0.1" : "0.0.0.0").replace("0.0.0.0", "");
|
||||
this.sncpAddress = new InetSocketAddress(host.isEmpty() ? application.localAddress.getHostAddress() : host, this.serverConf.getIntValue("port"));
|
||||
this.sncpAddress = new InetSocketAddress(host.isEmpty() ? application.localAddress.getAddress().getHostAddress() : host, this.serverConf.getIntValue("port"));
|
||||
this.sncpGroup = application.sncpTransportFactory.findGroupName(this.sncpAddress);
|
||||
//单向SNCP服务不需要对等group
|
||||
//if (this.sncpGroup == null) throw new RuntimeException("Server (" + String.valueOf(config).replaceAll("\\s+", " ") + ") not found <group> info");
|
||||
@@ -142,9 +153,10 @@ public abstract class NodeServer {
|
||||
//必须要进行初始化, 构建Service时需要使用Context中的ExecutorService
|
||||
server.init(this.serverConf);
|
||||
//init之后才有Executor
|
||||
resourceFactory.register(Server.RESNAME_SERVER_EXECUTOR, Executor.class, server.getExecutor());
|
||||
resourceFactory.register(Server.RESNAME_SERVER_EXECUTOR, ExecutorService.class, server.getExecutor());
|
||||
resourceFactory.register(Server.RESNAME_SERVER_EXECUTOR, ThreadPoolExecutor.class, server.getExecutor());
|
||||
//废弃 @since 2.3.0
|
||||
// resourceFactory.register(Server.RESNAME_SERVER_EXECUTOR, Executor.class, server.getWorkExecutor());
|
||||
// resourceFactory.register(Server.RESNAME_SERVER_EXECUTOR, ExecutorService.class, server.getWorkExecutor());
|
||||
// resourceFactory.register(Server.RESNAME_SERVER_EXECUTOR, ThreadPoolExecutor.class, server.getWorkExecutor());
|
||||
|
||||
initResource(); //给 DataSource、CacheSource 注册依赖注入时的监听回调事件。
|
||||
String interceptorClass = this.serverConf.getValue("interceptor", "");
|
||||
@@ -155,8 +167,8 @@ public abstract class NodeServer {
|
||||
|
||||
ClassFilter<Service> serviceFilter = createServiceClassFilter();
|
||||
if (application.singletonrun) { //singleton模式下只加载指定的Service
|
||||
final String ssc = System.getProperty("red" + "kale-singleton-serviceclass");
|
||||
final String extssc = System.getProperty("red" + "kale-singleton-extserviceclasses");
|
||||
final String ssc = System.getProperty("red" + "kale.singleton.serviceclass");
|
||||
final String extssc = System.getProperty("red" + "kale.singleton.extserviceclasses");
|
||||
if (ssc != null) {
|
||||
final List<String> sscList = new ArrayList<>();
|
||||
sscList.add(ssc);
|
||||
@@ -172,13 +184,14 @@ public abstract class NodeServer {
|
||||
ClassFilter<Servlet> servletFilter = createServletClassFilter();
|
||||
ClassFilter otherFilter = createOtherClassFilter();
|
||||
long s = System.currentTimeMillis();
|
||||
ClassFilter.Loader.load(application.getHome(), serverConf.getValue("excludelibs", "").split(";"), serviceFilter, filterFilter, servletFilter, otherFilter);
|
||||
ClassFilter.Loader.load(application.getHome(), ((application.excludelibs != null ? (application.excludelibs + ";") : "") + serverConf.getValue("excludelibs", "")).split(";"), serviceFilter, filterFilter, servletFilter, otherFilter);
|
||||
long e = System.currentTimeMillis() - s;
|
||||
logger.info(this.getClass().getSimpleName() + " load filter class in " + e + " ms");
|
||||
loadService(serviceFilter, otherFilter); //必须在servlet之前
|
||||
if (!application.singletonrun) { //非singleton模式下才加载Filter、Servlet
|
||||
loadFilter(filterFilter, otherFilter);
|
||||
loadServlet(servletFilter, otherFilter);
|
||||
postLoadServlets();
|
||||
}
|
||||
if (this.interceptor != null) this.resourceFactory.inject(this.interceptor);
|
||||
}
|
||||
@@ -198,7 +211,20 @@ public abstract class NodeServer {
|
||||
if (resources != null) {
|
||||
for (AnyValue sourceConf : resources.getAnyValues("source")) {
|
||||
try {
|
||||
Class type = serverClassLoader.loadClass(sourceConf.getValue("value"));
|
||||
String classval = sourceConf.getValue("value");
|
||||
Class type = null;
|
||||
if (classval == null || classval.isEmpty()) {
|
||||
Iterator<CacheSourceLoader> it = ServiceLoader.load(CacheSourceLoader.class, serverClassLoader).iterator();
|
||||
while (it.hasNext()) {
|
||||
CacheSourceLoader s = it.next();
|
||||
if (s != null && s.match(sourceConf)) {
|
||||
type = s.sourceClass();
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
type = serverClassLoader.loadClass(classval);
|
||||
}
|
||||
if (type == DataSource.class) {
|
||||
type = DataMemorySource.class;
|
||||
for (AnyValue itemConf : sourceConf.getAnyValues("property")) {
|
||||
@@ -245,6 +271,34 @@ public abstract class NodeServer {
|
||||
}
|
||||
}, AnyValue.class, AnyValue[].class);
|
||||
|
||||
//------------------------------------- 注册 Local AutoLoad(false) Service --------------------------------------------------------
|
||||
resourceFactory.register((ResourceFactory rf, final Object src, String resourceName, Field field, final Object attachment) -> {
|
||||
Class<Service> resServiceType = Service.class;
|
||||
try {
|
||||
if (field.getAnnotation(Resource.class) == null) return;
|
||||
if ((src instanceof Service) && Sncp.isRemote((Service) src)) return; //远程模式不得注入 AutoLoad Service
|
||||
if (!Service.class.isAssignableFrom(field.getType())) return;
|
||||
resServiceType = (Class) field.getType();
|
||||
if (resServiceType.getAnnotation(Local.class) == null) return;
|
||||
AutoLoad al = resServiceType.getAnnotation(AutoLoad.class);
|
||||
if (al == null || al.value()) return;
|
||||
|
||||
//ResourceFactory resfactory = (isSNCP() ? appResFactory : resourceFactory);
|
||||
SncpClient client = src instanceof Service ? Sncp.getSncpClient((Service) src) : null;
|
||||
final InetSocketAddress sncpAddr = client == null ? null : client.getClientAddress();
|
||||
final Set<String> groups = new HashSet<>();
|
||||
Service service = Sncp.createLocalService(serverClassLoader, resourceName, resServiceType, null, appResFactory, appSncpTranFactory, sncpAddr, groups, null);
|
||||
appResFactory.register(resourceName, resServiceType, service);
|
||||
|
||||
field.set(src, service);
|
||||
rf.inject(service, self); // 给其可能包含@Resource的字段赋值;
|
||||
service.init(null);
|
||||
logger.info("[" + Thread.currentThread().getName() + "] Load Service(@Local @AutoLoad) resourceName = " + resourceName + ", service = " + service);
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "[" + Thread.currentThread().getName() + "] Load @Local @AutoLoad(false) Service inject " + resServiceType + " to " + src + " error", e);
|
||||
}
|
||||
}, Service.class);
|
||||
|
||||
//------------------------------------- 注册 DataSource --------------------------------------------------------
|
||||
resourceFactory.register((ResourceFactory rf, final Object src, String resourceName, Field field, final Object attachment) -> {
|
||||
try {
|
||||
@@ -253,7 +307,6 @@ public abstract class NodeServer {
|
||||
SimpleEntry<Class, AnyValue> resEntry = dataResources.get(resourceName);
|
||||
AnyValue sourceConf = resEntry == null ? null : resEntry.getValue();
|
||||
DataSource source = null;
|
||||
boolean needinit = true;
|
||||
if (sourceConf != null) {
|
||||
final Class sourceType = resEntry.getKey();
|
||||
if (sourceType == DataJdbcSource.class) {
|
||||
@@ -271,39 +324,26 @@ public abstract class NodeServer {
|
||||
SncpClient client = Sncp.getSncpClient(srcService);
|
||||
final InetSocketAddress sncpAddr = client == null ? null : client.getClientAddress();
|
||||
final Set<String> groups = new HashSet<>();
|
||||
if (client != null && client.getSameGroup() != null) groups.add(client.getSameGroup());
|
||||
if (client != null && client.getDiffGroups() != null) groups.addAll(client.getDiffGroups());
|
||||
source = (DataSource) Sncp.createLocalService(serverClassLoader, resourceName, sourceType, appResFactory, appSncpTranFactory, sncpAddr, groups, Sncp.getConf(srcService));
|
||||
source = (DataSource) Sncp.createLocalService(serverClassLoader, resourceName, sourceType, client == null ? null : client.getMessageAgent(), appResFactory, appSncpTranFactory, sncpAddr, groups, Sncp.getConf(srcService));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (source == null) {
|
||||
source = DataSources.createDataSource(resourceName); //从persistence.xml配置中创建
|
||||
needinit = false;
|
||||
}
|
||||
|
||||
application.dataSources.add(source);
|
||||
if (source instanceof SearchSource) {
|
||||
appResFactory.register(resourceName, SearchSource.class, source);
|
||||
}
|
||||
appResFactory.register(resourceName, DataSource.class, source);
|
||||
|
||||
SncpClient client = Sncp.getSncpClient((Service) src);
|
||||
final InetSocketAddress sncpAddr = client == null ? null : client.getClientAddress();
|
||||
if ((src instanceof DataSource) && sncpAddr != null && resourceFactory.find(resourceName, DataCacheListener.class) == null) { //只有DataSourceService 才能赋值 DataCacheListener
|
||||
final NodeSncpServer sncpServer = application.findNodeSncpServer(sncpAddr);
|
||||
final Set<String> groups = new HashSet<>();
|
||||
if (client != null && client.getSameGroup() != null) groups.add(client.getSameGroup());
|
||||
if (client != null && client.getDiffGroups() != null) groups.addAll(client.getDiffGroups());
|
||||
Service cacheListenerService = Sncp.createLocalService(serverClassLoader, resourceName, DataCacheListenerService.class, appResFactory, appSncpTranFactory, sncpAddr, groups, Sncp.getConf((Service) src));
|
||||
appResFactory.register(resourceName, DataCacheListener.class, cacheListenerService);
|
||||
localServices.add(cacheListenerService);
|
||||
sncpServer.consumerAccept(cacheListenerService);
|
||||
rf.inject(cacheListenerService, self);
|
||||
logger.info("[" + Thread.currentThread().getName() + "] Load Service " + cacheListenerService);
|
||||
}
|
||||
field.set(src, source);
|
||||
rf.inject(source, self); // 给其可能包含@Resource的字段赋值;
|
||||
rf.inject(source, self); // 给AsyncGroup和其他@Resource的字段赋值;
|
||||
//NodeServer.this.watchFactory.inject(src);
|
||||
if (source instanceof Service && needinit) ((Service) source).init(sourceConf);
|
||||
if (source instanceof Service) ((Service) source).init(sourceConf);
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "DataSource inject error", e);
|
||||
logger.log(Level.SEVERE, "[" + Thread.currentThread().getName() + "] DataSource inject to " + src + " error", e);
|
||||
}
|
||||
}, DataSource.class);
|
||||
|
||||
@@ -318,31 +358,38 @@ public abstract class NodeServer {
|
||||
final Service srcService = (Service) src;
|
||||
SncpClient client = Sncp.getSncpClient(srcService);
|
||||
final InetSocketAddress sncpAddr = client == null ? null : client.getClientAddress();
|
||||
final Set<String> groups = new HashSet<>();
|
||||
if (client != null && client.getSameGroup() != null) groups.add(client.getSameGroup());
|
||||
if (client != null && client.getDiffGroups() != null) groups.addAll(client.getDiffGroups());
|
||||
|
||||
SimpleEntry<Class, AnyValue> resEntry = cacheResource.get(resourceName);
|
||||
AnyValue sourceConf = resEntry == null ? null : resEntry.getValue();
|
||||
if (sourceConf == null) {
|
||||
SimpleEntry<Class, AnyValue> resEntry2 = dataResources.get(resourceName);
|
||||
sourceConf = resEntry2 == null ? null : resEntry2.getValue();
|
||||
}
|
||||
final Class sourceType = sourceConf == null ? CacheMemorySource.class : serverClassLoader.loadClass(sourceConf.getValue("value"));
|
||||
Class sourceType0 = CacheMemorySource.class;
|
||||
if (sourceConf != null) {
|
||||
String classval = sourceConf.getValue("value");
|
||||
if (classval == null || classval.isEmpty()) {
|
||||
Iterator<CacheSourceLoader> it = ServiceLoader.load(CacheSourceLoader.class, serverClassLoader).iterator();
|
||||
while (it.hasNext()) {
|
||||
CacheSourceLoader s = it.next();
|
||||
if (s != null && s.match(sourceConf)) {
|
||||
sourceType0 = s.sourceClass();
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
sourceType0 = serverClassLoader.loadClass(classval);
|
||||
}
|
||||
}
|
||||
final Class sourceType = sourceType0;
|
||||
Object source = null;
|
||||
if (CacheSource.class.isAssignableFrom(sourceType)) { // CacheSource
|
||||
source = (CacheSource) Sncp.createLocalService(serverClassLoader, resourceName, sourceType, appResFactory, appSncpTranFactory, sncpAddr, groups, Sncp.getConf(srcService));
|
||||
source = Modifier.isFinal(sourceType.getModifiers()) ? sourceType.getConstructor().newInstance() : (CacheSource) Sncp.createLocalService(serverClassLoader, resourceName, sourceType, client == null ? null : client.getMessageAgent(), appResFactory, appSncpTranFactory, sncpAddr, null, Sncp.getConf(srcService));
|
||||
Type genericType = field.getGenericType();
|
||||
ParameterizedType pt = (genericType instanceof ParameterizedType) ? (ParameterizedType) genericType : null;
|
||||
Type valType = pt == null ? null : pt.getActualTypeArguments()[0];
|
||||
if (CacheSource.class.isAssignableFrom(sourceType)) {
|
||||
CacheSource cacheSource = (CacheSource) source;
|
||||
cacheSource.initValueType(valType instanceof Class ? (Class) valType : Object.class);
|
||||
cacheSource.initTransient(field.getAnnotation(Transient.class) != null); //必须在initValueType之后
|
||||
}
|
||||
application.cacheSources.add((CacheSource) source);
|
||||
appResFactory.register(resourceName, genericType, source);
|
||||
appResFactory.register(resourceName, CacheSource.class, source);
|
||||
if (genericType != CacheSource.class) {
|
||||
appResFactory.register(resourceName, genericType, source);
|
||||
}
|
||||
}
|
||||
field.set(src, source);
|
||||
rf.inject(source, self); //
|
||||
@@ -353,7 +400,7 @@ public abstract class NodeServer {
|
||||
sncpServer.getSncpServer().addSncpServlet((Service) source);
|
||||
//logger.info("[" + Thread.currentThread().getName() + "] Load Service " + source);
|
||||
}
|
||||
logger.info("[" + Thread.currentThread().getName() + "] Load Source " + source);
|
||||
logger.info("[" + Thread.currentThread().getName() + "] Load Source resourceName = " + resourceName + ", source = " + source);
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "DataSource inject error", e);
|
||||
}
|
||||
@@ -376,18 +423,21 @@ public abstract class NodeServer {
|
||||
if (nodeService == null) {
|
||||
final HashSet<String> groups = new HashSet<>();
|
||||
if (groups.isEmpty() && isSNCP() && NodeServer.this.sncpGroup != null) groups.add(NodeServer.this.sncpGroup);
|
||||
nodeService = Sncp.createLocalService(serverClassLoader, resourceName, WebSocketNodeService.class, application.getResourceFactory(), application.getSncpTransportFactory(), NodeServer.this.sncpAddress, groups, (AnyValue) null);
|
||||
nodeService = Sncp.createLocalService(serverClassLoader, resourceName, WebSocketNodeService.class, Sncp.getMessageAgent((Service) src), application.getResourceFactory(), application.getSncpTransportFactory(), NodeServer.this.sncpAddress, groups, (AnyValue) null);
|
||||
(isSNCP() ? appResFactory : resourceFactory).register(resourceName, WebSocketNode.class, nodeService);
|
||||
((WebSocketNodeService) nodeService).setName(resourceName);
|
||||
}
|
||||
resourceFactory.inject(nodeService, self);
|
||||
MessageAgent messageAgent = Sncp.getMessageAgent((Service) src);
|
||||
if (messageAgent != null && Sncp.getMessageAgent(nodeService) == null) Sncp.setMessageAgent(nodeService, messageAgent);
|
||||
field.set(src, nodeService);
|
||||
if (Sncp.isRemote(nodeService)) {
|
||||
remoteServices.add(nodeService);
|
||||
} else {
|
||||
if (field != null) rf.inject(nodeService); //动态加载的Service也存在按需加载的注入资源
|
||||
rf.inject(nodeService); //动态加载的Service也存在按需加载的注入资源
|
||||
localServices.add(nodeService);
|
||||
interceptorServices.add(nodeService);
|
||||
if (consumer != null) consumer.accept(nodeService);
|
||||
if (consumer != null) consumer.accept(null, nodeService);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "WebSocketNode inject error", e);
|
||||
@@ -405,7 +455,7 @@ public abstract class NodeServer {
|
||||
protected void loadService(ClassFilter<? extends Service> serviceFilter, ClassFilter otherFilter) throws Exception {
|
||||
if (serviceFilter == null) return;
|
||||
final long starts = System.currentTimeMillis();
|
||||
final String threadName = "[" + Thread.currentThread().getName() + "] ";
|
||||
final String localThreadName = "[" + Thread.currentThread().getName() + "] ";
|
||||
final Set<FilterEntry<? extends Service>> entrys = (Set) serviceFilter.getAllFilterEntrys();
|
||||
ResourceFactory regFactory = isSNCP() ? application.getResourceFactory() : resourceFactory;
|
||||
final ResourceFactory appResourceFactory = application.getResourceFactory();
|
||||
@@ -418,12 +468,11 @@ public abstract class NodeServer {
|
||||
if (Modifier.isAbstract(serviceImplClass.getModifiers())) continue; //修饰abstract的类跳过
|
||||
if (DataSource.class.isAssignableFrom(serviceImplClass)) continue;
|
||||
if (CacheSource.class.isAssignableFrom(serviceImplClass)) continue;
|
||||
if (DataCacheListener.class.isAssignableFrom(serviceImplClass)) continue;
|
||||
}
|
||||
if (entry.getName().contains("$")) throw new RuntimeException("<name> value cannot contains '$' in " + entry.getProperty());
|
||||
Service oldother = resourceFactory.find(entry.getName(), serviceImplClass);
|
||||
if (oldother != null) { //Server加载Service时需要判断是否已经加载过了。
|
||||
interceptorServices.add(oldother);
|
||||
if (!Sncp.isRemote(oldother)) interceptorServices.add(oldother);
|
||||
continue;
|
||||
}
|
||||
final HashSet<String> groups = entry.getGroups(); //groups.isEmpty()表示<services>没有配置groups属性。
|
||||
@@ -437,16 +486,28 @@ public abstract class NodeServer {
|
||||
final ResourceFactory.ResourceLoader resourceLoader = (ResourceFactory rf, final Object src, final String resourceName, Field field, final Object attachment) -> {
|
||||
try {
|
||||
if (SncpClient.parseMethod(serviceImplClass).isEmpty() && serviceImplClass.getAnnotation(Priority.class) == null) { //class没有可用的方法且没有标记启动优先级的, 通常为BaseService
|
||||
if (!serviceImplClass.getName().startsWith("org.redkale.")) logger.log(Level.FINE, serviceImplClass + " cannot load because not found less one public non-final method");
|
||||
if (!serviceImplClass.getName().startsWith("org.redkale.") && !serviceImplClass.getSimpleName().contains("Base")) {
|
||||
logger.log(Level.FINE, serviceImplClass + " cannot load because not found less one public non-final method");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
MessageAgent agent = null;
|
||||
if (entry.getProperty() != null && entry.getProperty().getValue("mq") != null) {
|
||||
agent = application.getMessageAgent(entry.getProperty().getValue("mq"));
|
||||
if (agent != null) messageAgents.put(agent.getName(), agent);
|
||||
}
|
||||
|
||||
Service service;
|
||||
boolean ws = src instanceof WebSocketServlet;
|
||||
final boolean ws = src instanceof WebSocketServlet;
|
||||
if (ws || localed) { //本地模式
|
||||
service = Sncp.createLocalService(serverClassLoader, resourceName, serviceImplClass, appResourceFactory, appSncpTransFactory, NodeServer.this.sncpAddress, groups, entry.getProperty());
|
||||
service = Sncp.createLocalService(serverClassLoader, resourceName, serviceImplClass, agent, appResourceFactory, appSncpTransFactory, NodeServer.this.sncpAddress, groups, entry.getProperty());
|
||||
} else {
|
||||
service = Sncp.createRemoteService(serverClassLoader, resourceName, serviceImplClass, appSncpTransFactory, NodeServer.this.sncpAddress, groups, entry.getProperty());
|
||||
service = Sncp.createRemoteService(serverClassLoader, resourceName, serviceImplClass, agent, appSncpTransFactory, NodeServer.this.sncpAddress, groups, entry.getProperty());
|
||||
}
|
||||
if (service instanceof WebSocketNodeService) {
|
||||
((WebSocketNodeService) service).setName(resourceName);
|
||||
if (agent != null) Sncp.setMessageAgent(service, agent);
|
||||
}
|
||||
final Class restype = Sncp.getResourceType(service);
|
||||
if (rf.find(resourceName, restype) == null) {
|
||||
@@ -456,11 +517,12 @@ public abstract class NodeServer {
|
||||
}
|
||||
if (Sncp.isRemote(service)) {
|
||||
remoteServices.add(service);
|
||||
if (agent != null) sncpRemoteAgents.put(agent.getName(), agent);
|
||||
} else {
|
||||
if (field != null) rf.inject(service); //动态加载的Service也存在按需加载的注入资源
|
||||
localServices.add(service);
|
||||
interceptorServices.add(service);
|
||||
if (consumer != null) consumer.accept(service);
|
||||
if (consumer != null) consumer.accept(agent, service);
|
||||
}
|
||||
} catch (RuntimeException ex) {
|
||||
throw ex;
|
||||
@@ -493,7 +555,13 @@ public abstract class NodeServer {
|
||||
|
||||
if (sb != null) {
|
||||
remoteServices.forEach(y -> {
|
||||
sb.append(threadName).append(Sncp.toSimpleString(y, maxNameLength, maxClassNameLength)).append(" load and inject").append(LINE_SEPARATOR);
|
||||
sb.append(localThreadName).append(Sncp.toSimpleString(y, maxNameLength, maxClassNameLength)).append(" load and inject").append(LINE_SEPARATOR);
|
||||
});
|
||||
}
|
||||
if (isSNCP() && !sncpRemoteAgents.isEmpty()) {
|
||||
sncpRemoteAgents.values().forEach(agent -> {
|
||||
// agent.putSncpResp((NodeSncpServer) this);
|
||||
// agent.startSncpRespConsumer();
|
||||
});
|
||||
}
|
||||
//----------------- init -----------------
|
||||
@@ -510,21 +578,25 @@ public abstract class NodeServer {
|
||||
localServices.clear();
|
||||
localServices.addAll(swlist);
|
||||
//this.loadPersistData();
|
||||
long preinits = System.currentTimeMillis();
|
||||
preInitServices(localServices, remoteServices);
|
||||
long preinite = System.currentTimeMillis() - preinits;
|
||||
final List<String> slist = sb == null ? null : new CopyOnWriteArrayList<>();
|
||||
localServices.stream().forEach(y -> {
|
||||
long s = System.currentTimeMillis();
|
||||
y.init(Sncp.getConf(y));
|
||||
long e = System.currentTimeMillis() - s;
|
||||
String serstr = Sncp.toSimpleString(y, maxNameLength, maxClassNameLength);
|
||||
if (slist != null) slist.add(new StringBuilder().append(threadName).append(serstr).append(" load and init in ").append(e).append(" ms").append(LINE_SEPARATOR).toString());
|
||||
if (slist != null) slist.add(new StringBuilder().append(localThreadName).append(serstr).append(" load and init in ").append(e).append(" ms").append(LINE_SEPARATOR).toString());
|
||||
});
|
||||
if (slist != null && sb != null) {
|
||||
List<String> wlist = new ArrayList<>(slist); //直接使用CopyOnWriteArrayList偶尔会出现莫名的异常(CopyOnWriteArrayList源码1185行)
|
||||
for (String s : wlist) {
|
||||
sb.append(s);
|
||||
}
|
||||
sb.append(threadName).append("All Services load cost " + (System.currentTimeMillis() - starts) + " ms" + LINE_SEPARATOR);
|
||||
sb.append(localThreadName).append("All Services load cost ").append(System.currentTimeMillis() - starts).append(" ms" + LINE_SEPARATOR);
|
||||
}
|
||||
if (sb != null && preinite > 10) sb.append(localThreadName).append(ClusterAgent.class.getSimpleName()).append(" register ").append(preinite).append(" ms" + LINE_SEPARATOR);
|
||||
if (sb != null && sb.length() > 0) logger.log(Level.INFO, sb.toString());
|
||||
}
|
||||
|
||||
@@ -533,6 +605,44 @@ public abstract class NodeServer {
|
||||
maxClassNameLength = Math.max(maxClassNameLength, Sncp.getResourceType(y).getName().length() + 1);
|
||||
}
|
||||
|
||||
//Service.init执行之前调用
|
||||
protected void preInitServices(Set<Service> localServices, Set<Service> remoteServices) {
|
||||
final ClusterAgent cluster = application.clusterAgent;
|
||||
if (cluster == null) return;
|
||||
NodeProtocol pros = getClass().getAnnotation(NodeProtocol.class);
|
||||
String protocol = pros.value().toUpperCase();
|
||||
if (!cluster.containsProtocol(protocol)) return;
|
||||
if (!cluster.containsPort(server.getSocketAddress().getPort())) return;
|
||||
cluster.register(this, protocol, localServices, remoteServices);
|
||||
}
|
||||
|
||||
//loadServlet执行之后调用
|
||||
protected void postLoadServlets() {
|
||||
|
||||
}
|
||||
|
||||
//Service.destroy执行之前调用
|
||||
protected void preDestroyServices(Set<Service> localServices, Set<Service> remoteServices) {
|
||||
if (application.clusterAgent != null) { //服务注销
|
||||
final ClusterAgent cluster = application.clusterAgent;
|
||||
NodeProtocol pros = getClass().getAnnotation(NodeProtocol.class);
|
||||
String protocol = pros.value().toUpperCase();
|
||||
if (cluster.containsProtocol(protocol) && cluster.containsPort(server.getSocketAddress().getPort())) {
|
||||
cluster.deregister(this, protocol, localServices, remoteServices);
|
||||
afterClusterDeregisterOnPreDestroyServices(cluster, protocol);
|
||||
}
|
||||
}
|
||||
if (!this.messageAgents.isEmpty()) { //MQ
|
||||
}
|
||||
}
|
||||
|
||||
protected void afterClusterDeregisterOnPreDestroyServices(ClusterAgent cluster, String protocol) {
|
||||
}
|
||||
|
||||
//Server.start执行之后调用
|
||||
protected void postStartServer(Set<Service> localServices, Set<Service> remoteServices) {
|
||||
}
|
||||
|
||||
protected abstract ClassFilter<Filter> createFilterClassFilter();
|
||||
|
||||
protected abstract ClassFilter<Servlet> createServletClassFilter();
|
||||
@@ -563,22 +673,24 @@ public abstract class NodeServer {
|
||||
}
|
||||
cf = null;
|
||||
for (AnyValue list : proplist) {
|
||||
DefaultAnyValue prop = null;
|
||||
AnyValue.DefaultAnyValue prop = null;
|
||||
String sc = list.getValue("groups");
|
||||
String mq = list.getValue("mq");
|
||||
if (sc != null) {
|
||||
sc = sc.trim();
|
||||
if (sc.endsWith(";")) sc = sc.substring(0, sc.length() - 1);
|
||||
}
|
||||
if (sc == null) sc = localGroup;
|
||||
if (sc != null) {
|
||||
if (sc != null || mq != null) {
|
||||
prop = new AnyValue.DefaultAnyValue();
|
||||
prop.addValue("groups", sc);
|
||||
if (sc != null) prop.addValue("groups", sc);
|
||||
if (mq != null) prop.addValue("mq", mq);
|
||||
}
|
||||
ClassFilter filter = new ClassFilter(this.serverClassLoader, ref, inter, excludeSuperClasses, prop);
|
||||
for (AnyValue av : list.getAnyValues(property)) { // <service>、<filter>、<servlet> 节点
|
||||
final AnyValue[] items = av.getAnyValues("property");
|
||||
if (av instanceof DefaultAnyValue && items.length > 0) { //存在 <property>节点
|
||||
DefaultAnyValue dav = DefaultAnyValue.create();
|
||||
if (av instanceof AnyValue.DefaultAnyValue && items.length > 0) { //存在 <property>节点
|
||||
AnyValue.DefaultAnyValue dav = AnyValue.DefaultAnyValue.create();
|
||||
final AnyValue.Entry<String>[] strings = av.getStringEntrys();
|
||||
if (strings != null) { //将<service>、<filter>、<servlet>节点的属性值传给dav
|
||||
for (AnyValue.Entry<String> en : strings) {
|
||||
@@ -591,7 +703,7 @@ public abstract class NodeServer {
|
||||
if (!"property".equals(en.name)) dav.addValue(en.name, en.getValue());
|
||||
}
|
||||
}
|
||||
DefaultAnyValue ps = DefaultAnyValue.create();
|
||||
AnyValue.DefaultAnyValue ps = AnyValue.DefaultAnyValue.create();
|
||||
for (AnyValue item : items) {
|
||||
ps.addValue(item.getValue("name"), item.getValue("value"));
|
||||
}
|
||||
@@ -627,6 +739,10 @@ public abstract class NodeServer {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Application getApplication() {
|
||||
return application;
|
||||
}
|
||||
|
||||
public ResourceFactory getResourceFactory() {
|
||||
return resourceFactory;
|
||||
}
|
||||
@@ -659,13 +775,15 @@ public abstract class NodeServer {
|
||||
|
||||
public void start() throws IOException {
|
||||
if (interceptor != null) interceptor.preStart(this);
|
||||
server.start();
|
||||
server.start(application);
|
||||
postStartServer(localServices, remoteServices);
|
||||
}
|
||||
|
||||
public void shutdown() throws IOException {
|
||||
if (interceptor != null) interceptor.preShutdown(this);
|
||||
final StringBuilder sb = logger.isLoggable(Level.INFO) ? new StringBuilder() : null;
|
||||
final boolean finest = logger.isLoggable(Level.FINEST);
|
||||
preDestroyServices(localServices, remoteServices);
|
||||
localServices.forEach(y -> {
|
||||
long s = System.currentTimeMillis();
|
||||
if (finest) logger.finest(y + " is destroying");
|
||||
@@ -680,6 +798,42 @@ public abstract class NodeServer {
|
||||
server.shutdown();
|
||||
}
|
||||
|
||||
public void command(String cmd) throws IOException {
|
||||
final StringBuilder sb = logger.isLoggable(Level.INFO) ? new StringBuilder() : null;
|
||||
final boolean finest = logger.isLoggable(Level.FINEST);
|
||||
localServices.forEach(y -> {
|
||||
Set<Method> methods = new HashSet<>();
|
||||
Class loop = y.getClass();
|
||||
//do { public方法不用递归
|
||||
for (Method m : loop.getMethods()) {
|
||||
Command c = m.getAnnotation(Command.class);
|
||||
if (c == null) continue;
|
||||
if (Modifier.isStatic(m.getModifiers())) continue;
|
||||
if (m.getReturnType() != void.class) continue;
|
||||
if (m.getParameterCount() != 1) continue;
|
||||
if (m.getParameterTypes()[0] != String.class) continue;
|
||||
methods.add(m);
|
||||
}
|
||||
//} while ((loop = loop.getSuperclass()) != Object.class);
|
||||
if (methods.isEmpty()) return;
|
||||
long s = System.currentTimeMillis();
|
||||
Method one = null;
|
||||
try {
|
||||
for (Method method : methods) {
|
||||
one = method;
|
||||
method.invoke(y, cmd);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
logger.log(Level.SEVERE, one + " run error, cmd = " + cmd, ex);
|
||||
}
|
||||
long e = System.currentTimeMillis() - s;
|
||||
if (e > 10 && sb != null) {
|
||||
sb.append(Sncp.toSimpleString(y, maxNameLength, maxClassNameLength)).append(" command (").append(cmd).append(") ").append(e).append("ms").append(LINE_SEPARATOR);
|
||||
}
|
||||
});
|
||||
if (sb != null && sb.length() > 0) logger.log(Level.INFO, sb.toString());
|
||||
}
|
||||
|
||||
public <T extends Server> T getServer() {
|
||||
return (T) server;
|
||||
}
|
||||
@@ -696,4 +850,7 @@ public abstract class NodeServer {
|
||||
return new LinkedHashSet<>(remoteServices);
|
||||
}
|
||||
|
||||
public String getThreadName() {
|
||||
return this.threadName;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,9 +10,10 @@ import java.net.*;
|
||||
import java.util.*;
|
||||
import java.util.logging.Level;
|
||||
import org.redkale.boot.ClassFilter.FilterEntry;
|
||||
import org.redkale.mq.MessageAgent;
|
||||
import org.redkale.net.*;
|
||||
import org.redkale.net.sncp.*;
|
||||
import org.redkale.service.Service;
|
||||
import org.redkale.service.*;
|
||||
import org.redkale.util.*;
|
||||
import org.redkale.util.AnyValue.DefaultAnyValue;
|
||||
|
||||
@@ -24,7 +25,7 @@ import org.redkale.util.AnyValue.DefaultAnyValue;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
@NodeProtocol({"SNCP"})
|
||||
@NodeProtocol("SNCP")
|
||||
public class NodeSncpServer extends NodeServer {
|
||||
|
||||
protected final SncpServer sncpServer;
|
||||
@@ -32,19 +33,20 @@ public class NodeSncpServer extends NodeServer {
|
||||
private NodeSncpServer(Application application, AnyValue serconf) {
|
||||
super(application, createServer(application, serconf));
|
||||
this.sncpServer = (SncpServer) this.server;
|
||||
this.consumer = sncpServer == null || application.singletonrun ? null : x -> sncpServer.addSncpServlet(x); //singleton模式下不生成SncpServlet
|
||||
this.consumer = sncpServer == null || application.singletonrun ? null : (agent, x) -> {//singleton模式下不生成SncpServlet
|
||||
if (x.getClass().getAnnotation(Local.class) != null) return;
|
||||
SncpDynServlet servlet = sncpServer.addSncpServlet(x);
|
||||
dynServletMap.put(x, servlet);
|
||||
if (agent != null) agent.putService(this, x, servlet);
|
||||
};
|
||||
}
|
||||
|
||||
public static NodeServer createNodeServer(Application application, AnyValue serconf) {
|
||||
if (serconf != null && serconf.getAnyValue("rest") != null) {
|
||||
((AnyValue.DefaultAnyValue) serconf).addValue("_$sncp", "true");
|
||||
return new NodeHttpServer(application, serconf);
|
||||
}
|
||||
return new NodeSncpServer(application, serconf);
|
||||
}
|
||||
|
||||
private static Server createServer(Application application, AnyValue serconf) {
|
||||
return new SncpServer(application.getStartTime(), application.getResourceFactory().createChild());
|
||||
return new SncpServer(application, application.getStartTime(), serconf, application.getResourceFactory().createChild());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -52,8 +54,8 @@ public class NodeSncpServer extends NodeServer {
|
||||
return sncpServer == null ? null : sncpServer.getSocketAddress();
|
||||
}
|
||||
|
||||
public void consumerAccept(Service service) {
|
||||
if (this.consumer != null) this.consumer.accept(service);
|
||||
public void consumerAccept(MessageAgent messageAgent, Service service) {
|
||||
if (this.consumer != null) this.consumer.accept(messageAgent, service);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -62,11 +64,11 @@ public class NodeSncpServer extends NodeServer {
|
||||
//-------------------------------------------------------------------
|
||||
if (sncpServer == null) return; //调试时server才可能为null
|
||||
final StringBuilder sb = logger.isLoggable(Level.FINE) ? new StringBuilder() : null;
|
||||
final String threadName = "[" + Thread.currentThread().getName() + "] ";
|
||||
final String localThreadName = "[" + Thread.currentThread().getName() + "] ";
|
||||
List<SncpServlet> servlets = sncpServer.getSncpServlets();
|
||||
Collections.sort(servlets);
|
||||
for (SncpServlet en : servlets) {
|
||||
if (sb != null) sb.append(threadName).append(" Load ").append(en).append(LINE_SEPARATOR);
|
||||
if (sb != null) sb.append(localThreadName).append(" Load ").append(en).append(LINE_SEPARATOR);
|
||||
}
|
||||
if (sb != null && sb.length() > 0) logger.log(Level.FINE, sb.toString());
|
||||
}
|
||||
@@ -88,7 +90,7 @@ public class NodeSncpServer extends NodeServer {
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void loadSncpFilter(final AnyValue servletsConf, final ClassFilter<? extends Filter> classFilter) throws Exception {
|
||||
final StringBuilder sb = logger.isLoggable(Level.INFO) ? new StringBuilder() : null;
|
||||
final String threadName = "[" + Thread.currentThread().getName() + "] ";
|
||||
final String localThreadName = "[" + Thread.currentThread().getName() + "] ";
|
||||
List<FilterEntry<? extends Filter>> list = new ArrayList(classFilter.getFilterEntrys());
|
||||
for (FilterEntry<? extends Filter> en : list) {
|
||||
Class<SncpFilter> clazz = (Class<SncpFilter>) en.getType();
|
||||
@@ -97,7 +99,7 @@ public class NodeSncpServer extends NodeServer {
|
||||
resourceFactory.inject(filter, this);
|
||||
DefaultAnyValue filterConf = (DefaultAnyValue) en.getProperty();
|
||||
this.sncpServer.addSncpFilter(filter, filterConf);
|
||||
if (sb != null) sb.append(threadName).append(" Load ").append(clazz.getName()).append(LINE_SEPARATOR);
|
||||
if (sb != null) sb.append(localThreadName).append(" Load ").append(clazz.getName()).append(LINE_SEPARATOR);
|
||||
}
|
||||
if (sb != null && sb.length() > 0) logger.log(Level.INFO, sb.toString());
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ import org.redkale.watch.*;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
@NodeProtocol({"WATCH"})
|
||||
@NodeProtocol("WATCH")
|
||||
public class NodeWatchServer extends NodeHttpServer {
|
||||
|
||||
public NodeWatchServer(Application application, AnyValue serconf) {
|
||||
|
||||
@@ -35,7 +35,7 @@ public class FilterWatchService extends AbstractWatchService {
|
||||
@Resource
|
||||
protected Application application;
|
||||
|
||||
@RestMapping(name = "addfilter", auth = false, comment = "动态增加Filter")
|
||||
@RestMapping(name = "addFilter", auth = false, comment = "动态增加Filter")
|
||||
public RetResult addFilter(@RestUploadFile(maxLength = 10 * 1024 * 1024, fileNameReg = "\\.jar$") byte[] jar,
|
||||
@RestParam(name = "server", comment = "Server节点名") final String serverName,
|
||||
@RestParam(name = "type", comment = "Filter类名") final String filterType) throws IOException {
|
||||
|
||||
@@ -50,7 +50,7 @@ public class ServerWatchService extends AbstractWatchService {
|
||||
return new RetResult(rs);
|
||||
}
|
||||
|
||||
@RestMapping(name = "changeaddress", comment = "更改Server的监听地址和端口")
|
||||
@RestMapping(name = "changeAddress", comment = "更改Server的监听地址和端口")
|
||||
public RetResult changeAddress(@RestParam(name = "#port:") final int oldport,
|
||||
@RestParam(name = "#newhost:") final String newhost, @RestParam(name = "#newport:") final int newport) {
|
||||
if (oldport < 1) return new RetResult(RET_WATCH_PARAMS_ILLEGAL, "not found param `oldport`");
|
||||
@@ -61,7 +61,7 @@ public class ServerWatchService extends AbstractWatchService {
|
||||
final Server server = node.getServer();
|
||||
InetSocketAddress newAddr = new InetSocketAddress(newhost == null || newhost.isEmpty() ? server.getSocketAddress().getHostString() : newhost, newport);
|
||||
try {
|
||||
server.changeAddress(newAddr);
|
||||
server.changeAddress(application, newAddr);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return new RetResult(RET_SERVER_CHANGEPORT_ERROR, "changeaddress error");
|
||||
@@ -72,7 +72,7 @@ public class ServerWatchService extends AbstractWatchService {
|
||||
private Map<String, Object> formatToMap(NodeServer node) {
|
||||
Server server = node.getServer();
|
||||
Map<String, Object> rs = new LinkedHashMap<>();
|
||||
String protocol = server.getProtocol();
|
||||
String protocol = server.getNetprotocol();
|
||||
if (node instanceof NodeSncpServer) {
|
||||
protocol += "/SNCP";
|
||||
} else if (node instanceof NodeWatchServer) {
|
||||
@@ -81,12 +81,11 @@ public class ServerWatchService extends AbstractWatchService {
|
||||
protocol += "/HTTP";
|
||||
} else {
|
||||
NodeProtocol np = node.getClass().getAnnotation(NodeProtocol.class);
|
||||
if (np != null && np.value().length > 0) protocol += "/" + np.value()[0];
|
||||
protocol += "/" + np.value();
|
||||
}
|
||||
rs.put("name", server.getName());
|
||||
rs.put("protocol", protocol);
|
||||
rs.put("address", server.getSocketAddress());
|
||||
rs.put("threads", server.getThreads());
|
||||
rs.put("backlog", server.getBacklog());
|
||||
rs.put("bufferCapacity", server.getBufferCapacity());
|
||||
rs.put("bufferPoolSize", server.getBufferPoolSize());
|
||||
|
||||
@@ -30,8 +30,8 @@ public class ServiceWatchService extends AbstractWatchService {
|
||||
protected Application application;
|
||||
|
||||
@RestConvert(type = void.class)
|
||||
@RestMapping(name = "setfield", auth = false, comment = "设置Service中指定字段的内容")
|
||||
public RetResult setfield(@RestParam(name = "name", comment = "Service的资源名") String name,
|
||||
@RestMapping(name = "setField", auth = false, comment = "设置Service中指定字段的内容")
|
||||
public RetResult setField(@RestParam(name = "name", comment = "Service的资源名") String name,
|
||||
@RestParam(name = "type", comment = "Service的类名") String type,
|
||||
@RestParam(name = "field", comment = "字段名") String field,
|
||||
@RestParam(name = "value", comment = "字段值") String value) {
|
||||
@@ -65,8 +65,8 @@ public class ServiceWatchService extends AbstractWatchService {
|
||||
}
|
||||
|
||||
@RestConvert(type = void.class)
|
||||
@RestMapping(name = "getfield", auth = false, comment = "查询Service中指定字段的内容")
|
||||
public RetResult getfield(@RestParam(name = "name", comment = "Service的资源名") String name,
|
||||
@RestMapping(name = "getField", auth = false, comment = "查询Service中指定字段的内容")
|
||||
public RetResult getField(@RestParam(name = "name", comment = "Service的资源名") String name,
|
||||
@RestParam(name = "type", comment = "Service的类名") String type,
|
||||
@RestParam(name = "field", comment = "字段名") String field) {
|
||||
if (name == null) name = "";
|
||||
@@ -98,8 +98,8 @@ public class ServiceWatchService extends AbstractWatchService {
|
||||
}
|
||||
|
||||
@RestConvert(type = void.class)
|
||||
@RestMapping(name = "runmethod", auth = false, comment = "调用Service中指定方法")
|
||||
public RetResult runmethod(@RestParam(name = "name", comment = "Service的资源名") String name,
|
||||
@RestMapping(name = "runMethod", auth = false, comment = "调用Service中指定方法")
|
||||
public RetResult runMethod(@RestParam(name = "name", comment = "Service的资源名") String name,
|
||||
@RestParam(name = "type", comment = "Service的类名") String type,
|
||||
@RestParam(name = "method", comment = "Service的方法名") String method,
|
||||
@RestParam(name = "params", comment = "方法的参数值") List<String> params,
|
||||
@@ -169,28 +169,28 @@ public class ServiceWatchService extends AbstractWatchService {
|
||||
return dest;
|
||||
}
|
||||
|
||||
@RestMapping(name = "load", auth = false, comment = "动态增加Service")
|
||||
@RestMapping(name = "loadService", auth = false, comment = "动态增加Service")
|
||||
public RetResult loadService(@RestParam(name = "type", comment = "Service的类名") String type,
|
||||
@RestUploadFile(maxLength = 10 * 1024 * 1024, fileNameReg = "\\.jar$") byte[] jar) {
|
||||
//待开发
|
||||
return RetResult.success();
|
||||
}
|
||||
|
||||
@RestMapping(name = "reload", auth = false, comment = "重新加载Service")
|
||||
@RestMapping(name = "reloadService", auth = false, comment = "重新加载Service")
|
||||
public RetResult reloadService(@RestParam(name = "name", comment = "Service的资源名") String name,
|
||||
@RestParam(name = "type", comment = "Service的类名") String type) {
|
||||
//待开发
|
||||
return RetResult.success();
|
||||
}
|
||||
|
||||
@RestMapping(name = "stop", auth = false, comment = "动态停止Service")
|
||||
@RestMapping(name = "stopService", auth = false, comment = "动态停止Service")
|
||||
public RetResult stopService(@RestParam(name = "name", comment = "Service的资源名") String name,
|
||||
@RestParam(name = "type", comment = "Service的类名") String type) {
|
||||
//待开发
|
||||
return RetResult.success();
|
||||
}
|
||||
|
||||
@RestMapping(name = "find", auth = false, comment = "查找Service")
|
||||
@RestMapping(name = "findService", auth = false, comment = "查找Service")
|
||||
public RetResult find(@RestParam(name = "name", comment = "Service的资源名") String name,
|
||||
@RestParam(name = "type", comment = "Service的类名") String type) {
|
||||
//待开发
|
||||
|
||||
@@ -25,13 +25,13 @@ public class ServletWatchService extends AbstractWatchService {
|
||||
@Resource
|
||||
protected TransportFactory transportFactory;
|
||||
//
|
||||
// @RestMapping(name = "load", auth = false, comment = "动态增加Servlet")
|
||||
// @RestMapping(name = "loadServlet", auth = false, comment = "动态增加Servlet")
|
||||
// public RetResult loadServlet(String type, @RestUploadFile(maxLength = 10 * 1024 * 1024, fileNameReg = "\\.jar$") byte[] jar) {
|
||||
// //待开发
|
||||
// return RetResult.success();
|
||||
// }
|
||||
//
|
||||
// @RestMapping(name = "stop", auth = false, comment = "动态停止Servlet")
|
||||
// @RestMapping(name = "stopServlet", auth = false, comment = "动态停止Servlet")
|
||||
// public RetResult stopServlet(String type) {
|
||||
// //待开发
|
||||
// return RetResult.success();
|
||||
|
||||
@@ -72,15 +72,6 @@ public class TransportWatchService extends AbstractWatchService {
|
||||
if (client.getRemoteGroups() != null && client.getRemoteGroups().contains(group)) {
|
||||
client.getRemoteGroupTransport().addRemoteAddresses(address);
|
||||
}
|
||||
} else {
|
||||
if (group.equals(client.getSameGroup())) {
|
||||
client.getSameGroupTransport().addRemoteAddresses(address);
|
||||
}
|
||||
if (client.getDiffGroups() != null && client.getDiffGroups().contains(group)) {
|
||||
for (Transport transport : client.getDiffGroupTransports()) {
|
||||
transport.addRemoteAddresses(address);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
DefaultAnyValue node = DefaultAnyValue.create("addr", addr).addValue("port", port);
|
||||
@@ -90,7 +81,7 @@ public class TransportWatchService extends AbstractWatchService {
|
||||
break;
|
||||
}
|
||||
}
|
||||
application.restoreConfig();
|
||||
//application.restoreConfig();
|
||||
}
|
||||
return RetResult.success();
|
||||
}
|
||||
@@ -114,15 +105,6 @@ public class TransportWatchService extends AbstractWatchService {
|
||||
if (client.getRemoteGroups() != null && client.getRemoteGroups().contains(group)) {
|
||||
client.getRemoteGroupTransport().removeRemoteAddresses(address);
|
||||
}
|
||||
} else {
|
||||
if (group.equals(client.getSameGroup())) {
|
||||
client.getSameGroupTransport().removeRemoteAddresses(address);
|
||||
}
|
||||
if (client.getDiffGroups() != null && client.getDiffGroups().contains(group)) {
|
||||
for (Transport transport : client.getDiffGroupTransports()) {
|
||||
transport.removeRemoteAddresses(address);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (AnyValue groupconf : application.getAppConfig().getAnyValue("resources").getAnyValues("group")) {
|
||||
@@ -131,7 +113,7 @@ public class TransportWatchService extends AbstractWatchService {
|
||||
break;
|
||||
}
|
||||
}
|
||||
application.restoreConfig();
|
||||
//application.restoreConfig();
|
||||
}
|
||||
return RetResult.success();
|
||||
}
|
||||
|
||||
327
src/org/redkale/cluster/CacheClusterAgent.java
Normal file
327
src/org/redkale/cluster/CacheClusterAgent.java
Normal file
@@ -0,0 +1,327 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.cluster;
|
||||
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.logging.Level;
|
||||
import javax.annotation.Resource;
|
||||
import org.redkale.boot.*;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.service.Service;
|
||||
import org.redkale.source.CacheSource;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
* 使用CacheSource实现的第三方服务发现管理接口cluster
|
||||
*
|
||||
*
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public class CacheClusterAgent extends ClusterAgent implements Resourcable {
|
||||
|
||||
@Resource(name = "$")
|
||||
private CacheSource source;
|
||||
|
||||
private String sourceName;
|
||||
|
||||
protected int ttls = 10; //定时检查的秒数
|
||||
|
||||
protected ScheduledThreadPoolExecutor scheduler;
|
||||
|
||||
//可能被HttpMessageClient用到的服务 key: servicename
|
||||
protected final ConcurrentHashMap<String, Collection<InetSocketAddress>> httpAddressMap = new ConcurrentHashMap<>();
|
||||
|
||||
//可能被mqtp用到的服务 key: servicename
|
||||
protected final ConcurrentHashMap<String, Collection<InetSocketAddress>> mqtpAddressMap = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public void init(AnyValue config) {
|
||||
super.init(config);
|
||||
this.sourceName = getSourceName();
|
||||
|
||||
AnyValue[] properties = config.getAnyValues("property");
|
||||
for (AnyValue property : properties) {
|
||||
if ("ttls".equalsIgnoreCase(property.getValue("name"))) {
|
||||
this.ttls = Integer.parseInt(property.getValue("value", "").trim());
|
||||
if (this.ttls < 5) this.ttls = 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy(AnyValue config) {
|
||||
if (scheduler != null) scheduler.shutdownNow();
|
||||
}
|
||||
|
||||
public String getSourceName() {
|
||||
AnyValue[] properties = config.getAnyValues("property");
|
||||
for (AnyValue property : properties) {
|
||||
if ("source".equalsIgnoreCase(property.getValue("name"))
|
||||
&& property.getValue("value") != null) {
|
||||
this.sourceName = property.getValue("value");
|
||||
return this.sourceName;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String resourceName() {
|
||||
return sourceName;
|
||||
}
|
||||
|
||||
@Override //ServiceLoader时判断配置是否符合当前实现类
|
||||
public boolean match(AnyValue config) {
|
||||
if (config == null) return false;
|
||||
AnyValue[] properties = config.getAnyValues("property");
|
||||
if (properties == null || properties.length == 0) return false;
|
||||
for (AnyValue property : properties) {
|
||||
if ("source".equalsIgnoreCase(property.getValue("name"))
|
||||
&& property.getValue("value") != null) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
if (this.scheduler == null) {
|
||||
this.scheduler = new ScheduledThreadPoolExecutor(4, (Runnable r) -> {
|
||||
final Thread t = new Thread(r, "Redkale-" + CacheClusterAgent.class.getSimpleName() + "-Task-Thread");
|
||||
t.setDaemon(true);
|
||||
return t;
|
||||
});
|
||||
|
||||
this.scheduler.scheduleAtFixedRate(() -> {
|
||||
try {
|
||||
checkApplicationHealth();
|
||||
checkHttpAddressHealth();
|
||||
loadMqtpAddressHealth();
|
||||
localEntrys.values().stream().filter(e -> !e.canceled).forEach(entry -> {
|
||||
checkLocalHealth(entry);
|
||||
});
|
||||
remoteEntrys.values().stream().filter(entry -> "SNCP".equalsIgnoreCase(entry.protocol)).forEach(entry -> {
|
||||
updateSncpTransport(entry);
|
||||
});
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "scheduleAtFixedRate check error", e);
|
||||
}
|
||||
}, Math.max(2000, ttls * 1000), Math.max(2000, ttls * 1000), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
protected void loadMqtpAddressHealth() {
|
||||
List<String> keys = source.queryKeysStartsWith("cluster.mqtp:");
|
||||
keys.forEach(servicename -> {
|
||||
try {
|
||||
this.mqtpAddressMap.put(servicename, queryAddress(servicename).get(3, TimeUnit.SECONDS));
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "loadMqtpAddressHealth check " + servicename + " error", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected void checkHttpAddressHealth() {
|
||||
try {
|
||||
this.httpAddressMap.keySet().stream().forEach(servicename -> {
|
||||
try {
|
||||
this.httpAddressMap.put(servicename, queryAddress(servicename).get(3, TimeUnit.SECONDS));
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "checkHttpAddressHealth check " + servicename + " error", e);
|
||||
}
|
||||
});
|
||||
} catch (Exception ex) {
|
||||
logger.log(Level.SEVERE, "checkHttpAddressHealth check error", ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected void checkLocalHealth(final ClusterEntry entry) {
|
||||
AddressEntry newaddr = new AddressEntry();
|
||||
newaddr.addr = entry.address;
|
||||
newaddr.nodeid = this.nodeid;
|
||||
newaddr.time = System.currentTimeMillis();
|
||||
source.hset(entry.checkname, entry.checkid, AddressEntry.class, newaddr);
|
||||
}
|
||||
|
||||
@Override //获取MQTP的HTTP远程服务的可用ip列表, key = servicename的后半段
|
||||
public CompletableFuture<Map<String, Collection<InetSocketAddress>>> queryMqtpAddress(String protocol, String module, String resname) {
|
||||
final Map<String, Collection<InetSocketAddress>> rsmap = new ConcurrentHashMap<>();
|
||||
final String servicenamprefix = generateHttpServiceName(protocol, module, null) + ":";
|
||||
mqtpAddressMap.keySet().stream().filter(k -> k.startsWith(servicenamprefix))
|
||||
.forEach(sn -> rsmap.put(sn.substring(servicenamprefix.length()), mqtpAddressMap.get(sn)));
|
||||
return CompletableFuture.completedFuture(rsmap);
|
||||
}
|
||||
|
||||
@Override //获取HTTP远程服务的可用ip列表
|
||||
public CompletableFuture<Collection<InetSocketAddress>> queryHttpAddress(String protocol, String module, String resname) {
|
||||
final String servicename = generateHttpServiceName(protocol, module, resname);
|
||||
Collection<InetSocketAddress> rs = httpAddressMap.get(servicename);
|
||||
if (rs != null) return CompletableFuture.completedFuture(rs);
|
||||
return queryAddress(servicename).thenApply(t -> {
|
||||
httpAddressMap.put(servicename, t);
|
||||
return t;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CompletableFuture<Collection<InetSocketAddress>> queryAddress(final ClusterEntry entry) {
|
||||
return queryAddress(entry.servicename);
|
||||
}
|
||||
|
||||
private CompletableFuture<Collection<InetSocketAddress>> queryAddress(final String servicename) {
|
||||
final CompletableFuture<Map<String, AddressEntry>> future = source.hmapAsync(servicename, AddressEntry.class, 0, 10000);
|
||||
return future.thenApply(map -> {
|
||||
final Set<InetSocketAddress> set = new HashSet<>();
|
||||
map.forEach((n, v) -> {
|
||||
if (v != null && (System.currentTimeMillis() - v.time) / 1000 < ttls) set.add(v.addr);
|
||||
});
|
||||
return set;
|
||||
});
|
||||
}
|
||||
|
||||
protected boolean isApplicationHealth() {
|
||||
String servicename = generateApplicationServiceName();
|
||||
String serviceid = generateApplicationServiceId();
|
||||
AddressEntry entry = (AddressEntry) source.hget(servicename, serviceid, AddressEntry.class);
|
||||
return entry != null && (System.currentTimeMillis() - entry.time) / 1000 < ttls;
|
||||
}
|
||||
|
||||
protected void checkApplicationHealth() {
|
||||
String checkname = generateApplicationServiceName();
|
||||
String checkid = generateApplicationCheckId();
|
||||
AddressEntry entry = new AddressEntry();
|
||||
entry.addr = this.appAddress;
|
||||
entry.nodeid = this.nodeid;
|
||||
entry.time = System.currentTimeMillis();
|
||||
source.hset(checkname, checkid, AddressEntry.class, entry);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(Application application) {
|
||||
if (isApplicationHealth()) throw new RuntimeException("application.nodeid=" + nodeid + " exists in cluster");
|
||||
deregister(application);
|
||||
|
||||
String serviceid = generateApplicationServiceId();
|
||||
String servicename = generateApplicationServiceName();
|
||||
AddressEntry entry = new AddressEntry();
|
||||
entry.addr = this.appAddress;
|
||||
entry.nodeid = this.nodeid;
|
||||
entry.time = System.currentTimeMillis();
|
||||
source.hset(servicename, serviceid, AddressEntry.class, entry);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deregister(Application application) {
|
||||
String servicename = generateApplicationServiceName();
|
||||
source.remove(servicename);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ClusterEntry register(NodeServer ns, String protocol, Service service) {
|
||||
deregister(ns, protocol, service, false);
|
||||
//
|
||||
ClusterEntry clusterEntry = new ClusterEntry(ns, protocol, service);
|
||||
AddressEntry entry = new AddressEntry();
|
||||
entry.addr = clusterEntry.address;
|
||||
entry.nodeid = this.nodeid;
|
||||
entry.time = System.currentTimeMillis();
|
||||
source.hset(clusterEntry.servicename, clusterEntry.serviceid, AddressEntry.class, entry);
|
||||
return clusterEntry;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void deregister(NodeServer ns, String protocol, Service service) {
|
||||
deregister(ns, protocol, service, true);
|
||||
}
|
||||
|
||||
protected void deregister(NodeServer ns, String protocol, Service service, boolean realcanceled) {
|
||||
String servicename = generateServiceName(ns, protocol, service);
|
||||
String serviceid = generateServiceId(ns, protocol, service);
|
||||
ClusterEntry currEntry = null;
|
||||
for (final ClusterEntry entry : localEntrys.values()) {
|
||||
if (entry.servicename.equals(servicename) && entry.serviceid.equals(serviceid)) {
|
||||
currEntry = entry;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (currEntry == null) {
|
||||
for (final ClusterEntry entry : remoteEntrys.values()) {
|
||||
if (entry.servicename.equals(servicename) && entry.serviceid.equals(serviceid)) {
|
||||
currEntry = entry;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
source.hremove(servicename, serviceid);
|
||||
if (realcanceled && currEntry != null) currEntry.canceled = true;
|
||||
if (!"mqtp".equals(protocol) && currEntry != null && currEntry.submqtp) {
|
||||
deregister(ns, "mqtp", service, realcanceled);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String generateApplicationServiceName() {
|
||||
return "cluster." + super.generateApplicationServiceName();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String generateServiceName(NodeServer ns, String protocol, Service service) {
|
||||
return "cluster." + super.generateServiceName(ns, protocol, service);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateHttpServiceName(String protocol, String module, String resname) {
|
||||
return "cluster." + super.generateHttpServiceName(protocol, module, resname);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String generateApplicationCheckName() {
|
||||
return generateApplicationServiceName();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String generateApplicationCheckId() {
|
||||
return generateApplicationServiceId();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String generateCheckName(NodeServer ns, String protocol, Service service) {
|
||||
return generateServiceName(ns, protocol, service);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String generateCheckId(NodeServer ns, String protocol, Service service) {
|
||||
return generateServiceId(ns, protocol, service);
|
||||
}
|
||||
|
||||
public static class AddressEntry {
|
||||
|
||||
public InetSocketAddress addr;
|
||||
|
||||
public int nodeid;
|
||||
|
||||
public long time;
|
||||
|
||||
public AddressEntry() {
|
||||
}
|
||||
|
||||
public AddressEntry refresh() {
|
||||
this.time = System.currentTimeMillis();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
342
src/org/redkale/cluster/ClusterAgent.java
Normal file
342
src/org/redkale/cluster/ClusterAgent.java
Normal file
@@ -0,0 +1,342 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.cluster;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.logging.Logger;
|
||||
import javax.annotation.Resource;
|
||||
import org.redkale.boot.*;
|
||||
import static org.redkale.boot.Application.*;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.mq.MessageMultiConsumer;
|
||||
import org.redkale.net.*;
|
||||
import org.redkale.net.http.*;
|
||||
import org.redkale.net.sncp.*;
|
||||
import org.redkale.service.*;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
* 第三方服务发现管理接口cluster
|
||||
*
|
||||
*
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*
|
||||
* @since 2.1.0
|
||||
*/
|
||||
public abstract class ClusterAgent {
|
||||
|
||||
protected final Logger logger = Logger.getLogger(this.getClass().getSimpleName());
|
||||
|
||||
@Resource(name = RESNAME_APP_NODEID)
|
||||
protected int nodeid;
|
||||
|
||||
@Resource(name = RESNAME_APP_NAME)
|
||||
protected String appName = "";
|
||||
|
||||
@Resource(name = RESNAME_APP_ADDR)
|
||||
protected InetSocketAddress appAddress;
|
||||
|
||||
protected String name;
|
||||
|
||||
protected boolean waits;
|
||||
|
||||
protected String[] protocols; //必须全大写
|
||||
|
||||
protected int[] ports;
|
||||
|
||||
protected AnyValue config;
|
||||
|
||||
protected TransportFactory transportFactory;
|
||||
|
||||
protected final ConcurrentHashMap<String, ClusterEntry> localEntrys = new ConcurrentHashMap<>();
|
||||
|
||||
protected final ConcurrentHashMap<String, ClusterEntry> remoteEntrys = new ConcurrentHashMap<>();
|
||||
|
||||
public void init(AnyValue config) {
|
||||
this.config = config;
|
||||
this.name = config.getValue("name", "");
|
||||
this.waits = config.getBoolValue("waits", false);
|
||||
{
|
||||
String ps = config.getValue("protocols", "").toUpperCase();
|
||||
if (ps == null || ps.isEmpty()) ps = "SNCP;HTTP";
|
||||
this.protocols = ps.split(";");
|
||||
}
|
||||
String ts = config.getValue("ports", "");
|
||||
if (ts != null && !ts.isEmpty()) {
|
||||
String[] its = ts.split(";");
|
||||
List<Integer> list = new ArrayList<>();
|
||||
for (String str : its) {
|
||||
if (str.trim().isEmpty()) continue;
|
||||
list.add(Integer.parseInt(str.trim()));
|
||||
}
|
||||
if (!list.isEmpty()) this.ports = list.stream().mapToInt(x -> x).toArray();
|
||||
}
|
||||
}
|
||||
|
||||
public void destroy(AnyValue config) {
|
||||
}
|
||||
|
||||
//ServiceLoader时判断配置是否符合当前实现类
|
||||
public abstract boolean match(AnyValue config);
|
||||
|
||||
public boolean containsProtocol(String protocol) {
|
||||
if (protocol == null || protocol.isEmpty()) return false;
|
||||
return protocols == null || Utility.contains(protocols, protocol.toUpperCase());
|
||||
}
|
||||
|
||||
public boolean containsPort(int port) {
|
||||
if (ports == null || ports.length == 0) return true;
|
||||
return Utility.contains(ports, port);
|
||||
}
|
||||
|
||||
public abstract void register(Application application);
|
||||
|
||||
public abstract void deregister(Application application);
|
||||
|
||||
//注册服务, 在NodeService调用Service.init方法之前调用
|
||||
public void register(NodeServer ns, String protocol, Set<Service> localServices, Set<Service> remoteServices) {
|
||||
if (localServices.isEmpty()) return;
|
||||
//注册本地模式
|
||||
for (Service service : localServices) {
|
||||
if (!canRegister(protocol, service)) continue;
|
||||
ClusterEntry htentry = register(ns, protocol, service);
|
||||
localEntrys.put(htentry.serviceid, htentry);
|
||||
if (protocol.toLowerCase().startsWith("http")) {
|
||||
MessageMultiConsumer mmc = service.getClass().getAnnotation(MessageMultiConsumer.class);
|
||||
if (mmc != null) {
|
||||
ClusterEntry mqentry = register(ns, "mqtp", service);
|
||||
localEntrys.put(mqentry.serviceid, mqentry);
|
||||
htentry.submqtp = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
//远程模式加载IP列表, 只支持SNCP协议
|
||||
if (ns.isSNCP()) {
|
||||
for (Service service : remoteServices) {
|
||||
ClusterEntry entry = new ClusterEntry(ns, protocol, service);
|
||||
updateSncpTransport(entry);
|
||||
remoteEntrys.put(entry.serviceid, entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//注销服务, 在NodeService调用Service.destroy 方法之前调用
|
||||
public void deregister(NodeServer ns, String protocol, Set<Service> localServices, Set<Service> remoteServices) {
|
||||
//注销本地模式 远程模式不注册
|
||||
for (Service service : localServices) {
|
||||
if (!canRegister(protocol, service)) continue;
|
||||
deregister(ns, protocol, service);
|
||||
}
|
||||
afterDeregister(ns, protocol);
|
||||
}
|
||||
|
||||
protected boolean canRegister(String protocol, Service service) {
|
||||
if ("SNCP".equalsIgnoreCase(protocol) && service.getClass().getAnnotation(Local.class) != null) return false;
|
||||
AutoLoad al = service.getClass().getAnnotation(AutoLoad.class);
|
||||
if (al != null && !al.value() && service.getClass().getAnnotation(Local.class) != null) return false;
|
||||
if (service instanceof WebSocketNode) {
|
||||
if (((WebSocketNode) service).getLocalWebSocketEngine() == null) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
}
|
||||
|
||||
protected void afterDeregister(NodeServer ns, String protocol) {
|
||||
if (!this.waits) return;
|
||||
int s = intervalCheckSeconds();
|
||||
if (s > 0) { //暂停,弥补其他依赖本进程服务的周期偏差
|
||||
try {
|
||||
Thread.sleep(s * 1000);
|
||||
} catch (InterruptedException ex) {
|
||||
}
|
||||
logger.info(this.getClass().getSimpleName() + " wait for " + s * 1000 + "ms after deregister");
|
||||
}
|
||||
}
|
||||
|
||||
public int intervalCheckSeconds() {
|
||||
return 10;
|
||||
}
|
||||
|
||||
//获取MQTP的HTTP远程服务的可用ip列表, key = servicename的后半段
|
||||
public abstract CompletableFuture<Map<String, Collection<InetSocketAddress>>> queryMqtpAddress(String protocol, String module, String resname);
|
||||
|
||||
//获取HTTP远程服务的可用ip列表
|
||||
public abstract CompletableFuture<Collection<InetSocketAddress>> queryHttpAddress(String protocol, String module, String resname);
|
||||
|
||||
//获取远程服务的可用ip列表
|
||||
protected abstract CompletableFuture<Collection<InetSocketAddress>> queryAddress(ClusterEntry entry);
|
||||
|
||||
//注册服务
|
||||
protected abstract ClusterEntry register(NodeServer ns, String protocol, Service service);
|
||||
|
||||
//注销服务
|
||||
protected abstract void deregister(NodeServer ns, String protocol, Service service);
|
||||
|
||||
//格式: protocol:classtype-resourcename
|
||||
protected void updateSncpTransport(ClusterEntry entry) {
|
||||
Service service = entry.serviceref.get();
|
||||
if (service == null) return;
|
||||
Collection<InetSocketAddress> addrs = ClusterAgent.this.queryAddress(entry).join();
|
||||
Sncp.updateTransport(service, transportFactory, Sncp.getResourceType(service).getName() + "-" + Sncp.getResourceName(service), entry.netprotocol, entry.address, null, addrs);
|
||||
}
|
||||
|
||||
protected String generateApplicationServiceName() {
|
||||
return "application" + (appName == null || appName.isEmpty() ? "" : ("." + appName)) + ".node" + this.nodeid;
|
||||
}
|
||||
|
||||
protected String generateApplicationServiceId() { //与servicename相同
|
||||
return generateApplicationServiceName();
|
||||
}
|
||||
|
||||
protected String generateApplicationCheckName() {
|
||||
return "check-" + generateApplicationServiceName();
|
||||
}
|
||||
|
||||
protected String generateApplicationCheckId() {
|
||||
return "check-" + generateApplicationServiceId();
|
||||
}
|
||||
|
||||
//也会提供给HttpMessageClusterAgent适用
|
||||
public String generateHttpServiceName(String protocol, String module, String resname) {
|
||||
return protocol.toLowerCase() + ":" + module + (resname == null || resname.isEmpty() ? "" : ("-" + resname));
|
||||
}
|
||||
|
||||
//格式: protocol:classtype-resourcename
|
||||
protected String generateServiceName(NodeServer ns, String protocol, Service service) {
|
||||
if (protocol.toLowerCase().startsWith("http")) { //HTTP使用RestService.name方式是为了与MessageClient中的module保持一致, 因为HTTP依靠的url中的module,无法知道Service类名
|
||||
String resname = Sncp.getResourceName(service);
|
||||
String module = Rest.getRestModule(service).toLowerCase();
|
||||
return protocol.toLowerCase() + ":" + module + (resname.isEmpty() ? "" : ("-" + resname));
|
||||
}
|
||||
if ("mqtp".equalsIgnoreCase(protocol)) {
|
||||
MessageMultiConsumer mmc = service.getClass().getAnnotation(MessageMultiConsumer.class);
|
||||
String selfmodule = Rest.getRestModule(service).toLowerCase();
|
||||
return protocol.toLowerCase() + ":" + mmc.module() + ":" + selfmodule;
|
||||
}
|
||||
if (!Sncp.isSncpDyn(service)) return protocol.toLowerCase() + ":" + service.getClass().getName();
|
||||
String resname = Sncp.getResourceName(service);
|
||||
return protocol.toLowerCase() + ":" + Sncp.getResourceType(service).getName() + (resname.isEmpty() ? "" : ("-" + resname));
|
||||
}
|
||||
|
||||
//格式: protocol:classtype-resourcename:nodeid
|
||||
protected String generateServiceId(NodeServer ns, String protocol, Service service) {
|
||||
return generateServiceName(ns, protocol, service) + ":" + this.nodeid;
|
||||
}
|
||||
|
||||
protected String generateCheckName(NodeServer ns, String protocol, Service service) {
|
||||
return "check-" + generateServiceName(ns, protocol, service);
|
||||
}
|
||||
|
||||
protected String generateCheckId(NodeServer ns, String protocol, Service service) {
|
||||
return "check-" + generateServiceId(ns, protocol, service);
|
||||
}
|
||||
|
||||
protected ConcurrentHashMap<String, ClusterEntry> getLocalEntrys() {
|
||||
return localEntrys;
|
||||
}
|
||||
|
||||
protected ConcurrentHashMap<String, ClusterEntry> getRemoteEntrys() {
|
||||
return remoteEntrys;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
}
|
||||
|
||||
public TransportFactory getTransportFactory() {
|
||||
return transportFactory;
|
||||
}
|
||||
|
||||
public void setTransportFactory(TransportFactory transportFactory) {
|
||||
this.transportFactory = transportFactory;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String[] getProtocols() {
|
||||
return protocols;
|
||||
}
|
||||
|
||||
public void setProtocols(String[] protocols) {
|
||||
this.protocols = protocols;
|
||||
}
|
||||
|
||||
public int[] getPorts() {
|
||||
return ports;
|
||||
}
|
||||
|
||||
public void setPorts(int[] ports) {
|
||||
this.ports = ports;
|
||||
}
|
||||
|
||||
public AnyValue getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public void setConfig(AnyValue config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public class ClusterEntry {
|
||||
|
||||
public String serviceid;
|
||||
|
||||
public String servicename;
|
||||
|
||||
public String checkid;
|
||||
|
||||
public String checkname;
|
||||
|
||||
public String protocol;
|
||||
|
||||
public String netprotocol;
|
||||
|
||||
public WeakReference<Service> serviceref;
|
||||
|
||||
public InetSocketAddress address;
|
||||
|
||||
public boolean canceled;
|
||||
|
||||
public boolean submqtp;
|
||||
|
||||
public ClusterEntry(NodeServer ns, String protocol, Service service) {
|
||||
this.serviceid = generateServiceId(ns, protocol, service);
|
||||
this.servicename = generateServiceName(ns, protocol, service);
|
||||
this.checkid = generateCheckId(ns, protocol, service);
|
||||
this.checkname = generateCheckName(ns, protocol, service);
|
||||
this.protocol = protocol;
|
||||
InetSocketAddress addr = ns.getSocketAddress();
|
||||
String host = addr.getHostString();
|
||||
if ("0.0.0.0".equals(host)) {
|
||||
host = appAddress.getHostString();
|
||||
addr = new InetSocketAddress(host, addr.getPort());
|
||||
}
|
||||
this.address = addr;
|
||||
this.serviceref = new WeakReference(service);
|
||||
Server server = ns.getServer();
|
||||
this.netprotocol = server instanceof SncpServer ? ((SncpServer) server).getNetprotocol() : Transport.DEFAULT_NETPROTOCOL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
25
src/org/redkale/cluster/ClusterAgentLoader.java
Normal file
25
src/org/redkale/cluster/ClusterAgentLoader.java
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.cluster;
|
||||
|
||||
import org.redkale.util.AnyValue;
|
||||
|
||||
/**
|
||||
* 自定义的ClusterAgent加载器
|
||||
*
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @since 2.4.0
|
||||
*/
|
||||
public interface ClusterAgentLoader {
|
||||
|
||||
public boolean match(AnyValue config);
|
||||
|
||||
public Class<? extends ClusterAgent> agentClass();
|
||||
}
|
||||
@@ -31,8 +31,14 @@ public final class AnyEncoder<T> implements Encodeable<Writer, T> {
|
||||
out.writeClassName(null);
|
||||
out.writeNull();
|
||||
} else {
|
||||
if (out.needWriteClassName()) out.writeClassName(factory.getEntityAlias(value.getClass()));
|
||||
factory.loadEncoder(value.getClass()).convertTo(out, value);
|
||||
Class clazz = value.getClass();
|
||||
if (clazz == Object.class) {
|
||||
out.writeObjectB(value);
|
||||
out.writeObjectE(value);
|
||||
return;
|
||||
}
|
||||
if (out.needWriteClassName()) out.writeClassName(factory.getEntityAlias(clazz));
|
||||
factory.loadEncoder(clazz).convertTo(out, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -119,6 +119,7 @@ public class ArrayDecoder<T> implements Decodeable<Reader, T[]> {
|
||||
}
|
||||
|
||||
protected T readMemberValue(Reader in, DeMember member, Decodeable<Reader, T> decoder, boolean first) {
|
||||
if (in == null) return null;
|
||||
return decoder.convertFrom(in);
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,8 @@ public class ArrayEncoder<T> implements Encodeable<Writer, T[]> {
|
||||
|
||||
protected final Encodeable<Writer, Object> componentEncoder;
|
||||
|
||||
protected final boolean subtypefinal;
|
||||
|
||||
protected volatile boolean inited = false;
|
||||
|
||||
protected final Object lock = new Object();
|
||||
@@ -47,6 +49,7 @@ public class ArrayEncoder<T> implements Encodeable<Writer, T[]> {
|
||||
factory.register(type, this);
|
||||
this.componentEncoder = factory.loadEncoder(this.componentType);
|
||||
this.anyEncoder = factory.getAnyEncoder();
|
||||
this.subtypefinal = (this.componentType instanceof Class) && Modifier.isFinal(((Class) this.componentType).getModifiers());
|
||||
} finally {
|
||||
inited = true;
|
||||
synchronized (lock) {
|
||||
@@ -65,8 +68,9 @@ public class ArrayEncoder<T> implements Encodeable<Writer, T[]> {
|
||||
out.writeNull();
|
||||
return;
|
||||
}
|
||||
if (value.length == 0) {
|
||||
out.writeArrayB(0, componentEncoder, value);
|
||||
int iMax = value.length - 1;
|
||||
if (iMax == -1) {
|
||||
out.writeArrayB(0, this, componentEncoder, value);
|
||||
out.writeArrayE();
|
||||
return;
|
||||
}
|
||||
@@ -81,19 +85,30 @@ public class ArrayEncoder<T> implements Encodeable<Writer, T[]> {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (out.writeArrayB(value.length, componentEncoder, value) < 0) {
|
||||
final Type comp = this.componentType;
|
||||
boolean first = true;
|
||||
for (Object v : value) {
|
||||
if (!first) out.writeArrayMark();
|
||||
writeMemberValue(out, member, ((v != null && (v.getClass() == comp || out.specify() == comp)) ? componentEncoder : anyEncoder), v, first);
|
||||
if (first) first = false;
|
||||
Encodeable<Writer, Object> itemEncoder = this.componentEncoder;
|
||||
if (subtypefinal) {
|
||||
if (out.writeArrayB(value.length, this, itemEncoder, value) < 0) {
|
||||
for (int i = 0;; i++) {
|
||||
writeMemberValue(out, member, itemEncoder, value[i], i);
|
||||
if (i == iMax) break;
|
||||
out.writeArrayMark();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (out.writeArrayB(value.length, this, itemEncoder, value) < 0) {
|
||||
final Type comp = this.componentType;
|
||||
for (int i = 0;; i++) {
|
||||
Object v = value[i];
|
||||
writeMemberValue(out, member, ((v != null && (v.getClass() == comp || out.specify() == comp)) ? itemEncoder : anyEncoder), v, i);
|
||||
if (i == iMax) break;
|
||||
out.writeArrayMark();
|
||||
}
|
||||
}
|
||||
}
|
||||
out.writeArrayE();
|
||||
}
|
||||
|
||||
protected void writeMemberValue(Writer out, EnMember member, Encodeable<Writer, Object> encoder, Object value, boolean first) {
|
||||
protected void writeMemberValue(Writer out, EnMember member, Encodeable<Writer, Object> encoder, Object value, int index) {
|
||||
encoder.convertTo(out, value);
|
||||
}
|
||||
|
||||
|
||||
@@ -32,5 +32,4 @@ public abstract class BinaryConvert<R extends Reader, W extends Writer> extends
|
||||
|
||||
public abstract byte[] convertTo(final Type type, final Object value);
|
||||
|
||||
public abstract byte[] convertMapTo(final Object... values);
|
||||
}
|
||||
|
||||
@@ -129,6 +129,7 @@ public class CollectionDecoder<T> implements Decodeable<Reader, Collection<T>> {
|
||||
}
|
||||
|
||||
protected T readMemberValue(Reader in, DeMember member, Decodeable<Reader, T> decoder, boolean first) {
|
||||
if (in == null) return null;
|
||||
return decoder.convertFrom(in);
|
||||
}
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ public class CollectionEncoder<T> implements Encodeable<Writer, Collection<T>> {
|
||||
return;
|
||||
}
|
||||
if (value.isEmpty()) {
|
||||
out.writeArrayB(0, componentEncoder, value);
|
||||
out.writeArrayB(0, this, componentEncoder, value);
|
||||
out.writeArrayE();
|
||||
return;
|
||||
}
|
||||
@@ -76,18 +76,18 @@ public class CollectionEncoder<T> implements Encodeable<Writer, Collection<T>> {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (out.writeArrayB(value.size(), componentEncoder, value) < 0) {
|
||||
if (out.writeArrayB(value.size(), this, componentEncoder, value) < 0) {
|
||||
boolean first = true;
|
||||
for (Object v : value) {
|
||||
if (!first) out.writeArrayMark();
|
||||
writeValue(out, member, v);
|
||||
writeMemberValue(out, member, v, first);
|
||||
if (first) first = false;
|
||||
}
|
||||
}
|
||||
out.writeArrayE();
|
||||
}
|
||||
|
||||
protected void writeValue(Writer out, EnMember member, Object value) {
|
||||
protected void writeMemberValue(Writer out, EnMember member, Object value, boolean first) {
|
||||
componentEncoder.convertTo(out, value);
|
||||
}
|
||||
|
||||
@@ -96,6 +96,11 @@ public class CollectionEncoder<T> implements Encodeable<Writer, Collection<T>> {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.getClass().getSimpleName() + "{componentType:" + this.type + ", encoder:" + this.componentEncoder + "}";
|
||||
}
|
||||
|
||||
public Encodeable<Writer, Object> getComponentEncoder() {
|
||||
return componentEncoder;
|
||||
}
|
||||
|
||||
@@ -7,7 +7,8 @@ package org.redkale.convert;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.function.*;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
* 序列化/反序列化操作类
|
||||
@@ -31,18 +32,45 @@ public abstract class Convert<R extends Reader, W extends Writer> {
|
||||
return this.factory;
|
||||
}
|
||||
|
||||
protected <S extends W> S configWrite(S writer) {
|
||||
return writer;
|
||||
}
|
||||
|
||||
protected <S extends W> S fieldFunc(S writer, BiFunction<Attribute, Object, Object> objFieldFunc, Function<Object, ConvertField[]> objExtFunc) {
|
||||
writer.objFieldFunc = objFieldFunc;
|
||||
writer.objExtFunc = objExtFunc;
|
||||
return writer;
|
||||
}
|
||||
|
||||
public abstract Convert<R, W> newConvert(final BiFunction<Attribute, Object, Object> objFieldFunc);
|
||||
|
||||
public abstract Convert<R, W> newConvert(final BiFunction<Attribute, Object, Object> objFieldFunc, Function<Object, ConvertField[]> objExtFunc);
|
||||
|
||||
public abstract boolean isBinary();
|
||||
|
||||
public abstract <T> T convertFrom(final Type type, final byte[] bytes);
|
||||
|
||||
//@since 2.2.0
|
||||
public abstract <T> T convertFrom(final Type type, final byte[] bytes, final int offset, final int length);
|
||||
|
||||
public abstract <T> T convertFrom(final Type type, final ByteBuffer... buffers);
|
||||
|
||||
public abstract <T> T convertFrom(final Type type, final ConvertMask mask, final ByteBuffer... buffers);
|
||||
|
||||
public abstract byte[] convertToBytes(final Object value);
|
||||
|
||||
public abstract byte[] convertToBytes(final Type type, final Object value);
|
||||
|
||||
public abstract void convertToBytes(final Object value, final ConvertBytesHandler handler);
|
||||
|
||||
public abstract void convertToBytes(final Type type, final Object value, final ConvertBytesHandler handler);
|
||||
|
||||
public abstract void convertToBytes(final ByteArray array, final Object value);
|
||||
|
||||
public abstract void convertToBytes(final ByteArray array, final Type type, final Object value);
|
||||
|
||||
public abstract ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Object value);
|
||||
|
||||
public abstract ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Type type, final Object value);
|
||||
|
||||
public abstract ByteBuffer[] convertMapTo(final Supplier<ByteBuffer> supplier, final Object... values);
|
||||
|
||||
}
|
||||
|
||||
23
src/org/redkale/convert/ConvertBytesHandler.java
Normal file
23
src/org/redkale/convert/ConvertBytesHandler.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.convert;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
*
|
||||
* convertToBytes系列的方法的回调
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public interface ConvertBytesHandler {
|
||||
|
||||
<A> void completed(byte[] bs, int offset, int length, Consumer<A> callback, A attachment);
|
||||
}
|
||||
@@ -16,7 +16,9 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.*;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.*;
|
||||
import org.redkale.convert.bson.BsonConvert;
|
||||
import org.redkale.convert.ext.*;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
@@ -32,11 +34,15 @@ import org.redkale.util.*;
|
||||
@SuppressWarnings("unchecked")
|
||||
public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
|
||||
private static final AtomicBoolean loaderInited = new AtomicBoolean();
|
||||
|
||||
private static Convert defProtobufConvert;
|
||||
|
||||
private final ConvertFactory parent;
|
||||
|
||||
protected Convert<R, W> convert;
|
||||
|
||||
protected boolean tiny;
|
||||
protected boolean tiny; //String类型值为"",Boolean类型值为false时是否需要输出, 默认为true
|
||||
|
||||
private final Encodeable<W, ?> anyEncoder = new AnyEncoder(this);
|
||||
|
||||
@@ -65,6 +71,7 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
this.parent = parent;
|
||||
if (parent == null) {
|
||||
//---------------------------------------------------------
|
||||
|
||||
this.register(boolean.class, BoolSimpledCoder.instance);
|
||||
this.register(Boolean.class, BoolSimpledCoder.instance);
|
||||
|
||||
@@ -91,7 +98,7 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
|
||||
this.register(Number.class, NumberSimpledCoder.instance);
|
||||
this.register(String.class, StringSimpledCoder.instance);
|
||||
this.register(StringConvertWrapper.class, StringConvertWrapperSimpledCoder.instance);
|
||||
this.register(StringWrapper.class, StringWrapperSimpledCoder.instance);
|
||||
this.register(CharSequence.class, CharSequenceSimpledCoder.instance);
|
||||
this.register(StringBuilder.class, CharSequenceSimpledCoder.StringBuilderSimpledCoder.instance);
|
||||
this.register(java.util.Date.class, DateSimpledCoder.instance);
|
||||
@@ -106,6 +113,7 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
this.register(InetSocketAddress.class, InetAddressSimpledCoder.InetSocketAddressSimpledCoder.instance);
|
||||
this.register(Pattern.class, PatternSimpledCoder.instance);
|
||||
this.register(File.class, FileSimpledCoder.instance);
|
||||
this.register(Throwable.class, ThrowableSimpledCoder.instance);
|
||||
this.register(CompletionHandler.class, CompletionHandlerSimpledCoder.instance);
|
||||
this.register(URL.class, URLSimpledCoder.instance);
|
||||
this.register(URI.class, URISimpledCoder.instance);
|
||||
@@ -133,6 +141,54 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
}
|
||||
|
||||
});
|
||||
try {
|
||||
Class sqldateClass = Class.forName("java.sql.Date");
|
||||
this.register(sqldateClass, new SimpledCoder<R, W, java.sql.Date>() {
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, java.sql.Date value) {
|
||||
out.writeSmallString(value == null ? null : value.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public java.sql.Date convertFrom(R in) {
|
||||
String t = in.readSmallString();
|
||||
return t == null ? null : java.sql.Date.valueOf(t);
|
||||
}
|
||||
|
||||
});
|
||||
Class sqltimeClass = Class.forName("java.sql.Time");
|
||||
this.register(sqltimeClass, new SimpledCoder<R, W, java.sql.Time>() {
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, java.sql.Time value) {
|
||||
out.writeSmallString(value == null ? null : value.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public java.sql.Time convertFrom(R in) {
|
||||
String t = in.readSmallString();
|
||||
return t == null ? null : java.sql.Time.valueOf(t);
|
||||
}
|
||||
|
||||
});
|
||||
Class timestampClass = Class.forName("java.sql.Timestamp");
|
||||
this.register(timestampClass, new SimpledCoder<R, W, java.sql.Timestamp>() {
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, java.sql.Timestamp value) {
|
||||
out.writeSmallString(value == null ? null : value.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public java.sql.Timestamp convertFrom(R in) {
|
||||
String t = in.readSmallString();
|
||||
return t == null ? null : java.sql.Timestamp.valueOf(t);
|
||||
}
|
||||
|
||||
});
|
||||
} catch (Throwable t) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,6 +196,30 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
return this.parent;
|
||||
}
|
||||
|
||||
public static Convert findConvert(ConvertType type) {
|
||||
if (type == null) return null;
|
||||
if (type == ConvertType.JSON) return JsonConvert.root();
|
||||
if (type == ConvertType.BSON) return BsonConvert.root();
|
||||
if (loaderInited.get()) {
|
||||
if (type == ConvertType.PROTOBUF) return defProtobufConvert;
|
||||
}
|
||||
synchronized (loaderInited) {
|
||||
if (!loaderInited.get()) {
|
||||
Iterator<ConvertLoader> it = ServiceLoader.load(ConvertLoader.class).iterator();
|
||||
while (it.hasNext()) {
|
||||
ConvertLoader cl = it.next();
|
||||
if (cl.type() == ConvertType.PROTOBUF) defProtobufConvert = cl.convert();
|
||||
}
|
||||
loaderInited.set(true);
|
||||
}
|
||||
}
|
||||
return type == ConvertType.PROTOBUF ? defProtobufConvert : null;
|
||||
}
|
||||
|
||||
protected static boolean getSystemPropertyBoolean(String key, String parentkey, boolean defvalue) {
|
||||
return Boolean.parseBoolean(System.getProperty(key, System.getProperty(parentkey, String.valueOf(defvalue))));
|
||||
}
|
||||
|
||||
public abstract ConvertType getConvertType();
|
||||
|
||||
public abstract boolean isReversible(); //是否可逆的
|
||||
@@ -154,11 +234,15 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
return new EnumSimpledCoder(enumClass);
|
||||
}
|
||||
|
||||
protected <E> Encodeable<W, E> createDyncEncoder(Type type) {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected ObjectDecoder createObjectDecoder(Type type) {
|
||||
return new ObjectDecoder(type);
|
||||
}
|
||||
|
||||
protected ObjectEncoder createObjectEncoder(Type type) {
|
||||
protected <E> ObjectEncoder<W, E> createObjectEncoder(Type type) {
|
||||
return new ObjectEncoder(type);
|
||||
}
|
||||
|
||||
@@ -234,10 +318,14 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
final Method method = (Method) element;
|
||||
fieldName = readGetSetFieldName(method);
|
||||
if (fieldName != null) {
|
||||
try {
|
||||
ccs = method.getDeclaringClass().getDeclaredField(fieldName).getAnnotationsByType(ConvertColumn.class);
|
||||
} catch (Exception e) { //说明没有该字段,忽略异常
|
||||
}
|
||||
Class mclz = method.getDeclaringClass();
|
||||
do {
|
||||
try {
|
||||
ccs = mclz.getDeclaredField(fieldName).getAnnotationsByType(ConvertColumn.class);
|
||||
break;
|
||||
} catch (Exception e) { //说明没有该字段,忽略异常
|
||||
}
|
||||
} while (mclz != Object.class && (mclz = mclz.getSuperclass()) != Object.class);
|
||||
}
|
||||
}
|
||||
if (onlyColumns != null && fieldName == null) {
|
||||
@@ -711,7 +799,8 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
encoder = new OptionalCoder(this, type);
|
||||
} else if (clazz == Object.class) {
|
||||
return (Encodeable<W, E>) this.anyEncoder;
|
||||
} else if (!clazz.getName().startsWith("java.") || java.net.HttpCookie.class == clazz || java.util.AbstractMap.SimpleEntry.class == clazz) {
|
||||
} else if (!clazz.getName().startsWith("java.") || java.net.HttpCookie.class == clazz
|
||||
|| java.util.Map.Entry.class == clazz || java.util.AbstractMap.SimpleEntry.class == clazz) {
|
||||
Encodeable simpleCoder = null;
|
||||
for (final Method method : clazz.getDeclaredMethods()) {
|
||||
if (!Modifier.isStatic(method.getModifiers())) continue;
|
||||
@@ -727,8 +816,11 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
}
|
||||
}
|
||||
if (simpleCoder == null) {
|
||||
oe = createObjectEncoder(type);
|
||||
encoder = oe;
|
||||
encoder = createDyncEncoder(type);
|
||||
if (encoder == null) {
|
||||
oe = createObjectEncoder(type);
|
||||
encoder = oe;
|
||||
}
|
||||
} else {
|
||||
encoder = simpleCoder;
|
||||
}
|
||||
|
||||
102
src/org/redkale/convert/ConvertField.java
Normal file
102
src/org/redkale/convert/ConvertField.java
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.convert;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Type;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
|
||||
/**
|
||||
* newConvert参数中的Function返回结果的数据类
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class ConvertField implements Serializable {
|
||||
|
||||
protected String name;
|
||||
|
||||
protected Type type;
|
||||
|
||||
protected int position;
|
||||
|
||||
protected Object value;
|
||||
|
||||
public ConvertField() {
|
||||
}
|
||||
|
||||
public ConvertField(String name, Object value) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public ConvertField(String name, int position, Object value) {
|
||||
this.name = name;
|
||||
this.position = position;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public ConvertField(String name, Type type, Object value) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public ConvertField(String name, Type type, int position, Object value) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.position = position;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static ConvertField[] ofArray(Object... items) {
|
||||
int len = items.length / 2;
|
||||
ConvertField[] rs = new ConvertField[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
rs[i] = new ConvertField(items[i * 2].toString(), items[i * 2 + 1]);
|
||||
}
|
||||
return rs;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(Type type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public int getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
public void setPosition(int position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public Object getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
}
|
||||
}
|
||||
23
src/org/redkale/convert/ConvertLoader.java
Normal file
23
src/org/redkale/convert/ConvertLoader.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.convert;
|
||||
|
||||
/**
|
||||
* Convert的扩展实现类加载器
|
||||
*
|
||||
*
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*
|
||||
* @since 2.2.0
|
||||
*/
|
||||
public interface ConvertLoader {
|
||||
|
||||
public ConvertType type();
|
||||
|
||||
public Convert convert();
|
||||
}
|
||||
27
src/org/redkale/convert/ConvertSmallString.java
Normal file
27
src/org/redkale/convert/ConvertSmallString.java
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.convert;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* 序列化时标记String字段的值是否为无转义字符且长度不超过255的字符串
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*
|
||||
* @since 2.3.0
|
||||
*
|
||||
*/
|
||||
@Target({METHOD, FIELD})
|
||||
@Retention(RUNTIME)
|
||||
public @interface ConvertSmallString {
|
||||
|
||||
}
|
||||
@@ -17,8 +17,10 @@ public enum ConvertType {
|
||||
|
||||
JSON(1),
|
||||
BSON(2),
|
||||
DIY(64),
|
||||
ALL(127);
|
||||
PROTOBUF(64),
|
||||
PROTOBUF_JSON(64 + 1),
|
||||
DIY(256),
|
||||
ALL(1023);
|
||||
|
||||
private final int value;
|
||||
|
||||
@@ -26,8 +28,19 @@ public enum ConvertType {
|
||||
this.value = v;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public boolean contains(ConvertType type) {
|
||||
if (type == null) return false;
|
||||
return this.value >= type.value && (this.value & type.value) > 0;
|
||||
}
|
||||
|
||||
public static ConvertType find(int value) {
|
||||
for (ConvertType t : ConvertType.values()) {
|
||||
if (value == t.value) return t;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,8 @@ public final class DeMember<R extends Reader, T, F> {
|
||||
|
||||
protected int position; //从1开始
|
||||
|
||||
protected int tag; //主要给protobuf使用
|
||||
|
||||
protected final Attribute<T, F> attribute;
|
||||
|
||||
protected Decodeable<R, F> decoder;
|
||||
@@ -84,8 +86,13 @@ public final class DeMember<R extends Reader, T, F> {
|
||||
return this.position;
|
||||
}
|
||||
|
||||
public int getTag() {
|
||||
return this.tag;
|
||||
}
|
||||
|
||||
public int compareTo(boolean fieldSort, DeMember<R, T, F> o) {
|
||||
if (o == null) return -1;
|
||||
if (this.position != o.position) return (this.position == 0 ? Integer.MAX_VALUE : this.position) - (o.position == 0 ? Integer.MAX_VALUE : o.position);
|
||||
if (this.index != o.index) return (this.index == 0 ? Integer.MAX_VALUE : this.index) - (o.index == 0 ? Integer.MAX_VALUE : o.index);
|
||||
if (this.index != 0) throw new RuntimeException("fields (" + attribute.field() + ", " + o.attribute.field() + ") have same ConvertColumn.index(" + this.index + ") in " + attribute.declaringClass());
|
||||
return fieldSort ? this.attribute.field().compareTo(o.attribute.field()) : 0;
|
||||
@@ -106,6 +113,6 @@ public final class DeMember<R extends Reader, T, F> {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DeMember{" + "attribute=" + attribute.field() + ", decoder=" + (decoder == null ? null : decoder.getClass().getName()) + '}';
|
||||
return "DeMember{" + "attribute=" + attribute.field() + ", position=" + position + ", tag=" + tag + ", decoder=" + (decoder == null ? null : decoder.getClass().getName()) + '}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,21 +26,29 @@ public final class EnMember<W extends Writer, T, F> {
|
||||
|
||||
final Encodeable<W, F> encoder;
|
||||
|
||||
final boolean istring;
|
||||
final boolean string;
|
||||
|
||||
//final boolean isnumber;
|
||||
final boolean isbool;
|
||||
final boolean bool;
|
||||
|
||||
final char[] jsonFieldNameChars;
|
||||
|
||||
final byte[] jsonFieldNameBytes;
|
||||
|
||||
protected int index;
|
||||
|
||||
protected int position; //从1开始
|
||||
|
||||
protected int tag; //主要给protobuf使用
|
||||
|
||||
public EnMember(Attribute<T, F> attribute, Encodeable<W, F> encoder) {
|
||||
this.attribute = attribute;
|
||||
this.encoder = encoder;
|
||||
Class t = attribute.type();
|
||||
this.istring = CharSequence.class.isAssignableFrom(t);
|
||||
this.isbool = t == Boolean.class || t == boolean.class;
|
||||
this.string = CharSequence.class.isAssignableFrom(t);
|
||||
this.bool = t == Boolean.class || t == boolean.class;
|
||||
this.jsonFieldNameChars = ('"' + attribute.field() + "\":").toCharArray();
|
||||
this.jsonFieldNameBytes = ('"' + attribute.field() + "\":").getBytes();
|
||||
//this.isnumber = Number.class.isAssignableFrom(t) || (!this.isbool && t.isPrimitive());
|
||||
}
|
||||
|
||||
@@ -69,16 +77,24 @@ public final class EnMember<W extends Writer, T, F> {
|
||||
return attribute;
|
||||
}
|
||||
|
||||
public char[] getJsonFieldNameChars() {
|
||||
return jsonFieldNameChars;
|
||||
}
|
||||
|
||||
public byte[] getJsonFieldNameBytes() {
|
||||
return jsonFieldNameBytes;
|
||||
}
|
||||
|
||||
public Encodeable<W, F> getEncoder() {
|
||||
return encoder;
|
||||
}
|
||||
|
||||
public boolean isStringType() {
|
||||
return istring;
|
||||
return string;
|
||||
}
|
||||
|
||||
public boolean isBoolType() {
|
||||
return isbool;
|
||||
return bool;
|
||||
}
|
||||
|
||||
public int getIndex() {
|
||||
@@ -89,8 +105,13 @@ public final class EnMember<W extends Writer, T, F> {
|
||||
return this.position;
|
||||
}
|
||||
|
||||
public int getTag() {
|
||||
return this.tag;
|
||||
}
|
||||
|
||||
public int compareTo(boolean fieldSort, EnMember<W, T, F> o) {
|
||||
if (o == null) return -1;
|
||||
if (this.position != o.position) return (this.position == 0 ? Integer.MAX_VALUE : this.position) - (o.position == 0 ? Integer.MAX_VALUE : o.position);
|
||||
if (this.index != o.index) return (this.index == 0 ? Integer.MAX_VALUE : this.index) - (o.index == 0 ? Integer.MAX_VALUE : o.index);
|
||||
if (this.index != 0) throw new RuntimeException("fields (" + attribute.field() + ", " + o.attribute.field() + ") have same ConvertColumn.index(" + this.index + ") in " + attribute.declaringClass());
|
||||
return fieldSort ? this.attribute.field().compareTo(o.attribute.field()) : 0;
|
||||
@@ -111,6 +132,6 @@ public final class EnMember<W extends Writer, T, F> {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "EnMember{" + "attribute=" + attribute.field() + ", encoder=" + (encoder == null ? null : encoder.getClass().getName()) + '}';
|
||||
return "EnMember{" + "attribute=" + attribute.field() + ", position=" + position + ", encoder=" + (encoder == null ? null : encoder.getClass().getName()) + '}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,4 +28,9 @@ public interface Encodeable<W extends Writer, T> {
|
||||
*/
|
||||
public Type getType();
|
||||
|
||||
//是否需要检查Writer.specify
|
||||
default boolean specifyable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ package org.redkale.convert;
|
||||
import org.redkale.util.Creator;
|
||||
import java.lang.reflect.*;
|
||||
import java.util.*;
|
||||
import org.redkale.convert.ext.StringSimpledCoder;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
@@ -29,7 +30,7 @@ public class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T> {
|
||||
|
||||
protected Creator<T> creator;
|
||||
|
||||
protected DeMember<R, T, ?>[] creatorConstructorMembers = new DeMember[0];
|
||||
protected DeMember<R, T, ?>[] creatorConstructorMembers = null;
|
||||
|
||||
protected DeMember[] members;
|
||||
|
||||
@@ -96,12 +97,18 @@ public class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T> {
|
||||
if (factory.isConvertDisabled(field)) continue;
|
||||
ref = factory.findRef(clazz, field);
|
||||
if (ref != null && ref.ignore()) continue;
|
||||
Decodeable<R, ?> fieldCoder = factory.findFieldCoder(clazz, field.getName());
|
||||
ConvertSmallString small = field.getAnnotation(ConvertSmallString.class);
|
||||
Decodeable<R, ?> fieldCoder;
|
||||
if (small != null && field.getType() == String.class) {
|
||||
fieldCoder = StringSimpledCoder.SmallStringSimpledCoder.instance;
|
||||
} else {
|
||||
fieldCoder = factory.findFieldCoder(clazz, field.getName());
|
||||
}
|
||||
if (fieldCoder == null) {
|
||||
Type t = TypeToken.createClassType(TypeToken.getGenericType(field.getGenericType(), this.type), this.type);
|
||||
fieldCoder = factory.loadDecoder(t);
|
||||
}
|
||||
DeMember member = new DeMember(ObjectEncoder.createAttribute(factory, clazz, field, null, null), fieldCoder);
|
||||
DeMember member = new DeMember(ObjectEncoder.createAttribute(factory, type, clazz, field, null, null), fieldCoder);
|
||||
if (ref != null) member.index = ref.getIndex();
|
||||
list.add(member);
|
||||
}
|
||||
@@ -126,12 +133,18 @@ public class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T> {
|
||||
ref = factory.findRef(clazz, method);
|
||||
if (ref != null && ref.ignore()) continue;
|
||||
|
||||
Decodeable<R, ?> fieldCoder = factory.findFieldCoder(clazz, ConvertFactory.readGetSetFieldName(method));
|
||||
ConvertSmallString small = method.getAnnotation(ConvertSmallString.class);
|
||||
Decodeable<R, ?> fieldCoder;
|
||||
if (small != null && method.getReturnType() == String.class) {
|
||||
fieldCoder = StringSimpledCoder.SmallStringSimpledCoder.instance;
|
||||
} else {
|
||||
fieldCoder = factory.findFieldCoder(clazz, ConvertFactory.readGetSetFieldName(method));
|
||||
}
|
||||
if (fieldCoder == null) {
|
||||
Type t = TypeToken.createClassType(TypeToken.getGenericType(method.getGenericParameterTypes()[0], this.type), this.type);
|
||||
fieldCoder = factory.loadDecoder(t);
|
||||
}
|
||||
DeMember member = new DeMember(ObjectEncoder.createAttribute(factory, clazz, null, null, method), fieldCoder);
|
||||
DeMember member = new DeMember(ObjectEncoder.createAttribute(factory, type, clazz, null, null, method), fieldCoder);
|
||||
if (ref != null) member.index = ref.getIndex();
|
||||
list.add(member);
|
||||
}
|
||||
@@ -149,7 +162,7 @@ public class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T> {
|
||||
try {
|
||||
Field f = clazz.getDeclaredField(constructorField);
|
||||
Type t = TypeToken.createClassType(f.getGenericType(), this.type);
|
||||
list.add(new DeMember(ObjectEncoder.createAttribute(factory, clazz, f, null, null), factory.loadDecoder(t)));
|
||||
list.add(new DeMember(ObjectEncoder.createAttribute(factory, type, clazz, f, null, null), factory.loadDecoder(t)));
|
||||
} catch (NoSuchFieldException nsfe) { //不存在field, 可能存在getter方法
|
||||
char[] fs = constructorField.toCharArray();
|
||||
fs[0] = Character.toUpperCase(fs[0]);
|
||||
@@ -161,11 +174,12 @@ public class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T> {
|
||||
getter = clazz.getMethod("is" + mn);
|
||||
}
|
||||
Type t = TypeToken.createClassType(TypeToken.getGenericType(getter.getGenericParameterTypes()[0], this.type), this.type);
|
||||
list.add(new DeMember(ObjectEncoder.createAttribute(factory, clazz, null, getter, null), factory.loadDecoder(t)));
|
||||
list.add(new DeMember(ObjectEncoder.createAttribute(factory, type, clazz, null, getter, null), factory.loadDecoder(t)));
|
||||
}
|
||||
}
|
||||
}
|
||||
this.members = list.toArray(new DeMember[list.size()]);
|
||||
//先排序一次
|
||||
Arrays.sort(this.members, (a, b) -> a.compareTo(factory.isFieldSort(), b));
|
||||
Set<Integer> pos = new HashSet<>();
|
||||
for (int i = 0; i < this.members.length; i++) {
|
||||
@@ -179,7 +193,9 @@ public class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T> {
|
||||
while (pos.contains(++pidx));
|
||||
member.position = pidx;
|
||||
}
|
||||
initForEachDeMember(factory, member);
|
||||
}
|
||||
Arrays.sort(this.members, (a, b) -> a.compareTo(factory.isFieldSort(), b));
|
||||
|
||||
if (cps != null) {
|
||||
final String[] fields = cps;
|
||||
@@ -205,6 +221,13 @@ public class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T> {
|
||||
}
|
||||
}
|
||||
|
||||
protected void initForEachDeMember(ConvertFactory factory, DeMember member) {
|
||||
}
|
||||
|
||||
protected void setTag(DeMember member, int tag) {
|
||||
member.tag = tag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对象格式: [0x1][short字段个数][字段名][字段值]...[0x2]
|
||||
*
|
||||
@@ -214,9 +237,10 @@ public class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T> {
|
||||
*/
|
||||
@Override
|
||||
public T convertFrom(final R in) {
|
||||
final String clazz = in.readObjectB(typeClass);
|
||||
R objin = objectReader(in);
|
||||
final String clazz = objin.readObjectB(typeClass);
|
||||
if (clazz == null) return null;
|
||||
if (!clazz.isEmpty()) return (T) factory.loadDecoder(factory.getEntityAlias(clazz)).convertFrom(in);
|
||||
if (!clazz.isEmpty()) return (T) factory.loadDecoder(factory.getEntityAlias(clazz)).convertFrom(objin);
|
||||
if (!this.inited) {
|
||||
synchronized (lock) {
|
||||
try {
|
||||
@@ -234,17 +258,17 @@ public class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T> {
|
||||
if (this.creatorConstructorMembers == null) { //空构造函数
|
||||
final T result = this.creator == null ? null : this.creator.create();
|
||||
boolean first = true;
|
||||
while (hasNext(in, first)) {
|
||||
DeMember member = in.readFieldName(members);
|
||||
in.readBlank();
|
||||
while (hasNext(objin, first)) {
|
||||
DeMember member = objin.readFieldName(members);
|
||||
objin.readBlank();
|
||||
if (member == null) {
|
||||
in.skipValue(); //跳过不存在的属性的值
|
||||
objin.skipValue(); //跳过不存在的属性的值
|
||||
} else {
|
||||
readMemberValue(in, member, result, first);
|
||||
readMemberValue(objin, member, result, first);
|
||||
}
|
||||
first = false;
|
||||
}
|
||||
in.readObjectE(typeClass);
|
||||
objin.readObjectE(typeClass);
|
||||
return result;
|
||||
} else { //带参数的构造函数
|
||||
final DeMember<R, T, ?>[] fields = this.creatorConstructorMembers;
|
||||
@@ -252,13 +276,13 @@ public class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T> {
|
||||
final Object[][] otherParams = new Object[this.members.length][2];
|
||||
int oc = 0;
|
||||
boolean first = true;
|
||||
while (hasNext(in, first)) {
|
||||
DeMember member = in.readFieldName(members);
|
||||
in.readBlank();
|
||||
while (hasNext(objin, first)) {
|
||||
DeMember member = objin.readFieldName(members);
|
||||
objin.readBlank();
|
||||
if (member == null) {
|
||||
in.skipValue(); //跳过不存在的属性的值
|
||||
objin.skipValue(); //跳过不存在的属性的值
|
||||
} else {
|
||||
Object val = readMemberValue(in, member, first);
|
||||
Object val = readMemberValue(objin, member, first);
|
||||
boolean flag = true;
|
||||
for (int i = 0; i < fields.length; i++) {
|
||||
if (member == fields[i]) {
|
||||
@@ -272,7 +296,7 @@ public class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T> {
|
||||
}
|
||||
first = false;
|
||||
}
|
||||
in.readObjectE(typeClass);
|
||||
objin.readObjectE(typeClass);
|
||||
if (this.creator == null) return null;
|
||||
final T result = this.creator.create(constructorParams);
|
||||
for (int i = 0; i < oc; i++) {
|
||||
@@ -282,6 +306,10 @@ public class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T> {
|
||||
}
|
||||
}
|
||||
|
||||
protected R objectReader(R in) {
|
||||
return in;
|
||||
}
|
||||
|
||||
protected boolean hasNext(R in, boolean first) {
|
||||
return in.hasNext();
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ package org.redkale.convert;
|
||||
|
||||
import java.lang.reflect.*;
|
||||
import java.util.*;
|
||||
import org.redkale.convert.ext.StringSimpledCoder;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
@@ -77,12 +78,18 @@ public class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T> {
|
||||
if (factory.isConvertDisabled(field)) continue;
|
||||
ref = factory.findRef(clazz, field);
|
||||
if (ref != null && ref.ignore()) continue;
|
||||
Encodeable<W, ?> fieldCoder = factory.findFieldCoder(clazz, field.getName());
|
||||
ConvertSmallString small = field.getAnnotation(ConvertSmallString.class);
|
||||
Encodeable<W, ?> fieldCoder;
|
||||
if (small != null && field.getType() == String.class) {
|
||||
fieldCoder = StringSimpledCoder.SmallStringSimpledCoder.instance;
|
||||
} else {
|
||||
fieldCoder = factory.findFieldCoder(clazz, field.getName());
|
||||
}
|
||||
if (fieldCoder == null) {
|
||||
Type t = TypeToken.createClassType(TypeToken.getGenericType(field.getGenericType(), this.type), this.type);
|
||||
fieldCoder = factory.loadEncoder(t);
|
||||
}
|
||||
EnMember member = new EnMember(createAttribute(factory, clazz, field, null, null), fieldCoder);
|
||||
EnMember member = new EnMember(createAttribute(factory, type, clazz, field, null, null), fieldCoder);
|
||||
if (ref != null) member.index = ref.getIndex();
|
||||
list.add(member);
|
||||
}
|
||||
@@ -96,7 +103,8 @@ public class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T> {
|
||||
if (factory.isConvertDisabled(method)) continue;
|
||||
if (method.getParameterTypes().length != 0) continue;
|
||||
if (method.getReturnType() == void.class) continue;
|
||||
if (reversible && (cps == null || !contains(cps, ConvertFactory.readGetSetFieldName(method)))) {
|
||||
String convertname = ConvertFactory.readGetSetFieldName(method);
|
||||
if (reversible && (cps == null || !contains(cps, convertname))) {
|
||||
boolean is = method.getName().startsWith("is");
|
||||
try {
|
||||
clazz.getMethod(method.getName().replaceFirst(is ? "is" : "get", "set"), method.getReturnType());
|
||||
@@ -106,16 +114,30 @@ public class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T> {
|
||||
}
|
||||
ref = factory.findRef(clazz, method);
|
||||
if (ref != null && ref.ignore()) continue;
|
||||
Encodeable<W, ?> fieldCoder = factory.findFieldCoder(clazz, ConvertFactory.readGetSetFieldName(method));
|
||||
ConvertSmallString small = method.getAnnotation(ConvertSmallString.class);
|
||||
if (small == null) {
|
||||
try {
|
||||
Field f = clazz.getDeclaredField(convertname);
|
||||
if (f != null) small = f.getAnnotation(ConvertSmallString.class);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
Encodeable<W, ?> fieldCoder;
|
||||
if (small != null && method.getReturnType() == String.class) {
|
||||
fieldCoder = StringSimpledCoder.SmallStringSimpledCoder.instance;
|
||||
} else {
|
||||
fieldCoder = factory.findFieldCoder(clazz, ConvertFactory.readGetSetFieldName(method));
|
||||
}
|
||||
if (fieldCoder == null) {
|
||||
Type t = TypeToken.createClassType(TypeToken.getGenericType(method.getGenericReturnType(), this.type), this.type);
|
||||
fieldCoder = factory.loadEncoder(t);
|
||||
}
|
||||
EnMember member = new EnMember(createAttribute(factory, clazz, null, method, null), fieldCoder);
|
||||
EnMember member = new EnMember(createAttribute(factory, type, clazz, null, method, null), fieldCoder);
|
||||
if (ref != null) member.index = ref.getIndex();
|
||||
list.add(member);
|
||||
}
|
||||
this.members = list.toArray(new EnMember[list.size()]);
|
||||
//先排序一次
|
||||
Arrays.sort(this.members, (a, b) -> a.compareTo(factory.isFieldSort(), b));
|
||||
Set<Integer> pos = new HashSet<>();
|
||||
for (int i = 0; i < this.members.length; i++) {
|
||||
@@ -129,10 +151,12 @@ public class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T> {
|
||||
while (pos.contains(++pidx));
|
||||
member.position = pidx;
|
||||
}
|
||||
initForEachEnMember(factory, member);
|
||||
}
|
||||
Arrays.sort(this.members, (a, b) -> a.compareTo(factory.isFieldSort(), b));
|
||||
|
||||
} catch (Exception ex) {
|
||||
throw new ConvertException(ex);
|
||||
throw new ConvertException("ObjectEncoder init type=" + this.type + " error", ex);
|
||||
}
|
||||
} finally {
|
||||
inited = true;
|
||||
@@ -142,6 +166,13 @@ public class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T> {
|
||||
}
|
||||
}
|
||||
|
||||
protected void initForEachEnMember(ConvertFactory factory, EnMember member) {
|
||||
}
|
||||
|
||||
protected void setTag(EnMember member, int tag) {
|
||||
member.tag = tag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, T value) {
|
||||
if (value == null) {
|
||||
@@ -163,12 +194,30 @@ public class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T> {
|
||||
factory.loadEncoder(clz).convertTo(out, value);
|
||||
return;
|
||||
}
|
||||
if (out.writeObjectB(value) < 0) {
|
||||
W objout = objectWriter(out, value);
|
||||
if (objout.writeObjectB(value) < 0) {
|
||||
int maxPosition = 0;
|
||||
for (EnMember member : members) {
|
||||
out.writeObjectField(member, value);
|
||||
maxPosition = member.getPosition();
|
||||
objout.writeObjectField(member, value);
|
||||
}
|
||||
if (objout.objExtFunc != null) {
|
||||
ConvertField[] extFields = objout.objExtFunc.apply(value);
|
||||
if (extFields != null) {
|
||||
Encodeable<W, ?> anyEncoder = factory.getAnyEncoder();
|
||||
for (ConvertField en : extFields) {
|
||||
if (en == null) continue;
|
||||
maxPosition++;
|
||||
objout.writeObjectField(en.getName(), en.getType(), Math.max(en.getPosition(), maxPosition), anyEncoder, en.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
out.writeObjectE(value);
|
||||
objout.writeObjectE(value);
|
||||
}
|
||||
|
||||
protected W objectWriter(W out, T value) {
|
||||
return out;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -250,7 +299,7 @@ public class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T> {
|
||||
}
|
||||
}
|
||||
|
||||
static Attribute createAttribute(final ConvertFactory factory, Class clazz, final Field field, final Method getter, final Method setter) {
|
||||
static Attribute createAttribute(final ConvertFactory factory, Type realType, Class clazz, final Field field, final Method getter, final Method setter) {
|
||||
String fieldalias;
|
||||
if (field != null) { // public field
|
||||
ConvertColumnEntry ref = factory.findRef(clazz, field);
|
||||
@@ -276,7 +325,7 @@ public class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T> {
|
||||
}
|
||||
fieldalias = ref == null || ref.name().isEmpty() ? mfieldname : ref.name();
|
||||
}
|
||||
return Attribute.create(clazz, fieldalias, field, getter, setter);
|
||||
return Attribute.create(realType, clazz, fieldalias, null, field, getter, setter, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,9 +20,7 @@ import java.util.*;
|
||||
*/
|
||||
public class OptionalCoder<R extends Reader, W extends Writer, T> extends SimpledCoder<R, W, Optional<T>> {
|
||||
|
||||
private final Type type;
|
||||
|
||||
private final Type componentType;
|
||||
protected final Type componentType;
|
||||
|
||||
protected final Class componentClass;
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@ public class StreamEncoder<T> implements Encodeable<Writer, Stream<T>> {
|
||||
}
|
||||
Object[] array = value.toArray();
|
||||
if (array.length == 0) {
|
||||
out.writeArrayB(0, componentEncoder, array);
|
||||
out.writeArrayB(0, this, componentEncoder, array);
|
||||
out.writeArrayE();
|
||||
return;
|
||||
}
|
||||
@@ -77,7 +77,7 @@ public class StreamEncoder<T> implements Encodeable<Writer, Stream<T>> {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (out.writeArrayB(array.length, componentEncoder, array) < 0) {
|
||||
if (out.writeArrayB(array.length, this, componentEncoder, array) < 0) {
|
||||
boolean first = true;
|
||||
for (Object v : array) {
|
||||
if (!first) out.writeArrayMark();
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.convert;
|
||||
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
|
||||
/**
|
||||
* 序列化去掉引号的String对象。
|
||||
* <blockquote><pre>
|
||||
* 场景: JavaBean bean = ... ;
|
||||
* Map map = new HashMap();
|
||||
* map.put("bean", a);
|
||||
* records.add(map);
|
||||
* records需要在后期序列化写入库。 但是在这期间bean的内部字段值可能就变化了,会导致入库时并不是records.add的快照信息。
|
||||
* 所以需要使用StringConvertWrapper:
|
||||
* Map map = new HashMap();
|
||||
* map.put("bean", new StringConvertWrapper(bean.toString()));
|
||||
* records.add(map);
|
||||
* 这样既可以保持快照又不会在bean的值上面多一层引号。
|
||||
* </pre></blockquote>
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class StringConvertWrapper {
|
||||
|
||||
protected String value;
|
||||
|
||||
public StringConvertWrapper() {
|
||||
}
|
||||
|
||||
public StringConvertWrapper(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static StringConvertWrapper create(Object value) {
|
||||
return create(JsonConvert.root(), value);
|
||||
}
|
||||
|
||||
public static StringConvertWrapper create(TextConvert convert, Object value) {
|
||||
if (value == null) return new StringConvertWrapper(null);
|
||||
if (value instanceof String) return new StringConvertWrapper((String) value);
|
||||
return new StringConvertWrapper(convert.convertTo(value));
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -32,5 +32,4 @@ public abstract class TextConvert<R extends Reader, W extends Writer> extends Co
|
||||
|
||||
public abstract String convertTo(final Type type, final Object value);
|
||||
|
||||
public abstract String convertMapTo(final Object... values);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
package org.redkale.convert;
|
||||
|
||||
import java.lang.reflect.*;
|
||||
import java.util.function.*;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
* 序列化的数据输出流
|
||||
@@ -23,6 +25,12 @@ public abstract class Writer {
|
||||
//convertTo时是否以指定Type的ObjectEncoder进行处理
|
||||
protected Type specify;
|
||||
|
||||
//对某个字段值进行动态处理
|
||||
protected BiFunction<Attribute, Object, Object> objFieldFunc;
|
||||
|
||||
//对某个对象进行动态扩展字段值处理
|
||||
protected Function<Object, ConvertField[]> objExtFunc;
|
||||
|
||||
/**
|
||||
* 设置specify
|
||||
*
|
||||
@@ -38,6 +46,11 @@ public abstract class Writer {
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean recycle() {
|
||||
this.objFieldFunc = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回specify
|
||||
*
|
||||
@@ -105,20 +118,63 @@ public abstract class Writer {
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public void writeObjectField(final EnMember member, Object obj) {
|
||||
Object value = member.attribute.get(obj);
|
||||
Object value;
|
||||
if (objFieldFunc == null) {
|
||||
value = member.attribute.get(obj);
|
||||
} else {
|
||||
value = objFieldFunc.apply(member.attribute, obj);
|
||||
}
|
||||
if (value == null) return;
|
||||
if (tiny()) {
|
||||
if (member.istring) {
|
||||
if (member.string) {
|
||||
if (((CharSequence) value).length() == 0) return;
|
||||
} else if (member.isbool) {
|
||||
} else if (member.bool) {
|
||||
if (!((Boolean) value)) return;
|
||||
}
|
||||
}
|
||||
this.writeFieldName(member);
|
||||
Attribute attr = member.getAttribute();
|
||||
this.writeFieldName(member, attr.field(), attr.genericType(), member.getPosition());
|
||||
member.encoder.convertTo(this, value);
|
||||
this.comma = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出一个对象的某个扩展字段
|
||||
*
|
||||
*
|
||||
* @param fieldName 字段名称
|
||||
* @param fieldType 字段类型
|
||||
* @param fieldPos 字段顺序
|
||||
* @param anyEncoder Encoder
|
||||
* @param value 写入的字段对象
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public void writeObjectField(final String fieldName, Type fieldType, int fieldPos, Encodeable anyEncoder, Object value) {
|
||||
if (value == null) return;
|
||||
if (fieldType == null) fieldType = value.getClass();
|
||||
if (tiny() && fieldType instanceof Class) {
|
||||
Class clazz = (Class) fieldType;
|
||||
if (CharSequence.class.isAssignableFrom(clazz)) {
|
||||
if (((CharSequence) value).length() == 0) return;
|
||||
} else if (clazz == boolean.class || clazz == Boolean.class) {
|
||||
if (!((Boolean) value)) return;
|
||||
}
|
||||
}
|
||||
this.writeFieldName(null, fieldName, fieldType, fieldPos);
|
||||
anyEncoder.convertTo(this, value);
|
||||
this.comma = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出一个字段名
|
||||
*
|
||||
* @param member 字段
|
||||
*/
|
||||
public final void writeFieldName(final EnMember member) {
|
||||
Attribute attr = member.getAttribute();
|
||||
this.writeFieldName(member, attr.field(), attr.genericType(), member.getPosition());
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出一个对象后的操作
|
||||
*
|
||||
@@ -130,12 +186,13 @@ public abstract class Writer {
|
||||
* 输出一个数组前的操作
|
||||
*
|
||||
* @param size 数组长度
|
||||
* @param arrayEncoder Encodeable 可能是ArrayEncoder、CollectionEncoder或StreamEncoder
|
||||
* @param componentEncoder Encodeable
|
||||
* @param obj 对象, 不一定是数组、Collection对象,也可能是伪Collection对象
|
||||
*
|
||||
* @return 返回-1表示还没有写入对象内容,大于-1表示已写入对象内容,返回对象内容大小
|
||||
*/
|
||||
public abstract int writeArrayB(int size, Encodeable<Writer, Object> componentEncoder, Object obj);
|
||||
public abstract int writeArrayB(int size, Encodeable arrayEncoder, Encodeable<Writer, Object> componentEncoder, Object obj);
|
||||
|
||||
/**
|
||||
* 输出数组元素间的间隔符
|
||||
@@ -176,9 +233,12 @@ public abstract class Writer {
|
||||
/**
|
||||
* 输出一个字段名
|
||||
*
|
||||
* @param member 字段的EnMember对象
|
||||
* @param member EnMember
|
||||
* @param fieldName 字段名称
|
||||
* @param fieldType 字段类型
|
||||
* @param fieldPos 字段顺序
|
||||
*/
|
||||
public abstract void writeFieldName(EnMember member);
|
||||
public abstract void writeFieldName(EnMember member, String fieldName, Type fieldType, int fieldPos);
|
||||
|
||||
/**
|
||||
* 写入一个boolean值
|
||||
@@ -262,5 +322,5 @@ public abstract class Writer {
|
||||
*
|
||||
* @param value StringConvertWrapper值
|
||||
*/
|
||||
public abstract void writeWrapper(StringConvertWrapper value);
|
||||
public abstract void writeWrapper(StringWrapper value);
|
||||
}
|
||||
|
||||
@@ -6,10 +6,10 @@
|
||||
package org.redkale.convert.bson;
|
||||
|
||||
import java.nio.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import org.redkale.convert.*;
|
||||
import static org.redkale.convert.Reader.SIGN_NULL;
|
||||
import org.redkale.convert.ext.ByteSimpledCoder;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
* 以ByteBuffer为数据载体的BsonReader
|
||||
@@ -233,6 +233,6 @@ public class BsonByteBufferReader extends BsonReader {
|
||||
int len = readInt();
|
||||
if (len == SIGN_NULL) return null;
|
||||
if (len == 0) return "";
|
||||
return new String(Utility.decodeUTF8(read(len)));
|
||||
return new String(read(len), StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,9 +131,25 @@ public class BsonByteBufferWriter extends BsonWriter {
|
||||
|
||||
@Override
|
||||
protected boolean recycle() {
|
||||
super.recycle();
|
||||
this.index = 0;
|
||||
this.specify = null;
|
||||
this.buffers = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] content() {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //无需实现
|
||||
}
|
||||
|
||||
@Override
|
||||
public int offset() {
|
||||
throw new UnsupportedOperationException("Not supported yet.");//无需实现
|
||||
}
|
||||
|
||||
@Override
|
||||
public int length() {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //无需实现
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,11 +37,11 @@ import org.redkale.util.*;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public final class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
||||
public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
||||
|
||||
private static final ObjectPool<BsonReader> readerPool = BsonReader.createPool(Integer.getInteger("convert.bson.pool.size", 16));
|
||||
private final ThreadLocal<BsonWriter> writerPool = ThreadLocal.withInitial(BsonWriter::new);
|
||||
|
||||
private static final ObjectPool<BsonWriter> writerPool = BsonWriter.createPool(Integer.getInteger("convert.bson.pool.size", 16));
|
||||
private final Consumer<BsonWriter> offerConsumer = w -> offerBsonWriter(w);
|
||||
|
||||
private final boolean tiny;
|
||||
|
||||
@@ -59,6 +59,21 @@ public final class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
||||
return BsonFactory.root().getConvert();
|
||||
}
|
||||
|
||||
@Override
|
||||
public BsonConvert newConvert(final BiFunction<Attribute, Object, Object> fieldFunc) {
|
||||
return newConvert(fieldFunc, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BsonConvert newConvert(final BiFunction<Attribute, Object, Object> fieldFunc, Function<Object, ConvertField[]> objExtFunc) {
|
||||
return new BsonConvert(getFactory(), tiny) {
|
||||
@Override
|
||||
protected <S extends BsonWriter> S configWrite(S writer) {
|
||||
return fieldFunc(writer, fieldFunc, objExtFunc);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//------------------------------ reader -----------------------------------------------------------
|
||||
public BsonReader pollBsonReader(final ByteBuffer... buffers) {
|
||||
return new BsonByteBufferReader((ConvertMask) null, buffers);
|
||||
@@ -69,44 +84,53 @@ public final class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
||||
}
|
||||
|
||||
public BsonReader pollBsonReader() {
|
||||
return readerPool.get();
|
||||
return new BsonReader();
|
||||
}
|
||||
|
||||
public void offerBsonReader(final BsonReader in) {
|
||||
if (in != null) readerPool.accept(in);
|
||||
//无需回收
|
||||
}
|
||||
|
||||
//------------------------------ writer -----------------------------------------------------------
|
||||
public BsonByteBufferWriter pollBsonWriter(final Supplier<ByteBuffer> supplier) {
|
||||
return new BsonByteBufferWriter(tiny, supplier);
|
||||
return configWrite(new BsonByteBufferWriter(tiny, supplier));
|
||||
}
|
||||
|
||||
public BsonWriter pollBsonWriter(final OutputStream out) {
|
||||
return new BsonStreamWriter(tiny, out);
|
||||
protected BsonWriter pollBsonWriter(final OutputStream out) {
|
||||
return configWrite(new BsonStreamWriter(tiny, out));
|
||||
}
|
||||
|
||||
public BsonWriter pollBsonWriter() {
|
||||
return writerPool.get().tiny(tiny);
|
||||
BsonWriter writer = writerPool.get();
|
||||
if (writer == null) {
|
||||
writer = new BsonWriter();
|
||||
} else {
|
||||
writerPool.set(null);
|
||||
}
|
||||
return configWrite(writer.tiny(tiny));
|
||||
}
|
||||
|
||||
public void offerBsonWriter(final BsonWriter out) {
|
||||
if (out != null) writerPool.accept(out);
|
||||
if (out != null) {
|
||||
out.recycle();
|
||||
writerPool.set(out);
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------ convertFrom -----------------------------------------------------------
|
||||
@Override
|
||||
public <T> T convertFrom(final Type type, final byte[] bytes) {
|
||||
if (bytes == null) return null;
|
||||
return convertFrom(type, bytes, 0, bytes.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T convertFrom(final Type type, final byte[] bytes, final int start, final int len) {
|
||||
public <T> T convertFrom(final Type type, final byte[] bytes, final int offset, final int len) {
|
||||
if (type == null) return null;
|
||||
final BsonReader in = readerPool.get();
|
||||
in.setBytes(bytes, start, len);
|
||||
final BsonReader in = new BsonReader(bytes, offset, len);
|
||||
@SuppressWarnings("unchecked")
|
||||
T rs = (T) factory.loadDecoder(type).convertFrom(in);
|
||||
readerPool.accept(in);
|
||||
return rs;
|
||||
}
|
||||
|
||||
@@ -142,10 +166,10 @@ public final class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
||||
@Override
|
||||
public byte[] convertTo(final Object value) {
|
||||
if (value == null) {
|
||||
final BsonWriter out = writerPool.get().tiny(tiny);
|
||||
final BsonWriter out = pollBsonWriter();
|
||||
out.writeNull();
|
||||
byte[] result = out.toArray();
|
||||
writerPool.accept(out);
|
||||
offerBsonWriter(out);
|
||||
return result;
|
||||
}
|
||||
return convertTo(value.getClass(), value);
|
||||
@@ -154,52 +178,76 @@ public final class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
||||
@Override
|
||||
public byte[] convertTo(final Type type, final Object value) {
|
||||
if (type == null) return null;
|
||||
final BsonWriter out = writerPool.get().tiny(tiny);
|
||||
factory.loadEncoder(type).convertTo(out, value);
|
||||
byte[] result = out.toArray();
|
||||
writerPool.accept(out);
|
||||
final BsonWriter writer = pollBsonWriter();
|
||||
factory.loadEncoder(type).convertTo(writer, value);
|
||||
byte[] result = writer.toArray();
|
||||
offerBsonWriter(writer);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] convertMapTo(final Object... values) {
|
||||
if (values == null) return null;
|
||||
final BsonWriter out = writerPool.get().tiny(tiny);
|
||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(out, values);
|
||||
byte[] result = out.toArray();
|
||||
writerPool.accept(out);
|
||||
return result;
|
||||
public byte[] convertToBytes(final Object value) {
|
||||
return convertTo(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] convertToBytes(final Type type, final Object value) {
|
||||
return convertTo(type, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void convertToBytes(final Object value, final ConvertBytesHandler handler) {
|
||||
convertToBytes(value == null ? null : value.getClass(), value, handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void convertToBytes(final Type type, final Object value, final ConvertBytesHandler handler) {
|
||||
final BsonWriter writer = pollBsonWriter();
|
||||
if (type == null) {
|
||||
writer.writeNull();
|
||||
} else {
|
||||
factory.loadEncoder(type).convertTo(writer, value);
|
||||
}
|
||||
writer.completed(handler, offerConsumer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void convertToBytes(final ByteArray array, final Object value) {
|
||||
convertToBytes(array, value == null ? null : value.getClass(), value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void convertToBytes(final ByteArray array, final Type type, final Object value) {
|
||||
final BsonWriter writer = configWrite(new BsonWriter(array).tiny(tiny));
|
||||
if (type == null) {
|
||||
writer.writeNull();
|
||||
} else {
|
||||
factory.loadEncoder(type).convertTo(writer, value);
|
||||
}
|
||||
writer.directTo(array);
|
||||
}
|
||||
|
||||
public void convertTo(final OutputStream out, final Object value) {
|
||||
if (value == null) {
|
||||
new BsonStreamWriter(tiny, out).writeNull();
|
||||
pollBsonWriter(out).writeNull();
|
||||
} else {
|
||||
factory.loadEncoder(value.getClass()).convertTo(new BsonStreamWriter(tiny, out), value);
|
||||
factory.loadEncoder(value.getClass()).convertTo(pollBsonWriter(out), value);
|
||||
}
|
||||
}
|
||||
|
||||
public void convertTo(final OutputStream out, final Type type, final Object value) {
|
||||
if (type == null) return;
|
||||
if (value == null) {
|
||||
new BsonStreamWriter(tiny, out).writeNull();
|
||||
pollBsonWriter(out).writeNull();
|
||||
} else {
|
||||
factory.loadEncoder(type).convertTo(new BsonStreamWriter(tiny, out), value);
|
||||
}
|
||||
}
|
||||
|
||||
public void convertMapTo(final OutputStream out, final Object... values) {
|
||||
if (values == null) {
|
||||
new BsonStreamWriter(tiny, out).writeNull();
|
||||
} else {
|
||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(new BsonStreamWriter(tiny, out), values);
|
||||
factory.loadEncoder(type).convertTo(pollBsonWriter(out), value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Object value) {
|
||||
if (supplier == null) return null;
|
||||
BsonByteBufferWriter out = new BsonByteBufferWriter(tiny, supplier);
|
||||
BsonByteBufferWriter out = pollBsonWriter(supplier);
|
||||
if (value == null) {
|
||||
out.writeNull();
|
||||
} else {
|
||||
@@ -211,25 +259,13 @@ public final class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
||||
@Override
|
||||
public ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Type type, final Object value) {
|
||||
if (supplier == null || type == null) return null;
|
||||
BsonByteBufferWriter out = new BsonByteBufferWriter(tiny, supplier);
|
||||
BsonByteBufferWriter writer = pollBsonWriter(supplier);
|
||||
if (value == null) {
|
||||
out.writeNull();
|
||||
writer.writeNull();
|
||||
} else {
|
||||
factory.loadEncoder(type).convertTo(out, value);
|
||||
factory.loadEncoder(type).convertTo(writer, value);
|
||||
}
|
||||
return out.toBuffers();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer[] convertMapTo(final Supplier<ByteBuffer> supplier, final Object... values) {
|
||||
if (supplier == null) return null;
|
||||
BsonByteBufferWriter out = new BsonByteBufferWriter(tiny, supplier);
|
||||
if (values == null) {
|
||||
out.writeNull();
|
||||
} else {
|
||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(out, values);
|
||||
}
|
||||
return out.toBuffers();
|
||||
return writer.toBuffers();
|
||||
}
|
||||
|
||||
public void convertTo(final BsonWriter writer, final Object value) {
|
||||
@@ -245,14 +281,6 @@ public final class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
||||
factory.loadEncoder(type).convertTo(writer, value);
|
||||
}
|
||||
|
||||
public void convertMapTo(final BsonWriter writer, final Object... values) {
|
||||
if (values == null) {
|
||||
writer.writeNull();
|
||||
} else {
|
||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(writer, values);
|
||||
}
|
||||
}
|
||||
|
||||
public BsonWriter convertToWriter(final Object value) {
|
||||
if (value == null) return null;
|
||||
return convertToWriter(value.getClass(), value);
|
||||
@@ -260,14 +288,8 @@ public final class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
||||
|
||||
public BsonWriter convertToWriter(final Type type, final Object value) {
|
||||
if (type == null) return null;
|
||||
final BsonWriter out = writerPool.get().tiny(tiny);
|
||||
factory.loadEncoder(type).convertTo(out, value);
|
||||
return out;
|
||||
}
|
||||
|
||||
public BsonWriter convertMapToWriter(final Object... values) {
|
||||
final BsonWriter out = writerPool.get().tiny(tiny);
|
||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(out, values);
|
||||
return out;
|
||||
final BsonWriter writer = writerPool.get().tiny(tiny);
|
||||
factory.loadEncoder(type).convertTo(writer, value);
|
||||
return writer;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ import org.redkale.util.*;
|
||||
@SuppressWarnings("unchecked")
|
||||
public final class BsonFactory extends ConvertFactory<BsonReader, BsonWriter> {
|
||||
|
||||
private static final BsonFactory instance = new BsonFactory(null, Boolean.getBoolean("convert.bson.tiny"));
|
||||
private static final BsonFactory instance = new BsonFactory(null, getSystemPropertyBoolean("convert.bson.tiny", "convert.tiny", true));
|
||||
|
||||
static final Decodeable objectDecoder = instance.loadDecoder(Object.class);
|
||||
|
||||
@@ -69,7 +69,7 @@ public final class BsonFactory extends ConvertFactory<BsonReader, BsonWriter> {
|
||||
}
|
||||
|
||||
public static BsonFactory create() {
|
||||
return new BsonFactory(null, Boolean.getBoolean("convert.bson.tiny"));
|
||||
return new BsonFactory(null, getSystemPropertyBoolean("convert.bson.tiny", "convert.tiny", true));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
package org.redkale.convert.bson;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import org.redkale.convert.*;
|
||||
import static org.redkale.convert.Reader.SIGN_NULL;
|
||||
import org.redkale.convert.ext.*;
|
||||
@@ -42,7 +43,7 @@ public class BsonReader extends Reader {
|
||||
}
|
||||
|
||||
public static ObjectPool<BsonReader> createPool(int max) {
|
||||
return new ObjectPool<>(max, (Object... params) -> new BsonReader(), null, (t) -> t.recycle());
|
||||
return ObjectPool.createSafePool(max, (Object... params) -> new BsonReader(), null, (t) -> t.recycle());
|
||||
}
|
||||
|
||||
public BsonReader(byte[] bytes) {
|
||||
@@ -341,14 +342,14 @@ public class BsonReader extends Reader {
|
||||
int len = readInt();
|
||||
if (len == SIGN_NULL) return null;
|
||||
if (len == 0) return "";
|
||||
String value = new String(Utility.decodeUTF8(content, ++this.position, len));
|
||||
String value = new String(content, ++this.position, len, StandardCharsets.UTF_8);
|
||||
this.position += len - 1;//上一行已经++this.position,所以此处要-1
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValueType readType() {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,7 +5,9 @@
|
||||
*/
|
||||
package org.redkale.convert.bson;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.function.Consumer;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.convert.ext.ByteSimpledCoder;
|
||||
import org.redkale.util.*;
|
||||
@@ -17,9 +19,9 @@ import org.redkale.util.*;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class BsonWriter extends Writer {
|
||||
public class BsonWriter extends Writer implements ByteTuple {
|
||||
|
||||
private static final int defaultSize = Integer.getInteger("convert.bson.writer.buffer.defsize", 1024);
|
||||
private static final int defaultSize = Integer.getInteger("convert.bson.writer.buffer.defsize", Integer.getInteger("convert.writer.buffer.defsize", 1024));
|
||||
|
||||
private byte[] content;
|
||||
|
||||
@@ -28,7 +30,48 @@ public class BsonWriter extends Writer {
|
||||
protected boolean tiny;
|
||||
|
||||
public static ObjectPool<BsonWriter> createPool(int max) {
|
||||
return new ObjectPool<>(max, (Object... params) -> new BsonWriter(), null, (t) -> t.recycle());
|
||||
return ObjectPool.createSafePool(max, (Object... params) -> new BsonWriter(), null, (t) -> t.recycle());
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] content() {
|
||||
return content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int offset() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int length() {
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接获取全部数据, 实际数据需要根据count长度来截取
|
||||
*
|
||||
* @return byte[]
|
||||
*/
|
||||
public byte[] directBytes() {
|
||||
return content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将本对象的内容引用复制给array
|
||||
*
|
||||
* @param array ByteArray
|
||||
*/
|
||||
public void directTo(ByteArray array) {
|
||||
array.directFrom(content, count);
|
||||
}
|
||||
|
||||
public void completed(ConvertBytesHandler handler, Consumer<BsonWriter> callback) {
|
||||
handler.completed(content, 0, count, callback, this);
|
||||
}
|
||||
|
||||
public ByteArray toByteArray() {
|
||||
return new ByteArray(this);
|
||||
}
|
||||
|
||||
public byte[] toArray() {
|
||||
@@ -43,7 +86,7 @@ public class BsonWriter extends Writer {
|
||||
}
|
||||
|
||||
protected BsonWriter(byte[] bs) {
|
||||
this.content = bs;
|
||||
this.content = bs == null ? new byte[0] : bs;
|
||||
}
|
||||
|
||||
public BsonWriter() {
|
||||
@@ -54,6 +97,11 @@ public class BsonWriter extends Writer {
|
||||
this.content = new byte[size > 128 ? size : 128];
|
||||
}
|
||||
|
||||
public BsonWriter(ByteArray array) {
|
||||
this.content = array.content();
|
||||
this.count = array.length();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean tiny() {
|
||||
return tiny;
|
||||
@@ -97,10 +145,12 @@ public class BsonWriter extends Writer {
|
||||
count += len;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean recycle() {
|
||||
super.recycle();
|
||||
this.count = 0;
|
||||
this.specify = null;
|
||||
if (this.content.length > defaultSize) {
|
||||
if (this.content != null && this.content.length > defaultSize) {
|
||||
this.content = new byte[defaultSize];
|
||||
}
|
||||
return true;
|
||||
@@ -132,7 +182,7 @@ public class BsonWriter extends Writer {
|
||||
writeNull();
|
||||
return;
|
||||
}
|
||||
writeArrayB(values.length, null, values);
|
||||
writeArrayB(values.length, null, null, values);
|
||||
boolean flag = false;
|
||||
for (byte v : values) {
|
||||
if (flag) writeArrayMark();
|
||||
@@ -198,11 +248,10 @@ public class BsonWriter extends Writer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void writeFieldName(EnMember member) {
|
||||
Attribute attribute = member.getAttribute();
|
||||
public final void writeFieldName(EnMember member, String fieldName, Type fieldType, int fieldPos) {
|
||||
writeByte(BsonReader.SIGN_HASNEXT);
|
||||
writeSmallString(attribute.field());
|
||||
writeByte(BsonFactory.typeEnum(attribute.type()));
|
||||
writeSmallString(fieldName);
|
||||
writeByte(BsonFactory.typeEnum(fieldType));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -242,7 +291,7 @@ public class BsonWriter extends Writer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void writeWrapper(StringConvertWrapper value) {
|
||||
public final void writeWrapper(StringWrapper value) {
|
||||
this.writeString(value == null ? null : value.getValue());
|
||||
}
|
||||
|
||||
@@ -252,7 +301,7 @@ public class BsonWriter extends Writer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int writeArrayB(int size, Encodeable<Writer, Object> componentEncoder, Object obj) {
|
||||
public final int writeArrayB(int size, Encodeable arrayEncoder, Encodeable<Writer, Object> componentEncoder, Object obj) {
|
||||
writeInt(size);
|
||||
if (componentEncoder != null && componentEncoder != ByteSimpledCoder.instance) {
|
||||
writeByte(BsonFactory.typeEnum(componentEncoder.getType()));
|
||||
|
||||
@@ -29,7 +29,7 @@ public final class BoolArraySimpledCoder<R extends Reader, W extends Writer> ext
|
||||
out.writeNull();
|
||||
return;
|
||||
}
|
||||
if (out.writeArrayB(values.length, BoolSimpledCoder.instance, values) < 0) {
|
||||
if (out.writeArrayB(values.length, this, BoolSimpledCoder.instance, values) < 0) {
|
||||
boolean flag = false;
|
||||
for (boolean v : values) {
|
||||
if (flag) out.writeArrayMark();
|
||||
|
||||
@@ -30,7 +30,7 @@ public final class ByteBufferSimpledCoder<R extends Reader, W extends Writer> ex
|
||||
out.writeNull();
|
||||
return;
|
||||
}
|
||||
if (out.writeArrayB(value.remaining(), ByteSimpledCoder.instance, value) < 0) {
|
||||
if (out.writeArrayB(value.remaining(), this, ByteSimpledCoder.instance, value) < 0) {
|
||||
boolean flag = false;
|
||||
for (byte v : value.array()) {
|
||||
if (flag) out.writeArrayMark();
|
||||
|
||||
@@ -29,7 +29,7 @@ public final class CharArraySimpledCoder<R extends Reader, W extends Writer> ext
|
||||
out.writeNull();
|
||||
return;
|
||||
}
|
||||
if (out.writeArrayB(values.length, CharSimpledCoder.instance, values) < 0) {
|
||||
if (out.writeArrayB(values.length, this, CharSimpledCoder.instance, values) < 0) {
|
||||
boolean flag = false;
|
||||
for (char v : values) {
|
||||
if (flag) out.writeArrayMark();
|
||||
|
||||
@@ -30,7 +30,7 @@ public final class DoubleArraySimpledCoder<R extends Reader, W extends Writer> e
|
||||
out.writeNull();
|
||||
return;
|
||||
}
|
||||
if (out.writeArrayB(values.length, DoubleSimpledCoder.instance, values) < 0) {
|
||||
if (out.writeArrayB(values.length, this, DoubleSimpledCoder.instance, values) < 0) {
|
||||
boolean flag = false;
|
||||
for (double v : values) {
|
||||
if (flag) out.writeArrayMark();
|
||||
|
||||
@@ -22,8 +22,6 @@ import org.redkale.convert.Writer;
|
||||
*/
|
||||
public final class EnumSimpledCoder<R extends Reader, W extends Writer, E extends Enum> extends SimpledCoder<R, W, E> {
|
||||
|
||||
private final Class<E> type;
|
||||
|
||||
public EnumSimpledCoder(Class<E> type) {
|
||||
this.type = type;
|
||||
}
|
||||
@@ -42,11 +40,11 @@ public final class EnumSimpledCoder<R extends Reader, W extends Writer, E extend
|
||||
public E convertFrom(final R in) {
|
||||
String value = in.readSmallString();
|
||||
if (value == null) return null;
|
||||
return (E) Enum.valueOf(type, value);
|
||||
return (E) Enum.valueOf((Class<E>) type, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<E> getType() {
|
||||
return type;
|
||||
return (Class<E>) type;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ import org.redkale.convert.*;
|
||||
*/
|
||||
public class FileSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, File> {
|
||||
|
||||
public static final PatternSimpledCoder instance = new PatternSimpledCoder();
|
||||
public static final FileSimpledCoder instance = new FileSimpledCoder();
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, File value) {
|
||||
|
||||
@@ -29,7 +29,7 @@ public final class FloatArraySimpledCoder<R extends Reader, W extends Writer> ex
|
||||
out.writeNull();
|
||||
return;
|
||||
}
|
||||
if (out.writeArrayB(values.length, FloatSimpledCoder.instance, values) < 0) {
|
||||
if (out.writeArrayB(values.length, this, FloatSimpledCoder.instance, values) < 0) {
|
||||
boolean flag = false;
|
||||
for (float v : values) {
|
||||
if (flag) out.writeArrayMark();
|
||||
|
||||
@@ -30,7 +30,7 @@ public final class IntArraySimpledCoder<R extends Reader, W extends Writer> exte
|
||||
out.writeNull();
|
||||
return;
|
||||
}
|
||||
if (out.writeArrayB(values.length, IntSimpledCoder.instance, values) < 0) {
|
||||
if (out.writeArrayB(values.length, this, IntSimpledCoder.instance, values) < 0) {
|
||||
boolean flag = false;
|
||||
for (int v : values) {
|
||||
if (flag) out.writeArrayMark();
|
||||
|
||||
@@ -30,7 +30,7 @@ public final class LongArraySimpledCoder<R extends Reader, W extends Writer> ext
|
||||
out.writeNull();
|
||||
return;
|
||||
}
|
||||
if (out.writeArrayB(values.length, LongSimpledCoder.instance, values) < 0) {
|
||||
if (out.writeArrayB(values.length, this, LongSimpledCoder.instance, values) < 0) {
|
||||
boolean flag = false;
|
||||
for (long v : values) {
|
||||
if (flag) out.writeArrayMark();
|
||||
|
||||
@@ -29,7 +29,7 @@ public final class ShortArraySimpledCoder<R extends Reader, W extends Writer> ex
|
||||
out.writeNull();
|
||||
return;
|
||||
}
|
||||
if (out.writeArrayB(values.length, ShortSimpledCoder.instance, values) < 0) {
|
||||
if (out.writeArrayB(values.length, this, ShortSimpledCoder.instance, values) < 0) {
|
||||
boolean flag = false;
|
||||
for (short v : values) {
|
||||
if (flag) out.writeArrayMark();
|
||||
|
||||
@@ -27,7 +27,7 @@ public final class StringArraySimpledCoder<R extends Reader, W extends Writer> e
|
||||
out.writeNull();
|
||||
return;
|
||||
}
|
||||
if (out.writeArrayB(values.length, StringSimpledCoder.instance, values) < 0) {
|
||||
if (out.writeArrayB(values.length, this, StringSimpledCoder.instance, values) < 0) {
|
||||
boolean flag = false;
|
||||
for (String v : values) {
|
||||
if (flag) out.writeArrayMark();
|
||||
|
||||
@@ -12,7 +12,9 @@ import org.redkale.convert.Writer;
|
||||
/**
|
||||
* String 的SimpledCoder实现
|
||||
*
|
||||
* <p> 详情见: https://redkale.org
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @param <R> Reader输入的子类型
|
||||
* @param <W> Writer输出的子类型
|
||||
@@ -31,4 +33,19 @@ public final class StringSimpledCoder<R extends Reader, W extends Writer> extend
|
||||
return in.readString();
|
||||
}
|
||||
|
||||
public final static class SmallStringSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, String> {
|
||||
|
||||
public static final SmallStringSimpledCoder instance = new SmallStringSimpledCoder();
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, String value) {
|
||||
out.writeSmallString(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertFrom(R in) {
|
||||
return in.readSmallString();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
package org.redkale.convert.ext;
|
||||
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.util.StringWrapper;
|
||||
|
||||
/**
|
||||
* String 的SimpledCoder实现
|
||||
@@ -17,18 +18,18 @@ import org.redkale.convert.*;
|
||||
* @param <R> Reader输入的子类型
|
||||
* @param <W> Writer输出的子类型
|
||||
*/
|
||||
public final class StringConvertWrapperSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, StringConvertWrapper> {
|
||||
public final class StringWrapperSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, StringWrapper> {
|
||||
|
||||
public static final StringConvertWrapperSimpledCoder instance = new StringConvertWrapperSimpledCoder();
|
||||
public static final StringWrapperSimpledCoder instance = new StringWrapperSimpledCoder();
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, StringConvertWrapper value) {
|
||||
public void convertTo(W out, StringWrapper value) {
|
||||
out.writeWrapper(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public StringConvertWrapper convertFrom(R in) {
|
||||
return new StringConvertWrapper(in.readString());
|
||||
public StringWrapper convertFrom(R in) {
|
||||
return new StringWrapper(in.readString());
|
||||
}
|
||||
|
||||
}
|
||||
40
src/org/redkale/convert/ext/ThrowableSimpledCoder.java
Normal file
40
src/org/redkale/convert/ext/ThrowableSimpledCoder.java
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.convert.ext;
|
||||
|
||||
import org.redkale.convert.*;
|
||||
|
||||
/**
|
||||
* 文件 的SimpledCoder实现
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @param <R> Reader输入的子类型
|
||||
* @param <W> Writer输出的子类型
|
||||
*/
|
||||
public class ThrowableSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Throwable> {
|
||||
|
||||
public static final ThrowableSimpledCoder instance = new ThrowableSimpledCoder();
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, Throwable value) {
|
||||
if (value == null) {
|
||||
out.writeNull();
|
||||
} else {
|
||||
out.writeString(value.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Throwable convertFrom(R in) {
|
||||
String value = in.readString();
|
||||
if (value == null) return null;
|
||||
return new Exception(value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -69,6 +69,10 @@ public class JsonByteBufferReader extends JsonReader {
|
||||
*/
|
||||
@Override
|
||||
protected final char nextChar() {
|
||||
return nextChar(null);
|
||||
}
|
||||
|
||||
protected final char nextChar(StringBuilder sb) {
|
||||
if (currentChar != 0) {
|
||||
char ch = currentChar;
|
||||
this.currentChar = 0;
|
||||
@@ -78,14 +82,18 @@ public class JsonByteBufferReader extends JsonReader {
|
||||
int remain = this.currentBuffer.remaining();
|
||||
if (remain == 0 && this.currentIndex + 1 >= this.buffers.length) return 0;
|
||||
}
|
||||
byte b1 = nextByte();
|
||||
if (b1 >= 0) {// 1 byte, 7 bits: 0xxxxxxx
|
||||
return (char) b1;
|
||||
} else if ((b1 >> 5) == -2 && (b1 & 0x1e) != 0) { // 2 bytes, 11 bits: 110xxxxx 10xxxxxx
|
||||
return (char) (((b1 << 6) ^ nextByte()) ^ (((byte) 0xC0 << 6) ^ ((byte) 0x80)));
|
||||
} else if ((b1 >> 4) == -2) { // 3 bytes, 16 bits: 1110xxxx 10xxxxxx 10xxxxxx
|
||||
return (char) ((b1 << 12) ^ (nextByte() << 6) ^ (nextByte() ^ (((byte) 0xE0 << 12) ^ ((byte) 0x80 << 6) ^ ((byte) 0x80))));
|
||||
} else { // 4 bytes, 21 bits: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
||||
byte b = nextByte();
|
||||
if (b >= 0) {// 1 byte, 7 bits: 0xxxxxxx
|
||||
return (char) b;
|
||||
} else if ((b >> 5) == -2 && (b & 0x1e) != 0) { // 2 bytes, 11 bits: 110xxxxx 10xxxxxx
|
||||
return (char) (((b << 6) ^ nextByte()) ^ (((byte) 0xC0 << 6) ^ ((byte) 0x80)));
|
||||
} else if ((b >> 4) == -2) { // 3 bytes, 16 bits: 1110xxxx 10xxxxxx 10xxxxxx
|
||||
return (char) ((b << 12) ^ (nextByte() << 6) ^ (nextByte() ^ (((byte) 0xE0 << 12) ^ ((byte) 0x80 << 6) ^ ((byte) 0x80))));
|
||||
} else if ((b >> 3) == -2) {// 4 bytes, 21 bits: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
||||
int uc = ((b << 18) ^ (nextByte() << 12) ^ (nextByte() << 6) ^ (nextByte() ^ (((byte) 0xF0 << 18) ^ ((byte) 0x80 << 12) ^ ((byte) 0x80 << 6) ^ ((byte) 0x80))));
|
||||
if (sb != null) sb.append(Character.highSurrogate(uc));
|
||||
return Character.lowSurrogate(uc);
|
||||
} else {
|
||||
throw new RuntimeException(new UnmappableCharacterException(4));
|
||||
}
|
||||
}
|
||||
@@ -208,9 +216,9 @@ public class JsonByteBufferReader extends JsonReader {
|
||||
if (ch == '"' || ch == '\'') {
|
||||
final char quote = ch;
|
||||
for (;;) {
|
||||
ch = nextChar();
|
||||
ch = nextChar(sb);
|
||||
if (ch == '\\') {
|
||||
char c = nextChar();
|
||||
char c = nextChar(sb);
|
||||
switch (c) {
|
||||
case '"':
|
||||
case '\'':
|
||||
@@ -249,9 +257,9 @@ public class JsonByteBufferReader extends JsonReader {
|
||||
} else {
|
||||
sb.append(ch);
|
||||
for (;;) {
|
||||
ch = nextChar();
|
||||
ch = nextChar(sb);
|
||||
if (ch == '\\') {
|
||||
char c = nextChar();
|
||||
char c = nextChar(sb);
|
||||
switch (c) {
|
||||
case '"':
|
||||
case '\'':
|
||||
|
||||
@@ -22,6 +22,10 @@ import org.redkale.util.*;
|
||||
*/
|
||||
public class JsonByteBufferWriter extends JsonWriter {
|
||||
|
||||
private static final char[] CHARS_TUREVALUE = "true".toCharArray();
|
||||
|
||||
private static final char[] CHARS_FALSEVALUE = "false".toCharArray();
|
||||
|
||||
protected Charset charset;
|
||||
|
||||
private final Supplier<ByteBuffer> supplier;
|
||||
@@ -36,7 +40,7 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
|
||||
protected JsonByteBufferWriter(boolean tiny, Charset charset, Supplier<ByteBuffer> supplier) {
|
||||
this.tiny = tiny;
|
||||
this.charset = StandardCharsets.UTF_8.equals(charset) ? null : charset;
|
||||
this.charset = charset;
|
||||
this.supplier = supplier;
|
||||
}
|
||||
|
||||
@@ -48,6 +52,7 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
|
||||
@Override
|
||||
protected boolean recycle() {
|
||||
super.recycle();
|
||||
this.index = 0;
|
||||
this.specify = null;
|
||||
this.charset = null;
|
||||
@@ -55,7 +60,6 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer[] toBuffers() {
|
||||
if (buffers == null) return new ByteBuffer[0];
|
||||
for (int i = index; i < this.buffers.length; i++) {
|
||||
@@ -65,7 +69,6 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
return this.buffers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int count() {
|
||||
if (this.buffers == null) return 0;
|
||||
int len = 0;
|
||||
@@ -110,11 +113,37 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
writeTo(-1, false, chs, start, len);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(final byte ch) { //只能是 0 - 127 的字符
|
||||
expand(1);
|
||||
this.buffers[index].put(ch);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(final byte[] chs, final int start, final int len) { //只能是 0 - 127 的字符
|
||||
int expandsize = expand(len);
|
||||
if (expandsize == 0) { // 只需要一个buffer
|
||||
this.buffers[index].put(chs, start, len);
|
||||
} else {
|
||||
ByteBuffer buffer = this.buffers[index];
|
||||
int remain = len;
|
||||
int offset = start;
|
||||
while (remain > 0) {
|
||||
int bsize = Math.min(buffer.remaining(), remain);
|
||||
buffer.put(chs, offset, bsize);
|
||||
offset += bsize;
|
||||
remain -= bsize;
|
||||
if (remain < 1) break;
|
||||
buffer = nextByteBuffer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void writeTo(int expandsize, final boolean quote, final char[] chs, final int start, final int len) {
|
||||
int byteLength = quote ? 2 : 0;
|
||||
ByteBuffer bb = null;
|
||||
if (charset == null) {
|
||||
byteLength += encodeUTF8Length(chs, start, len);
|
||||
byteLength += Utility.encodeUTF8Length(chs, start, len);
|
||||
} else {
|
||||
bb = charset.encode(CharBuffer.wrap(chs, start, len));
|
||||
byteLength += bb.remaining();
|
||||
@@ -133,6 +162,13 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
} else if (c < 0x800) {
|
||||
buffer.put((byte) (0xc0 | (c >> 6)));
|
||||
buffer.put((byte) (0x80 | (c & 0x3f)));
|
||||
} else if (Character.isSurrogate(c)) { //连取两个
|
||||
int uc = Character.toCodePoint(c, chs[i + 1]);
|
||||
buffer.put((byte) (0xf0 | ((uc >> 18))));
|
||||
buffer.put((byte) (0x80 | ((uc >> 12) & 0x3f)));
|
||||
buffer.put((byte) (0x80 | ((uc >> 6) & 0x3f)));
|
||||
buffer.put((byte) (0x80 | (uc & 0x3f)));
|
||||
i++;
|
||||
} else {
|
||||
buffer.put((byte) (0xe0 | ((c >> 12))));
|
||||
buffer.put((byte) (0x80 | ((c >> 6) & 0x3f)));
|
||||
@@ -154,7 +190,34 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
if (charset == null) { //UTF-8
|
||||
final int limit = start + len;
|
||||
for (int i = start; i < limit; i++) {
|
||||
buffer = putUTF8Char(buffer, chs[i]);
|
||||
char c = chs[i];
|
||||
if (c < 0x80) {
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) c);
|
||||
} else if (c < 0x800) {
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0xc0 | (c >> 6)));
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0x80 | (c & 0x3f)));
|
||||
} else if (Character.isSurrogate(c)) { //连取两个
|
||||
int uc = Character.toCodePoint(c, chs[i + 1]);
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0xf0 | ((uc >> 18))));
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0x80 | ((uc >> 12) & 0x3f)));
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0x80 | ((uc >> 6) & 0x3f)));
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0x80 | (uc & 0x3f)));
|
||||
i++;
|
||||
} else {
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0xe0 | ((c >> 12))));
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0x80 | ((c >> 6) & 0x3f)));
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0x80 | (c & 0x3f)));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
while (bb.hasRemaining()) {
|
||||
@@ -168,50 +231,18 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
}
|
||||
}
|
||||
|
||||
private ByteBuffer putUTF8Char(ByteBuffer buffer, char c) {
|
||||
if (c < 0x80) {
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) c);
|
||||
} else if (c < 0x800) {
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0xc0 | (c >> 6)));
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0x80 | (c & 0x3f)));
|
||||
} else {
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0xe0 | ((c >> 12))));
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0x80 | ((c >> 6) & 0x3f)));
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0x80 | (c & 0x3f)));
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
private ByteBuffer nextByteBuffer() {
|
||||
this.buffers[this.index].flip();
|
||||
return this.buffers[++this.index];
|
||||
}
|
||||
|
||||
protected static int encodeUTF8Length(final char[] text, final int start, final int len) {
|
||||
char c;
|
||||
int size = 0;
|
||||
final char[] chars = text;
|
||||
final int limit = start + len;
|
||||
for (int i = start; i < limit; i++) {
|
||||
c = chars[i];
|
||||
size += (c < 0x80 ? 1 : (c < 0x800 ? 2 : 3));
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
protected static int encodeEscapeUTF8Length(final char[] text, final int start, final int len) {
|
||||
char c;
|
||||
int size = 0;
|
||||
final char[] chars = text;
|
||||
final char[] chs = text;
|
||||
final int limit = start + len;
|
||||
for (int i = start; i < limit; i++) {
|
||||
c = chars[i];
|
||||
c = chs[i];
|
||||
switch (c) {
|
||||
case '\n': size += 2;
|
||||
break;
|
||||
@@ -224,7 +255,7 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
case '"': size += 2;
|
||||
break;
|
||||
default:
|
||||
size += (c < 0x80 ? 1 : (c < 0x800 ? 2 : 3));
|
||||
size += (c < 0x80 ? 1 : (c < 0x800 || Character.isSurrogate(c) ? 2 : 3));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -238,19 +269,44 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
* @param value String值
|
||||
*/
|
||||
@Override
|
||||
public void writeTo(final boolean quote, final String value) {
|
||||
char[] chs = Utility.charArray(value);
|
||||
writeTo(-1, quote, chs, 0, chs.length);
|
||||
public void writeLatin1To(final boolean quote, final String value) {
|
||||
byte[] bs = Utility.byteArray(value);
|
||||
int expandsize = expand(bs.length + (quote ? 2 : 0));
|
||||
if (expandsize == 0) {// 只需要一个buffer
|
||||
final ByteBuffer buffer = this.buffers[index];
|
||||
if (quote) buffer.put((byte) '"');
|
||||
buffer.put(bs);
|
||||
if (quote) buffer.put((byte) '"');
|
||||
} else {
|
||||
ByteBuffer buffer = this.buffers[index];
|
||||
if (quote) {
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) '"');
|
||||
}
|
||||
for (byte b : bs) {
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put(b);
|
||||
}
|
||||
if (quote) {
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) '"');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBoolean(boolean value) {
|
||||
writeTo(value ? CHARS_TUREVALUE : CHARS_FALSEVALUE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeInt(int value) {
|
||||
writeTo(false, String.valueOf(value));
|
||||
writeLatin1To(false, String.valueOf(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeLong(long value) {
|
||||
writeTo(false, String.valueOf(value));
|
||||
writeLatin1To(false, String.valueOf(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -288,7 +344,8 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
if (expandsize == 0) { // 只需要一个buffer
|
||||
final ByteBuffer buffer = this.buffers[index];
|
||||
buffer.put((byte) '"');
|
||||
for (char c : chs) {
|
||||
for (int i = 0; i < chs.length; i++) {
|
||||
char c = chs[i];
|
||||
switch (c) {
|
||||
case '\n': buffer.put((byte) '\\').put((byte) 'n');
|
||||
break;
|
||||
@@ -306,6 +363,13 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
} else if (c < 0x800) {
|
||||
buffer.put((byte) (0xc0 | (c >> 6)));
|
||||
buffer.put((byte) (0x80 | (c & 0x3f)));
|
||||
} else if (Character.isSurrogate(c)) { //连取两个
|
||||
int uc = Character.toCodePoint(c, chs[i + 1]);
|
||||
buffer.put((byte) (0xf0 | ((uc >> 18))));
|
||||
buffer.put((byte) (0x80 | ((uc >> 12) & 0x3f)));
|
||||
buffer.put((byte) (0x80 | ((uc >> 6) & 0x3f)));
|
||||
buffer.put((byte) (0x80 | (uc & 0x3f)));
|
||||
i++;
|
||||
} else {
|
||||
buffer.put((byte) (0xe0 | ((c >> 12))));
|
||||
buffer.put((byte) (0x80 | ((c >> 6) & 0x3f)));
|
||||
|
||||
419
src/org/redkale/convert/json/JsonBytesWriter.java
Normal file
419
src/org/redkale/convert/json/JsonBytesWriter.java
Normal file
@@ -0,0 +1,419 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.convert.json;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.function.Consumer;
|
||||
import org.redkale.convert.*;
|
||||
import static org.redkale.convert.json.JsonWriter.*;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* writeTo系列的方法输出的字符不能含特殊字符
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public class JsonBytesWriter extends JsonWriter implements ByteTuple {
|
||||
|
||||
private static final boolean greatejdk8 = Utility.greaterJDK8();
|
||||
|
||||
private static final byte[] BYTES_TUREVALUE = "true".getBytes();
|
||||
|
||||
private static final byte[] BYTES_FALSEVALUE = "false".getBytes();
|
||||
|
||||
private static final int TENTHOUSAND_MAX = 10001;
|
||||
|
||||
private static final byte[][] TENTHOUSAND_BYTES = new byte[TENTHOUSAND_MAX][];
|
||||
|
||||
static {
|
||||
for (int i = 0; i < TENTHOUSAND_BYTES.length; i++) {
|
||||
TENTHOUSAND_BYTES[i] = String.valueOf(i).getBytes();
|
||||
}
|
||||
}
|
||||
|
||||
private int count;
|
||||
|
||||
private byte[] content;
|
||||
|
||||
public JsonBytesWriter() {
|
||||
this(defaultSize);
|
||||
}
|
||||
|
||||
public JsonBytesWriter(int size) {
|
||||
this.content = new byte[size > 1024 ? size : 1024];
|
||||
}
|
||||
|
||||
public JsonBytesWriter(ByteArray array) {
|
||||
this.content = array.content();
|
||||
this.count = array.length();
|
||||
}
|
||||
|
||||
public JsonBytesWriter(boolean tiny, ByteArray array) {
|
||||
this.tiny = tiny;
|
||||
this.content = array.content();
|
||||
this.count = array.length();
|
||||
}
|
||||
|
||||
protected byte[] expand(int len) {
|
||||
int newcount = count + len;
|
||||
if (newcount <= content.length) return content;
|
||||
byte[] newdata = new byte[Math.max(content.length * 3 / 2, newcount)];
|
||||
System.arraycopy(content, 0, newdata, 0, count);
|
||||
this.content = newdata;
|
||||
return newdata;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] content() {
|
||||
return content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int offset() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int length() {
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将本对象的内容引用复制给array
|
||||
*
|
||||
* @param array ByteArray
|
||||
*/
|
||||
public void directTo(ByteArray array) {
|
||||
array.directFrom(content, count);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void writeFieldName(EnMember member, String fieldName, Type fieldType, int fieldPos) {
|
||||
if (this.comma) writeTo(',');
|
||||
if (member != null) {
|
||||
byte[] bs = member.getJsonFieldNameBytes();
|
||||
expand(bs.length);
|
||||
System.arraycopy(bs, 0, content, count, bs.length);
|
||||
count += bs.length;
|
||||
} else {
|
||||
writeLatin1To(true, fieldName);
|
||||
writeTo(':');
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(final char ch) { //只能是 0 - 127 的字符
|
||||
expand(1);
|
||||
content[count++] = (byte) ch;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(final char[] chs, final int start, final int len) { //只能是 0 - 127 的字符
|
||||
expand(len);
|
||||
for (int i = 0; i < len; i++) {
|
||||
content[count + i] = (byte) chs[start + i];
|
||||
}
|
||||
count += len;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(final byte ch) { //只能是 0 - 127 的字符
|
||||
expand(1);
|
||||
content[count++] = ch;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(final byte[] chs, final int start, final int len) { //只能是 0 - 127 的字符
|
||||
expand(len);
|
||||
System.arraycopy(chs, start, content, count, len);
|
||||
count += len;
|
||||
}
|
||||
|
||||
/**
|
||||
* <b>注意:</b> 该String值不能为null且不会进行转义, 只用于不含需要转义字符的字符串,例如enum、double、BigInteger转换的String
|
||||
*
|
||||
* @param quote 是否加双引号
|
||||
* @param value 非null且不含需要转义的字符的String值
|
||||
*/
|
||||
@Override
|
||||
public void writeLatin1To(final boolean quote, final String value) {
|
||||
byte[] bs = Utility.byteArray(value);
|
||||
int len = bs.length;
|
||||
if (quote) {
|
||||
byte[] src = expand(len + 2);
|
||||
src[count++] = '"';
|
||||
System.arraycopy(bs, 0, src, count, bs.length);
|
||||
count += len;
|
||||
src[count++] = '"';
|
||||
} else {
|
||||
byte[] src = expand(len);
|
||||
System.arraycopy(bs, 0, src, count, bs.length);
|
||||
count += len;
|
||||
}
|
||||
}
|
||||
|
||||
public JsonBytesWriter clear() {
|
||||
this.count = 0;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean recycle() {
|
||||
super.recycle();
|
||||
this.count = 0;
|
||||
this.specify = null;
|
||||
if (this.content != null && this.content.length > defaultSize * 100) {
|
||||
this.content = new byte[defaultSize];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接获取全部数据, 实际数据需要根据count长度来截取
|
||||
*
|
||||
* @return byte[]
|
||||
*/
|
||||
public byte[] directBytes() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void completed(ConvertBytesHandler handler, Consumer<JsonBytesWriter> callback) {
|
||||
handler.completed(content, 0, count, callback, this);
|
||||
}
|
||||
|
||||
public byte[] toBytes() {
|
||||
byte[] copy = new byte[count];
|
||||
System.arraycopy(content, 0, copy, 0, count);
|
||||
return copy;
|
||||
}
|
||||
|
||||
public int count() {
|
||||
return this.count;
|
||||
}
|
||||
|
||||
private void writeEscapeLatinString(byte[] value) {
|
||||
byte[] bytes = expand(value.length * 2 + 2);
|
||||
int curr = count;
|
||||
bytes[curr++] = '"';
|
||||
for (byte b : value) {
|
||||
if (b == '"') {
|
||||
bytes[curr++] = '\\';
|
||||
bytes[curr++] = '"';
|
||||
} else if (b == '\\') {
|
||||
bytes[curr++] = '\\';
|
||||
bytes[curr++] = '\\';
|
||||
} else if (b < 32) {
|
||||
if (b == '\n') {
|
||||
bytes[curr++] = '\\';
|
||||
bytes[curr++] = 'n';
|
||||
} else if (b == '\r') {
|
||||
bytes[curr++] = '\\';
|
||||
bytes[curr++] = 'r';
|
||||
} else if (b == '\t') {
|
||||
bytes[curr++] = '\\';
|
||||
bytes[curr++] = 't';
|
||||
} else {
|
||||
bytes[curr++] = b;
|
||||
}
|
||||
} else {
|
||||
bytes[curr++] = b;
|
||||
}
|
||||
}
|
||||
bytes[curr++] = '"';
|
||||
count = curr;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeString(String value) {
|
||||
if (value == null) {
|
||||
writeNull();
|
||||
return;
|
||||
}
|
||||
if (greatejdk8 && Utility.isLatin1(value)) {
|
||||
writeEscapeLatinString(Utility.byteArray(value));
|
||||
return;
|
||||
}
|
||||
byte[] bytes = expand(value.length() * 4 + 2);
|
||||
int curr = count;
|
||||
bytes[curr++] = '"';
|
||||
int len = value.length();
|
||||
for (int i = 0; i < len; i++) {
|
||||
char ch = value.charAt(i);
|
||||
switch (ch) {
|
||||
case '\n':
|
||||
bytes[curr++] = '\\';
|
||||
bytes[curr++] = 'n';
|
||||
break;
|
||||
case '\r':
|
||||
bytes[curr++] = '\\';
|
||||
bytes[curr++] = 'r';
|
||||
break;
|
||||
case '\t':
|
||||
bytes[curr++] = '\\';
|
||||
bytes[curr++] = 't';
|
||||
break;
|
||||
case '\\':
|
||||
bytes[curr++] = '\\';
|
||||
bytes[curr++] = '\\';
|
||||
break;
|
||||
case '"':
|
||||
bytes[curr++] = '\\';
|
||||
bytes[curr++] = '"';
|
||||
break;
|
||||
default:
|
||||
if (ch < 0x80) {
|
||||
bytes[curr++] = (byte) ch;
|
||||
} else if (ch < 0x800) {
|
||||
bytes[curr++] = (byte) (0xc0 | (ch >> 6));
|
||||
bytes[curr++] = (byte) (0x80 | (ch & 0x3f));
|
||||
} else if (Character.isSurrogate(ch)) { //连取两个
|
||||
int uc = Character.toCodePoint(ch, value.charAt(++i));
|
||||
bytes[curr++] = (byte) (0xf0 | ((uc >> 18)));
|
||||
bytes[curr++] = (byte) (0x80 | ((uc >> 12) & 0x3f));
|
||||
bytes[curr++] = (byte) (0x80 | ((uc >> 6) & 0x3f));
|
||||
bytes[curr++] = (byte) (0x80 | (uc & 0x3f));
|
||||
} else {
|
||||
bytes[curr++] = (byte) (0xe0 | ((ch >> 12)));
|
||||
bytes[curr++] = (byte) (0x80 | ((ch >> 6) & 0x3f));
|
||||
bytes[curr++] = (byte) (0x80 | (ch & 0x3f));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
bytes[curr++] = '"';
|
||||
count = curr;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new String(content, 0, count, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------------
|
||||
@Override
|
||||
public void writeBoolean(boolean value) {
|
||||
byte[] bs = value ? BYTES_TUREVALUE : BYTES_FALSEVALUE;
|
||||
expand(bs.length);
|
||||
System.arraycopy(bs, 0, content, count, bs.length);
|
||||
count += bs.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeInt(int value) {
|
||||
if (value >= 0 && value < TENTHOUSAND_MAX) {
|
||||
byte[] bs = TENTHOUSAND_BYTES[value];
|
||||
expand(bs.length);
|
||||
System.arraycopy(bs, 0, content, count, bs.length);
|
||||
count += bs.length;
|
||||
return;
|
||||
}
|
||||
final char sign = value >= 0 ? 0 : '-';
|
||||
if (value < 0) value = -value;
|
||||
int size;
|
||||
for (int i = 0;; i++) {
|
||||
if (value <= sizeTable[i]) {
|
||||
size = i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (sign != 0) size++; //负数
|
||||
byte[] bytes = expand(size);
|
||||
|
||||
int q, r;
|
||||
int charPos = count + size;
|
||||
|
||||
// Generate two digits per iteration
|
||||
while (value >= 65536) {
|
||||
q = value / 100;
|
||||
// really: r = i - (q * 100);
|
||||
r = value - ((q << 6) + (q << 5) + (q << 2));
|
||||
value = q;
|
||||
bytes[--charPos] = (byte) DigitOnes[r];
|
||||
bytes[--charPos] = (byte) DigitTens[r];
|
||||
}
|
||||
|
||||
// Fall thru to fast mode for smaller numbers
|
||||
// assert(i <= 65536, i);
|
||||
for (;;) {
|
||||
q = (value * 52429) >>> (16 + 3);
|
||||
r = value - ((q << 3) + (q << 1)); // r = i-(q*10) ...
|
||||
bytes[--charPos] = (byte) digits[r];
|
||||
value = q;
|
||||
if (value == 0) break;
|
||||
}
|
||||
if (sign != 0) bytes[--charPos] = (byte) sign;
|
||||
count += size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeLong(long value) {
|
||||
if (value >= 0 && value < TENTHOUSAND_MAX) {
|
||||
byte[] bs = TENTHOUSAND_BYTES[(int) value];
|
||||
expand(bs.length);
|
||||
System.arraycopy(bs, 0, content, count, bs.length);
|
||||
count += bs.length;
|
||||
return;
|
||||
}
|
||||
final char sign = value >= 0 ? 0 : '-';
|
||||
if (value < 0) value = -value;
|
||||
int size = 19;
|
||||
long p = 10;
|
||||
for (int i = 1; i < 19; i++) {
|
||||
if (value < p) {
|
||||
size = i;
|
||||
break;
|
||||
}
|
||||
p = 10 * p;
|
||||
}
|
||||
if (sign != 0) size++; //负数
|
||||
byte[] bytes = expand(size);
|
||||
|
||||
long q;
|
||||
int r;
|
||||
int charPos = count + size;
|
||||
|
||||
// Get 2 digits/iteration using longs until quotient fits into an int
|
||||
while (value > Integer.MAX_VALUE) {
|
||||
q = value / 100;
|
||||
// really: r = i - (q * 100);
|
||||
r = (int) (value - ((q << 6) + (q << 5) + (q << 2)));
|
||||
value = q;
|
||||
content[--charPos] = (byte) DigitOnes[r];
|
||||
content[--charPos] = (byte) DigitTens[r];
|
||||
}
|
||||
|
||||
// Get 2 digits/iteration using ints
|
||||
int q2;
|
||||
int i2 = (int) value;
|
||||
while (i2 >= 65536) {
|
||||
q2 = i2 / 100;
|
||||
// really: r = i2 - (q * 100);
|
||||
r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2));
|
||||
i2 = q2;
|
||||
bytes[--charPos] = (byte) DigitOnes[r];
|
||||
bytes[--charPos] = (byte) DigitTens[r];
|
||||
}
|
||||
|
||||
// Fall thru to fast mode for smaller numbers
|
||||
// assert(i2 <= 65536, i2);
|
||||
for (;;) {
|
||||
q2 = (i2 * 52429) >>> (16 + 3);
|
||||
r = i2 - ((q2 << 3) + (q2 << 1)); // r = i2-(q2*10) ...
|
||||
bytes[--charPos] = (byte) digits[r];
|
||||
i2 = q2;
|
||||
if (i2 == 0) break;
|
||||
}
|
||||
if (sign != 0) bytes[--charPos] = (byte) sign;
|
||||
count += size;
|
||||
}
|
||||
|
||||
}
|
||||
285
src/org/redkale/convert/json/JsonCharsWriter.java
Normal file
285
src/org/redkale/convert/json/JsonCharsWriter.java
Normal file
@@ -0,0 +1,285 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.convert.json;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* writeTo系列的方法输出的字符不能含特殊字符
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public class JsonCharsWriter extends JsonWriter {
|
||||
|
||||
private static final char[] CHARS_TUREVALUE = "true".toCharArray();
|
||||
|
||||
private static final char[] CHARS_FALSEVALUE = "false".toCharArray();
|
||||
|
||||
private static final int TENTHOUSAND_MAX = 10001;
|
||||
|
||||
private static final char[][] TENTHOUSAND_BYTES = new char[TENTHOUSAND_MAX][];
|
||||
|
||||
static {
|
||||
for (int i = 0; i < TENTHOUSAND_BYTES.length; i++) {
|
||||
TENTHOUSAND_BYTES[i] = String.valueOf(i).toCharArray();
|
||||
}
|
||||
}
|
||||
|
||||
private int count;
|
||||
|
||||
private char[] content;
|
||||
|
||||
public JsonCharsWriter() {
|
||||
this(defaultSize);
|
||||
}
|
||||
|
||||
public JsonCharsWriter(int size) {
|
||||
this.content = new char[size > 1024 ? size : 1024];
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* 返回指定至少指定长度的缓冲区
|
||||
*
|
||||
* @param len
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private char[] expand(int len) {
|
||||
int newcount = count + len;
|
||||
if (newcount <= content.length) return content;
|
||||
char[] newdata = new char[Math.max(content.length * 3 / 2, newcount)];
|
||||
System.arraycopy(content, 0, newdata, 0, count);
|
||||
this.content = newdata;
|
||||
return newdata;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(final char ch) { //只能是 0 - 127 的字符
|
||||
expand(1);
|
||||
content[count++] = ch;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(final char[] chs, final int start, final int len) { //只能是 0 - 127 的字符
|
||||
expand(len);
|
||||
System.arraycopy(chs, start, content, count, len);
|
||||
count += len;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(final byte ch) { //只能是 0 - 127 的字符
|
||||
expand(1);
|
||||
content[count++] = (char) ch;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(final byte[] chs, final int start, final int len) { //只能是 0 - 127 的字符
|
||||
expand(len);
|
||||
for (int i = 0; i < len; i++) {
|
||||
content[count + i] = (char) chs[start + i];
|
||||
}
|
||||
count += len;
|
||||
}
|
||||
|
||||
/**
|
||||
* <b>注意:</b> 该String值不能为null且不会进行转义, 只用于不含需要转义字符的字符串,例如enum、double、BigInteger转换的String
|
||||
*
|
||||
* @param quote 是否加双引号
|
||||
* @param value 非null且不含需要转义的字符的String值
|
||||
*/
|
||||
@Override
|
||||
public void writeLatin1To(final boolean quote, final String value) {
|
||||
int len = value.length();
|
||||
expand(len + (quote ? 2 : 0));
|
||||
if (quote) content[count++] = '"';
|
||||
value.getChars(0, len, content, count);
|
||||
count += len;
|
||||
if (quote) content[count++] = '"';
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean recycle() {
|
||||
super.recycle();
|
||||
this.count = 0;
|
||||
this.specify = null;
|
||||
if (this.content != null && this.content.length > defaultSize) {
|
||||
this.content = new char[defaultSize];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public ByteBuffer[] toBuffers() {
|
||||
return new ByteBuffer[]{ByteBuffer.wrap(Utility.encodeUTF8(content, 0, count))};
|
||||
}
|
||||
|
||||
public byte[] toBytes() {
|
||||
return Utility.encodeUTF8(content, 0, count);
|
||||
}
|
||||
|
||||
public int count() {
|
||||
return this.count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeString(String value) {
|
||||
if (value == null) {
|
||||
writeNull();
|
||||
return;
|
||||
}
|
||||
expand(value.length() * 2 + 2);
|
||||
content[count++] = '"';
|
||||
for (char ch : Utility.charArray(value)) {
|
||||
switch (ch) {
|
||||
case '\n':
|
||||
content[count++] = '\\';
|
||||
content[count++] = 'n';
|
||||
break;
|
||||
case '\r':
|
||||
content[count++] = '\\';
|
||||
content[count++] = 'r';
|
||||
break;
|
||||
case '\t':
|
||||
content[count++] = '\\';
|
||||
content[count++] = 't';
|
||||
break;
|
||||
case '\\':
|
||||
content[count++] = '\\';
|
||||
content[count++] = ch;
|
||||
break;
|
||||
case '"':
|
||||
content[count++] = '\\';
|
||||
content[count++] = ch;
|
||||
break;
|
||||
default:
|
||||
content[count++] = ch;
|
||||
break;
|
||||
}
|
||||
}
|
||||
content[count++] = '"';
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new String(content, 0, count);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------------
|
||||
@Override
|
||||
public void writeBoolean(boolean value) {
|
||||
writeTo(value ? CHARS_TUREVALUE : CHARS_FALSEVALUE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeInt(int value) {
|
||||
if (value >= 0 && value < TENTHOUSAND_MAX) {
|
||||
writeTo(TENTHOUSAND_BYTES[value]);
|
||||
return;
|
||||
}
|
||||
final char sign = value >= 0 ? 0 : '-';
|
||||
if (value < 0) value = -value;
|
||||
int size;
|
||||
for (int i = 0;; i++) {
|
||||
if (value <= sizeTable[i]) {
|
||||
size = i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (sign != 0) size++; //负数
|
||||
expand(size);
|
||||
|
||||
int q, r;
|
||||
int charPos = count + size;
|
||||
|
||||
// Generate two digits per iteration
|
||||
while (value >= 65536) {
|
||||
q = value / 100;
|
||||
// really: r = i - (q * 100);
|
||||
r = value - ((q << 6) + (q << 5) + (q << 2));
|
||||
value = q;
|
||||
content[--charPos] = DigitOnes[r];
|
||||
content[--charPos] = DigitTens[r];
|
||||
}
|
||||
|
||||
// Fall thru to fast mode for smaller numbers
|
||||
// assert(i <= 65536, i);
|
||||
for (;;) {
|
||||
q = (value * 52429) >>> (16 + 3);
|
||||
r = value - ((q << 3) + (q << 1)); // r = i-(q*10) ...
|
||||
content[--charPos] = digits[r];
|
||||
value = q;
|
||||
if (value == 0) break;
|
||||
}
|
||||
if (sign != 0) content[--charPos] = sign;
|
||||
count += size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeLong(long value) {
|
||||
if (value >= 0 && value < TENTHOUSAND_MAX) {
|
||||
writeTo(TENTHOUSAND_BYTES[(int) value]);
|
||||
return;
|
||||
}
|
||||
final char sign = value >= 0 ? 0 : '-';
|
||||
if (value < 0) value = -value;
|
||||
int size = 19;
|
||||
long p = 10;
|
||||
for (int i = 1; i < 19; i++) {
|
||||
if (value < p) {
|
||||
size = i;
|
||||
break;
|
||||
}
|
||||
p = 10 * p;
|
||||
}
|
||||
if (sign != 0) size++; //负数
|
||||
expand(size);
|
||||
|
||||
long q;
|
||||
int r;
|
||||
int charPos = count + size;
|
||||
|
||||
// Get 2 digits/iteration using longs until quotient fits into an int
|
||||
while (value > Integer.MAX_VALUE) {
|
||||
q = value / 100;
|
||||
// really: r = i - (q * 100);
|
||||
r = (int) (value - ((q << 6) + (q << 5) + (q << 2)));
|
||||
value = q;
|
||||
content[--charPos] = DigitOnes[r];
|
||||
content[--charPos] = DigitTens[r];
|
||||
}
|
||||
|
||||
// Get 2 digits/iteration using ints
|
||||
int q2;
|
||||
int i2 = (int) value;
|
||||
while (i2 >= 65536) {
|
||||
q2 = i2 / 100;
|
||||
// really: r = i2 - (q * 100);
|
||||
r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2));
|
||||
i2 = q2;
|
||||
content[--charPos] = DigitOnes[r];
|
||||
content[--charPos] = DigitTens[r];
|
||||
}
|
||||
|
||||
// Fall thru to fast mode for smaller numbers
|
||||
// assert(i2 <= 65536, i2);
|
||||
for (;;) {
|
||||
q2 = (i2 * 52429) >>> (16 + 3);
|
||||
r = i2 - ((q2 << 3) + (q2 << 1)); // r = i2-(q2*10) ...
|
||||
content[--charPos] = digits[r];
|
||||
i2 = q2;
|
||||
if (i2 == 0) break;
|
||||
}
|
||||
if (sign != 0) content[--charPos] = sign;
|
||||
count += size;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import java.nio.*;
|
||||
import java.nio.charset.*;
|
||||
import java.util.function.*;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.service.RetResult;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
@@ -21,17 +22,26 @@ import org.redkale.util.*;
|
||||
* @author zhangjx
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
|
||||
public static final Type TYPE_MAP_STRING_STRING = new TypeToken<java.util.HashMap<String, String>>() {
|
||||
}.getType();
|
||||
|
||||
private static final ObjectPool<JsonReader> readerPool = JsonReader.createPool(Integer.getInteger("convert.json.pool.size", 16));
|
||||
public static final Type TYPE_RETRESULT_STRING = new TypeToken<RetResult<String>>() {
|
||||
}.getType();
|
||||
|
||||
private static final ObjectPool<JsonWriter> writerPool = JsonWriter.createPool(Integer.getInteger("convert.json.pool.size", 16));
|
||||
private final ThreadLocal<JsonCharsWriter> charsWriterPool = ThreadLocal.withInitial(JsonCharsWriter::new);
|
||||
|
||||
private final ThreadLocal<JsonBytesWriter> bytesWriterPool = ThreadLocal.withInitial(JsonBytesWriter::new);
|
||||
|
||||
private final Consumer<JsonBytesWriter> offerBytesConsumer = w -> offerJsonBytesWriter(w);
|
||||
|
||||
private final boolean tiny;
|
||||
|
||||
private Encodeable lastConvertEncodeable;
|
||||
|
||||
private Decodeable lastConvertDecodeable;
|
||||
|
||||
protected JsonConvert(JsonFactory factory, boolean tiny) {
|
||||
super(factory);
|
||||
this.tiny = tiny;
|
||||
@@ -46,50 +56,69 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
return JsonFactory.root().getConvert();
|
||||
}
|
||||
|
||||
//------------------------------ reader -----------------------------------------------------------
|
||||
public JsonReader pollJsonReader(final ByteBuffer... buffers) {
|
||||
return new JsonByteBufferReader((ConvertMask) null, buffers);
|
||||
@Override
|
||||
public JsonConvert newConvert(final BiFunction<Attribute, Object, Object> fieldFunc) {
|
||||
return newConvert(fieldFunc, null);
|
||||
}
|
||||
|
||||
public JsonReader pollJsonReader(final InputStream in) {
|
||||
return new JsonStreamReader(in);
|
||||
}
|
||||
|
||||
public JsonReader pollJsonReader() {
|
||||
return readerPool.get();
|
||||
}
|
||||
|
||||
public void offerJsonReader(final JsonReader in) {
|
||||
if (in != null) readerPool.accept(in);
|
||||
@Override
|
||||
public JsonConvert newConvert(final BiFunction<Attribute, Object, Object> fieldFunc, Function<Object, ConvertField[]> objExtFunc) {
|
||||
return new JsonConvert(getFactory(), tiny) {
|
||||
@Override
|
||||
protected <S extends JsonWriter> S configWrite(S writer) {
|
||||
return fieldFunc(writer, fieldFunc, objExtFunc);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//------------------------------ writer -----------------------------------------------------------
|
||||
public JsonByteBufferWriter pollJsonWriter(final Supplier<ByteBuffer> supplier) {
|
||||
return new JsonByteBufferWriter(tiny, supplier);
|
||||
private JsonCharsWriter pollJsonCharsWriter() {
|
||||
JsonCharsWriter writer = charsWriterPool.get();
|
||||
if (writer == null) {
|
||||
writer = new JsonCharsWriter();
|
||||
} else {
|
||||
charsWriterPool.set(null);
|
||||
}
|
||||
return configWrite((JsonCharsWriter) writer.tiny(tiny));
|
||||
}
|
||||
|
||||
public JsonWriter pollJsonWriter(final OutputStream out) {
|
||||
return new JsonStreamWriter(tiny, out);
|
||||
private JsonBytesWriter pollJsonBytesWriter() {
|
||||
JsonBytesWriter writer = bytesWriterPool.get();
|
||||
if (writer == null) {
|
||||
writer = new JsonBytesWriter();
|
||||
} else {
|
||||
bytesWriterPool.set(null);
|
||||
}
|
||||
return configWrite((JsonBytesWriter) writer.tiny(tiny));
|
||||
}
|
||||
|
||||
public JsonWriter pollJsonWriter(final Charset charset, final OutputStream out) {
|
||||
return new JsonStreamWriter(tiny, charset, out);
|
||||
private void offerJsonCharsWriter(final JsonCharsWriter writer) {
|
||||
if (writer != null) {
|
||||
writer.recycle();
|
||||
charsWriterPool.set(writer);
|
||||
}
|
||||
}
|
||||
|
||||
public JsonWriter pollJsonWriter() {
|
||||
return writerPool.get().tiny(tiny);
|
||||
}
|
||||
|
||||
public void offerJsonWriter(final JsonWriter writer) {
|
||||
if (writer != null) writerPool.accept(writer);
|
||||
private void offerJsonBytesWriter(final JsonBytesWriter writer) {
|
||||
if (writer != null) {
|
||||
writer.recycle();
|
||||
bytesWriterPool.set(writer);
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------ convertFrom -----------------------------------------------------------
|
||||
@Override
|
||||
public <T> T convertFrom(final Type type, final byte[] bytes) {
|
||||
if (bytes == null) return null;
|
||||
return convertFrom(type, new String(bytes, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T convertFrom(final Type type, final byte[] bytes, final int offset, final int length) {
|
||||
if (bytes == null) return null;
|
||||
return convertFrom(type, new String(bytes, offset, length, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
public <T> T convertFrom(final Type type, final String text) {
|
||||
if (text == null) return null;
|
||||
return convertFrom(type, Utility.charArray(text));
|
||||
@@ -100,36 +129,58 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
return convertFrom(type, text, 0, text.length);
|
||||
}
|
||||
|
||||
public <T> T convertFrom(final Type type, final char[] text, final int start, final int len) {
|
||||
public <T> T convertFrom(final Type type, final char[] text, final int offset, final int length) {
|
||||
if (text == null || type == null) return null;
|
||||
final JsonReader in = readerPool.get();
|
||||
in.setText(text, start, len);
|
||||
T rs = (T) factory.loadDecoder(type).convertFrom(in);
|
||||
readerPool.accept(in);
|
||||
Decodeable decoder = this.lastConvertDecodeable;
|
||||
if (decoder == null || decoder.getType() != type) {
|
||||
decoder = factory.loadDecoder(type);
|
||||
this.lastConvertDecodeable = decoder;
|
||||
}
|
||||
T rs = (T) decoder.convertFrom(new JsonReader(text, offset, length));
|
||||
return rs;
|
||||
}
|
||||
|
||||
public <T> T convertFrom(final Type type, final InputStream in) {
|
||||
if (type == null || in == null) return null;
|
||||
return (T) factory.loadDecoder(type).convertFrom(new JsonStreamReader(in));
|
||||
Decodeable decoder = this.lastConvertDecodeable;
|
||||
if (decoder == null || decoder.getType() != type) {
|
||||
decoder = factory.loadDecoder(type);
|
||||
this.lastConvertDecodeable = decoder;
|
||||
}
|
||||
return (T) decoder.convertFrom(new JsonStreamReader(in));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T convertFrom(final Type type, final ByteBuffer... buffers) {
|
||||
if (type == null || buffers == null || buffers.length == 0) return null;
|
||||
return (T) factory.loadDecoder(type).convertFrom(new JsonByteBufferReader((ConvertMask) null, buffers));
|
||||
Decodeable decoder = this.lastConvertDecodeable;
|
||||
if (decoder == null || decoder.getType() != type) {
|
||||
decoder = factory.loadDecoder(type);
|
||||
this.lastConvertDecodeable = decoder;
|
||||
}
|
||||
return (T) decoder.convertFrom(new JsonByteBufferReader((ConvertMask) null, buffers));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T convertFrom(final Type type, final ConvertMask mask, final ByteBuffer... buffers) {
|
||||
if (type == null || buffers == null || buffers.length == 0) return null;
|
||||
return (T) factory.loadDecoder(type).convertFrom(new JsonByteBufferReader(mask, buffers));
|
||||
Decodeable decoder = this.lastConvertDecodeable;
|
||||
if (decoder == null || decoder.getType() != type) {
|
||||
decoder = factory.loadDecoder(type);
|
||||
this.lastConvertDecodeable = decoder;
|
||||
}
|
||||
return (T) decoder.convertFrom(new JsonByteBufferReader(mask, buffers));
|
||||
}
|
||||
|
||||
public <T> T convertFrom(final Type type, final JsonReader reader) {
|
||||
if (type == null) return null;
|
||||
Decodeable decoder = this.lastConvertDecodeable;
|
||||
if (decoder == null || decoder.getType() != type) {
|
||||
decoder = factory.loadDecoder(type);
|
||||
this.lastConvertDecodeable = decoder;
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
T rs = (T) factory.loadDecoder(type).convertFrom(reader);
|
||||
T rs = (T) decoder.convertFrom(reader);
|
||||
return rs;
|
||||
}
|
||||
|
||||
@@ -146,12 +197,12 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
}
|
||||
|
||||
//返回非null的值是由String、ArrayList、HashMap任意组合的对象
|
||||
public <V> V convertFrom(final char[] text, final int start, final int len) {
|
||||
public <V> V convertFrom(final char[] text, final int offset, final int length) {
|
||||
if (text == null) return null;
|
||||
final JsonReader in = readerPool.get();
|
||||
in.setText(text, start, len);
|
||||
Object rs = new AnyDecoder(factory).convertFrom(in);
|
||||
readerPool.accept(in);
|
||||
//final JsonReader in = readerPool.get();
|
||||
//in.setText(text, offset, length);
|
||||
Object rs = new AnyDecoder(factory).convertFrom(new JsonReader(text, offset, length));
|
||||
//readerPool.accept(in);
|
||||
return (V) rs;
|
||||
}
|
||||
|
||||
@@ -190,27 +241,91 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
public String convertTo(final Type type, final Object value) {
|
||||
if (type == null) return null;
|
||||
if (value == null) return "null";
|
||||
final JsonWriter writer = writerPool.get().tiny(tiny);
|
||||
writer.specify(type);
|
||||
factory.loadEncoder(type).convertTo(writer, value);
|
||||
JsonCharsWriter writer = pollJsonCharsWriter();
|
||||
Encodeable encoder = this.lastConvertEncodeable;
|
||||
if (encoder == null || encoder.getType() != type) {
|
||||
encoder = factory.loadEncoder(type);
|
||||
this.lastConvertEncodeable = encoder;
|
||||
}
|
||||
if (encoder.specifyable()) writer.specify(type);
|
||||
encoder.convertTo(writer, value);
|
||||
|
||||
String result = writer.toString();
|
||||
writerPool.accept(writer);
|
||||
offerJsonCharsWriter(writer);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertMapTo(final Object... values) {
|
||||
if (values == null) return "null";
|
||||
final JsonWriter writer = writerPool.get().tiny(tiny);
|
||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(writer, values);
|
||||
String result = writer.toString();
|
||||
writerPool.accept(writer);
|
||||
public byte[] convertToBytes(final Object value) {
|
||||
if (value == null) return null;
|
||||
return convertToBytes(value.getClass(), value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] convertToBytes(final Type type, final Object value) {
|
||||
if (type == null) return null;
|
||||
if (value == null) return null;
|
||||
JsonBytesWriter writer = pollJsonBytesWriter();
|
||||
Encodeable encoder = this.lastConvertEncodeable;
|
||||
if (encoder == null || encoder.getType() != type) {
|
||||
encoder = factory.loadEncoder(type);
|
||||
this.lastConvertEncodeable = encoder;
|
||||
}
|
||||
if (encoder.specifyable()) writer.specify(type);
|
||||
encoder.convertTo(writer, value);
|
||||
|
||||
byte[] result = writer.toBytes();
|
||||
offerJsonBytesWriter(writer);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void convertToBytes(final Object value, final ConvertBytesHandler handler) {
|
||||
convertToBytes(value == null ? null : value.getClass(), value, handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void convertToBytes(final Type type, final Object value, final ConvertBytesHandler handler) {
|
||||
JsonBytesWriter writer = pollJsonBytesWriter();
|
||||
if (type == null) {
|
||||
writer.writeNull();
|
||||
} else {
|
||||
Encodeable encoder = this.lastConvertEncodeable;
|
||||
if (encoder == null || encoder.getType() != type) {
|
||||
encoder = factory.loadEncoder(type);
|
||||
this.lastConvertEncodeable = encoder;
|
||||
}
|
||||
if (encoder.specifyable()) writer.specify(type);
|
||||
encoder.convertTo(writer, value);
|
||||
}
|
||||
writer.completed(handler, offerBytesConsumer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void convertToBytes(final ByteArray array, final Object value) {
|
||||
convertToBytes(array, value == null ? null : value.getClass(), value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void convertToBytes(final ByteArray array, final Type type, final Object value) {
|
||||
JsonBytesWriter writer = configWrite(new JsonBytesWriter(tiny, array));
|
||||
if (type == null) {
|
||||
writer.writeNull();
|
||||
} else {
|
||||
Encodeable encoder = this.lastConvertEncodeable;
|
||||
if (encoder == null || encoder.getType() != type) {
|
||||
encoder = factory.loadEncoder(type);
|
||||
this.lastConvertEncodeable = encoder;
|
||||
}
|
||||
if (encoder.specifyable()) writer.specify(type);
|
||||
encoder.convertTo(writer, value);
|
||||
}
|
||||
writer.directTo(array);
|
||||
}
|
||||
|
||||
public void convertTo(final OutputStream out, final Object value) {
|
||||
if (value == null) {
|
||||
new JsonStreamWriter(tiny, out).writeNull();
|
||||
configWrite(new JsonStreamWriter(tiny, out)).writeNull();
|
||||
} else {
|
||||
convertTo(out, value.getClass(), value);
|
||||
}
|
||||
@@ -219,41 +334,23 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
public void convertTo(final OutputStream out, final Type type, final Object value) {
|
||||
if (type == null) return;
|
||||
if (value == null) {
|
||||
new JsonStreamWriter(tiny, out).writeNull();
|
||||
configWrite(new JsonStreamWriter(tiny, out)).writeNull();
|
||||
} else {
|
||||
final JsonWriter writer = writerPool.get().tiny(tiny);
|
||||
writer.specify(type);
|
||||
factory.loadEncoder(type).convertTo(writer, value);
|
||||
byte[] bs = writer.toBytes();
|
||||
writerPool.accept(writer);
|
||||
try {
|
||||
out.write(bs);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void convertMapTo(final OutputStream out, final Object... values) {
|
||||
if (values == null) {
|
||||
new JsonStreamWriter(tiny, out).writeNull();
|
||||
} else {
|
||||
final JsonWriter writer = writerPool.get().tiny(tiny);
|
||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(writer, values);
|
||||
byte[] bs = writer.toBytes();
|
||||
writerPool.accept(writer);
|
||||
try {
|
||||
out.write(bs);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
JsonStreamWriter writer = configWrite(new JsonStreamWriter(tiny, out));
|
||||
Encodeable encoder = this.lastConvertEncodeable;
|
||||
if (encoder == null || encoder.getType() != type) {
|
||||
encoder = factory.loadEncoder(type);
|
||||
this.lastConvertEncodeable = encoder;
|
||||
}
|
||||
if (encoder.specifyable()) writer.specify(type);
|
||||
encoder.convertTo(writer, value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Object value) {
|
||||
if (supplier == null) return null;
|
||||
JsonByteBufferWriter out = new JsonByteBufferWriter(tiny, null, supplier);
|
||||
JsonByteBufferWriter out = configWrite(new JsonByteBufferWriter(tiny, supplier));
|
||||
if (value == null) {
|
||||
out.writeNull();
|
||||
} else {
|
||||
@@ -265,7 +362,7 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
@Override
|
||||
public ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Type type, final Object value) {
|
||||
if (supplier == null || type == null) return null;
|
||||
JsonByteBufferWriter out = new JsonByteBufferWriter(tiny, null, supplier);
|
||||
JsonByteBufferWriter out = configWrite(new JsonByteBufferWriter(tiny, supplier));
|
||||
if (value == null) {
|
||||
out.writeNull();
|
||||
} else {
|
||||
@@ -275,18 +372,6 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
return out.toBuffers();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer[] convertMapTo(final Supplier<ByteBuffer> supplier, final Object... values) {
|
||||
if (supplier == null) return null;
|
||||
JsonByteBufferWriter out = new JsonByteBufferWriter(tiny, null, supplier);
|
||||
if (values == null) {
|
||||
out.writeNull();
|
||||
} else {
|
||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(out, values);
|
||||
}
|
||||
return out.toBuffers();
|
||||
}
|
||||
|
||||
public void convertTo(final JsonWriter writer, final Object value) {
|
||||
if (value == null) {
|
||||
writer.writeNull();
|
||||
@@ -305,30 +390,4 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
}
|
||||
}
|
||||
|
||||
public void convertMapTo(final JsonWriter writer, final Object... values) {
|
||||
if (values == null) {
|
||||
writer.writeNull();
|
||||
} else {
|
||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(writer, values);
|
||||
}
|
||||
}
|
||||
|
||||
public JsonWriter convertToWriter(final Object value) {
|
||||
if (value == null) return null;
|
||||
return convertToWriter(value.getClass(), value);
|
||||
}
|
||||
|
||||
public JsonWriter convertToWriter(final Type type, final Object value) {
|
||||
if (type == null) return null;
|
||||
final JsonWriter writer = writerPool.get().tiny(tiny);
|
||||
writer.specify(type);
|
||||
factory.loadEncoder(type).convertTo(writer, value);
|
||||
return writer;
|
||||
}
|
||||
|
||||
public JsonWriter convertMapToWriter(final Object... values) {
|
||||
final JsonWriter writer = writerPool.get().tiny(tiny);
|
||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(writer, values);
|
||||
return writer;
|
||||
}
|
||||
}
|
||||
|
||||
564
src/org/redkale/convert/json/JsonDynEncoder.java
Normal file
564
src/org/redkale/convert/json/JsonDynEncoder.java
Normal file
@@ -0,0 +1,564 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.convert.json;
|
||||
|
||||
import java.lang.reflect.*;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.*;
|
||||
import org.redkale.asm.*;
|
||||
import static org.redkale.asm.ClassWriter.COMPUTE_FRAMES;
|
||||
import static org.redkale.asm.Opcodes.*;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.convert.ext.*;
|
||||
|
||||
/**
|
||||
* 简单对象的JSON序列化操作类
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @since 2.3.0
|
||||
*
|
||||
* @param <T> 序列化的数据类型
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public abstract class JsonDynEncoder<T> implements Encodeable<JsonWriter, T> {
|
||||
|
||||
protected final Class typeClass;
|
||||
|
||||
protected final ObjectEncoder<JsonWriter, T> objectEncoder;
|
||||
|
||||
protected JsonDynEncoder(final JsonFactory factory, Type type) {
|
||||
this.typeClass = (Class) type;
|
||||
factory.register(type, this);
|
||||
this.objectEncoder = factory.createObjectEncoder(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean specifyable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean checkMemberType(final JsonFactory factory, Type type, Class clazz) {
|
||||
if (type == String.class) return true;
|
||||
if (clazz.isPrimitive()) return true;
|
||||
if (clazz.isEnum()) return true;
|
||||
if (type == boolean[].class) return true;
|
||||
if (type == byte[].class) return true;
|
||||
if (type == short[].class) return true;
|
||||
if (type == char[].class) return true;
|
||||
if (type == int[].class) return true;
|
||||
if (type == float[].class) return true;
|
||||
if (type == long[].class) return true;
|
||||
if (type == double[].class) return true;
|
||||
if (type == Boolean[].class) return true;
|
||||
if (type == Byte[].class) return true;
|
||||
if (type == Short[].class) return true;
|
||||
if (type == Character[].class) return true;
|
||||
if (type == Integer[].class) return true;
|
||||
if (type == Float[].class) return true;
|
||||
if (type == Long[].class) return true;
|
||||
if (type == Double[].class) return true;
|
||||
if (type == String[].class) return true;
|
||||
if (Collection.class.isAssignableFrom(clazz) && type instanceof ParameterizedType) {
|
||||
Type[] ts = ((ParameterizedType) type).getActualTypeArguments();
|
||||
if (ts.length == 1) {
|
||||
Type t = ts[0];
|
||||
if (t == Boolean.class || t == Byte.class || t == Short.class || t == Character.class
|
||||
|| t == Integer.class || t == Float.class || t == Long.class || t == Double.class
|
||||
|| t == String.class || ((t instanceof Class) && ((Class) t).isEnum())) return true;
|
||||
if (factory.loadEncoder(t) instanceof JsonDynEncoder) return true;
|
||||
}
|
||||
}
|
||||
if (type instanceof TypeVariable) return false;
|
||||
try {
|
||||
if (factory.loadEncoder(type) instanceof JsonDynEncoder) return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//字段全部是primitive或String类型,且没有泛型的类才能动态生成JsonDynEncoder, 不支持的返回null
|
||||
public static JsonDynEncoder createDyncEncoder(final JsonFactory factory, final Type type) {
|
||||
if (!(type instanceof Class)) return null;
|
||||
//发现有自定义的基础数据类型Encoder就不动态生成JsonDynEncoder了
|
||||
if (factory.loadEncoder(boolean.class) != BoolSimpledCoder.instance) return null;
|
||||
if (factory.loadEncoder(byte.class) != ByteSimpledCoder.instance) return null;
|
||||
if (factory.loadEncoder(short.class) != ShortSimpledCoder.instance) return null;
|
||||
if (factory.loadEncoder(char.class) != CharSimpledCoder.instance) return null;
|
||||
if (factory.loadEncoder(int.class) != IntSimpledCoder.instance) return null;
|
||||
if (factory.loadEncoder(float.class) != FloatSimpledCoder.instance) return null;
|
||||
if (factory.loadEncoder(long.class) != LongSimpledCoder.instance) return null;
|
||||
if (factory.loadEncoder(double.class) != DoubleSimpledCoder.instance) return null;
|
||||
if (factory.loadEncoder(String.class) != StringSimpledCoder.instance) return null;
|
||||
//array
|
||||
if (factory.loadEncoder(boolean[].class) != BoolArraySimpledCoder.instance) return null;
|
||||
if (factory.loadEncoder(byte[].class) != ByteArraySimpledCoder.instance) return null;
|
||||
if (factory.loadEncoder(short[].class) != ShortArraySimpledCoder.instance) return null;
|
||||
if (factory.loadEncoder(char[].class) != CharArraySimpledCoder.instance) return null;
|
||||
if (factory.loadEncoder(int[].class) != IntArraySimpledCoder.instance) return null;
|
||||
if (factory.loadEncoder(float[].class) != FloatArraySimpledCoder.instance) return null;
|
||||
if (factory.loadEncoder(long[].class) != LongArraySimpledCoder.instance) return null;
|
||||
if (factory.loadEncoder(double[].class) != DoubleArraySimpledCoder.instance) return null;
|
||||
if (factory.loadEncoder(String[].class) != StringArraySimpledCoder.instance) return null;
|
||||
|
||||
final Class clazz = (Class) type;
|
||||
List<AccessibleObject> members = null;
|
||||
Set<String> names = new HashSet<>();
|
||||
try {
|
||||
ConvertColumnEntry ref;
|
||||
for (final Field field : clazz.getFields()) {
|
||||
if (Modifier.isStatic(field.getModifiers())) continue;
|
||||
if (factory.isConvertDisabled(field)) continue;
|
||||
ref = factory.findRef(clazz, field);
|
||||
if (ref != null && ref.ignore()) continue;
|
||||
if (!(checkMemberType(factory, field.getGenericType(), field.getType()))) return null;
|
||||
String name = convertFieldName(factory, clazz, field);
|
||||
if (names.contains(name)) continue;
|
||||
names.add(name);
|
||||
if (members == null) members = new ArrayList<>();
|
||||
members.add(field);
|
||||
}
|
||||
for (final Method method : clazz.getMethods()) {
|
||||
if (Modifier.isStatic(method.getModifiers())) continue;
|
||||
if (Modifier.isAbstract(method.getModifiers())) continue;
|
||||
if (method.isSynthetic()) continue;
|
||||
if (method.getName().length() < 3) continue;
|
||||
if (method.getName().equals("getClass")) continue;
|
||||
if (!method.getName().startsWith("is") && !method.getName().startsWith("get")) continue;
|
||||
if (factory.isConvertDisabled(method)) continue;
|
||||
if (method.getParameterTypes().length != 0) continue;
|
||||
if (method.getReturnType() == void.class) continue;
|
||||
ref = factory.findRef(clazz, method);
|
||||
if (ref != null && ref.ignore()) continue;
|
||||
if (!(checkMemberType(factory, method.getGenericReturnType(), method.getReturnType()))) return null;
|
||||
String name = convertFieldName(factory, clazz, method);
|
||||
if (names.contains(name)) continue;
|
||||
names.add(name);
|
||||
if (members == null) members = new ArrayList<>();
|
||||
members.add(method);
|
||||
}
|
||||
if (members == null) return null;
|
||||
Collections.sort(members, (o1, o2) -> {
|
||||
ConvertColumnEntry ref1 = factory.findRef(clazz, o1);
|
||||
ConvertColumnEntry ref2 = factory.findRef(clazz, o2);
|
||||
if ((ref1 != null && ref1.getIndex() > 0) || (ref2 != null && ref2.getIndex() > 0)) {
|
||||
int idx1 = ref1 == null ? Integer.MAX_VALUE / 2 : ref1.getIndex();
|
||||
int idx2 = ref2 == null ? Integer.MAX_VALUE / 2 : ref2.getIndex();
|
||||
if (idx1 != idx2) return idx1 - idx2;
|
||||
}
|
||||
String n1 = ref1 == null || ref1.name().isEmpty() ? readGetSetFieldName(o1) : ref1.name();
|
||||
String n2 = ref2 == null || ref2.name().isEmpty() ? readGetSetFieldName(o2) : ref2.name();
|
||||
return n1.compareTo(n2);
|
||||
});
|
||||
return generateDyncEncoder(factory, clazz, members);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected static String convertFieldName(final JsonFactory factory, Class clazz, AccessibleObject element) {
|
||||
ConvertColumnEntry ref = factory.findRef(clazz, element);
|
||||
String name = ref == null || ref.name().isEmpty() ? readGetSetFieldName(element) : ref.name();
|
||||
return name;
|
||||
}
|
||||
|
||||
protected static ConvertSmallString readConvertSmallString(AccessibleObject element) {
|
||||
if (element instanceof Field) return ((Field) element).getAnnotation(ConvertSmallString.class);
|
||||
Method method = (Method) element;
|
||||
ConvertSmallString small = method.getAnnotation(ConvertSmallString.class);
|
||||
if (small == null) {
|
||||
try {
|
||||
Field f = method.getDeclaringClass().getDeclaredField(readGetSetFieldName(method));
|
||||
if (f != null) small = f.getAnnotation(ConvertSmallString.class);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
return small;
|
||||
}
|
||||
|
||||
protected static Class readGetSetFieldType(AccessibleObject element) {
|
||||
if (element instanceof Field) return ((Field) element).getType();
|
||||
return element == null ? null : ((Method) element).getReturnType();
|
||||
}
|
||||
|
||||
protected static String readGetSetFieldName(AccessibleObject element) {
|
||||
if (element instanceof Field) return ((Field) element).getName();
|
||||
Method method = (Method) element;
|
||||
if (method == null) return null;
|
||||
String fname = method.getName();
|
||||
if (!fname.startsWith("is") && !fname.startsWith("get") && !fname.startsWith("set")) return fname;
|
||||
fname = fname.substring(fname.startsWith("is") ? 2 : 3);
|
||||
if (fname.length() > 1 && !(fname.charAt(1) >= 'A' && fname.charAt(1) <= 'Z')) {
|
||||
fname = Character.toLowerCase(fname.charAt(0)) + fname.substring(1);
|
||||
} else if (fname.length() == 1) {
|
||||
fname = "" + Character.toLowerCase(fname.charAt(0));
|
||||
}
|
||||
return fname;
|
||||
}
|
||||
|
||||
protected static JsonDynEncoder generateDyncEncoder(final JsonFactory factory, final Class clazz, final List<AccessibleObject> members) {
|
||||
final String supDynName = JsonDynEncoder.class.getName().replace('.', '/');
|
||||
final String valtypeName = clazz.getName().replace('.', '/');
|
||||
final String writerName = JsonWriter.class.getName().replace('.', '/');
|
||||
final String encodeableName = Encodeable.class.getName().replace('.', '/');
|
||||
final String objEncoderName = ObjectEncoder.class.getName().replace('.', '/');
|
||||
final String typeDesc = org.redkale.asm.Type.getDescriptor(Type.class);
|
||||
final String jsonfactoryDesc = org.redkale.asm.Type.getDescriptor(JsonFactory.class);
|
||||
final String jsonwriterDesc = org.redkale.asm.Type.getDescriptor(JsonWriter.class);
|
||||
final String writerDesc = org.redkale.asm.Type.getDescriptor(Writer.class);
|
||||
final String encodeableDesc = org.redkale.asm.Type.getDescriptor(Encodeable.class);
|
||||
final String objEncoderDesc = org.redkale.asm.Type.getDescriptor(ObjectEncoder.class);
|
||||
final String valtypeDesc = org.redkale.asm.Type.getDescriptor(clazz);
|
||||
|
||||
String newDynName = supDynName + "_Dyn" + clazz.getSimpleName();
|
||||
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
||||
if (String.class.getClassLoader() != clazz.getClassLoader()) {
|
||||
loader = clazz.getClassLoader();
|
||||
newDynName = valtypeName + "_" + JsonDynEncoder.class.getSimpleName();
|
||||
}
|
||||
try {
|
||||
return (JsonDynEncoder) loader.loadClass(newDynName.replace('/', '.')).getDeclaredConstructor().newInstance();
|
||||
} catch (Throwable ex) {
|
||||
}
|
||||
// ------------------------------------------------------------------------------
|
||||
ClassWriter cw = new ClassWriter(COMPUTE_FRAMES);
|
||||
FieldVisitor fv;
|
||||
MethodVisitor mv;
|
||||
AnnotationVisitor av0;
|
||||
|
||||
cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, newDynName, "L" + supDynName + "<" + valtypeDesc + ">;", supDynName, null);
|
||||
Map<String, AccessibleObject> mixedNames = null;
|
||||
for (AccessibleObject element : members) {
|
||||
ConvertColumnEntry ref1 = factory.findRef(clazz, element);
|
||||
final String fieldname = ref1 == null || ref1.name().isEmpty() ? readGetSetFieldName(element) : ref1.name();
|
||||
fv = cw.visitField(ACC_PROTECTED + ACC_FINAL, fieldname + "FieldBytes", "[B", null, null);
|
||||
fv.visitEnd();
|
||||
fv = cw.visitField(ACC_PROTECTED + ACC_FINAL, fieldname + "CommaFieldBytes", "[B", null, null);
|
||||
fv.visitEnd();
|
||||
final Class fieldtype = readGetSetFieldType(element);
|
||||
if (fieldtype != String.class && !fieldtype.isPrimitive()) {
|
||||
if (mixedNames == null) mixedNames = new HashMap<>();
|
||||
mixedNames.put(fieldname, element);
|
||||
fv = cw.visitField(ACC_PROTECTED, fieldname + "Encoder", encodeableDesc, null, null);
|
||||
fv.visitEnd();
|
||||
}
|
||||
}
|
||||
|
||||
{ // 构造函数
|
||||
mv = (cw.visitMethod(ACC_PUBLIC, "<init>", "(" + jsonfactoryDesc + typeDesc + ")V", null, null));
|
||||
//mv.setDebug(true);
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitVarInsn(ALOAD, 2);
|
||||
mv.visitMethodInsn(INVOKESPECIAL, supDynName, "<init>", "(" + jsonfactoryDesc + typeDesc + ")V", false);
|
||||
|
||||
for (AccessibleObject element : members) {
|
||||
ConvertColumnEntry ref1 = factory.findRef(clazz, element);
|
||||
final String fieldname = ref1 == null || ref1.name().isEmpty() ? readGetSetFieldName(element) : ref1.name();
|
||||
//xxxFieldBytes
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitLdcInsn("\"" + fieldname + "\":");
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "getBytes", "()[B", false);
|
||||
mv.visitFieldInsn(PUTFIELD, newDynName, fieldname + "FieldBytes", "[B");
|
||||
//xxxCommaFieldBytes
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitLdcInsn(",\"" + fieldname + "\":");
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "getBytes", "()[B", false);
|
||||
mv.visitFieldInsn(PUTFIELD, newDynName, fieldname + "CommaFieldBytes", "[B");
|
||||
}
|
||||
mv.visitInsn(RETURN);
|
||||
mv.visitMaxs(1 + members.size(), 1 + members.size());
|
||||
mv.visitEnd();
|
||||
}
|
||||
|
||||
{
|
||||
mv = (cw.visitMethod(ACC_PUBLIC, "convertTo", "(" + jsonwriterDesc + valtypeDesc + ")V", null, null));
|
||||
//mv.setDebug(true);
|
||||
{ //if (value == null) { out.writeObjectNull(null); return; }
|
||||
mv.visitVarInsn(ALOAD, 2);
|
||||
Label valif = new Label();
|
||||
mv.visitJumpInsn(IFNONNULL, valif);
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitInsn(ACONST_NULL);
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, writerName, "writeObjectNull", "(Ljava/lang/Class;)V", false);
|
||||
mv.visitInsn(RETURN);
|
||||
mv.visitLabel(valif);
|
||||
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
|
||||
}
|
||||
{ //if (!out.isExtFuncEmpty()) { objectEncoder.convertTo(out, value); return; }
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, writerName, "isExtFuncEmpty", "()Z", false);
|
||||
Label extif = new Label();
|
||||
mv.visitJumpInsn(IFNE, extif);
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitFieldInsn(GETFIELD, newDynName, "objectEncoder", objEncoderDesc);
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitVarInsn(ALOAD, 2);
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, objEncoderName, "convertTo", "(" + org.redkale.asm.Type.getDescriptor(Writer.class) + "Ljava/lang/Object;)V", false);
|
||||
mv.visitInsn(RETURN);
|
||||
mv.visitLabel(extif);
|
||||
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
|
||||
}
|
||||
{ //out.writeTo('{');
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitIntInsn(BIPUSH, '{');
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, writerName, "writeTo", "(B)V", false);
|
||||
}
|
||||
|
||||
int maxLocals = 4;
|
||||
int elementIndex = -1;
|
||||
final Class firstType = readGetSetFieldType(members.get(0));
|
||||
final boolean mustHadComma = firstType.isPrimitive() && (firstType != boolean.class || !factory.tiny()); //byte/short/char/int/float/long/double
|
||||
if (!mustHadComma) { //boolean comma = false;
|
||||
mv.visitInsn(ICONST_0);
|
||||
mv.visitVarInsn(ISTORE, 3);
|
||||
}
|
||||
for (AccessibleObject element : members) {
|
||||
elementIndex++;
|
||||
ConvertColumnEntry ref1 = factory.findRef(clazz, element);
|
||||
final String fieldname = ref1 == null || ref1.name().isEmpty() ? readGetSetFieldName(element) : ref1.name();
|
||||
final Class fieldtype = readGetSetFieldType(element);
|
||||
int storeid = ASTORE;
|
||||
int loadid = ALOAD;
|
||||
{ //String message = value.getMessage();
|
||||
mv.visitVarInsn(ALOAD, 2); //加载 value
|
||||
if (element instanceof Field) {
|
||||
mv.visitFieldInsn(GETFIELD, valtypeName, ((Field) element).getName(), org.redkale.asm.Type.getDescriptor(fieldtype));
|
||||
} else {
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, valtypeName, ((Method) element).getName(), "()" + org.redkale.asm.Type.getDescriptor(fieldtype), false);
|
||||
}
|
||||
if (fieldtype == boolean.class) {
|
||||
storeid = ISTORE;
|
||||
loadid = ILOAD;
|
||||
mv.visitVarInsn(storeid, maxLocals);
|
||||
} else if (fieldtype == byte.class) {
|
||||
storeid = ISTORE;
|
||||
loadid = ILOAD;
|
||||
mv.visitVarInsn(storeid, maxLocals);
|
||||
} else if (fieldtype == short.class) {
|
||||
storeid = ISTORE;
|
||||
loadid = ILOAD;
|
||||
mv.visitVarInsn(storeid, maxLocals);
|
||||
} else if (fieldtype == char.class) {
|
||||
storeid = ISTORE;
|
||||
loadid = ILOAD;
|
||||
mv.visitVarInsn(storeid, maxLocals);
|
||||
} else if (fieldtype == int.class) {
|
||||
storeid = ISTORE;
|
||||
loadid = ILOAD;
|
||||
mv.visitVarInsn(storeid, maxLocals);
|
||||
} else if (fieldtype == float.class) {
|
||||
storeid = FSTORE;
|
||||
loadid = FLOAD;
|
||||
mv.visitVarInsn(storeid, maxLocals);
|
||||
} else if (fieldtype == long.class) {
|
||||
storeid = LSTORE;
|
||||
loadid = LLOAD;
|
||||
mv.visitVarInsn(storeid, maxLocals);
|
||||
} else if (fieldtype == double.class) {
|
||||
storeid = DSTORE;
|
||||
loadid = DLOAD;
|
||||
mv.visitVarInsn(storeid, maxLocals);
|
||||
} else {
|
||||
//storeid = ASTORE;
|
||||
//loadid = ALOAD;
|
||||
mv.visitVarInsn(storeid, maxLocals);
|
||||
}
|
||||
}
|
||||
Label msgnotemptyif = null;
|
||||
if (!fieldtype.isPrimitive()) { //if (message != null) { start
|
||||
mv.visitVarInsn(loadid, maxLocals);
|
||||
msgnotemptyif = new Label();
|
||||
mv.visitJumpInsn(IFNULL, msgnotemptyif);
|
||||
if (factory.tiny() && fieldtype == String.class) {
|
||||
mv.visitVarInsn(loadid, maxLocals);
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "isEmpty", "()Z", false);
|
||||
mv.visitJumpInsn(IFNE, msgnotemptyif);
|
||||
}
|
||||
} else if (fieldtype == boolean.class && factory.tiny()) {
|
||||
mv.visitVarInsn(loadid, maxLocals);
|
||||
msgnotemptyif = new Label();
|
||||
mv.visitJumpInsn(IFEQ, msgnotemptyif);
|
||||
}
|
||||
if (mustHadComma) { //第一个字段必然会写入
|
||||
if (elementIndex == 0) { //第一个
|
||||
//out.writeTo(messageFieldBytes);
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitFieldInsn(GETFIELD, newDynName, fieldname + "FieldBytes", "[B");
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, writerName, "writeTo", "([B)V", false);
|
||||
} else {
|
||||
//out.writeTo(messageCommaFieldBytes);
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitFieldInsn(GETFIELD, newDynName, fieldname + "CommaFieldBytes", "[B");
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, writerName, "writeTo", "([B)V", false);
|
||||
}
|
||||
} else { //if(comma) {} else {} 代码块
|
||||
//if (comma) { start
|
||||
mv.visitVarInsn(ILOAD, 3);
|
||||
Label commaif = new Label();
|
||||
mv.visitJumpInsn(IFEQ, commaif);
|
||||
|
||||
//out.writeTo(messageCommaFieldBytes);
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitFieldInsn(GETFIELD, newDynName, fieldname + "CommaFieldBytes", "[B");
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, writerName, "writeTo", "([B)V", false);
|
||||
|
||||
Label commaelse = new Label();
|
||||
mv.visitJumpInsn(GOTO, commaelse);
|
||||
mv.visitLabel(commaif);
|
||||
if (fieldtype == boolean.class) {
|
||||
mv.visitFrame(Opcodes.F_APPEND, 1, new Object[]{Opcodes.INTEGER}, 0, null);
|
||||
} else if (fieldtype == byte.class) {
|
||||
mv.visitFrame(Opcodes.F_APPEND, 1, new Object[]{Opcodes.INTEGER}, 0, null);
|
||||
} else if (fieldtype == short.class) {
|
||||
mv.visitFrame(Opcodes.F_APPEND, 1, new Object[]{Opcodes.INTEGER}, 0, null);
|
||||
} else if (fieldtype == char.class) {
|
||||
mv.visitFrame(Opcodes.F_APPEND, 1, new Object[]{Opcodes.INTEGER}, 0, null);
|
||||
} else if (fieldtype == int.class) {
|
||||
mv.visitFrame(Opcodes.F_APPEND, 1, new Object[]{Opcodes.INTEGER}, 0, null);
|
||||
} else if (fieldtype == float.class) {
|
||||
mv.visitFrame(Opcodes.F_APPEND, 1, new Object[]{Opcodes.FLOAT}, 0, null);
|
||||
} else if (fieldtype == long.class) {
|
||||
mv.visitFrame(Opcodes.F_APPEND, 1, new Object[]{Opcodes.LONG}, 0, null);
|
||||
} else if (fieldtype == double.class) {
|
||||
mv.visitFrame(Opcodes.F_APPEND, 1, new Object[]{Opcodes.DOUBLE}, 0, null);
|
||||
} else {
|
||||
mv.visitFrame(Opcodes.F_APPEND, 2, new Object[]{Opcodes.INTEGER, "java/lang/String"}, 0, null); // } else { comma
|
||||
}
|
||||
//out.writeTo(messageFieldBytes);
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitFieldInsn(GETFIELD, newDynName, fieldname + "FieldBytes", "[B");
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, writerName, "writeTo", "([B)V", false);
|
||||
//comma = true;
|
||||
mv.visitInsn(ICONST_1);
|
||||
mv.visitVarInsn(ISTORE, 3);
|
||||
mv.visitLabel(commaelse);
|
||||
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); //if (comma) } end
|
||||
}
|
||||
//out.writeString(message);
|
||||
if (fieldtype == boolean.class) {
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitVarInsn(loadid, maxLocals);
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, writerName, "writeBoolean", "(Z)V", false);
|
||||
} else if (fieldtype == byte.class) {
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitVarInsn(loadid, maxLocals);
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, writerName, "writeByte", "(B)V", false);
|
||||
} else if (fieldtype == short.class) {
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitVarInsn(loadid, maxLocals);
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, writerName, "writeShort", "(S)V", false);
|
||||
} else if (fieldtype == char.class) {
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitVarInsn(loadid, maxLocals);
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, writerName, "writeChar", "(C)V", false);
|
||||
} else if (fieldtype == int.class) {
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitVarInsn(loadid, maxLocals);
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, writerName, "writeInt", "(I)V", false);
|
||||
} else if (fieldtype == float.class) {
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitVarInsn(loadid, maxLocals);
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, writerName, "writeFloat", "(F)V", false);
|
||||
} else if (fieldtype == long.class) {
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitVarInsn(loadid, maxLocals);
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, writerName, "writeLong", "(J)V", false);
|
||||
} else if (fieldtype == double.class) {
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitVarInsn(loadid, maxLocals);
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, writerName, "writeDouble", "(D)V", false);
|
||||
} else if (fieldtype == String.class) {
|
||||
if (readConvertSmallString(element) == null) {
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitVarInsn(loadid, maxLocals);
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, writerName, "writeString", "(Ljava/lang/String;)V", false);
|
||||
} else {
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitInsn(ICONST_1);
|
||||
mv.visitVarInsn(loadid, maxLocals);
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, writerName, "writeLatin1To", "(ZLjava/lang/String;)V", false);
|
||||
}
|
||||
} else { //int[],Boolean[],String[]
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitFieldInsn(GETFIELD, newDynName, fieldname + "Encoder", encodeableDesc);
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitVarInsn(loadid, maxLocals);
|
||||
mv.visitMethodInsn(INVOKEINTERFACE, encodeableName, "convertTo", "(" + writerDesc + "Ljava/lang/Object;)V", true);
|
||||
}
|
||||
if (!fieldtype.isPrimitive()) { //if (message != null) } end
|
||||
mv.visitLabel(msgnotemptyif);
|
||||
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
|
||||
} else if (fieldtype == boolean.class && factory.tiny()) {
|
||||
mv.visitLabel(msgnotemptyif);
|
||||
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
|
||||
}
|
||||
if (fieldtype == long.class || fieldtype == double.class) {
|
||||
maxLocals += 2;
|
||||
} else {
|
||||
maxLocals++;
|
||||
}
|
||||
}
|
||||
{ //out.writeTo('}');
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitIntInsn(BIPUSH, '}');
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, writerName, "writeTo", "(B)V", false);
|
||||
}
|
||||
mv.visitInsn(RETURN);
|
||||
mv.visitMaxs(maxLocals, maxLocals);
|
||||
mv.visitEnd();
|
||||
}
|
||||
{
|
||||
mv = (cw.visitMethod(ACC_PUBLIC + ACC_BRIDGE + ACC_SYNTHETIC, "convertTo", "(" + jsonwriterDesc + "Ljava/lang/Object;)V", null, null));
|
||||
//mv.setDebug(true);
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitVarInsn(ALOAD, 2);
|
||||
mv.visitTypeInsn(CHECKCAST, valtypeName);
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, newDynName, "convertTo", "(" + jsonwriterDesc + valtypeDesc + ")V", false);
|
||||
mv.visitInsn(RETURN);
|
||||
mv.visitMaxs(3, 3);
|
||||
mv.visitEnd();
|
||||
}
|
||||
cw.visitEnd();
|
||||
// ------------------------------------------------------------------------------
|
||||
byte[] bytes = cw.toByteArray();
|
||||
Class<?> creatorClazz = new ClassLoader(loader) {
|
||||
public final Class<?> loadClass(String name, byte[] b) {
|
||||
return defineClass(name, b, 0, b.length);
|
||||
}
|
||||
}.loadClass(newDynName.replace('/', '.'), bytes);
|
||||
try {
|
||||
JsonDynEncoder resultEncoder = (JsonDynEncoder) creatorClazz.getDeclaredConstructor(JsonFactory.class, Type.class).newInstance(factory, clazz);
|
||||
if (mixedNames != null) {
|
||||
for (Map.Entry<String, AccessibleObject> en : mixedNames.entrySet()) {
|
||||
Field f = creatorClazz.getDeclaredField(en.getKey() + "Encoder");
|
||||
f.setAccessible(true);
|
||||
f.set(resultEncoder, factory.loadEncoder(en.getValue() instanceof Field ? ((Field) en.getValue()).getGenericType() : ((Method) en.getValue()).getGenericReturnType()));
|
||||
}
|
||||
}
|
||||
return resultEncoder;
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract void convertTo(JsonWriter out, T value);
|
||||
|
||||
@Override
|
||||
public Type getType() {
|
||||
return typeClass;
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user