Changeset 13560 for trunk/gsdl3/src/java/org/greenstone/server/Server.java
- Timestamp:
- 2007-01-11T13:15:37+13:00 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl3/src/java/org/greenstone/server/Server.java
r13229 r13560 7 7 import java.io.InputStream; 8 8 import java.util.Properties; 9 import java.io.BufferedReader; 10 import java.io.InputStream; 11 import java.io.InputStreamReader; 12 13 import org.apache.log4j.*; 9 14 10 15 import org.greenstone.gsdl3.util.Dictionary; 11 12 //import org.greenstone.gsdl3.util.GlobalProperties; 13 14 public class Server 15 { 16 public static Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize(); 16 import org.greenstone.gsdl3.util.GlobalProperties; 17 18 public class Server { 19 /** Ant command for managing Tomcat */ 20 static final private String START_CMD = "start"; 21 static final private String RESTART_CMD = "restart"; 22 static final private String CONFIGURE_CMD = "configure"; 23 static final private String STOP_CMD = "stop"; 24 25 public static final int SERVER_STARTED = 0; 26 public static final int SERVER_START_FAILED = 1; 27 public static final int BROWSER_LAUNCHED = 2; 28 public static final int BROWSER_LAUNCH_FAILED = 3; 29 30 private int server_state = -1; 31 32 private boolean configure_required = false; 33 17 34 public static File build_properties_file; 18 public static String images_path; // should be in classpath??19 35 public static Properties build_properties; 20 36 public static Dictionary dictionary; 21 37 38 private ServerControl server_control; 39 40 static Logger logger = Logger.getLogger(org.greenstone.server.Server.class.getName()); 41 22 42 public Server(String gsdl3_src_home) 23 43 { 24 //this.gsdl3_home = gsdl3_home; 25 //this.gsdl3_src_home = gsdl3_src_home; 26 27 //this.global_properties=gsdl3_home+File.separatorChar+"WEB-INF"+File.separatorChar+"classes"+File.separatorChar+"global.properties"; 44 28 45 String build_properties_path=gsdl3_src_home+File.separatorChar+"build.properties"; 29 46 this.build_properties_file = new File(build_properties_path); 30 47 if (!this.build_properties_file.exists()) { 31 System.err.println("Can't find build.properties file "+build_properties_path);48 logger.fatal("Can't find build.properties file "+build_properties_path); 32 49 System.exit(1); 33 50 } 51 34 52 build_properties = new Properties(); 35 53 reloadBuildProperties(); 36 this.images_path = gsdl3_src_home+File.separatorChar+"resources"+File.separatorChar+"images"+File.separatorChar;37 54 38 55 // make it dynamic language 39 56 this.dictionary = new Dictionary("server", "en", this.getClass().getClassLoader()); 40 ServerControl serverControl = new ServerControl(); 41 } 42 43 public static void reloadBuildProperties() { 57 58 server_control = new ServerControl(this); 59 60 String auto_start = build_properties.getProperty(StaticStrings.AUTOSTART_PROPERTY); 61 if (auto_start != null && auto_start.equals("true")) { 62 restart(); 63 } 64 else{ 65 start(); 66 } 67 68 } 69 70 public int getServerState(){ 71 return server_state; 72 } 73 74 private int runAnt(String cmd){ 75 RunAnt runAnt = new RunAnt(); 76 runAnt.setAntCmd(cmd); 77 runAnt.run(); 78 return runAnt.getState(); 79 } 80 81 82 public void reconfigRequired(){ 83 configure_required = true; 84 } 85 86 public void start() { 87 int state = -1; 88 server_state = -1; 89 server_control.updateControl(); 90 stop(); 91 92 // reconfigure if necessary 93 if (configure_required){ 94 server_control.displayMessage(dictionary.get("ServerControl.Configuring")); 95 state = runAnt(CONFIGURE_CMD); 96 97 if (state != RunAnt.SUCCESS){ 98 recordError(CONFIGURE_CMD); 99 } 100 GlobalProperties.reload(); // properties file may have changed, so reload it 101 configure_required = false; 102 } 103 else{ 104 recordSuccess(CONFIGURE_CMD); 105 } 106 107 server_control.displayMessage(dictionary.get("ServerControl.Starting")); 108 state = runAnt(START_CMD); 109 110 if (state != RunAnt.SUCCESS){ 111 recordError(START_CMD); 112 server_state = SERVER_START_FAILED; 113 114 } 115 else{ 116 recordSuccess(START_CMD); 117 server_state = SERVER_STARTED; 118 } 119 120 server_control.updateControl(); 121 } 122 123 private void recordError(String message){ 124 message = dictionary.get("ServerControl.Error",new String[]{message,GlobalProperties.getGSDL3Home()}); 125 server_control.displayMessage(message); 126 logger.error(dictionary.get("ServerControl.Failed",new String[]{message})); 127 } 128 129 130 private void recordError(String message, Exception e){ 131 message = dictionary.get("ServerControl.Error",new String[]{message,GlobalProperties.getGSDL3Home()}); 132 server_control.displayMessage(message); 133 logger.error(dictionary.get("ServerControl.Failed",new String[]{message}),e); 134 } 135 136 private void recordSuccess(String message){ 137 message = dictionary.get("ServerControl.Success",new String[]{message}); 138 server_control.displayMessage(message); 139 logger.info(message); 140 } 141 142 143 public void launchBrowser() { 144 server_state = -1; 145 server_control.updateControl(); 146 String message = dictionary.get("ServerControl.LaunchingBrowser"); 147 server_control.displayMessage(message); 148 String url = GlobalProperties.getGSDL3WebAddress()+ build_properties.getProperty(StaticStrings.DEFAULT_SERVLET_PROPERTY); 149 BrowserLauncher launcher = new BrowserLauncher(build_properties.getProperty(StaticStrings.BROWSER_PATH),url); 150 logger.info(message); 151 152 launcher.start(); 153 154 //wait for a while 155 while(launcher.getState() == -1){ 156 try{ 157 Thread.sleep(3000); 158 } 159 catch(Exception e){ 160 logger.error(e); 161 } 162 } 163 164 if (launcher.getState() != BrowserLauncher.LAUNCHSUCCESS ){ 165 recordError(dictionary.get("ServerControl.LaunchBrowser")); 166 server_state = BROWSER_LAUNCH_FAILED; 167 } 168 else{ 169 recordSuccess(dictionary.get("ServerControl.LaunchBrowser")); 170 server_state = BROWSER_LAUNCHED; 171 } 172 173 server_control.updateControl(); 174 } 175 176 public void restart(){ 177 start(); 178 if (server_state == SERVER_STARTED){ 179 launchBrowser(); 180 } 181 } 182 183 public void stop() { 184 server_control.displayMessage(dictionary.get("ServerControl.Stopping")); 185 int state = runAnt(STOP_CMD); 186 187 if (state != RunAnt.SUCCESS){ 188 recordError(STOP_CMD); 189 } 190 else{ 191 recordSuccess(STOP_CMD); 192 } 193 194 } 195 196 public void reloadBuildProperties() { 44 197 try { 45 InputStream in = Class.forName("org.greenstone.server.Server").getClassLoader().getResourceAsStream("build.properties"); 198 InputStream in = this.getClass().getClassLoader().getResourceAsStream("build.properties"); 199 46 200 if (in != null) { 47 System.err.println("loading build properties");201 logger.info("loading build properties"); 48 202 build_properties.load(in); 49 203 in.close(); 50 204 } else { 51 System.err.println("couldn't load build properties!");205 logger.error("couldn't load build properties!"); 52 206 } 53 207 } catch (Exception e) { 54 System.err.println("Exception trying to reload build.properties: "+e); 55 } 56 57 58 } 208 logger.error("Exception trying to reload build.properties: "+e); 209 } 210 211 } 212 213 214 59 215 public static void main (String[] args){ 60 System.out.println(args[0]); 61 if (args.length != 1) { 216 if (args.length != 1) { 62 217 System.err.println("Usage: java org.greenstone.server.ServerMain <gsdl3 src home> "); 63 218 System.exit(1); … … 66 221 File gsdl3_src_dir = new File(args[0]); 67 222 if (!gsdl3_src_dir.isDirectory()) { 68 System.err.println("Usage: java org.greenstone.server.ServerMain <gsdl3 src home> ");69 223 System.err.println("src directory does not exist!"); 70 224 System.exit(1); 71 225 } 72 Server server = new Server(args[0]); 226 227 new Server(args[0]); 73 228 } 74 229 }
Note:
See TracChangeset
for help on using the changeset viewer.