1 | /******************************************************************************
|
---|
2 | *
|
---|
3 | * A component of the Greenstone digital library software from the New Zealand
|
---|
4 | * Digital Library Project at the # University of Waikato, New Zealand.
|
---|
5 | * Copyright (C) 2006 New Zealand Digital Library Project
|
---|
6 | *
|
---|
7 | * This program is free software; you can redistribute it and/or modify it
|
---|
8 | * under the terms of the GNU General Public License as published by the Free
|
---|
9 | * Software Foundation; either version 2 of the License, or (at your option)
|
---|
10 | * any later version.
|
---|
11 | *
|
---|
12 | * This program is distributed in the hope that it will be useful, but
|
---|
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
---|
14 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
---|
15 | * for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License along
|
---|
18 | * with this program; if not, write to the Free Software Foundation, Inc.,
|
---|
19 | * 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
20 | *****************************************************************************/
|
---|
21 | /** @author jmt12, GSDL **/
|
---|
22 | package org.nzdl.gsdl;
|
---|
23 |
|
---|
24 | import java.io.*;
|
---|
25 | import java.lang.*;
|
---|
26 | import java.nio.file.*;
|
---|
27 | import java.util.*;
|
---|
28 |
|
---|
29 | /** @class GSInfoDB
|
---|
30 | */
|
---|
31 | public class GSInfoDB
|
---|
32 | {
|
---|
33 | private boolean debug = true;
|
---|
34 |
|
---|
35 | private PrintWriter dbwriter;
|
---|
36 | private Process dbprocess;
|
---|
37 |
|
---|
38 | private Path file_path;
|
---|
39 | private Path tmp_file_path;
|
---|
40 |
|
---|
41 | private String infodb_type;
|
---|
42 |
|
---|
43 | // Seventy hyphens - GDBM's default record separator
|
---|
44 | private static String separator = "----------------------------------------------------------------------";
|
---|
45 |
|
---|
46 | /** @function GSInfoDB(String)
|
---|
47 | */
|
---|
48 | public GSInfoDB(String gsdl_home, String infodb_type, String file)
|
---|
49 | {
|
---|
50 | if (debug)
|
---|
51 | {
|
---|
52 | System.err.println("[DEBUG] GSInfoDB(" + gsdl_home + ", " + infodb_type + ", " + file + ")");
|
---|
53 | }
|
---|
54 | this.infodb_type = infodb_type;
|
---|
55 | // most of the following code throws IOExceptions...
|
---|
56 | try
|
---|
57 | {
|
---|
58 | this.file_path = FileSystems.getDefault().getPath(file);
|
---|
59 | Path tmp_dir = FileSystems.getDefault().getPath("/tmp");
|
---|
60 | this.tmp_file_path = Files.createTempFile(tmp_dir, null, "." + infodb_type);
|
---|
61 | if (debug)
|
---|
62 | {
|
---|
63 | System.err.println("[DEBUG] Temporary file path: " + tmp_file_path);
|
---|
64 | }
|
---|
65 | ProcessBuilder pb = new ProcessBuilder(gsdl_home + "/ext/tdb-edit/linux/bin/txt2tdb", "-append", tmp_file_path.toString());
|
---|
66 | if (debug)
|
---|
67 | {
|
---|
68 | System.err.println("[DEBUG] Command: " + pb.command());
|
---|
69 | }
|
---|
70 | // - alter environment
|
---|
71 | Map<String, String> pb_env = pb.environment();
|
---|
72 | pb_env.put("PATH", gsdl_home + "/ext/tdb-edit/linux/bin:" + gsdl_home + "/ext/tdb-edit/bin/script:" + pb_env.get("PATH"));
|
---|
73 | pb_env.put("LD_LIBRARY_PATH", gsdl_home + "/ext/tdb-edit/linux/lib:" + pb_env.get("LD_LIBRARY_PATH"));
|
---|
74 | // Start the writer listening
|
---|
75 | dbprocess = pb.start();
|
---|
76 | OutputStream os = dbprocess.getOutputStream();
|
---|
77 | dbwriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(os)));
|
---|
78 | }
|
---|
79 | catch (Exception ex)
|
---|
80 | {
|
---|
81 | System.err.println("Error! Failed to open GSInfoBD: " + ex.toString());
|
---|
82 | ex.printStackTrace();
|
---|
83 | }
|
---|
84 | }
|
---|
85 | /** GSInfoDB(String) **/
|
---|
86 |
|
---|
87 |
|
---|
88 | /** @function close()
|
---|
89 | */
|
---|
90 | public void close()
|
---|
91 | {
|
---|
92 | if (debug)
|
---|
93 | {
|
---|
94 | System.err.println("[DEBUG] GSInfoBD::close()");
|
---|
95 | }
|
---|
96 | try
|
---|
97 | {
|
---|
98 | dbwriter.close();
|
---|
99 | dbprocess.waitFor();
|
---|
100 | if (debug)
|
---|
101 | {
|
---|
102 | System.err.println("[DEBUG] temporary database size: " + this.tmp_file_path.toFile().length() + " bytes");
|
---|
103 | }
|
---|
104 | // Move the database into its final location
|
---|
105 | Files.move(this.tmp_file_path, this.file_path, StandardCopyOption.REPLACE_EXISTING);
|
---|
106 |
|
---|
107 |
|
---|
108 | Runtime rut = Runtime.getRuntime();
|
---|
109 | Process process = rut.exec(new String[] {"mv", this.tmp_file_path.toString(), this.file_path.toString()});
|
---|
110 | // The below always says the file doesn't exist over NFS?
|
---|
111 | if (!this.file_path.toFile().exists())
|
---|
112 | {
|
---|
113 | System.err.println("Error! Failed to move database file: " + this.tmp_file_path.toString() + " => " + this.file_path.toString());
|
---|
114 | }
|
---|
115 | if (debug)
|
---|
116 | {
|
---|
117 | System.err.println("[DEBUG] final database size: " + this.file_path.toFile().length() + " bytes");
|
---|
118 | }
|
---|
119 | }
|
---|
120 | catch (Exception ex)
|
---|
121 | {
|
---|
122 | System.err.println("Error! Failed to close GSInfoBD: " + ex.toString());
|
---|
123 | ex.printStackTrace();
|
---|
124 | }
|
---|
125 | }
|
---|
126 | /** close() **/
|
---|
127 |
|
---|
128 |
|
---|
129 | /** @function write(String payload)
|
---|
130 | */
|
---|
131 | public void writeEntry(String payload)
|
---|
132 | {
|
---|
133 | if (debug)
|
---|
134 | {
|
---|
135 | System.err.println("[DEBUG] GSInfoDB::writeEntry(" + payload + ")");
|
---|
136 | }
|
---|
137 | try
|
---|
138 | {
|
---|
139 | dbwriter.println(payload);
|
---|
140 | dbwriter.println(separator);
|
---|
141 | }
|
---|
142 | catch (Exception ex)
|
---|
143 | {
|
---|
144 | System.err.println("Error! Failed to write to GSInfoDB: " + ex.toString());
|
---|
145 | ex.printStackTrace();
|
---|
146 | }
|
---|
147 | }
|
---|
148 | /** write(String payload) **/
|
---|
149 |
|
---|
150 | }
|
---|
151 | /** class GSInfoDB **/
|
---|
152 |
|
---|