5
5
import com .kodedu .controller .ApplicationController ;
6
6
import com .kodedu .helper .IOHelper ;
7
7
import com .kodedu .helper .TaskbarHelper ;
8
+ import com .kodedu .other .RenderResult ;
8
9
import com .kodedu .service .DirectoryService ;
9
10
import com .kodedu .service .FileOpenListener ;
10
11
import com .kodedu .service .ThreadService ;
11
- import com .kodedu .service .ui .IndikatorService ;
12
+ import com .kodedu .service .convert .docbook .DocBookConverter ;
13
+ import com .kodedu .service .convert .html .HtmlBookConverter ;
14
+ import com .kodedu .service .convert .pdf .PdfBookConverter ;
12
15
import com .kodedu .service .ui .TabService ;
13
- import com .kodedu .terminalfx .helper .ThreadHelper ;
14
16
import de .tototec .cmdoption .CmdlineParser ;
15
17
import de .tototec .cmdoption .CmdlineParserException ;
16
18
import javafx .application .Application ;
27
29
import javafx .stage .Modality ;
28
30
import javafx .stage .Screen ;
29
31
import javafx .stage .Stage ;
32
+ import org .apache .commons .lang3 .StringUtils ;
30
33
import org .slf4j .Logger ;
31
34
import org .slf4j .LoggerFactory ;
32
35
import org .springframework .boot .SpringApplication ;
45
48
import java .nio .file .Path ;
46
49
import java .nio .file .Paths ;
47
50
import java .security .SecureRandom ;
51
+ import java .util .Arrays ;
52
+ import java .util .Objects ;
48
53
import java .util .Set ;
49
54
import java .util .concurrent .ThreadLocalRandom ;
50
55
import java .util .function .Consumer ;
56
61
57
62
public class AppStarter extends Application {
58
63
59
- private Logger logger = LoggerFactory .getLogger (AppStarter .class );
64
+ private static Logger logger = LoggerFactory .getLogger (AppStarter .class );
60
65
61
66
private static ApplicationController controller ;
62
67
private static ConfigurableApplicationContext context ;
@@ -65,30 +70,40 @@ public class AppStarter extends Application {
65
70
private ConfigurationService configurationService ;
66
71
private Image logoImage ;
67
72
private long startTime ;
73
+ public static CmdlineConfig config ;
68
74
69
75
@ Override
70
76
public void start (final Stage stage ) {
71
77
this .startTime = System .currentTimeMillis ();
72
- initializeSSLContext ();
73
78
stage .setTitle ("AsciidocFX" );
74
79
logoImage = setApplicationIcon (stage );
75
80
Thread .setDefaultUncaughtExceptionHandler ((t , e ) -> logger .error (e .getMessage (), e ));
76
81
Thread .startVirtualThread (() -> {
77
82
loadRequiredFonts ();
78
83
});
79
84
80
- // http://bit.ly/1Euk8hh
81
- System .setProperty ("jsse.enableSNIExtension" , "false" );
82
- // System.setProperty("https.protocols", "SSLv3");
85
+ Thread .startVirtualThread (() -> {
86
+ try {
87
+ startApp (stage );
88
+ } catch (final Throwable e ) {
89
+ logger .error ("Problem occured while starting AsciidocFX" , e );
90
+ }
91
+ });
83
92
84
- System .setProperty ("sun.net.http.allowRestrictedHeaders" , "true" );
85
- System .setProperty ("java.awt.headless" , "false" );
93
+ }
86
94
95
+ private static void setupMonocle () {
96
+ System .setProperty ("monocle.platform" , "Headless" );
97
+ System .setProperty ("glass.platform" , "Monocle" );
98
+ System .setProperty ("prism.order" , "sw" );
99
+ }
100
+
101
+ private static CmdlineConfig parseCmdArgs (String [] args ) {
87
102
final CmdlineConfig config = new CmdlineConfig ();
88
103
final CmdlineParser cp = new CmdlineParser (config );
89
104
90
105
try {
91
- cp .parse (getParameters (). getRaw (). toArray ( new String [ 0 ]) );
106
+ cp .parse (args );
92
107
} catch (final CmdlineParserException e ) {
93
108
System .err .println ("Invalid commandline given: " + e .getMessage ());
94
109
System .exit (1 );
@@ -98,20 +113,21 @@ public void start(final Stage stage) {
98
113
cp .usage ();
99
114
System .exit (0 );
100
115
}
116
+ return config ;
117
+ }
101
118
102
- Thread .startVirtualThread (() -> {
103
- try {
104
- startApp (stage , config );
105
- } catch (final Throwable e ) {
106
- logger .error ("Problem occured while starting AsciidocFX" , e );
107
- }
108
- });
119
+ private static void setJvmProperties () {
120
+ // http://bit.ly/1Euk8hh
121
+ System .setProperty ("jsse.enableSNIExtension" , "false" );
122
+ // System.setProperty("https.protocols", "SSLv3");
109
123
124
+ System .setProperty ("sun.net.http.allowRestrictedHeaders" , "true" );
125
+ System .setProperty ("java.awt.headless" , "false" );
110
126
}
111
127
112
- private void initializeSSLContext () {
128
+ private static void initializeSSLContext () {
113
129
try {
114
- SSLContext sc = SSLContext .getInstance ("SSL " );
130
+ SSLContext sc = SSLContext .getInstance ("TLS " );
115
131
sc .init (null , new TrustManager []{new FakeTrustManager ()}, new SecureRandom ());
116
132
HttpsURLConnection .setDefaultSSLSocketFactory (sc .getSocketFactory ());
117
133
HttpsURLConnection .setDefaultHostnameVerifier ((h , s ) -> true );
@@ -121,9 +137,10 @@ private void initializeSSLContext() {
121
137
}
122
138
}
123
139
124
- public void registerStartupListener () {
125
- Thread .startVirtualThread (() -> {
126
- StartupNotification .registerStartupListener (parameters -> {
140
+ public void registerAssociatedFileHandler () {
141
+ StartupNotification .registerStartupListener (parameters -> {
142
+ threadService .runTaskLater (() -> {
143
+ controller .waitAdocPreviewReadyLatch ();
127
144
FileOpenListener fileOpenListener = context .getBean (FileOpenListener .class );
128
145
fileOpenListener .startupPerformed (parameters );
129
146
});
@@ -149,14 +166,15 @@ public void loadRequiredFonts() {
149
166
}
150
167
}
151
168
152
- private void startApp (final Stage stage , final CmdlineConfig config ) throws Throwable {
169
+ private void startApp (final Stage stage ) throws Throwable {
153
170
154
171
this .stage = stage ;
155
172
SpringApplication app = new SpringApplication (SpringAppConfig .class );
156
173
// app.setApplicationStartup(new BufferingApplicationStartup(20000));
157
174
context = app .run (new String []{});
158
175
logger .debug ("Spring context loaded in {} ms." , System .currentTimeMillis () - startTime );
159
176
controller = context .getBean (ApplicationController .class );
177
+ registerAssociatedFileHandler ();
160
178
threadService = context .getBean (ThreadService .class );
161
179
configurationService = context .getBean (ConfigurationService .class );
162
180
@@ -180,7 +198,6 @@ private void startApp(final Stage stage, final CmdlineConfig config) throws Thro
180
198
configurationService .loadConfigurations ();
181
199
controller .applyInitialConfigurations ();
182
200
controller .checkStageInsideScreens ();
183
- registerStartupListener ();
184
201
});
185
202
186
203
stage .setOnShown (e -> {
@@ -261,6 +278,7 @@ private void startApp(final Stage stage, final CmdlineConfig config) throws Thro
261
278
262
279
threadService .start (() -> {
263
280
try {
281
+ controller .waitAdocPreviewReadyLatch ();
264
282
registerStartupListener (config );
265
283
} catch (Exception e ) {
266
284
logger .error ("Problem occured in startup listener" , e );
@@ -326,7 +344,13 @@ private void registerStartupListener(CmdlineConfig config) {
326
344
if (workingDirectory == null ) {
327
345
directoryService .changeWorkigDir (file .toPath ().getParent ());
328
346
}
329
- tabService .addTab (file .toPath ());
347
+ tabService .addTab (file .toPath (), () -> {
348
+ threadService .runTaskLater (() -> {
349
+ if (Objects .nonNull (config .backend )) {
350
+ processFileConversion (config );
351
+ }
352
+ });
353
+ });
330
354
} else {
331
355
// TODO: do we want to create such a file on demand?
332
356
logger .error ("Cannot open non-existent file: {}" , file );
@@ -336,6 +360,38 @@ private void registerStartupListener(CmdlineConfig config) {
336
360
});
337
361
}
338
362
363
+ private void processFileConversion (CmdlineConfig config ) {
364
+ if (StringUtils .containsIgnoreCase (config .backend , "pdf" )) {
365
+ PdfBookConverter pdfBookConverter = context .getBean (PdfBookConverter .class );
366
+ pdfBookConverter .convert (false , getRenderResultConsumer ());
367
+ } else if (StringUtils .containsIgnoreCase (config .backend , "html" )) {
368
+ HtmlBookConverter htmlBookConverter = context .getBean (HtmlBookConverter .class );
369
+ htmlBookConverter .convert (false , getRenderResultConsumer ());
370
+ } else if (StringUtils .containsIgnoreCase (config .backend , "docbook" )) {
371
+ DocBookConverter docBookConverter = context .getBean (DocBookConverter .class );
372
+ docBookConverter .convert (false , getRenderResultConsumer ());
373
+ }
374
+ }
375
+
376
+ private Consumer <RenderResult > getRenderResultConsumer () {
377
+ return renderResult -> {
378
+ if (renderResult .isSuccessful ()) {
379
+ logger .info ("Completed {}" , renderResult .getDestination ());
380
+ } else {
381
+ Exception exception = renderResult .getException ();
382
+ logger .info ("Error {}" , exception .getMessage (), exception );
383
+ }
384
+
385
+ if (!config .noQuitAfter ) {
386
+ try {
387
+ stop ();
388
+ } catch (Exception e ) {
389
+ logger .error (e .getMessage (), e );
390
+ }
391
+ }
392
+ };
393
+ }
394
+
339
395
private Path resolveWorkingDirectory (CmdlineConfig config ) {
340
396
if (config .workingDirectory != null ) {
341
397
File workingDirectory = new File (config .workingDirectory );
@@ -396,6 +452,15 @@ public void stop() throws Exception {
396
452
* @param args the command line arguments
397
453
*/
398
454
public static void main (String [] args ) {
455
+ setJvmProperties ();
456
+ config = parseCmdArgs (args );
457
+ logger .info ("Args {}" , Arrays .stream (args ).collect (Collectors .joining ("," )));
458
+ logger .info ("Parsed cmd configs: {}" , config );
459
+ if (config .headless ) {
460
+ logger .info ("Starting in headless mode.." );
461
+ setupMonocle ();
462
+ }
463
+ initializeSSLContext ();
399
464
launch (args );
400
465
}
401
466
0 commit comments