forked from tada/pljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
3135 lines (2901 loc) · 101 KB
/
Copy pathNode.java
File metadata and controls
3135 lines (2901 loc) · 101 KB
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) 2015-2024 Tada AB and other contributors, as listed below.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the The BSD 3-Clause License
* which accompanies this distribution, and is available at
* http://opensource.org/licenses/BSD-3-Clause
*
* Contributors:
* Chapman Flack (this file, 2020)
* PostgreSQL Global Development Group, Michael Paquier, Alvaro Herrera
* (PostgresNode.pm, 2015, of which similar methods here are ports)
*/
package org.postgresql.pljava.packaging;
import org.gjt.cuspy.JarX;
import java.io.InputStream;
import static java.lang.System.getProperty;
import static java.lang.System.setProperty;
import java.nio.ByteBuffer;
import static java.nio.charset.Charset.defaultCharset;
import java.util.regex.Matcher;
import static java.util.regex.Pattern.compile;
/*
* For "Node" behavior:
*/
import static java.lang.ProcessBuilder.Redirect.INHERIT;
import java.lang.reflect.InvocationHandler; // flexible SAM allowing exceptions
import java.lang.reflect.UndeclaredThrowableException;
import static java.lang.Thread.interrupted;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles.Lookup;
import static java.lang.invoke.MethodHandles.explicitCastArguments;
import static java.lang.invoke.MethodHandles.filterReturnValue;
import static java.lang.invoke.MethodHandles.publicLookup;
import static java.lang.invoke.MethodType.methodType;
import static java.net.InetAddress.getLoopbackAddress;
import static java.net.URLEncoder.encode;
import java.net.ServerSocket;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.file.Files.createTempFile;
import static java.nio.file.Files.createTempDirectory;
import static java.nio.file.Files.deleteIfExists;
import static java.nio.file.Files.exists;
import static java.nio.file.Files.getLastModifiedTime;
import static java.nio.file.Files.lines;
import static java.nio.file.Files.walk;
import static java.nio.file.Files.write;
import java.nio.file.Path;
import java.nio.file.Paths;
import static java.nio.file.StandardWatchEventKinds.*;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.nio.file.AccessDeniedException;
import java.nio.file.NoSuchFileException;
import java.sql.Connection;
import static java.sql.DriverManager.drivers;
import static java.sql.DriverManager.getConnection;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Types;
import java.sql.SQLException;
import java.sql.SQLWarning;
import javax.sql.rowset.RowSetProvider;
import javax.sql.rowset.WebRowSet;
import javax.sql.rowset.RowSetMetaDataImpl;
import java.util.ArrayDeque;
import java.util.Base64;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Objects;
import static java.util.Objects.requireNonNull;
import java.util.Properties;
import java.util.Random;
import java.util.Spliterator;
import static java.util.Spliterator.IMMUTABLE;
import static java.util.Spliterator.NONNULL;
import static java.util.Spliterator.ORDERED;
import static java.util.Spliterators.spliteratorUnknownSize;
import java.util.WeakHashMap;
import java.util.concurrent.Callable; // like a Supplier but allows exceptions!
import java.util.concurrent.CancellationException;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.jar.JarFile;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static java.util.stream.StreamSupport.stream;
/**
* Extends the JarX extraction tool to provide a {@code resolve} method that
* replaces prefixes {@code pljava/foo/} in path names stored in the archive
* with the result of {@code pg_config --foo}.
*<p>
* As this represents a second extra {@code .class} file that has to be added
* to the installer jar anyway, it will also contain some methods intended to be
* useful for tasks related to installation and testing. The idea is not to go
* overboard, but supply a few methods largely modeled on the most basic ones of
* PostgreSQL's {@code PostgreSQL::Test::Cluster} Perl module (formerly named
* {@code PostgresNode}, from which the name of this class was taken). The
* methods can be invoked from {@code jshell} if its classpath includes the
* installer jar (and one of the PostgreSQL JDBC drivers).
*<p>
* An
* <a href="../../../../../../develop/node.html">introduction with examples</a>
* is available.
*<p>
* Unlike the many capabilities of {@code PostgreSQL::Test::Cluster}, this only
* deals in TCP sockets bound to {@code localhost}
* ({@code StandardProtocolFamily.UNIX}
* finally arrived in Java 16 but this class does not support it yet) and only
* a few of the most basic operations.
*<p>
* As in JarX itself, some liberties with coding style may be taken here to keep
* this one extra {@code .class} file from proliferating into a bunch of them.
*<p>
* As the testing-related methods here are intended for ad-hoc or scripted use
* in {@code jshell}, they are typically declared to throw any checked
* exception, without further specifics. There are many overloads of methods
* named {@code q} and {@code qp} (mnemonic of query and query-print), to make
* interactive use in {@code jshell} comfortable with just a few static imports.
*/
public class Node extends JarX {
private Matcher m_prefix;
private int m_fsepLength;
private String m_lineSep;
private boolean m_dryrun = false;
private static Node s_jarxHelper = new Node(null, 0, null, null);
private static boolean s_jarProcessed = false;
private static String s_examplesJar;
private static String s_sharedObject;
/**
* Performs an ordinary installation, using {@code pg_config} or the
* corresponding system properties to learn where the files belong, and
* unpacking the files (not including this class or its ancestors) there.
*/
public static void main(String[] args) throws Exception
{
if ( args.length > 0 )
{
System.err.println("usage: java -jar filename.jar");
System.exit(1);
}
s_jarxHelper.extract();
}
/**
* Extracts the jar contents, just as done in the normal case of running
* this class with {@code java -jar}.
*<p>
* Only to be called on the singleton instance {@code s_jarxHelper}.
*<p>
* For a version that doesn't really extract anything, but still primes the
* {@code resolve} method to know where things <em>should be</em> extracted,
* see {@link #dryExtract}.
*/
@Override
public void extract() throws Exception
{
super.extract();
s_jarProcessed = true;
}
/**
* Prepares the resolver, ignoring the passed string (ordinarily a script or
* rules); this resolver's rules are hardcoded.
*/
@Override
public void prepareResolver(String v) throws Exception
{
m_prefix = compile("^pljava/([^/]+dir)(?![^/])").matcher("");
m_fsepLength = getProperty("file.separator").length();
m_lineSep = getProperty("line.separator");
}
/**
* Replaces a prefix {@code pljava/}<em>key</em> in a path to be extracted
* with the value of the {@code pgconfig.}<em>key</em> system property, or
* the result of invoking {@code pg_config} (or the exact executable named
* in the {@code pgconfig} system property, if present) with the option
* {@code --}<em>key</em>.
*/
@Override
public String resolve(String storedPath, String platformPath)
throws Exception
{
if ( m_prefix.reset(storedPath).lookingAt() )
{
int prefixLength = m_prefix.end();
String key = m_prefix.group(1);
String propkey = "pgconfig." + key;
String replacement = getProperty(propkey);
if ( null == replacement )
{
String pgc = getProperty("pgconfig", "pg_config");
ProcessBuilder pb = new ProcessBuilder(pgc, "--"+key);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
Process proc = pb.start();
byte[] output;
try ( InputStream instream = proc.getInputStream() )
{
proc.getOutputStream().close();
output = instream.readAllBytes();
}
finally
{
int status = proc.waitFor();
if ( 0 != status )
{
System.err.println(
"ERROR: pg_config status is "+status);
System.exit(1);
}
}
/*
* pg_config output is the saved value followed by one \n only.
* However, on Windows, the C library treats stdout as text mode
* by default, and pg_config does nothing to change that, so the
* single \n written by pg_config gets turned to \r\n before it
* arrives here. The earlier use of the trim() method papered
* over the problem, but trim() can remove too much. Simply have
* to assume that the string will end with line.separator, and
* remove that.
*/
replacement = defaultCharset().newDecoder()
.decode(ByteBuffer.wrap(output, 0, output.length))
.toString();
assert replacement.endsWith(m_lineSep);
replacement = replacement.substring(0,
replacement.length() - m_lineSep.length());
setProperty(propkey, replacement);
}
int plen = m_fsepLength - 1; /* original separator had length 1 */
plen += prefixLength;
replacement += platformPath.substring(plen);
if ( -1 != storedPath.indexOf("/pljava-examples-") )
s_examplesJar = replacement;
else if ( storedPath.matches(
"pljava/pkglibdir/(?:lib)?+pljava-so-.*") )
s_sharedObject = replacement;
if ( ! m_dryrun )
return replacement;
return null;
}
System.err.println("WARNING: extraneous jar entry not extracted: "
+ storedPath);
return null;
}
/*
* Members below this point represent the state and behavior of an instance
* of this class that is acting as a "Node" rather than as the JarX helper.
*/
/**
* True if the platform is determined to be Windows.
*<p>
* On Windows, {@link #forWindowsCRuntime forWindowsCRuntime} should be
* applied to any {@code ProcessBuilder} before invoking it; the details of
* the transformation applied by
* {@link #asPgCtlInvocation asPgCtlInvocation} change, and
* {@link #use_pg_ctl use_pg_ctl} may prove useful, as {@code pg_ctl} on
* Windows is able to drop administrative privileges that would otherwise
* prevent {@code postgres} from starting.
*/
public static final boolean s_isWindows =
getProperty("os.name").startsWith("Windows");
/**
* The first form of PostgreSQL JDBC driver connection URL found to be
* recognized by an available driver, or {@code URL_FORM_NONE}.
*/
public static final int s_urlForm;
/**
* Value of {@link #s_urlForm s_urlForm} indicating no available JDBC driver
* was found to accept any of the supported connection URL forms.
*/
public static final int URL_FORM_NONE = -1;
/**
* Value of {@link #s_urlForm s_urlForm} indicating an available JDBC driver
* reported accepting a connection URL in the PGJDBC form starting with
* {@code "jdbc:postgresql:"}.
*/
public static final int URL_FORM_PGJDBC = 0;
/**
* Value of {@link #s_urlForm s_urlForm} indicating an available JDBC driver
* reported accepting a connection URL in the pgjdbc-ng form starting with
* {@code "jdbc:pgsql:"}.
*/
public static final int URL_FORM_PGJDBCNG = 1;
/**
* A function to map an {@code SQLWarning} to a rough classification
* (info, warning) of its severity.
*<p>
* If the PGJDBC {@code PSQLWarning} class is available for access to the
* severity tag from the backend, "warning" will be returned if that tag is
* {@code WARNING}, and "info" will be returned in any other case. (The next
* more severe backup level is {@code ERROR}, which would not appear here as
* an {@code SQLWarning}.)
*<p>
* If the severity tag is not available, "info" will be returned if the
* class (leftmost two positions of SQLState) is 00, otherwise "warning".
*/
private static final Function<SQLWarning,String> s_toSeverity;
private static String s_WARNING_localized = "WARNING";
/**
* Changes the severity string used to recognize when the backend is sending
* a {@code WARNING}.
*<p>
* When the driver is PGJDBC, the classification done here of
* {@code SQLWarning} instances into actual warning messages or informative
* ones depends on a tag ("WARNING" in English) that the backend delivers
* in the local language. For the classification to happen correctly when
* a different language is selected, use this method to supply the string
* (for example, "PERINGATAN" in Indonesian) that the backend uses for
* warnings in that language.
*/
public static void set_WARNING_localized(String s)
{
s_WARNING_localized = requireNonNull(s);
}
static
{
String[] candidateURLs = { "jdbc:postgresql:", "jdbc:pgsql:x" };
s_urlForm =
IntStream.range(0, candidateURLs.length)
.filter(i ->
drivers().anyMatch(d ->
{
try
{
return d.acceptsURL(candidateURLs[i]);
}
catch ( SQLException e )
{
throw new ExceptionInInitializerError(e);
}
}))
.findFirst()
.orElse(URL_FORM_NONE);
Function<SQLWarning,String> toSeverity = Node::toSeverityFallback;
try
{
Class<?> psqlWarning =
Class.forName("org.postgresql.util.PSQLWarning");
Class<?> sErrMessage =
Class.forName("org.postgresql.util.ServerErrorMessage");
Lookup pub = publicLookup();
MethodHandle getserrm =
pub.findVirtual(psqlWarning, "getServerErrorMessage",
methodType(sErrMessage));
MethodHandle getSev =
pub.findVirtual(sErrMessage, "getSeverity",
methodType(String.class));
MethodHandle h = explicitCastArguments(
filterReturnValue(getserrm, getSev),
methodType(String.class, Object.class));
toSeverity = w ->
{
if ( psqlWarning.isInstance(w) )
{
try
{
String s = (String)h.invokeExact(psqlWarning.cast(w));
if ( null == s || s_WARNING_localized.equals(s) )
return "warning";
return "info";
}
catch ( Throwable t )
{
throw new UndeclaredThrowableException(t, t.getMessage());
}
}
return toSeverityFallback(w);
};
}
catch ( ReflectiveOperationException e )
{
}
s_toSeverity = toSeverity;
}
private static String toSeverityFallback(SQLWarning w)
{
if ( w.getSQLState().startsWith("00") )
return "info";
else
return "warning";
}
/**
* A state (see {@link #stateMachine stateMachine}) that expects nothing
* (if the driver is pgjdbc-ng) or a zero row count (if the driver is
* PGJDBC).
*<p>
* For some utility statements (such as {@code CREATE EXTENSION}) with no
* result, the pgjdbc-ng driver will produce no result, while the PGJDBC
* driver produces a zero count, as it would for a DML statement that did
* not affect any rows. This state handles either case.
*<p>
* When {@code URL_FORM_PGJDBCNG == s_urlForm}, this state consumes nothing
* and moves to the numerically next state. Otherwise (JDBC), it checks
* that the current object is a zero row count, consuming it and moving to
* the numerically next state if it is, returning false otherwise.
*/
public static final InvocationHandler NOTHING_OR_PGJDBC_ZERO_COUNT=(o,p,q)->
{
int myStateNum = (int)q[0];
if ( URL_FORM_PGJDBCNG == s_urlForm )
return -(1 + myStateNum);
return 0 == as(Long.class, o) ? 1 + myStateNum : false;
};
/**
* Name of a "Node"; null for an ordinary Node instance.
*/
private final String m_name;
/**
* A TCP port on {@code localhost} that was free when {@code get_new_node}
* was called, and is likeliest to still be free if {@code start} is then
* called without undue delay.
*/
private final int m_port;
/**
* A temporary base directory chosen and created in {@code java.io.tmpdir}
* by {@code get_new_node} and removed by {@code clean_node}.
*/
private final Path m_basedir;
/**
* A password generated at {@code get_new_node} time, and used by
* {@code init} as the database-superuser password passed to {@code initdb}.
*/
private final String m_password;
/**
* The server process handle after a successful {@code start}
* via {@code pg_ctl}; null again after a successful {@code stop}.
*<p>
* If {@code pg_ctl} was not used, this will be null and {@code m_server}
* will have a value.
*/
private ProcessHandle m_serverHandle;
/**
* The server process after a successful {@code start}; null again after a
* successful {@code stop}.
*<p>
* If {@code pg_ctl} was used to start the server, this will be null and
* {@code m_serverHandle} will have a value after {@code wait_for_pid_file}.
*/
private Process m_server;
/**
* A count of connections, used to supply a distinct default
* {@code ApplicationName} per connection.
*/
private long m_connCount = 0;
/**
* Whether to invoke {@code postgres} directly when starting the server,
* or use {@code pg_ctl} to start and stop it.
*<p>
* On Windows, {@code pg_ctl} is able to drop administrator rights and
* start the server from an account that would otherwise trigger
* the server's refusal to start from a privileged account.
*/
private boolean m_usePostgres = true;
/**
* A weakly-held collection of {@link Connection}s, so that any remaining
* unclosed when {@link #stop(UnaryOperator) stop} is called can be closed
* then.
*<p>
* Java takes care of removing {@code Connection}s from this map as they
* become unreachable. In case any become unreachable before being closed,
* both supported JDBC drivers have cleaner actions that will eventually
* close them.
*/
private final WeakHashMap<Connection,Void> m_connections;
/**
* True during a {@link #stop(UnaryOperator) stop} call.
*<p>
* Used to prevent any new unclosed {@code Connection} being added to
* {@link m_connections m_connections} undetected.
*/
private boolean m_stopping = false;
/**
* Identifying information for a "node" instance, or for the singleton
* extractor instance.
*/
@Override
public String toString()
{
if ( null == m_name )
return "Extractor instance";
return "\"Node\": " + m_name;
}
/**
* Constructs an instance; all nulls for the parameters are passed by the
* static initializer to make the singleton extractor instance, and any
* other instance is constructed by {@code get_new_node} for controlling
* a PostgreSQL instance.
*/
private Node(String nodeName, int port, Path basedir, String password)
{
m_name = nodeName;
m_port = port;
m_basedir = basedir;
m_password = password;
m_connections = null == nodeName ? null : new WeakHashMap<>();
}
/**
* Returns a new {@code Node} that can be used to initialize and start a
* PostgreSQL instance.
*<p>
* Establishes a VM shutdown hook that will stop the server (if started)
* and recursively remove the <em>basedir</em> before the VM exits.
*/
public static Node get_new_node(String name) throws Exception
{
byte[] pwbytes = new byte [ 6 ];
new Random().nextBytes(pwbytes);
Node n = new Node(
requireNonNull(name),
get_free_port(),
createTempDirectory("t_pljava_" + name + "_data"),
Base64.getEncoder().encodeToString(pwbytes));
Thread t =
new Thread(() ->
{
try
{
n.stop();
n.clean_node();
}
catch ( Exception e )
{
e.printStackTrace();
}
}, "Node " + name + " shutdown");
Runtime.getRuntime().addShutdownHook(t);
return n;
}
/**
* Returns a TCP port on the loopback interface that is free at the moment
* this method is called.
*/
public static int get_free_port() throws Exception
{
try (ServerSocket s = new ServerSocket(0, 0, getLoopbackAddress()))
{
return s.getLocalPort();
}
}
/**
* Recursively removes the <em>basedir</em> and its descendants.
*/
public void clean_node() throws Exception
{
clean_node(false);
}
/**
* Recursively removes the <em>basedir</em> (unless <var>keepRoot</var>)
* and its descendants.
* @param keepRoot if true, the descendants are removed, but not the basedir
* itself.
*/
public void clean_node(boolean keepRoot) throws Exception
{
/*
* How can Java *still* not have a deleteTree()?
*/
ArrayDeque<Path> stk = new ArrayDeque<>();
for ( Path p : (Iterable<Path>)walk(m_basedir)::iterator )
{
while ( ! stk.isEmpty() && ! p.startsWith(stk.peek()) )
{
Path toDelete = stk.pop();
try
{
deleteIfExists(toDelete);
}
catch ( AccessDeniedException e )
{
if (!toDelete.equals(data_dir().resolve("postmaster.pid")))
throw e;
/*
* See comments for stopViaPgCtl regarding this weirdness.
*/
Thread.sleep(500);
deleteIfExists(toDelete);
}
}
stk.push(p);
}
if ( keepRoot )
stk.pollLast();
for ( Path p : stk )
deleteIfExists(p);
}
/**
* Processes the jar without really extracting, to compute
* the path mappings.
*/
private static void dryExtract() throws Exception
{
if ( s_jarProcessed )
return;
try
{
s_jarxHelper.m_dryrun = true;
s_jarxHelper.extract();
}
finally
{
s_jarxHelper.m_dryrun = false;
}
}
/**
* Given a path from the archive, or any path <em>resembling</em> one in
* the archive (that is, always {@code /} as the separator, and starting
* with {@code pljava/}<em>key</em> where {@code --}<em>key</em> is known
* to {@code pg_config}, returns the platform-specific path where it would
* be installed.
*/
private static String resolve(String archivePath) throws Exception
{
return s_jarxHelper.resolve(
archivePath, Paths.get("", archivePath.split("/")).toString());
}
/**
* Returns the directory name to be used as the PostgreSQL data directory
* for this node.
*/
public Path data_dir()
{
return m_basedir.resolve("pgdata");
}
/**
* Like {@code init()} but returns an {@code AutoCloseable} that will
* recursively remove the files and directories under the <em>basedir</em>
* (but not the <em>basedir</em> itself) on the exit of a calling
* try-with-resources scope.
*/
public AutoCloseable initialized_cluster()
throws Exception
{
return initialized_cluster(Map.of(), UnaryOperator.identity());
}
/**
* Like {@code init()} but returns an {@code AutoCloseable} that will
* recursively remove the files and directories under the <em>basedir</em>
* (but not the <em>basedir</em> itself) on the exit of a calling
* try-with-resources scope.
*/
public AutoCloseable initialized_cluster(Map<String,String> suppliedOptions)
throws Exception
{
return initialized_cluster(suppliedOptions, UnaryOperator.identity());
}
/**
* Like {@link #init(Map,UnaryOperator) init()} but returns
* an {@code AutoCloseable} that will
* recursively remove the files and directories under the <em>basedir</em>
* (but not the <em>basedir</em> itself) on the exit of a calling
* try-with-resources scope.
*/
public AutoCloseable initialized_cluster(
UnaryOperator<ProcessBuilder> tweaks)
throws Exception
{
return initialized_cluster(Map.of(), tweaks);
}
/**
* Like {@link #init(Map,UnaryOperator) init()} but returns
* an {@code AutoCloseable} that will
* recursively remove the files and directories under the <em>basedir</em>
* (but not the <em>basedir</em> itself) on the exit of a calling
* try-with-resources scope.
*/
public AutoCloseable initialized_cluster(
Map<String,String> suppliedOptions,
UnaryOperator<ProcessBuilder> tweaks)
throws Exception
{
init(suppliedOptions, tweaks);
return () ->
{
clean_node(true);
};
}
/**
* Invokes {@code initdb} for the node, passing default options appropriate
* for this setting.
*/
public void init() throws Exception
{
init(Map.of(), UnaryOperator.identity());
}
/**
* Invokes {@code initdb} for the node, with <em>suppliedOptions</em>
* overriding or supplementing the ones that would be passed by default.
*/
public void init(Map<String,String> suppliedOptions) throws Exception
{
init(suppliedOptions, UnaryOperator.identity());
}
/**
* Invokes {@code initdb} for the node, passing default options appropriate
* for this setting, and {@linkplain #init(Map,UnaryOperator) tweaks} to be
* applied to the {@code ProcessBuilder} before it is started.
*/
public void init(UnaryOperator<ProcessBuilder> tweaks) throws Exception
{
init(Map.of(), tweaks);
}
/**
* Invokes {@code initdb} for the node, with <em>suppliedOptions</em>
* overriding or supplementing the ones that would be passed by default,
* and <em>tweaks</em> to be applied to the {@code ProcessBuilder}
* before it is started.
*<p>
* By default, {@code postgres} will be the name of the superuser, UTF-8
* will be the encoding, {@code auth-local} will be {@code peer} and
* {@code auth-host} will be {@code md5}. The initialization will skip
* {@code fsync} for speed rather than safety (if something goes wrong, just
* {@code clean_node()} and start over).
*<p>
* The {@code initdb} that will be run is the one in the {@code bindir}
* reported by {@code pg_config} (or set by {@code -Dpgconfig.bindir}).
* @param suppliedOptions a Map where each key is an option to initdb
* (for example, --encoding), and the value corresponds.
* @param tweaks a lambda applicable to the {@code ProcessBuilder} to
* further configure it. On Windows, the tweaks will be applied ahead of
* transformation of the arguments by
* {@link #forWindowsCRuntime forWindowsCRuntime}.
*/
public void init(
Map<String,String> suppliedOptions,
UnaryOperator<ProcessBuilder> tweaks) throws Exception
{
dryExtract();
/*
* For extract/install purposes, there is already a resolve() method
* that expands keys like pljava/bindir to pg_config --bindir output.
*/
String initdb = resolve("pljava/bindir/initdb");
if ( s_isWindows )
{
/*
* This is irksome. The mingw64 postgresql package has both
* initdb.exe and initdb, a bash script that runs it under winpty.
* If the script were not there, the .exe suffix would be added
* implicitly, but with both there, we try to exec the bash script.
*/
Path p1 = Paths.get(initdb);
Path p2 = Paths.get(initdb + ".exe");
if ( exists(p1) && exists(p2) )
initdb = p2.toString();
}
Path pwfile = createTempFile(m_basedir, "pw", "");
Map<String,String> options = new HashMap<>(suppliedOptions);
options.putIfAbsent("--pgdata", data_dir().toString());
options.putIfAbsent("--username", "postgres");
options.putIfAbsent("--encoding", "utf-8");
options.putIfAbsent("--pwfile", pwfile.toString());
options.putIfAbsent("--auth-local", "peer");
options.putIfAbsent("--auth-host", "md5");
options.putIfAbsent("-N", null);
String[] args =
Stream.concat(
Stream.of(initdb),
options.entrySet().stream()
.flatMap(e ->
null == e.getValue()
? Stream.of(e.getKey())
: Stream.of(e.getKey(), e.getValue()))
)
.toArray(String[]::new);
try
{
write(pwfile, List.of(m_password), US_ASCII);
ProcessBuilder pb =
new ProcessBuilder(args)
.redirectOutput(INHERIT)
.redirectError(INHERIT);
pb = tweaks.apply(pb);
if ( s_isWindows )
pb = forWindowsCRuntime(pb);
Process p = pb.start();
p.getOutputStream().close();
if ( 0 != p.waitFor() )
throw new AssertionError(
"Nonzero initdb result: " + p.waitFor());
}
finally
{
deleteIfExists(pwfile);
}
}
/**
* Like {@code start()} but returns an {@code AutoCloseable} that will
* stop the server on the exit of a calling try-with-resources scope.
*/
public AutoCloseable started_server()
throws Exception
{
return started_server(Map.of(), UnaryOperator.identity());
}
/**
* Like {@code start()} but returns an {@code AutoCloseable} that will
* stop the server on the exit of a calling try-with-resources scope.
*/
public AutoCloseable started_server(Map<String,String> suppliedOptions)
throws Exception
{
return started_server(suppliedOptions, UnaryOperator.identity());
}
/**
* Like {@link #start(Map,UnaryOperator) start()} but returns
* an {@code AutoCloseable} that will
* stop the server on the exit of a calling try-with-resources scope.
*<p>
* Supplied <em>tweaks</em> will be applied to the {@code ProcessBuilder}
* used to start the server; if {@code pg_ctl} is being used, they will also
* be applied when running {@code pg_ctl stop} to stop it.
*/
public AutoCloseable started_server(UnaryOperator<ProcessBuilder> tweaks)
throws Exception
{
return started_server(Map.of(), tweaks);
}
/**
* Like {@link #start(Map,UnaryOperator) start()} but returns
* an {@code AutoCloseable} that will
* stop the server on the exit of a calling try-with-resources scope.
*<p>
* Supplied <em>tweaks</em> will be applied to the {@code ProcessBuilder}
* used to start the server; if {@code pg_ctl} is being used, they will also
* be applied when running {@code pg_ctl stop} to stop it.
*/
public AutoCloseable started_server(
Map<String,String> suppliedOptions,
UnaryOperator<ProcessBuilder> tweaks)
throws Exception
{
start(suppliedOptions, tweaks);
return () ->
{
stop(tweaks);
};
}
/**
* Starts a PostgreSQL server for the node with default options appropriate
* for this setting.
*/
public void start() throws Exception
{
start(Map.of(), UnaryOperator.identity());
}
/**
* Starts a PostgreSQL server for the node, with <em>suppliedOptions</em>
* overriding or supplementing the ones that would be passed by default.
*/
public void start(Map<String,String> suppliedOptions) throws Exception
{
start(suppliedOptions, UnaryOperator.identity());
}
/**
* Starts a PostgreSQL server for the node, passing default options
* appropriate for this setting, and
* {@linkplain #start(Map,UnaryOperator) tweaks} to be
* applied to the {@code ProcessBuilder} before it is started.
*/
public void start(UnaryOperator<ProcessBuilder> tweaks) throws Exception
{
start(Map.of(), tweaks);
}
/**
* Starts a PostgreSQL server for the node, with <em>suppliedOptions</em>
* overriding or supplementing the ones that would be passed by default, and
* <em>tweaks</em> to be applied to the {@code ProcessBuilder} before it
* is started.
*<p>
* By default, the server will listen only on the loopback interface and
* not on any Unix-domain socket, on the port selected when this Node was
* created, and for a maximum of 16 connections. Its cluster name will be
* the name given to this Node, and fsync will be off to favor speed over
* durability. The log line prefix will be shortened to just the node name
* and (when connected) the {@code application_name}.
*<p>
* The server that will be run is the one in the {@code bindir}
* reported by {@code pg_config} (or set by {@code -Dpgconfig.bindir}).
*<p>
* If the server is PostgreSQL 10 or later, it is definitely ready to accept
* connections when this method returns. If not, it is highly likely to be
* ready, but no test connection has been made to confirm it.
* @param suppliedOptions a Map where the key is a configuration variable
* name as seen in {@code postgresql.conf} or passed to the server with
* {@code -c} and the value corresponds.
* @param tweaks a lambda applicable to the {@code ProcessBuilder} to
* further configure it. Under {@link #use_pg_ctl use_pg_ctl(true)}, the
* tweaks are applied after the arguments have been transformed by
* {@link #asPgCtlInvocation asPgCtlInvocation}. On Windows, they are
* applied ahead of transformation of the arguments by
* {@link #forWindowsCRuntime forWindowsCRuntime}.
*/
public void start(
Map<String,String> suppliedOptions,
UnaryOperator<ProcessBuilder> tweaks) throws Exception
{
if ( null != m_server && m_server.isAlive() )
throw new IllegalStateException(