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

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

GS3 Java code has moved away from using tomcat.port to using tomcat.port.<protocol>.

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