KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > base > HelpApplication


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.base;
12 import java.io.File JavaDoc;
13 import java.io.FileOutputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.RandomAccessFile JavaDoc;
16 import java.nio.channels.FileLock JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Properties JavaDoc;
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 /**
29  * Help application. Starts webserver and help web application for use by
30  * infocenter and stand-alone help. Application takes a parameter "mode", that
31  * can take values: "infocenter" - when help system should run as infocenter,
32  * "standalone" - when help system should run as standalone.
33  */

34 public class HelpApplication implements IApplication, IExecutableExtension {
35     private static final String JavaDoc APPLICATION_LOCK_FILE = ".applicationlock"; //$NON-NLS-1$
36
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 JavaDoc metadata;
41     private FileLock JavaDoc lock;
42
43     /* (non-Javadoc)
44      * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
45      */

46     public synchronized Object JavaDoc start(IApplicationContext context) throws Exception JavaDoc {
47         if (status == STATE_RESTARTING) {
48             return EXIT_RESTART;
49         }
50
51         metadata = new File JavaDoc(Platform.getLocation().toFile(), ".metadata/"); //$NON-NLS-1$
52
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             //try running UI loop if possible
66
DisplayUtils.runUI();
67         }
68         //run a headless loop;
69
while (status == STATE_RUNNING) {
70             try {
71                 Thread.sleep(100);
72             } catch (InterruptedException JavaDoc ie) {
73                 break;
74             }
75         }
76         releaseLock();
77         if (status == STATE_RESTARTING) {
78             return EXIT_RESTART;
79         }
80         return EXIT_OK;
81     }
82     
83     /* (non-Javadoc)
84      * @see org.eclipse.equinox.app.IApplication#stop()
85      */

86     public void stop() {
87         stopHelp();
88         
89         // wait until start has finished
90
synchronized(this) {};
91     }
92     
93     /**
94      * Causes help service to stop and exit
95      */

96     public static void stopHelp() {
97         status = STATE_EXITING;
98         if (BaseHelpSystem.MODE_STANDALONE == BaseHelpSystem.getMode()) {
99             // UI loop may be sleeping if no SWT browser is up
100
DisplayUtils.wakeupUI();
101         }
102     }
103
104     /**
105      * Causes help service to exit and start again
106      */

107     public static void restartHelp() {
108         if (status != STATE_EXITING) {
109             status = STATE_RESTARTING;
110         }
111     }
112
113     /* (non-Javadoc)
114      * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement, java.lang.String, java.lang.Object)
115      */

116     public void setInitializationData(IConfigurationElement configElement, String JavaDoc propertyName, Object JavaDoc data) {
117         String JavaDoc value = (String JavaDoc)((Map JavaDoc)data).get("mode"); //$NON-NLS-1$
118
if ("infocenter".equalsIgnoreCase(value)) { //$NON-NLS-1$
119
BaseHelpSystem.setMode(BaseHelpSystem.MODE_INFOCENTER);
120         }
121         else if ("standalone".equalsIgnoreCase(value)) { //$NON-NLS-1$
122
BaseHelpSystem.setMode(BaseHelpSystem.MODE_STANDALONE);
123         }
124     }
125     
126     private void writeHostAndPort() throws IOException JavaDoc {
127         Properties JavaDoc p = new Properties JavaDoc();
128         p.put("host", WebappManager.getHost()); //$NON-NLS-1$
129
p.put("port", "" + WebappManager.getPort()); //$NON-NLS-1$ //$NON-NLS-2$
130

131         File JavaDoc hostPortFile = new File JavaDoc(metadata, ".connection"); //$NON-NLS-1$
132
hostPortFile.deleteOnExit();
133         FileOutputStream JavaDoc out = null;
134         try {
135             out = new FileOutputStream JavaDoc(hostPortFile);
136             p.store(out, null);
137         } finally {
138             if (out != null) {
139                 try {
140                     out.close();
141                 } catch (IOException JavaDoc ioe2) {
142                 }
143             }
144         }
145
146     }
147     
148     private void obtainLock() {
149         File JavaDoc lockFile = new File JavaDoc(metadata, APPLICATION_LOCK_FILE);
150         try {
151             RandomAccessFile JavaDoc raf = new RandomAccessFile JavaDoc(lockFile, "rw"); //$NON-NLS-1$
152
lock = raf.getChannel().lock();
153         } catch (IOException JavaDoc ioe) {
154             lock = null;
155         }
156     }
157     
158     private void releaseLock() {
159         if (lock != null) {
160             try {
161                 lock.channel().close();
162             } catch (IOException JavaDoc ioe) {
163             }
164         }
165     }
166
167     public static boolean isRunning() {
168         return status == STATE_RUNNING;
169     }
170 }
171
Popular Tags