1
1
// file:noinspection unused
2
2
package ru.easydata.webfx.app
3
3
4
- import getl.config.ConfigSlurper
5
- import getl.utils.BoolUtils
6
4
import getl.utils.FileUtils
7
- import getl.utils.Logs
8
5
import getl.utils.MapUtils
9
6
import javafx.application.Application
10
7
import javafx.fxml.FXMLLoader
11
8
import javafx.scene.Parent
12
9
import javafx.scene.Scene
13
10
import javafx.scene.image.Image
11
+ import javafx.stage.Screen
14
12
import javafx.stage.Stage
13
+ import ru.easydata.webfx.config.ConfigManager
15
14
import ru.easydata.webfx.controllers.MainController
16
- import ru.easydata.webfx.exception.LocalServerAlreadyRegister
17
- import ru.easydata.webfx.exception.LocalServerUnknown
18
- import ru.easydata.webfx.servers.LocalServer
19
- import ru.easydata.webfx.utils.Functions
20
15
21
16
class WebFxApplication extends Application {
22
- @SuppressWarnings (' SpellCheckingInspection' )
23
17
static void main (String [] args ) {
24
- Logs.Init ()
25
-
18
+ Map<String , Object > mainArguments = null
26
19
if (args != null && args. length > 0 )
27
- mainArguments. putAll( MapUtils.ProcessArguments (args) )
20
+ mainArguments = MapUtils.ProcessArguments (args)
28
21
29
- titleMainWindow = System . getProperty(' APP_TITLE' )?: ' EasyData WebFx Browser'
30
- userDirPath = FileUtils.ConvertToUnixPath ((System . getProperty(' USER_DIR' ) != null )?
22
+ def titleMainWindow = System . getProperty(' APP_TITLE' )?: ' EasyData WebFx Browser'
23
+ def userDirPath = FileUtils.ConvertToUnixPath ((System . getProperty(' USER_DIR' ) != null )?
31
24
System . getProperty(' USER_DIR' ):System . getProperty(" user.home" ) + " /easydata/webfxapplication" )
32
25
33
- FileUtils.ValidFilePath (userDirPath)
34
- FileUtils.ValidFilePath (" $userDirPath /userdata" )
35
-
36
- Logs . global. logFileName = userDirPath + ' /log/easyworkspace-{date}.log'
37
- Logs.Info " *** Start $titleMainWindow "
38
- Logs.Info " User data directory: $userDirPath "
39
-
40
- launch(this , args)
41
- }
42
-
43
- static public final Map<String , Object > mainArguments = [:] as Map<String , Object >
44
-
45
- static public String titleMainWindow
46
- static public final Image mainIcon
47
- static public String userDirPath
48
-
49
- static {
26
+ Image mainIcon
50
27
try (def iconStream = this . getResourceAsStream(" /icons/icon.png" )) {
51
28
mainIcon = new Image (iconStream)
52
29
}
53
- }
54
-
55
- public Stage mainWindow
56
- public final File userDir = new File (userDirPath)
57
- public final File engineConfigFile = new File (userDir. path + ' /engine.conf' )
58
-
59
- public final File favoritesConfigFile = new File (userDir. path + ' /favorites.conf' )
60
- private final MainController controller = new MainController (this )
61
-
62
- private Boolean autoStartServers
63
- Boolean getAutoStartServers () { autoStartServers }
64
-
65
- private Boolean autoStopServers
66
- Boolean getAutoStopServers () { autoStopServers }
67
-
68
- private Boolean sslVerification
69
- Boolean getSslVerification () { sslVerification }
70
-
71
- public final Map<String , Map<String , Map<String , Object > > > favorites = [:] as Map<String , Map<String , Map<String , Object > > >
72
-
73
- Map<String , Map<String , Map<String , Object > > > loadConfigFavorites () {
74
- if (! favoritesConfigFile. exists())
75
- return [:] as Map<String , Map<String , Map<String , Object > > >
76
-
77
- return ConfigSlurper.LoadConfigFile (file : favoritesConfigFile) as Map<String , Map<String , Map<String , Object > > >
78
- }
79
-
80
- void saveConfigFavorites () {
81
- if (favoritesConfigFile. exists()) {
82
- FileUtils.CopyToFile (favoritesConfigFile, new File (FileUtils.ExcludeFileExtension (favoritesConfigFile. path) + ' .bak' ))
83
- }
84
- ConfigSlurper.SaveConfigFile (data : favorites, file : favoritesConfigFile)
85
- }
86
-
87
- static private final Map<String , Object > defaultEngineOptions = [
88
- engine : [
89
- ssl_verification : false ,
90
- https : [
91
- protocols : ' TLSv1,TLSv1.1,TLSv1.2'
92
- ]
93
- ]
94
- ]
95
-
96
- public final List<LocalServer > localServers = new LinkedList<LocalServer > ()
97
-
98
- LocalServer findServerByName (String name ) {
99
- if (name == null )
100
- throw new NullPointerException (" Null name!" )
101
-
102
- name = name. trim(). toLowerCase()
103
- localServers. find { it. name. toLowerCase() == name }
104
- }
105
-
106
- LocalServer findServerByUri (URI uri ) {
107
- if (uri == null )
108
- throw new NullPointerException (" Null uri!" )
109
-
110
- localServers. find { (it. uri == uri) }
111
- }
112
-
113
- LocalServer findServerByUrl (String url ) {
114
- if (url == null )
115
- throw new NullPointerException (" Null url!" )
116
-
117
- return findServerByUri(Functions.Url2UriWithRoot (url))
118
- }
119
-
120
-
121
- void registerLocalServer (String name , String url , String directory , String command , Integer commandTimeout ,
122
- String shutdownService , Integer shutdownTimeout , String encode ) {
123
- if (name == null || name. length() == 0 )
124
- throw new NullPointerException (" Null name!" )
125
-
126
- if (url == null || url. length() == 0 )
127
- throw new NullPointerException (" Null url!" )
128
-
129
- if (command == null || command. length() == 0 )
130
- throw new NullPointerException (" Null command!" )
131
-
132
- if (findServerByName(name) != null || findServerByUrl(url) != null )
133
- throw new LocalServerAlreadyRegister (name, url)
134
-
135
- localServers. add(new LocalServer (this , name, url, directory, command, commandTimeout, shutdownService, shutdownTimeout, encode))
136
- Logs.Info " Registered local server $name with url $url "
137
- }
138
-
139
- void unregisterLocalServer (String name ) {
140
- if (name == null || name. length() == 0 )
141
- throw new NullPointerException (" Null name!" )
142
-
143
- def server = findServerByName(name)
144
- if (server == null )
145
- throw new LocalServerUnknown (name)
146
-
147
- if (! server. isStop) {
148
- server. stop()
149
- }
150
-
151
- localServers. remove(server)
152
- Logs.Info " Unregistered local server $name with ${ server.uri.toString()} "
153
- }
154
-
155
- void stopLocalServer (String name ) {
156
- if (name == null || name. length() == 0 )
157
- throw new NullPointerException (" Null name!" )
158
-
159
- def server = findServerByUrl(name)
160
- if (server == null )
161
- throw new LocalServerUnknown (name)
162
-
163
- if (! server. isStop)
164
- server. stop()
165
- }
166
-
167
- void initEngine () {
168
- def fileData = (engineConfigFile. exists())? ConfigSlurper.LoadConfigFile (file : engineConfigFile): defaultEngineOptions
169
- def engConfig = (fileData. engine as Map<String , Object > )?: [:] as Map<String , Object >
170
-
171
- sslVerification = BoolUtils.IsValue (engConfig. ssl_verification)
172
- Logs.Info " SSL verification: $sslVerification "
173
-
174
- autoStartServers = BoolUtils.IsValue (engConfig. autostart_servers)
175
- Logs.Info " Auto start local servers: $autoStartServers "
176
-
177
- autoStopServers = BoolUtils.IsValue (engConfig. autostop_servers)
178
- Logs.Info " Auto stop local servers: $autoStopServers "
179
-
180
- def httpsConfig = (engConfig. https as Map<String , Object > )?: [:] as Map<String , Object >
181
- httpsConfig. each { key , value ->
182
- Logs.Info " https.${ key} = $value "
183
- System . setProperty(" https.$key " . toString(), value. toString())
184
- }
185
-
186
- def httpConfig = (engConfig. http as Map<String , Object > )?: [:] as Map<String , Object >
187
- httpConfig. each { key , value ->
188
- Logs.Info " http.${ key} = $value "
189
- System . setProperty(" http.$key " . toString(), value. toString())
190
- }
191
30
192
- if (! sslVerification)
193
- Functions.DisableSslVerification ()
194
-
195
- def serversConfig = (fileData. servers as Map<String , Map<String , Object > > )?: [:] as Map<String , Map<String , Object > >
196
- serversConfig. each { name , data ->
197
- registerLocalServer(name, data. url as String , data. directory as String , data. command as String , data. command_timeout as Integer ,
198
- data. shutdown as String , data. shutdown_timeout as Integer , data. encode as String )
199
- }
200
- Logs.Info " >>> Application started"
201
- }
202
-
203
- void unregisterLocalServers () {
204
- synchronized (localServers) {
205
- localServers. each { server ->
206
- unregisterLocalServer(server. name)
207
- }
208
- }
31
+ ConfigManager . config. init(userDirPath, titleMainWindow, mainIcon, mainArguments)
32
+ launch(this , args)
209
33
}
210
34
211
35
@Override
212
36
void start (Stage stage ) {
213
- initEngine()
214
- favorites. putAll(loadConfigFavorites())
37
+ def controller = new MainController (stage)
215
38
216
39
def loader = new FXMLLoader ()
217
40
def url = getClass(). getResource(' /fxml/main.fxml' )
218
41
loader. setLocation(url)
219
42
loader. controller = controller
220
43
def root = loader. load() as Parent
221
-
222
44
controller. initFavorites()
223
45
224
46
stage. tap {
225
- addShutdownHook {
226
- unregisterLocalServers()
227
- }
228
-
229
- setTitle(titleMainWindow)
230
- icons. add(mainIcon)
47
+ setTitle(ConfigManager . config. titleMainWindow)
48
+ icons. add(ConfigManager . config. mainIcon)
231
49
232
50
scene = new Scene (root)
233
- stage. maximized = true
234
- stage. width = 600
235
- stage. height = 400
236
- stage. show()
237
- }
238
51
239
- mainWindow = stage
52
+ minWidth = 620
53
+ minHeight = 460
54
+
55
+ def screenBounds = Screen . getPrimary(). getBounds()
56
+
57
+ width = screenBounds. width - 10
58
+ height = screenBounds. height - 100
59
+ centerOnScreen()
60
+
61
+ // maximized = true
62
+
63
+ ConfigManager . config. monitorWindow(' MainWindow' , stage)
64
+
65
+ show()
66
+ }
240
67
}
241
68
242
69
void stop () {
243
- unregisterLocalServers ()
70
+ ConfigManager . config . doneEngine ()
244
71
}
245
72
}
0 commit comments