1 |
|
---|
2 | package org.greenstone.server;
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.FileInputStream;
|
---|
6 | import java.io.FileOutputStream;
|
---|
7 | import java.util.Properties;
|
---|
8 | import java.util.ArrayList;
|
---|
9 |
|
---|
10 | import org.apache.log4j.*;
|
---|
11 |
|
---|
12 | import org.greenstone.server.BaseServer;
|
---|
13 | import org.greenstone.server.BaseProperty;
|
---|
14 |
|
---|
15 | public class Server2 extends BaseServer
|
---|
16 | {
|
---|
17 | protected String libraryURL;
|
---|
18 |
|
---|
19 | public Server2(String gsdl2_home, String lang, String configfile)
|
---|
20 | {
|
---|
21 | super(gsdl2_home, lang, configfile, "etc"+File.separator+"logs-gsi");
|
---|
22 | // configfile can be either glisite.cfg or llssite.cfg
|
---|
23 |
|
---|
24 |
|
---|
25 | Property = new Server2Property();
|
---|
26 |
|
---|
27 | String frame_title = dictionary.get("ServerControl.Frame_Title");
|
---|
28 | server_control_ = new Server2Control(this,frame_title);
|
---|
29 |
|
---|
30 | /** Make command tagets for managing Web server */
|
---|
31 | START_CMD = "web-start";
|
---|
32 | RESTART_CMD = "web-restart";
|
---|
33 | CONFIGURE_CMD = "configure-web " + configfile;
|
---|
34 | STOP_CMD = "web-stop";
|
---|
35 |
|
---|
36 | autoStart();
|
---|
37 | }
|
---|
38 |
|
---|
39 | // Prepare the log4j.properties for GS2
|
---|
40 | protected void initLogger() {
|
---|
41 |
|
---|
42 | String libjavaFolder = gsdl_home+File.separator+"lib"+File.separator+"java"+File.separator;
|
---|
43 | File propsFile = new File(libjavaFolder+"log4j.properties");
|
---|
44 |
|
---|
45 | // create it from the template file log4j.properties.in
|
---|
46 | if(!propsFile.exists()) {
|
---|
47 | try {
|
---|
48 | // need to set gsdl2.home property's value to be gsdl_home,
|
---|
49 | // so that the location of the log files gets resolved correctly
|
---|
50 |
|
---|
51 | // load the template log4j.properties.in file into logProps
|
---|
52 | FileInputStream infile = new FileInputStream(new File(libjavaFolder+"log4j.properties.in"));
|
---|
53 | if(infile != null) {
|
---|
54 | Properties logProps = new Properties();
|
---|
55 | logProps.load(infile);
|
---|
56 | infile.close();
|
---|
57 |
|
---|
58 | // set gsdl3.home to gsdl_home
|
---|
59 | logProps.setProperty("gsdl2.home", gsdl_home);
|
---|
60 |
|
---|
61 | // write the customised properties out to a custom log4j.properties file
|
---|
62 | FileOutputStream outfile = new FileOutputStream(propsFile);
|
---|
63 | if(outfile != null) {
|
---|
64 | logProps.store(outfile, "Customised log4j.properties file");
|
---|
65 | outfile.close();
|
---|
66 | } else {
|
---|
67 | System.err.println("Could not store properties file " + propsFile + " for Server2.");
|
---|
68 | }
|
---|
69 | }
|
---|
70 | } catch(Exception e) {
|
---|
71 | System.err.println("Exception occurred when custom-configuring the logger for Server2.\n" + e);
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | // now configure the logger with the custom log4j.properties file
|
---|
76 | if(propsFile.exists()) {
|
---|
77 | PropertyConfigurator.configure(propsFile.getAbsolutePath());
|
---|
78 | } else {
|
---|
79 | System.err.println("Could not create properties file " + propsFile + " for Server2.");
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | protected int runTarget(String cmd)
|
---|
85 | {
|
---|
86 | RunMake runMake = new RunMake();
|
---|
87 | runMake.setTargetCmd(cmd);
|
---|
88 | runMake.run();
|
---|
89 | return runMake.getTargetState();
|
---|
90 | }
|
---|
91 |
|
---|
92 | public String getBrowserURL() {
|
---|
93 | return libraryURL;
|
---|
94 | }
|
---|
95 |
|
---|
96 | // works out the library URL again
|
---|
97 | public void reload() {
|
---|
98 | // default values, to be replaced with what's in gsdlsite.cfg
|
---|
99 | String host = "localhost";
|
---|
100 | String port = "8080";
|
---|
101 | String gwcgi;
|
---|
102 | String httpprefix = "greenstone";
|
---|
103 | String suffix = "/cgi-bin/library.cgi";
|
---|
104 |
|
---|
105 | // get the prefix from the gsdlsite.cfg and build.properties files
|
---|
106 | // (port number and servername)
|
---|
107 | try{
|
---|
108 | File gsdlsite_cfg = new File(gsdl_home + File.separator + "cgi-bin" + File.separator + "gsdlsite.cfg");
|
---|
109 | FileInputStream fin = new FileInputStream(gsdlsite_cfg);
|
---|
110 | Properties gsdlProperties = new Properties();
|
---|
111 | if(fin != null) {
|
---|
112 | gsdlProperties.load(fin);
|
---|
113 |
|
---|
114 | gwcgi = gsdlProperties.getProperty("gwcgi");
|
---|
115 | if(gwcgi != null) {
|
---|
116 | suffix = gwcgi;
|
---|
117 | } else {
|
---|
118 | httpprefix = gsdlProperties.getProperty("httpprefix", httpprefix);
|
---|
119 | suffix = httpprefix + suffix;
|
---|
120 | }
|
---|
121 | fin.close();
|
---|
122 | } else {
|
---|
123 | recordError("Could not open gsdlsite_cfg for reading, using default library prefix.");
|
---|
124 | }
|
---|
125 | reloadConfigProperties();
|
---|
126 | port = config_properties.getProperty("portnumber", port);
|
---|
127 |
|
---|
128 | } catch(Exception e) {
|
---|
129 | recordError("Exception trying to load properties from gsdlsite_cfg. Using default library prefix.", e);
|
---|
130 | suffix = httpprefix + suffix;
|
---|
131 | }
|
---|
132 |
|
---|
133 | libraryURL = "http://" + host + ":" + port + suffix;
|
---|
134 |
|
---|
135 | // the URL has been changed
|
---|
136 | // write the URL to the file since this should work like GS2's Local Lib Server for Windows
|
---|
137 | ScriptReadWrite scriptReadWrite = new ScriptReadWrite();
|
---|
138 | ArrayList fileLines = scriptReadWrite.readInFile(BaseServer.config_properties_file);
|
---|
139 | scriptReadWrite.replaceOrAddLine(fileLines, "url", libraryURL, true);
|
---|
140 | scriptReadWrite.writeOutFile(config_properties_file, fileLines);
|
---|
141 | }
|
---|
142 |
|
---|
143 | // About to stop the webserver
|
---|
144 | // Custom GS2 action: remove the url property from the config file
|
---|
145 | protected void preStop() {
|
---|
146 | ScriptReadWrite scriptReadWrite = new ScriptReadWrite();
|
---|
147 | ArrayList fileLines = scriptReadWrite.readInFile(BaseServer.config_properties_file);
|
---|
148 | if (fileLines.contains("url="+getBrowserURL())) {
|
---|
149 | // would be last element, remove it:
|
---|
150 | fileLines.remove(fileLines.size()-1);
|
---|
151 | }
|
---|
152 | scriptReadWrite.writeOutFile(config_properties_file, fileLines);
|
---|
153 | }
|
---|
154 |
|
---|
155 | public static void main (String[] args)
|
---|
156 | {
|
---|
157 | if ((args.length < 1) || (args.length > 3)) {
|
---|
158 | System.err.println("Usage: java org.greenstone.server.Server2 <gsdl2-home-dir> [lang] [--config=configfile]");
|
---|
159 | System.exit(1);
|
---|
160 | }
|
---|
161 |
|
---|
162 | String gsdl2_home = args[0];
|
---|
163 | File gsdl2_dir = new File(gsdl2_home);
|
---|
164 | if (!gsdl2_dir.isDirectory()) {
|
---|
165 | System.err.println("gsdl-home-dir directory does not exist!");
|
---|
166 | System.exit(1);
|
---|
167 | }
|
---|
168 |
|
---|
169 | String lang = (args.length>=2) ? args[1] : "en";
|
---|
170 |
|
---|
171 | // if no config file is given, then the following defaults to llssite.cfg
|
---|
172 | String configfile = (args.length==3 && args[2].startsWith("--config=")) ? args[2] : gsdl2_home+File.separator+"llssite.cfg";
|
---|
173 | int equalSign = configfile.indexOf('=');
|
---|
174 | if(equalSign != -1) {
|
---|
175 | configfile = configfile.substring(equalSign+1);
|
---|
176 | }
|
---|
177 |
|
---|
178 | new Server2(gsdl2_home,lang,configfile);
|
---|
179 | }
|
---|
180 | }
|
---|