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

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

Shifting from log4j.properties and its .in file in resources/web to .in version renamed to log4.properties.svn in resources/web and the log4j.properties file only in web/WEB-INF/classes

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