SelfHoster: Avoid some compiler warnings.
[online-glom:gwt-glom.git] / src / test / java / org / glom / web / server / SelfHoster.java
1 /*
2  * Copyright (C) 2012 Openismus GmbH
3  *
4  * This file is part of GWT-Glom.
5  *
6  * GWT-Glom is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with GWT-Glom.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package org.glom.web.server;
21
22 import java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.net.ServerSocket;
27 import java.sql.Connection;
28 import java.sql.DriverManager;
29 import java.sql.SQLException;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Locale;
33 import java.util.Map;
34 import java.util.Map.Entry;
35 import java.util.Properties;
36
37 import org.apache.commons.lang3.StringUtils;
38 import org.glom.web.server.libglom.Document;
39 import org.glom.web.shared.DataItem;
40 import org.glom.web.shared.libglom.Field;
41 import org.jooq.InsertResultStep;
42 import org.jooq.InsertSetStep;
43 import org.jooq.Record;
44 import org.jooq.SQLDialect;
45 import org.jooq.Table;
46 import org.jooq.exception.DataAccessException;
47 import org.jooq.impl.Factory;
48
49 import com.google.common.io.Files;
50 import com.ibm.icu.text.NumberFormat;
51
52 /**
53  * @author Murray Cumming <murrayc@murrayc.com>
54  * 
55  */
56 public class SelfHoster {
57         // private String tempFilepathDir = "";
58         private boolean selfHostingActive = false;
59         private Document document = null;
60         private String username = "";
61         private String password = "";
62
63         SelfHoster(final Document document) {
64                 this.document = document;
65         }
66
67         private static final int PORT_POSTGRESQL_SELF_HOSTED_START = 5433;
68         private static final int PORT_POSTGRESQL_SELF_HOSTED_END = 5500;
69
70         private static final String DEFAULT_CONFIG_PG_HBA_LOCAL_8p4 = "# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD\n"
71                         + "\n"
72                         + "# local is for Unix domain socket connections only\n"
73                         + "# trust allows connection from the current PC without a password:\n"
74                         + "local   all         all                               trust\n"
75                         + "local   all         all                               ident\n"
76                         + "local   all         all                               md5\n"
77                         + "\n"
78                         + "# TCP connections from the same computer, with a password:\n"
79                         + "host    all         all         127.0.0.1    255.255.255.255    md5\n"
80                         + "# IPv6 local connections:\n"
81                         + "host    all         all         ::1/128               md5\n";
82
83         private static final String DEFAULT_CONFIG_PG_IDENT = "";
84         private static final String FILENAME_DATA = "data";
85
86         public boolean createAndSelfHostFromExample(final Document.HostingMode hostingMode) {
87
88                 if (!createAndSelfHostNewEmpty(hostingMode)) {
89                         // std::cerr << G_STRFUNC << ": test_create_and_selfhost_new_empty() failed." << std::endl;
90                         return false;
91                 }
92
93                 final boolean recreated = recreateDatabaseFromDocument(); /* TODO: Progress callback */
94                 if (!recreated) {
95                         if (!cleanup()) {
96                                 return false;
97                         }
98                 }
99
100                 return recreated;
101         }
102
103         /**
104          * @param document
105          * @param
106          * @param subDirectoryPath
107          * @return
108          */
109         private boolean createAndSelfHostNewEmpty(final Document.HostingMode hostingMode) {
110                 if (hostingMode != Document.HostingMode.HOSTING_MODE_POSTGRES_SELF) {
111                         // TODO: std::cerr << G_STRFUNC << ": This test function does not support the specified hosting_mode: " <<
112                         // hosting_mode << std::endl;
113                         return false;
114                 }
115
116                 // Save a copy, specifying the path to file in a directory:
117                 // For instance, /tmp/testglom/testglom.glom");
118                 final String tempFilename = "testglom";
119                 final File tempFolder = Files.createTempDir();
120                 final File tempDir = new File(tempFolder, tempFilename);
121
122                 final String tempDirPath = tempDir.getPath();
123                 final String tempFilePath = tempDirPath + File.separator + tempFilename;
124                 final File file = new File(tempFilePath);
125
126                 // Make sure that the file does not exist yet:
127                 {
128                         tempDir.delete();
129                 }
130
131                 // Save the example as a real file:
132                 document.setFileURI(file.getPath());
133
134                 document.setHostingMode(hostingMode);
135                 document.setIsExampleFile(false);
136                 final boolean saved = document.save();
137                 if (!saved) {
138                         System.out.println("createAndSelfHostNewEmpty(): Document.save() failed.");
139                         return false; // TODO: Delete the directory.
140                 }
141
142                 // We must specify a default username and password:
143                 final String user = "glom_default_developer_user";
144                 final String password = "glom_default_developer_password";
145
146                 // Create the self-hosting files:
147                 if (!initialize(user, password)) {
148                         System.out.println("createAndSelfHostNewEmpty(): initialize failed.");
149                         // TODO: Delete directory.
150                 }
151
152                 // Check that it really created some files:
153                 if (!tempDir.exists()) {
154                         System.out.println("createAndSelfHostNewEmpty(): tempDir does not exist.");
155                         // TODO: Delete directory.
156                 }
157
158                 return selfHost(user, password);
159         }
160
161         /**
162          * @param document
163          * @param user
164          * @param password
165          * @return
166          */
167         private boolean selfHost(final String user, final String password) {
168                 // TODO: m_network_shared = network_shared;
169
170                 if (getSelfHostingActive()) {
171                         // TODO: std::cerr << G_STRFUNC << ": Already started." << std::endl;
172                         return false; // STARTUPERROR_NONE; //Just do it once.
173                 }
174
175                 final String dbDirData = getSelfHostingDataPath(false);
176                 if (StringUtils.isEmpty(dbDirData) || !fileExists(dbDirData)) {
177                         /*
178                          * final String dbDirBackup = dbDir + File.separator + FILENAME_BACKUP;
179                          * 
180                          * if(fileExists(dbDirBackup)) { //TODO: std::cerr << G_STRFUNC <<
181                          * ": There is no data, but there is backup data." << std::endl; //Let the caller convert the backup to real
182                          * data and then try again: return false; // STARTUPERROR_FAILED_NO_DATA_HAS_BACKUP_DATA; } else {
183                          */
184                         // TODO: std::cerr << "ConnectionPool::create_self_hosting(): The data sub-directory could not be found." <<
185                         // dbdir_data_uri << std::endl;
186                         return false; // STARTUPERROR_FAILED_NO_DATA;
187                         // }
188                 }
189
190                 final int availablePort = discoverFirstFreePort(PORT_POSTGRESQL_SELF_HOSTED_START,
191                                 PORT_POSTGRESQL_SELF_HOSTED_END);
192                 // std::cout << "debug: " << G_STRFUNC << ":() : debug: Available port for self-hosting: " << available_port <<
193                 // std::endl;
194                 if (availablePort == 0) {
195                         // TODO: Use a return enum or exception so we can tell the user about this:
196                         // TODO: std::cerr << G_STRFUNC << ": No port was available between " << PORT_POSTGRESQL_SELF_HOSTED_START
197                         // << " and " << PORT_POSTGRESQL_SELF_HOSTED_END << std::endl;
198                         return false; // STARTUPERROR_FAILED_UNKNOWN_REASON;
199                 }
200
201                 final NumberFormat format = NumberFormat.getInstance(Locale.US);
202                 format.setGroupingUsed(false); // TODO: Does this change it system-wide?
203                 final String portAsText = format.format(availablePort);
204
205                 // -D specifies the data directory.
206                 // -c config_file= specifies the configuration file
207                 // -k specifies a directory to use for the socket. This must be writable by us.
208                 // Make sure to use double quotes for the executable path, because the
209                 // CreateProcess() API used on Windows does not support single quotes.
210                 final String dbDir = getSelfHostingPath("", false);
211                 final String dbDirConfig = getSelfHostingPath("config", false);
212                 final String dbDirHba = dbDirConfig + File.separator + "pg_hba.conf";
213                 final String dbDirIdent = dbDirConfig + File.separator + "pg_ident.conf";
214                 final String dbDirPid = getSelfHostingPath("pid", false);
215
216                 // Note that postgres returns this error if we split the arguments more,
217                 // for instance splitting -D and dbDirData into separate strings:
218                 // too many command-line arguments (first is "(null)")
219                 // Note: If we use "-D " instead of "-D" then the initdb seems to make the space part of the filepath,
220                 // though that does not happen with the normal command line.
221                 // However, we must have a space after -k.
222                 // Also, the c hba_file=path argument must be split after -c, or postgres will get a " hba_file" configuration
223                 // parameter instead of "hba_file".
224                 final String commandPathStart = getPathToPostgresExecutable("postgres");
225                 if (StringUtils.isEmpty(commandPathStart)) {
226                         System.out.println("selfHost(): getPathToPostgresExecutable(postgres) failed.");
227                         return false;
228                 }
229                 final ProcessBuilder commandPostgresStart = new ProcessBuilder(commandPathStart, "-D" + shellQuote(dbDirData),
230                                 "-p", portAsText, "-i", // Equivalent to -h "*", which in turn is equivalent
231                                                                                 // to
232                                 // listen_addresses in postgresql.conf. Listen to all IP addresses,
233                                 // so any client can connect (with a username+password)
234                                 "-c", "hba_file=" + shellQuote(dbDirHba), "-c", "ident_file=" + shellQuote(dbDirIdent), "-k"
235                                                 + shellQuote(dbDir), "--external_pid_file=" + shellQuote(dbDirPid));
236                 // std::cout << G_STRFUNC << ": debug: " << command_postgres_start << std::endl;
237
238                 // Make sure to use double quotes for the executable path, because the
239                 // CreateProcess() API used on Windows does not support single quotes.
240                 //
241                 // Note that postgres returns this error if we split the arguments more,
242                 // for instance splitting -D and dbDirData into separate strings:
243                 // too many command-line arguments (first is "(null)")
244                 // Note: If we use "-D " instead of "-D" then the initdb seems to make the space part of the filepath,
245                 // though that does not happen with the normal command line.
246                 final String commandPathCheck = getPathToPostgresExecutable("pg_ctl");
247                 if (StringUtils.isEmpty(commandPathCheck)) {
248                         System.out.println("selfHost(): getPathToPostgresExecutable(pg_ctl) failed.");
249                         return false;
250                 }
251                 final ProcessBuilder commandCheckPostgresHasStarted = new ProcessBuilder(commandPathCheck, "status", "-D"
252                                 + shellQuote(dbDirData));
253
254                 // For postgres 8.1, this is "postmaster is running".
255                 // For postgres 8.2, this is "server is running".
256                 // This is a big hack that we should avoid. murrayc.
257                 //
258                 // pg_ctl actually seems to return a 0 result code for "is running" and a 1 for not running, at least with
259                 // Postgres 8.2,
260                 // so maybe we can avoid this in future.
261                 // Please do test it with your postgres version, using "echo $?" to see the result code of the last command.
262                 final String secondCommandSuccessText = "is running"; // TODO: This is not a stable API. Also, watch out for
263                                                                                                                                 // localisation.
264
265                 // The first command does not return, but the second command can check whether it succeeded:
266                 // TODO: Progress
267                 final boolean result = executeCommandLineAndWaitUntilSecondCommandReturnsSuccess(commandPostgresStart,
268                                 commandCheckPostgresHasStarted, secondCommandSuccessText);
269                 if (!result) {
270                         System.out.println("selfHost(): Error while attempting to self-host a database.");
271                         return false; // STARTUPERROR_FAILED_UNKNOWN_REASON;
272                 }
273
274                 // Remember the port for later:
275                 document.setConnectionPort(availablePort);
276
277                 // Check that we can really connect:
278
279                 // pg_ctl sometimes reports success before it is really ready to let us connect,
280                 // so in this case we can just keep trying until it works, for a while:
281                 for (int i = 0; i < 10; i++) {
282
283                         try {
284                                 Thread.sleep(1000);
285                         } catch (InterruptedException e) {
286                                 // TODO Auto-generated catch block
287                                 e.printStackTrace();
288                         }
289
290                         final String dbName = document.getConnectionDatabase();
291                         document.setConnectionDatabase(""); // We have not created the database yet.
292                         final Connection connection = createConnection();
293                         document.setConnectionDatabase(dbName);
294                         if (connection != null) {
295                                 return true; // STARTUPERROR_NONE;
296                         }
297
298                         System.out
299                                         .println("selfHost(): Waiting and retrying the connection due to suspected too-early success of pg_ctl. retries="
300                                                         + i);
301                 }
302
303                 System.out.println("selfHost(): Test connection failed after multiple retries.");
304                 return false;
305         }
306
307         /**
308          * @param dbDirData
309          * @return
310          */
311         private String shellQuote(final String str) {
312                 // TODO: If we add the quotes then they seem to be used as part of the path, though that is not a problem with
313                 // the normal command line.
314                 return str;
315
316                 // TODO: Escape.
317                 // return "'" + str + "'";
318         }
319
320         private String getSelfHostingPath(final String subpath, final boolean create) {
321                 final String dbDir = document.getSelfHostedDirectoryPath();
322                 if (StringUtils.isEmpty(subpath)) {
323                         return dbDir;
324                 }
325
326                 final String dbDirData = dbDir + File.separator + subpath;
327                 final File file = new File(dbDirData);
328
329                 // Return the path regardless of whether it exists:
330                 if (!create) {
331                         return dbDirData;
332                 }
333
334                 if (!file.exists()) {
335                         try {
336                                 Files.createParentDirs(file);
337                         } catch (final IOException e) {
338                                 // TODO Auto-generated catch block
339                                 e.printStackTrace();
340                                 return "";
341                         }
342
343                         if (!file.mkdir()) {
344                                 return "";
345                         }
346                 }
347
348                 return dbDirData;
349         }
350
351         private String getSelfHostingDataPath(final boolean create) {
352                 return getSelfHostingPath(FILENAME_DATA, create);
353         }
354
355         private boolean executeCommandLineAndWait(final ProcessBuilder command) {
356
357                 command.redirectErrorStream(true);
358
359                 // Run the first command, and wait for it to return:
360                 Process process = null;
361                 try {
362                         process = command.start();
363                 } catch (final IOException e) {
364                         // TODO Auto-generated catch block
365                         e.printStackTrace();
366                         return false;
367                 }
368
369                 // final InputStream stderr = process.getInputStream();
370                 // final InputStreamReader isr = new InputStreamReader(stderr);
371                 // final BufferedReader br = new BufferedReader(isr);
372                 // String output = "";
373                 // String line;
374                 /*
375                  * try { //TODO: readLine() can hang, waiting for an end of line that never comes. while ((line = br.readLine())
376                  * != null) { output += line + "\n"; } } catch (final IOException e1) { e1.printStackTrace(); return false; }
377                  */
378
379                 int result = 0;
380                 try {
381                         result = process.waitFor();
382                 } catch (final InterruptedException e) {
383                         // TODO Auto-generated catch block
384                         e.printStackTrace();
385                         return false;
386                 }
387
388                 if (result != 0) {
389                         System.out.println("executeCommandLineAndWait(): Command failed: " + command.command().toString());
390                         // System.out.print("  Output: " + output);
391                         return false;
392                 }
393
394                 return true;
395         }
396
397         private boolean executeCommandLineAndWaitUntilSecondCommandReturnsSuccess(final ProcessBuilder command,
398                         final ProcessBuilder commandSecond, final String secondCommandSuccessText) {
399                 command.redirectErrorStream(true);
400
401                 // Run the first command, and do not wait for it to return:
402                 // Process process = null;
403                 try {
404                         // Process process =
405                         command.start();
406                 } catch (final IOException e) {
407                         // TODO Auto-generated catch block
408                         e.printStackTrace();
409                         return false;
410                 }
411
412                 // final InputStream stderr = process.getInputStream();
413                 // final InputStreamReader isr = new InputStreamReader(stderr);
414                 // final BufferedReader br = new BufferedReader(isr);
415
416                 /*
417                  * We do not wait, because this (postgres, for instance), does not return: final int result = process.waitFor();
418                  * if (result != 0) { // TODO: Warn. return false; }
419                  */
420
421                 // Now run the second command, usually to verify that the first command has really done its work:
422                 // We run this repeatedly until it succeeds, to show that the first command has finished.
423                 boolean result = false;
424                 while (true) {
425                         result = executeCommandLineAndWait(commandSecond);
426                         if (result) {
427                                 System.out.println("executeCommandLineAndWait(): second command succeeded.");
428                                 return true;
429                         } else {
430                                 try {
431                                         Thread.sleep(1000);
432                                 } catch (InterruptedException e) {
433                                         // TODO Auto-generated catch block
434                                         e.printStackTrace();
435                                         return false;
436                                 }
437
438                                 System.out.println("executeCommandLineAndWait(): Trying the second command again.");
439                         }
440                 }
441
442                 // Try to get the output:
443                 /*
444                  * if (!result) { String output = ""; /* String line; try { // TODO: readLine() can hang, waiting for an end of
445                  * line that never comes. while ((line = br.readLine()) != null) { output += line + "\n";
446                  * System.out.println(line); } } catch (final IOException e1) { // TODO Auto-generated catch block
447                  * e1.printStackTrace(); return false; }
448                  */
449
450                 // System.out.println("  Output of first command: " + output);
451                 // System.out.println("  first command: " + command.command().toString());
452                 // System.out.println("  second command: " + commandSecond.command().toString());
453                 // }
454         }
455
456         /**
457          * @param string
458          * @return
459          */
460         private static String getPathToPostgresExecutable(final String string) {
461                 final List<String> dirPaths = new ArrayList<String>();
462                 dirPaths.add("/usr/bin");
463                 dirPaths.add("/usr/lib/postgresql/9.1/bin");
464                 dirPaths.add("/usr/lib/postgresql/9.0/bin");
465                 dirPaths.add("/usr/lib/postgresql/8.4/bin");
466
467                 for (String dir : dirPaths) {
468                         final String path = dir + File.separator + string;
469                         if (fileExistsAndIsExecutable(path)) {
470                                 return path;
471                         }
472                 }
473
474                 return "";
475         }
476
477         /**
478          * @param path
479          * @return
480          */
481         private static boolean fileExistsAndIsExecutable(String path) {
482                 final File file = new File(path);
483                 if (!file.exists()) {
484                         return false;
485                 }
486
487                 if (!file.canExecute()) {
488                         return false;
489                 }
490
491                 return true;
492         }
493
494         /**
495          * @param start
496          * @param end
497          * @return
498          */
499         private static int discoverFirstFreePort(final int start, final int end) {
500                 for (int port = start; port <= end; ++port) {
501                         try {
502                                 final ServerSocket socket = new ServerSocket(port);
503
504                                 // If the instantiation succeeded then the port was free:
505                                 return socket.getLocalPort(); // This must equal port.
506                         } catch (final IOException ex) {
507                                 continue; // try next port
508                         }
509                 }
510
511                 return 0;
512         }
513
514         /**
515          * @param dbDir
516          * @return
517          */
518         private static boolean fileExists(final String filePath) {
519                 final File file = new File(filePath);
520                 return file.exists();
521         }
522
523         /**
524          * @return
525          */
526         private boolean getSelfHostingActive() {
527                 return selfHostingActive;
528         }
529
530         /**
531          * @param cpds
532          * @return
533          */
534         private boolean initialize(final String initialUsername, final String initialPassword) {
535                 if (!initializeConfFiles()) {
536                         System.out.println("initialize(): initializeConfFiles() failed.");
537                         return false;
538                 }
539
540                 // initdb creates a new postgres database cluster:
541
542                 // Get file:// URI for the tmp/ directory:
543                 File filePwFile = null;
544                 try {
545                         filePwFile = File.createTempFile("glom_initdb_pwfile", "");
546                 } catch (final IOException e) {
547                         // TODO Auto-generated catch block
548                         e.printStackTrace();
549                 }
550                 final String tempPwFile = filePwFile.getPath();
551
552                 final boolean pwfileCreationSucceeded = createTextFile(tempPwFile, initialPassword);
553                 if (!pwfileCreationSucceeded) {
554                         System.out.println("initialize(): createTextFile() failed.");
555                         return false;
556                 }
557
558                 // Make sure to use double quotes for the executable path, because the
559                 // CreateProcess() API used on Windows does not support single quotes.
560                 final String dbDirData = getSelfHostingDataPath(false /* create */);
561
562                 // Note that initdb returns this error if we split the arguments more,
563                 // for instance splitting -D and dbDirData into separate strings:
564                 // too many command-line arguments (first is "(null)")
565                 // TODO: If we quote tempPwFile then initdb says that it cannot find it.
566                 // Note: If we use "-D " instead of "-D" then the initdb seems to make the space part of the filepath,
567                 // though that does not happen with the normal command line.
568                 boolean result = false;
569                 final String commandPath = getPathToPostgresExecutable("initdb");
570                 if (StringUtils.isEmpty(commandPath)) {
571                         System.out.println("initialize(): getPathToPostgresExecutable(initdb) failed.");
572                 } else {
573                         final ProcessBuilder commandInitdb = new ProcessBuilder(commandPath, "-D" + shellQuote(dbDirData), "-U",
574                                         initialUsername, "--pwfile=" + tempPwFile);
575
576                         // Note that --pwfile takes the password from the first line of a file. It's an alternative to supplying it
577                         // when
578                         // prompted on stdin.
579                         result = executeCommandLineAndWait(commandInitdb);
580                 }
581
582                 // Of course, we don't want this to stay around. It would be a security risk.
583                 final File fileTempPwFile = new File(tempPwFile);
584                 if (!fileTempPwFile.delete()) {
585                         System.out.println("initialize(): Failed to delete the password file.");
586                 }
587
588                 if (!result) {
589                         System.out.println("initialize(): Error while attempting to create self-hosting database.");
590                         return false;
591                 }
592
593                 // Save the username and password for later;
594                 this.username = initialUsername;
595                 this.password = initialPassword;
596
597                 return result; // ? INITERROR_NONE : INITERROR_COULD_NOT_START_SERVER;
598
599         }
600
601         private boolean initializeConfFiles() {
602                 final String dataDirPath = document.getSelfHostedDirectoryPath();
603
604                 final String dbDirConfig = dataDirPath + File.separator + "config";
605                 // String defaultConfContents = "";
606
607                 // Choose the configuration contents based on the postgresql version
608                 // and whether we want to be network-shared:
609                 // final float postgresqlVersion = 9.0f; //TODO: get_postgresql_utils_version_as_number(slot_progress);
610                 // final boolean networkShared = true;
611                 // std::cout << "DEBUG: postgresql_version=" << postgresql_version << std::endl;
612
613                 // TODO: Support the other configurations, as in libglom.
614                 final String defaultConfContents = DEFAULT_CONFIG_PG_HBA_LOCAL_8p4;
615
616                 // std::cout << "DEBUG: default_conf_contents=" << default_conf_contents << std::endl;
617
618                 final boolean hbaConfCreationSucceeded = createTextFile(dbDirConfig + File.separator + "pg_hba.conf",
619                                 defaultConfContents);
620                 if (!hbaConfCreationSucceeded) {
621                         System.out.println("initialize(): createTextFile() failed.");
622                         return false;
623                 }
624
625                 final boolean identConfCreationSucceeded = createTextFile(dbDirConfig + File.separator + "pg_ident.conf",
626                                 DEFAULT_CONFIG_PG_IDENT);
627                 if (!identConfCreationSucceeded) {
628                         System.out.println("initialize(): createTextFile() failed.");
629                         return false;
630                 }
631
632                 return true;
633         }
634
635         /**
636          * @param path
637          * @param contents
638          * @return
639          */
640         private static boolean createTextFile(final String path, final String contents) {
641                 final File file = new File(path);
642                 final File parent = file.getParentFile();
643                 if (parent == null) {
644                         System.out.println("initialize(): getParentFile() failed.");
645                         return false;
646                 }
647
648                 parent.mkdirs();
649                 try {
650                         file.createNewFile();
651                 } catch (final IOException e) {
652                         // TODO Auto-generated catch block
653                         e.printStackTrace();
654                         return false;
655                 }
656
657                 FileOutputStream output = null;
658                 try {
659                         output = new FileOutputStream(file);
660                 } catch (final FileNotFoundException e) {
661                         // TODO Auto-generated catch block
662                         e.printStackTrace();
663                         return false;
664                 }
665
666                 try {
667                         output.write(contents.getBytes());
668                 } catch (final IOException e) {
669                         // TODO Auto-generated catch block
670                         e.printStackTrace();
671                         return false;
672                 }
673
674                 return true;
675         }
676
677         /**
678          * @param document
679          * @return
680          */
681         private boolean recreateDatabaseFromDocument() {
682                 // Check whether the database exists already.
683                 final String dbName = document.getConnectionDatabase();
684                 if (StringUtils.isEmpty(dbName)) {
685                         return false;
686                 }
687
688                 document.setConnectionDatabase(dbName);
689                 Connection connection = createConnection();
690                 if (connection != null) {
691                         // Connection to the database succeeded, so the database
692                         // exists already.
693                         try {
694                                 connection.close();
695                         } catch (final SQLException e) {
696                                 // TODO Auto-generated catch block
697                                 e.printStackTrace();
698                         }
699                         return false;
700                 }
701
702                 // Create the database:
703                 progress();
704                 document.setConnectionDatabase("");
705
706                 connection = createConnection();
707                 if (connection == null) {
708                         System.out.println("recreatedDatabase(): createConnection() failed, before creating the database.");
709                         return false;
710                 }
711
712                 final boolean dbCreated = createDatabase(connection, dbName);
713
714                 if (!dbCreated) {
715                         return false;
716                 }
717
718                 progress();
719
720                 // Check that we can connect:
721                 try {
722                         connection.close();
723                 } catch (final SQLException e) {
724                         // TODO Auto-generated catch block
725                         e.printStackTrace();
726                 }
727                 connection = null;
728
729                 document.setConnectionDatabase(dbName);
730                 connection = createConnection();
731                 if (connection == null) {
732                         System.out.println("recreatedDatabase(): createConnection() failed, after creating the database.");
733                         return false;
734                 }
735
736                 progress();
737
738                 // Create each table:
739                 final List<String> tables = document.getTableNames();
740                 for (final String tableName : tables) {
741
742                         // Create SQL to describe all fields in this table:
743                         final List<Field> fields = document.getTableFields(tableName);
744
745                         progress();
746                         final boolean tableCreationSucceeded = createTable(connection, document, tableName, fields);
747                         progress();
748                         if (!tableCreationSucceeded) {
749                                 // TODO: std::cerr << G_STRFUNC << ": CREATE TABLE failed with the newly-created database." <<
750                                 // std::endl;
751                                 return false;
752                         }
753                 }
754
755                 // Note that create_database() has already called add_standard_tables() and add_standard_groups(document).
756
757                 // Add groups from the document:
758                 progress();
759                 if (!addGroupsFromDocument(document)) {
760                         // TODO: std::cerr << G_STRFUNC << ": add_groups_from_document() failed." << std::endl;
761                         return false;
762                 }
763
764                 // Set table privileges, using the groups we just added:
765                 progress();
766                 if (!setTablePrivilegesGroupsFromDocument(document)) {
767                         // TODO: std::cerr << G_STRFUNC << ": set_table_privileges_groups_from_document() failed." << std::endl;
768                         return false;
769                 }
770
771                 for (final String tableName : tables) {
772                         // Add any example data to the table:
773                         progress();
774
775                         // try
776                         // {
777                         progress();
778                         final boolean tableInsertSucceeded = insertExampleData(connection, document, tableName);
779
780                         if (!tableInsertSucceeded) {
781                                 // TODO: std::cerr << G_STRFUNC << ": INSERT of example data failed with the newly-created database." <<
782                                 // std::endl;
783                                 return false;
784                         }
785                         // }
786                         // catch(final std::exception& ex)
787                         // {
788                         // std::cerr << G_STRFUNC << ": exception: " << ex.what() << std::endl;
789                         // HandleError(ex);
790                         // }
791
792                 } // for(tables)
793
794                 return true; // All tables created successfully.
795         }
796
797         /**
798          * @return
799          * @throws SQLException
800          */
801         private Connection createConnection() {
802                 final Properties connectionProps = new Properties();
803                 connectionProps.put("user", this.username);
804                 connectionProps.put("password", this.password);
805
806                 String jdbcURL = "jdbc:postgresql://" + document.getConnectionServer() + ":" + document.getConnectionPort();
807                 String db = document.getConnectionDatabase();
808                 if (StringUtils.isEmpty(db)) {
809                         // Use the default PostgreSQL database, because ComboPooledDataSource.connect() fails otherwise.
810                         db = "template1";
811                 }
812                 jdbcURL += "/" + db; // TODO: Quote the database name?
813
814                 Connection conn = null;
815                 try {
816                         conn = DriverManager.getConnection(jdbcURL + "/", connectionProps);
817                 } catch (final SQLException e) {
818                         // e.printStackTrace();
819                         return null;
820                 }
821
822                 return conn;
823         }
824
825         /**
826          *
827          */
828         private void progress() {
829                 // TODO Auto-generated method stub
830
831         }
832
833         /**
834          * @param document
835          * @param tableName
836          * @return
837          */
838         private boolean insertExampleData(final Connection connection, final Document document, final String tableName) {
839
840                 final Factory factory = new Factory(connection, SQLDialect.POSTGRES);
841                 final Table<Record> table = Factory.tableByName(tableName);
842
843                 final List<Map<String, DataItem>> exampleRows = document.getExampleRows(tableName);
844                 for (final Map<String, DataItem> row : exampleRows) {
845                         InsertSetStep<Record> insertStep = factory.insertInto(table);
846
847                         for (final Entry<String, DataItem> entry : row.entrySet()) {
848                                 final String fieldName = entry.getKey();
849                                 final DataItem value = entry.getValue();
850                                 if (value == null) {
851                                         continue;
852                                 }
853
854                                 final Field field = document.getField(tableName, fieldName);
855                                 if (field == null) {
856                                         continue;
857                                 }
858
859                                 final org.jooq.Field<Object> jooqField = Factory.fieldByName(field.getName());
860                                 if (jooqField == null) {
861                                         continue;
862                                 }
863
864                                 final Object fieldValue = value.getValue(field.getGlomType());
865                                 insertStep = insertStep.set(jooqField, fieldValue);
866                         }
867
868                         if (!(insertStep instanceof InsertResultStep<?>)) {
869                                 continue;
870                         }
871
872                         // We suppress the warning because we _do_ check the cast above.
873                         @SuppressWarnings("unchecked")
874                         final InsertResultStep<Record> insertResultStep = (InsertResultStep<Record>) insertStep;
875
876                         try {
877                                 insertResultStep.fetchOne();
878                         } catch (final DataAccessException e) {
879                                 // e.printStackTrace();
880                                 return false;
881                         }
882                         // TODO: Check that it worked.
883                 }
884
885                 return true;
886         }
887
888         /**
889          * @param document2
890          * @return
891          */
892         private boolean setTablePrivilegesGroupsFromDocument(final Document document2) {
893                 // TODO Auto-generated method stub
894                 return true;
895         }
896
897         /**
898          * @param document2
899          * @return
900          */
901         private boolean addGroupsFromDocument(final Document document2) {
902                 // TODO Auto-generated method stub
903                 return true;
904         }
905
906         /**
907          * @param document
908          * @param tableName
909          * @param fields
910          * @return
911          */
912         private boolean createTable(final Connection connection, final Document document, final String tableName,
913                         final List<Field> fields) {
914                 boolean tableCreationSucceeded = false;
915
916                 /*
917                  * TODO: //Create the standard field too: //(We don't actually use this yet) if(std::find_if(fields.begin(),
918                  * fields.end(), predicate_FieldHasName<Field>(GLOM_STANDARD_FIELD_LOCK)) == fields.end()) { sharedptr<Field>
919                  * field = sharedptr<Field>::create(); field->set_name(GLOM_STANDARD_FIELD_LOCK);
920                  * field->set_glom_type(Field::TYPE_TEXT); fields.push_back(field); }
921                  */
922
923                 // Create SQL to describe all fields in this table:
924                 String sqlFields = "";
925                 for (final Field field : fields) {
926                         // Create SQL to describe this field:
927                         String sqlFieldDescription = escapeSqlId(field.getName()) + " " + field.getSqlType();
928
929                         if (field.getPrimaryKey()) {
930                                 sqlFieldDescription += " NOT NULL  PRIMARY KEY";
931                         }
932
933                         // Append it:
934                         if (!StringUtils.isEmpty(sqlFields)) {
935                                 sqlFields += ", ";
936                         }
937
938                         sqlFields += sqlFieldDescription;
939                 }
940
941                 if (StringUtils.isEmpty(sqlFields)) {
942                         // TODO: std::cerr << G_STRFUNC << ": sql_fields is empty." << std::endl;
943                 }
944
945                 // Actually create the table
946                 final String query = "CREATE TABLE " + escapeSqlId(tableName) + " (" + sqlFields + ");";
947                 final Factory factory = new Factory(connection, SQLDialect.POSTGRES);
948                 factory.execute(query);
949                 tableCreationSucceeded = true;
950                 if (!tableCreationSucceeded) {
951                         System.out.println("recreatedDatabase(): CREATE TABLE() failed.");
952                 }
953
954                 return tableCreationSucceeded;
955         }
956
957         /**
958          * @param name
959          * @return
960          */
961         private String escapeSqlId(final String name) {
962                 // TODO:
963                 return "\"" + name + "\"";
964         }
965
966         /**
967          * @return
968          */
969         private static boolean createDatabase(final Connection connection, final String databaseName) {
970
971                 final String query = "CREATE DATABASE \"" + databaseName + "\""; // TODO: Escaping.
972                 final Factory factory = new Factory(connection, SQLDialect.POSTGRES);
973
974                 factory.execute(query);
975
976                 return true;
977         }
978
979         /**
980          *
981          */
982         public boolean cleanup() {
983                 boolean result = true;
984
985                 // Stop the server:
986                 if ((document != null) && (document.getConnectionPort() != 0)) {
987                         final String dbDirData = getSelfHostingDataPath(false);
988
989                         // -D specifies the data directory.
990                         // -c config_file= specifies the configuration file
991                         // -k specifies a directory to use for the socket. This must be writable by us.
992                         // We use "-m fast" instead of the default "-m smart" because that waits for clients to disconnect (and
993                         // sometimes never succeeds).
994                         // TODO: Warn about connected clients on other computers? Warn those other users?
995                         // Make sure to use double quotes for the executable path, because the
996                         // CreateProcess() API used on Windows does not support single quotes.
997                         final String commandPath = getPathToPostgresExecutable("pg_ctl");
998                         if (StringUtils.isEmpty(commandPath)) {
999                                 System.out.println("cleanup(): getPathToPostgresExecutable(pg_ctl) failed.");
1000                         } else {
1001                                 final ProcessBuilder commandPostgresStop = new ProcessBuilder(commandPath,
1002                                                 "-D" + shellQuote(dbDirData), "stop", "-m", "fast");
1003                                 result = executeCommandLineAndWait(commandPostgresStop);
1004                                 if (!result) {
1005                                         System.out.println("cleanup(): Failed to stop the PostgreSQL server.");
1006                                 }
1007                         }
1008
1009                         document.setConnectionPort(0);
1010                 }
1011
1012                 // Delete the files:
1013                 final String selfhostingPath = getSelfHostingPath("", false);
1014                 final File fileSelfHosting = new File(selfhostingPath);
1015                 fileSelfHosting.delete();
1016
1017                 final String docPath = document.getFileURI();
1018                 final File fileDoc = new File(docPath);
1019                 fileDoc.delete();
1020
1021                 return result;
1022         }
1023 }