source: main/trunk/greenstone3/src/java/org/greenstone/server/Server3.java@ 32419

Last change on this file since 32419 was 32419, checked in by ak19, 6 years ago

ProtocolPortProperties.java constructors can throw an Exception now.

File size: 5.4 KB
Line 
1package org.greenstone.server;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileOutputStream;
6import java.util.Properties;
7
8import org.apache.log4j.PropertyConfigurator;
9
10import org.greenstone.server.BaseServer;
11import org.greenstone.server.BaseProperty;
12import org.greenstone.util.GlobalProperties;
13import org.greenstone.util.ProtocolPortProperties;
14import org.greenstone.util.RunAnt;
15
16public class Server3 extends BaseServer
17{
18 String opt_ant_properties = null;
19 ProtocolPortProperties protocolPortProps = null;
20
21 public Server3(String gsdl3_src_home, String lang)
22 {
23 super(gsdl3_src_home, lang, gsdl3_src_home + File.separatorChar + "build.properties", "web"+File.separator+"logs");
24
25 // config properties are loaded into the config_properties object
26 // now test the port and protocol values are sane or exit.
27 try {
28 protocolPortProps = new ProtocolPortProperties(config_properties);
29 } catch(Exception e) {
30 String errorMsg = "Error with port/protocol in " + config_properties_file + ": " + protocolPortProps.getErrorMsg();
31
32 // print it everywhere, because port/protocol misconfiguration is a fatal error and we're exiting
33 logger_.error(errorMsg);
34 System.err.println(errorMsg);
35 // If error, then quit this app as soon as possible
36 System.exit(-1);
37 }
38
39 Property = protocolPortProps.legacyMode ? new Server3Property() : new Server3Property(protocolPortProps.getProtocol());
40
41 String frame_title = dictionary.get("ServerControl.Frame_Title");
42 server_control_ = new Server3Control(this, frame_title);
43
44 /** Ant command targets for managing Web server */
45 START_CMD = "start";
46 RESTART_CMD = "restart";
47 CONFIGURE_CMD = "configure";
48 STOP_CMD = "force-stop-tomcat force-stop-derby";
49
50 String is_read_only = System.getProperty("gsdl3home.isreadonly","false");
51 if (is_read_only.equals("true")) {
52 String default_gsdl3_home = gsdl3_src_home + File.separatorChar + "web";
53 String gsdl3_writablehome = System.getProperty("gsdl3.writablehome",default_gsdl3_home);
54 opt_ant_properties = "-Dgsdl3home.isreadonly=true -Dgsdl3.writablehome="+gsdl3_writablehome;
55 }
56
57 autoStart();
58 }
59
60 protected int runTarget(String cmd)
61 {
62 RunAnt runAnt = new RunAnt(opt_ant_properties);
63 runAnt.setTargetCmd(cmd);
64 runAnt.run();
65 return runAnt.getTargetState();
66 }
67
68 public String getBrowserURL()
69 {
70 return GlobalProperties.getFullGSDL3WebAddress() + config_properties.getProperty(BaseServer.Property.DEFAULT_SERVLET);
71 }
72
73 public String fallbackGSDL3Home()
74 {
75 return gsdl_home + File.separator + "web"; //System.getenv("GSDL3SRCHOME") + File.separator + "web";
76 }
77
78 public void reload()
79 {
80 String gsdl3_writablehome = System.getProperty("gsdl3.writablehome",fallbackGSDL3Home());
81
82 GlobalProperties.loadGlobalProperties(gsdl3_writablehome); // properties file may have changed, so reload it
83 }
84
85 public static void main(String[] args)
86 {
87 if ((args.length < 1) || (args.length > 2))
88 {
89 System.err.println("Usage: java org.greenstone.server.Server3 <gsdl3-src-home> [lang]");
90 System.exit(1);
91 }
92
93 String gsdl3_src_home = args[0];
94 File gsdl3_src_dir = new File(gsdl3_src_home);
95 if (!gsdl3_src_dir.isDirectory())
96 {
97 System.err.println("src directory does not exist!");
98 System.exit(1);
99 }
100
101 String lang = (args.length == 2) ? args[1] : "en";
102 new Server3(gsdl3_src_home, lang);
103 }
104
105 // Prepare the log4j.properties for GS3
106 protected void initLogger() {
107
108 String gsdl3_home = GlobalProperties.getGSDL3Home(); // may not be initialised at this stage
109 if(gsdl3_home == null) { // use gsdl_home/web
110 gsdl3_home = fallbackGSDL3Home();
111 }
112
113 String gsdl3_writablehome = System.getProperty("gsdl3.writablehome",gsdl3_home);
114
115 String propsFolder = gsdl3_writablehome + File.separator + "WEB-INF" + File.separator + "classes" + File.separator;
116 File propsFile = new File(propsFolder+"log4j.properties");
117
118 // create it from the template file GS3/resources/web/log4j.properties
119 // Always do this => helps make Greenstone3 portable
120
121
122 try {
123 // need to set gsdl3.home property's value to be gsdl_home/web,
124 // so that the location of the log files gets resolved correctly
125
126 // load the template log4j.properties.svn file into logProps
127 FileInputStream infile = new FileInputStream(new File(gsdl_home+File.separator+"resources"+File.separator+"web"+File.separator+"log4j.properties.svn"));
128 if(infile != null) {
129 Properties logProps = new Properties();
130 logProps.load(infile);
131 infile.close();
132
133 // set gsdl3.home to web home
134 logProps.setProperty("gsdl3.home", gsdl3_home);
135 logProps.setProperty("gsdl3.writablehome", gsdl3_writablehome);
136
137 // write the customised properties out to a custom log4j.properties file
138 FileOutputStream outfile = new FileOutputStream(propsFile);
139 if(outfile != null) {
140 logProps.store(outfile, "Customised log4j.properties file");
141 outfile.close();
142 } else {
143 System.err.println("Could not store properties file " + propsFile + " for Server3.");
144 }
145 }
146 } catch(Exception e) {
147 System.err.println("Exception occurred when custom-configuring the logger for Server3.\n" + e);
148 e.printStackTrace();
149 }
150
151 // now configure the logger with the custom log4j.properties file
152 if(propsFile.exists()) {
153 PropertyConfigurator.configure(propsFile.getAbsolutePath());
154 } else {
155 System.err.println("Could not create properties file " + propsFile + " for Server3.");
156 }
157 }
158}
Note: See TracBrowser for help on using the repository browser.