-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Blade.java
1093 lines (977 loc) · 30.8 KB
/
Blade.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2017, biezhi 王爵 (biezhi.me@gmail.com)
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.blade;
import com.blade.event.*;
import com.blade.event.EventListener;
import com.blade.exception.BladeException;
import com.blade.ioc.Ioc;
import com.blade.ioc.SimpleIoc;
import com.blade.kit.Assert;
import com.blade.kit.BladeKit;
import com.blade.kit.JsonKit;
import com.blade.kit.StringKit;
import com.blade.kit.reload.FileChangeDetector;
import com.blade.loader.BladeLoader;
import com.blade.mvc.handler.*;
import com.blade.mvc.hook.WebHook;
import com.blade.mvc.http.HttpMethod;
import com.blade.mvc.http.HttpSession;
import com.blade.mvc.http.Session;
import com.blade.mvc.http.session.SessionManager;
import com.blade.mvc.route.RouteMatcher;
import com.blade.mvc.ui.template.DefaultEngine;
import com.blade.mvc.ui.template.TemplateEngine;
import com.blade.security.web.cors.CorsMiddleware;
import com.blade.server.Server;
import com.blade.server.netty.NettyServer;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.BindException;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import static com.blade.mvc.Const.*;
/**
* Blade Core
* <p>
* The Blade is the core operating class of the framework,
* which can be used to register routes,
* modify the template engine, set the file list display,
* static resource directory, and so on.
*
* @author biezhi 2017/5/31
*/
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Blade {
/**
* Project middleware list,
* the default is empty, when you use the time you can call the use of methods to add.
* <p>
* Blade provide you with BasicAuthMiddleware, CsrfMiddleware,
* you can customize the implementation of some middleware
*/
private List<WebHook> middleware = new ArrayList<>();
/**
* BeanProcessor list, which stores all the actions that were performed before the project was started
*/
private List<BeanProcessor> processors = new ArrayList<>();
/**
* Blade loader list, which stores all the actions that were performed before the project was started
*/
private List<BladeLoader> loaders = new ArrayList<>();
/**
* All need to be scanned by the package, when you do not set the time will scan com.blade.plugin package
*/
private Set<String> packages = new LinkedHashSet<>(PLUGIN_PACKAGE_NAME);
/**
* All static resource URL prefixes,
* defaults to "/favicon.ico", "/robots.txt", "/static/", "/upload/", "/webjars/",
* which are located under classpath
*/
private Set<String> statics = new HashSet<>(DEFAULT_STATICS);
/**
* The default IOC container implementation
*/
private Ioc ioc = new SimpleIoc();
/**
* The default template engine implementation, this is a very simple, generally not put into production
*/
private TemplateEngine templateEngine = new DefaultEngine();
/**
* Event manager, which manages all the guys that will trigger events
*/
private EventManager eventManager = new EventManager();
/**
* Session manager, which manages session when you enable session
*/
private SessionManager sessionManager = new SessionManager(eventManager);
/**
* Used to wait for the start to complete the lock
*/
private CountDownLatch latch = new CountDownLatch(1);
/**
* Web server implementation, currently only netty
*/
private Server server = new NettyServer();
/**
* A route matcher that matches whether a route exists
*/
private RouteMatcher routeMatcher = new RouteMatcher();
/**
* Blade environment, which stores the parameters of the app.properties configuration file
*/
private Environment environment = Environment.empty();
/**
* Exception handling, it will output some logs when the error is initiated
*/
private Consumer<Exception> startupExceptionHandler = (e) -> log.error("Start blade failed", e);
/**
* Exception handler, default is DefaultExceptionHandler.
* <p>
* When you need to customize the handling of exceptions can be inherited from DefaultExceptionHandler
*/
private ExceptionHandler exceptionHandler = new DefaultExceptionHandler();
/**
* Used to identify whether the web server has started
*/
private boolean started = false;
/**
* Project main class, the main category is located in the root directory of the basic package,
* all the features will be in the sub-package below
*/
private Class<?> bootClass = null;
/**
* Session implementation type, the default is HttpSession.
* <p>
* When you need to be able to achieve similar RedisSession
*/
private Class<? extends Session> sessionImplType = HttpSession.class;
/**
* WebSocket path
*/
private String webSocketPath;
/**
* Blade app start banner, default is Const.BANNER
*/
private String bannerText;
/**
* Blade app start thread name, default is Const.DEFAULT_THREAD_NAME
*/
private String threadName;
/**
* WebSocket Handler
*/
private WebSocketHandler webSocketHandler;
/**
* Give your blade instance, from then on will get the energy
*
* @return return blade instance
* {@link #of }
*/
@Deprecated
public static Blade me() {
return Blade.of();
}
/**
* Give your blade instance, from then on will get the energy
*
* @return return blade instance
*/
public static Blade of() {
return new Blade();
}
/**
* Get blade ioc container, default is SimpleIoc implement.
* <p>
* IOC container will help you hosting Bean or component, it is actually a Map inside.
* In the blade in a single way to make objects reuse,
* you can save resources, to avoid the terrible memory leak
*
* @return return ioc container
*/
public Ioc ioc() {
return this.ioc;
}
/**
* Add a get route to routes
*
* @param path your route path
* @param handler route implement
* @return return blade instance
* @see #get(String, RouteHandler)
*/
@Deprecated
public Blade get(@NonNull String path, @NonNull RouteHandler0 handler) {
this.routeMatcher.addRoute(path, handler, HttpMethod.GET);
return this;
}
/**
* Add a get route to routes
*
* @param path your route path
* @param handler route implement
* @return return blade instance
*/
public Blade get(@NonNull String path, @NonNull RouteHandler handler) {
this.routeMatcher.addRoute(path, handler, HttpMethod.GET);
return this;
}
/**
* Add a post route to routes
*
* @param path your route path
* @param handler route implement
* @return return blade instance
* @see #post(String, RouteHandler)
*/
@Deprecated
public Blade post(@NonNull String path, @NonNull RouteHandler0 handler) {
this.routeMatcher.addRoute(path, handler, HttpMethod.POST);
return this;
}
/**
* Add a post route to routes
*
* @param path your route path
* @param handler route implement
* @return return blade instance
*/
public Blade post(@NonNull String path, @NonNull RouteHandler handler) {
this.routeMatcher.addRoute(path, handler, HttpMethod.POST);
return this;
}
/**
* Add a put route to routes
*
* @param path your route path
* @param handler route implement
* @return return blade instance
* @see #put(String, RouteHandler)
*/
@Deprecated
public Blade put(@NonNull String path, @NonNull RouteHandler0 handler) {
this.routeMatcher.addRoute(path, handler, HttpMethod.PUT);
return this;
}
/**
* Add a put route to routes
*
* @param path your route path
* @param handler route implement
* @return return blade instance
*/
public Blade put(@NonNull String path, @NonNull RouteHandler handler) {
this.routeMatcher.addRoute(path, handler, HttpMethod.PUT);
return this;
}
/**
* Add a delete route to routes
*
* @param path your route path
* @param handler route implement
* @return return blade instance
* @see #delete(String, RouteHandler)
*/
@Deprecated
public Blade delete(@NonNull String path, @NonNull RouteHandler0 handler) {
this.routeMatcher.addRoute(path, handler, HttpMethod.DELETE);
return this;
}
/**
* Add a delete route to routes
*
* @param path your route path
* @param handler route implement
* @return return blade instance
*/
public Blade delete(@NonNull String path, @NonNull RouteHandler handler) {
this.routeMatcher.addRoute(path, handler, HttpMethod.DELETE);
return this;
}
/**
* Add a before route to routes, the before route will be executed before matching route
*
* @param path your route path
* @param handler route implement
* @return return blade instance
* @see #before(String, RouteHandler)
*/
@Deprecated
public Blade before(@NonNull String path, @NonNull RouteHandler0 handler) {
this.routeMatcher.addRoute(path, handler, HttpMethod.BEFORE);
return this;
}
/**
* Add a before route to routes, the before route will be executed before matching route
*
* @param path your route path
* @param handler route implement
* @return return blade instance
*/
public Blade before(@NonNull String path, @NonNull RouteHandler handler) {
this.routeMatcher.addRoute(path, handler, HttpMethod.BEFORE);
return this;
}
/**
* Add a after route to routes, the before route will be executed after matching route
*
* @param path your route path
* @param handler route implement
* @return return blade instance
* @see #after(String, RouteHandler)
*/
@Deprecated
public Blade after(@NonNull String path, @NonNull RouteHandler0 handler) {
this.routeMatcher.addRoute(path, handler, HttpMethod.AFTER);
return this;
}
/**
* Add a after route to routes, the before route will be executed after matching route
*
* @param path your route path
* @param handler route implement
* @return return blade instance
*/
public Blade after(@NonNull String path, @NonNull RouteHandler handler) {
this.routeMatcher.addRoute(path, handler, HttpMethod.AFTER);
return this;
}
/**
* Setting blade mvc default templateEngine
*
* @param templateEngine TemplateEngine object
* @return blade
*/
public Blade templateEngine(@NonNull TemplateEngine templateEngine) {
this.templateEngine = templateEngine;
return this;
}
/**
* Get TemplateEngine, default is DefaultEngine
*
* @return return TemplateEngine
*/
public TemplateEngine templateEngine() {
return this.templateEngine;
}
/**
* Get RouteMatcher
*
* @return return RouteMatcher
*/
public RouteMatcher routeMatcher() {
return this.routeMatcher;
}
/**
* Register bean to ioc container
*
* @param bean bean object
* @return blade
*/
public Blade register(@NonNull Object bean) {
this.ioc.addBean(bean);
return this;
}
/**
* Register bean to ioc container
*
* @param cls bean class, the class must provide a no args constructor
* @return blade
*/
public Blade register(@NonNull Class<?> cls) {
this.ioc.addBean(cls);
return this;
}
/**
* Add multiple static resource file
* the default provides the static, upload
*
* @param folders static resource directory
* @return blade
*/
public Blade addStatics(@NonNull String... folders) {
this.statics.addAll(Arrays.asList(folders));
return this;
}
/**
* Set whether to show the file directory, default doesn't show
*
* @param fileList show the file directory
* @return blade
*/
public Blade showFileList(boolean fileList) {
this.environment.set(ENV_KEY_STATIC_LIST, fileList);
return this;
}
/**
* Set whether open gzip, default disabled
*
* @param gzipEnable enabled gzip
* @return blade
*/
public Blade gzip(boolean gzipEnable) {
this.environment.set(ENV_KEY_GZIP_ENABLE, gzipEnable);
return this;
}
/**
* Get ioc bean
*
* @param cls bean class type
* @return return bean instance
*/
public <T> T getBean(@NonNull Class<T> cls) {
return this.ioc.getBean(cls);
}
/**
* Get ExceptionHandler
*
* @return return ExceptionHandler
*/
public ExceptionHandler exceptionHandler() {
return this.exceptionHandler;
}
/**
* Set ExceptionHandler, when you need a custom exception handling
*
* @param exceptionHandler your ExceptionHandler instance
* @return return blade instance
*/
public Blade exceptionHandler(ExceptionHandler exceptionHandler) {
this.exceptionHandler = exceptionHandler;
return this;
}
/**
* Get current is developer mode
*
* @return return true is developer mode, else not.
*/
public boolean devMode() {
return this.environment.getBoolean(ENV_KEY_DEV_MODE, true);
}
/**
* Whether encoding setting mode for developers
* The default mode is developers
*
* @param devMode developer mode
* @return blade
*/
public Blade devMode(boolean devMode) {
this.environment.set(ENV_KEY_DEV_MODE, devMode);
return this;
}
public boolean isAutoRefreshDir() {
return this.environment.get(ENV_KEY_AUTO_REFRESH_DIR).isPresent();
}
public void setAutoRefreshDir(String dir) {
this.environment.set(ENV_KEY_AUTO_REFRESH_DIR, dir);
}
public Class<?> bootClass() {
return this.bootClass;
}
/**
* Set whether to enable cors
*
* @param enableCors enable cors
* @return blade
*/
public Blade enableCors(boolean enableCors) {
this.environment.set(ENV_KEY_CORS_ENABLE, enableCors);
if (enableCors) {
this.use(new CorsMiddleware());
}
return this;
}
/**
* Get blade statics list.
* e.g: "/favicon.ico", "/robots.txt", "/static/", "/upload/", "/webjars/"
*
* @return return statics
*/
public Set<String> getStatics() {
return this.statics;
}
/**
* When set to start blade scan packages
*
* @param packages package name
* @return blade
*/
public Blade scanPackages(@NonNull String... packages) {
this.packages.addAll(Arrays.asList(packages));
return this;
}
/**
* Get scan the package set.
*
* @return return packages set
*/
public Set<String> scanPackages() {
return packages;
}
/**
* Set to start blade configuration file by default
* Boot config properties file in classpath directory.
* <p>
* Without setting will read the classpath -> application.properties
*
* @param bootConf boot config file name
* @return blade
*/
public Blade bootConf(@NonNull String bootConf) {
this.environment.set(ENV_KEY_BOOT_CONF, bootConf);
return this;
}
/**
* Set the environment variable for global use here
* <p>
* {@link #env(String, String)}
*
* @param key environment key
* @param value environment value
* @return blade
*/
@Deprecated
public Blade environment(@NonNull String key, @NonNull Object value) {
this.environment.set(key, value);
return this;
}
/**
* Return the application's environment configuration information.
*
* @return Environment
*/
public Environment environment() {
return this.environment;
}
@Deprecated
public Blade environment(Environment environment) {
this.environment = environment;
return this;
}
/**
* Get application environment information.
*
* @param key environment key
* @return environment optional value
*/
public Optional<String> env(String key) {
return this.environment.get(key);
}
/**
* Get application environment information.
*
* @param key environment key
* @param defaultValue default value, if value is null
* @return environment optional value
*/
public String env(String key, String defaultValue) {
return this.environment.get(key, defaultValue);
}
/**
* Set to start the web server to monitor port, the default is 9000
*
* @param port web server port, default is 9000
* @return blade
*/
public Blade listen(int port) {
Assert.greaterThan(port, 0, "server port not is negative number.");
this.environment.set(ENV_KEY_SERVER_PORT, port);
return this;
}
/**
* Set to start the web server to listen the IP address and port
* The default will listen 0.0.0.0:9000
*
* @param address ip address
* @param port web server port
* @return blade
*/
public Blade listen(@NonNull String address, int port) {
Assert.greaterThan(port, 0, "server port not is negative number.");
this.environment.set(ENV_KEY_SERVER_ADDRESS, address);
this.environment.set(ENV_KEY_SERVER_PORT, port);
return this;
}
/**
* The use of multiple middleware, if any
*
* @param middleware middleware object array
* @return blade
*/
public Blade use(@NonNull WebHook... middleware) {
if (BladeKit.isEmpty(middleware)) {
return this;
}
this.middleware.addAll(Arrays.asList(middleware));
for (var webHook : middleware) {
this.register(webHook);
}
return this;
}
/**
* Get middleware list
*
* @return return middleware list
*/
public List<WebHook> middleware() {
return this.middleware;
}
/**
* Set in the name of the app blade application
*
* @param appName application name
* @return blade
*/
public Blade appName(@NonNull String appName) {
this.environment.set(ENV_KEY_APP_NAME, appName);
return this;
}
/**
* Add a event watcher
* When the trigger event is executed eventListener
*
* @param eventType event type
* @param eventListener event watcher
* @return blade
*/
public Blade event(@NonNull EventType eventType, @NonNull EventListener eventListener) {
this.eventManager.addEventListener(eventType, eventListener);
return this;
}
/**
* Add a event watcher
* When the trigger event is executed eventListener
*
* @param eventType event type
* @param eventListener event watcher
* @return blade
*/
public Blade on(@NonNull EventType eventType, @NonNull EventListener eventListener) {
this.eventManager.addEventListener(eventType, eventListener);
return this;
}
/**
* Get session implements Class Type
*
* @return return blade Session Type
*/
public Class<? extends Session> sessionType() {
return this.sessionImplType;
}
/**
* Set session implements Class Type, e.g: RedisSession
*
* @param sessionImplType Session Type implement
* @return return blade instance
*/
public Blade sessionType(Class<? extends Session> sessionImplType) {
this.sessionImplType = sessionImplType;
return this;
}
/**
* Event on started
*
* @param processor bean processor
* @return return blade instance
*/
@Deprecated
public Blade onStarted(@NonNull BeanProcessor processor) {
this.processors.add(processor);
return this;
}
/**
* Add blade loader
*
* @param loader
* @return
*/
public Blade addLoader(@NonNull BladeLoader loader) {
this.loaders.add(loader);
return this;
}
/**
* Get processors
*
* @return return processors
*/
@Deprecated
public List<BeanProcessor> processors() {
return this.processors;
}
public List<BladeLoader> loaders() {
return this.loaders;
}
/**
* Get EventManager
*
* @return return EventManager
*/
public EventManager eventManager() {
return this.eventManager;
}
/**
* Get SessionManager
*
* @return return SessionManager
*/
public SessionManager sessionManager() {
return this.sessionManager;
}
/**
* Disable session, default is open
*
* @return return blade instance
*/
public Blade disableSession() {
this.sessionManager = null;
return this;
}
public Blade watchEnvChange(boolean watchEnvChange) {
this.environment.set(ENV_KEY_APP_WATCH_ENV, watchEnvChange);
return this;
}
/**
* Start blade application.
* <p>
* When all the routing in the main function of situations you can use,
* Otherwise please do not call this method.
*
* @return return blade instance
*/
public Blade start() {
return this.start(null, null);
}
/**
* Start blade application
*
* @param mainCls main Class, the main class bag is basic package
* @param args command arguments
* @return return blade instance
*/
public Blade start(Class<?> mainCls, String... args) {
try {
this.loadConfig(args);
this.bootClass = mainCls;
eventManager.fireEvent(EventType.SERVER_STARTING, new Event().attribute("blade", this));
Thread thread = new Thread(() -> {
try {
server.start(Blade.this);
latch.countDown();
server.join();
} catch (BindException e) {
log.error("Bind address error\n", e);
System.exit(0);
} catch (Exception e) {
startupExceptionHandler.accept(e);
}
});
String threadName = null != this.threadName ? this.threadName : environment.get(ENV_KEY_APP_THREAD_NAME, null);
threadName = null != threadName ? threadName : DEFAULT_THREAD_NAME;
thread.setName(threadName);
thread.start();
this.started = true;
Thread resourceFilesRefreshThread = new Thread(() -> {
try {
FileChangeDetector fileChangeDetector = new FileChangeDetector(environment.get(ENV_KEY_AUTO_REFRESH_DIR).get());
fileChangeDetector.processEvent((event, filePath) -> {
try {
//TODO: add support for Create and Delete
if (event.equals(StandardWatchEventKinds.ENTRY_MODIFY)) {
Path destPath = FileChangeDetector.getDestPath(filePath, environment);
Files.copy(filePath, destPath, StandardCopyOption.REPLACE_EXISTING);
}
} catch (IOException e) {
log.error("Exception when trying to copy updated file");
startupExceptionHandler.accept(e);
}
});
} catch (IOException e) {
startupExceptionHandler.accept(e);
}
});
if (devMode() && isAutoRefreshDir()) {
log.info("auto refresh is enabled");
resourceFilesRefreshThread.start();
}
} catch (Exception e) {
startupExceptionHandler.accept(e);
}
return this;
}
/**
* Start the blade web server
*
* @param bootClass Start the boot class, used to scan the class in all of the packages
* @param address web server bind ip address
* @param port web server bind port
* @param args launch parameters
* @return blade
*/
@Deprecated
public Blade start(Class<?> bootClass, @NonNull String address, int port, String... args) {
return this;
}
/**
* Await web server started
*
* @return return blade instance
*/
public Blade await() {
if (!this.started) {
throw new IllegalStateException("Server hasn't been started. Call start() before calling this method.");
}
try {
this.latch.await();
} catch (Exception e) {
log.error("Blade start await error", e);
Thread.currentThread().interrupt();
}
return this;
}
/**
* Stop current blade application
* <p>
* Will stop synchronization waiting netty service
*/
public void stop() {
this.eventManager.fireEvent(EventType.SERVER_STOPPING, new Event().attribute("blade", this));
this.server.stopAndWait();
this.eventManager.fireEvent(EventType.SERVER_STOPPED, new Event().attribute("blade", this));
}
/**
* Register WebSocket path
*
* @param path websocket path
* @param handler websocket handler
* @return return blade instance
*/
public Blade webSocket(@NonNull String path, @NonNull WebSocketHandler handler) {
if (null != this.webSocketHandler) {
throw new BladeException(500, "There is already a WebSocket path.");
}
this.webSocketPath = path;
this.webSocketHandler = handler;
this.routeMatcher.addWebSocket(path);
return this;
}
/**
* Get webSocket path
*
* @return return websocket path
*/
public String webSocketPath() {
return this.webSocketPath;
}
/**
* Set blade start banner text
*
* @param bannerText banner text
* @return return blade instance
*/
public Blade bannerText(String bannerText) {
this.bannerText = bannerText;
return this;
}
/**
* Get banner text
*
* @return return blade start banner text
*/
public String bannerText() {
if (null != bannerText) return bannerText;
String bannerPath = environment.get(ENV_KEY_BANNER_PATH, null);
if (StringKit.isEmpty(bannerPath) || Files.notExists(Paths.get(bannerPath))) {
return null;
}
try {
BufferedReader bufferedReader = Files.newBufferedReader(Paths.get(bannerPath));
bannerText = bufferedReader.lines().collect(Collectors.joining("\r\n"));
} catch (Exception e) {
log.error("Load Start Banner file error", e);
}
return bannerText;
}
/**
* Set blade start thread name
*