1 11 package org.eclipse.help.internal.base; 12 import java.io.File ; 13 import java.io.FileOutputStream ; 14 import java.io.IOException ; 15 import java.io.RandomAccessFile ; 16 import java.nio.channels.FileLock ; 17 import java.util.Map ; 18 import java.util.Properties ; 19 20 import org.eclipse.core.runtime.IConfigurationElement; 21 import org.eclipse.core.runtime.IExecutableExtension; 22 import org.eclipse.core.runtime.Platform; 23 import org.eclipse.equinox.app.IApplication; 24 import org.eclipse.equinox.app.IApplicationContext; 25 import org.eclipse.help.internal.server.WebappManager; 26 import org.eclipse.osgi.util.NLS; 27 28 34 public class HelpApplication implements IApplication, IExecutableExtension { 35 private static final String APPLICATION_LOCK_FILE = ".applicationlock"; private static final int STATE_EXITING = 0; 37 private static final int STATE_RUNNING = 1; 38 private static final int STATE_RESTARTING = 2; 39 private static int status = STATE_RUNNING; 40 private File metadata; 41 private FileLock lock; 42 43 46 public synchronized Object start(IApplicationContext context) throws Exception { 47 if (status == STATE_RESTARTING) { 48 return EXIT_RESTART; 49 } 50 51 metadata = new File (Platform.getLocation().toFile(), ".metadata/"); if (!BaseHelpSystem.ensureWebappRunning()) { 53 System.out.println(NLS.bind(HelpBaseResources.HelpApplication_couldNotStart, Platform.getLogFileLocation().toOSString())); 54 return EXIT_OK; 55 } 56 57 if (status == STATE_RESTARTING) { 58 return EXIT_RESTART; 59 } 60 61 writeHostAndPort(); 62 obtainLock(); 63 64 if (BaseHelpSystem.MODE_STANDALONE == BaseHelpSystem.getMode()) { 65 DisplayUtils.runUI(); 67 } 68 while (status == STATE_RUNNING) { 70 try { 71 Thread.sleep(100); 72 } catch (InterruptedException ie) { 73 break; 74 } 75 } 76 releaseLock(); 77 if (status == STATE_RESTARTING) { 78 return EXIT_RESTART; 79 } 80 return EXIT_OK; 81 } 82 83 86 public void stop() { 87 stopHelp(); 88 89 synchronized(this) {}; 91 } 92 93 96 public static void stopHelp() { 97 status = STATE_EXITING; 98 if (BaseHelpSystem.MODE_STANDALONE == BaseHelpSystem.getMode()) { 99 DisplayUtils.wakeupUI(); 101 } 102 } 103 104 107 public static void restartHelp() { 108 if (status != STATE_EXITING) { 109 status = STATE_RESTARTING; 110 } 111 } 112 113 116 public void setInitializationData(IConfigurationElement configElement, String propertyName, Object data) { 117 String value = (String )((Map )data).get("mode"); if ("infocenter".equalsIgnoreCase(value)) { BaseHelpSystem.setMode(BaseHelpSystem.MODE_INFOCENTER); 120 } 121 else if ("standalone".equalsIgnoreCase(value)) { BaseHelpSystem.setMode(BaseHelpSystem.MODE_STANDALONE); 123 } 124 } 125 126 private void writeHostAndPort() throws IOException { 127 Properties p = new Properties (); 128 p.put("host", WebappManager.getHost()); p.put("port", "" + WebappManager.getPort()); 131 File hostPortFile = new File (metadata, ".connection"); hostPortFile.deleteOnExit(); 133 FileOutputStream out = null; 134 try { 135 out = new FileOutputStream (hostPortFile); 136 p.store(out, null); 137 } finally { 138 if (out != null) { 139 try { 140 out.close(); 141 } catch (IOException ioe2) { 142 } 143 } 144 } 145 146 } 147 148 private void obtainLock() { 149 File lockFile = new File (metadata, APPLICATION_LOCK_FILE); 150 try { 151 RandomAccessFile raf = new RandomAccessFile (lockFile, "rw"); lock = raf.getChannel().lock(); 153 } catch (IOException ioe) { 154 lock = null; 155 } 156 } 157 158 private void releaseLock() { 159 if (lock != null) { 160 try { 161 lock.channel().close(); 162 } catch (IOException ioe) { 163 } 164 } 165 } 166 167 public static boolean isRunning() { 168 return status == STATE_RUNNING; 169 } 170 } 171 | Popular Tags |