001: /*
002: * regain - A file search engine providing plenty of formats
003: * Copyright (C) 2004 Til Schneider
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: Til Schneider, info@murfman.de
020: *
021: * CVS information:
022: * $RCSfile$
023: * $Source$
024: * $Date: 2005-03-16 09:55:48 +0100 (Mi, 16 Mrz 2005) $
025: * $Author: til132 $
026: * $Revision: 87 $
027: */
028: package net.sf.regain.ui.desktop;
029:
030: import java.io.File;
031: import java.io.IOException;
032: import java.net.ServerSocket;
033:
034: import org.apache.log4j.Logger;
035:
036: import simple.http.ProtocolHandler;
037: import simple.http.connect.Connection;
038: import simple.http.connect.ConnectionFactory;
039: import simple.http.load.MapperEngine;
040: import simple.http.serve.FileContext;
041: import simple.http.serve.HandlerFactory;
042:
043: import net.sf.regain.RegainException;
044: import net.sf.regain.ui.desktop.config.DesktopConfig;
045: import net.sf.regain.ui.desktop.config.XmlDesktopConfig;
046: import net.sf.regain.util.sharedtag.simple.SharedTagService;
047: import net.sf.regain.util.ui.BrowserLauncher;
048:
049: /**
050: * A toolkit for the desktop search containing helper methods.
051: *
052: * @author Til Schneider, www.murfman.de
053: */
054: public class DesktopToolkit implements DesktopConstants {
055:
056: /** The logger for this class */
057: private static Logger mLog = Logger.getLogger(DesktopToolkit.class);
058:
059: /** The desktop configuration. */
060: private static DesktopConfig mConfig;
061:
062: /** The simpleweb connection */
063: private static Connection mSimplewebConnection;
064:
065: /** The current webserver socket. */
066: private static ServerSocket mCurrentSocket;
067:
068: /**
069: * Gets the desktop configuration.
070: *
071: * @return The desktop configuration.
072: */
073: public static DesktopConfig getDesktopConfig() {
074: if (mConfig == null) {
075: File xmlFile = new File("conf/DesktopConfiguration.xml");
076: mConfig = new XmlDesktopConfig(xmlFile);
077: }
078: return mConfig;
079: }
080:
081: /**
082: * Opens a page in the browser.
083: *
084: * @param page The page to open.
085: */
086: public static void openPageInBrowser(String page) {
087: String url = "http://localhost:"
088: + mCurrentSocket.getLocalPort() + "/" + page;
089: try {
090: String browser = getDesktopConfig().getBrowser();
091: if (browser != null) {
092: BrowserLauncher.setBrowser(browser);
093: }
094:
095: BrowserLauncher.openURL(url);
096: } catch (Exception exc) {
097: mLog.error("Opening browser failed", exc);
098:
099: // Show the browser selector
100: BrowserSelectorFrame frame = new BrowserSelectorFrame(page);
101: frame.show();
102: }
103: }
104:
105: /**
106: * Checks whether the webserver is running on the right port.
107: *
108: * @throws RegainException If creating or remapping the webserver failed.
109: */
110: public static void checkWebserver() throws RegainException {
111: int port = getDesktopConfig().getPort();
112: if ((mCurrentSocket == null)
113: || (mCurrentSocket.getLocalPort() != port)) {
114: if (mCurrentSocket != null) {
115: // The port has changed -> Close the old socket
116: try {
117: mCurrentSocket.close();
118: } catch (IOException exc) {
119: throw new RegainException(
120: "Closing the old socket failed", exc);
121: }
122: }
123:
124: // Create the simpleweb connection if nessesary
125: if (mSimplewebConnection == null) {
126: try {
127: FileContext context = new FileContext(new File(
128: "web"));
129: MapperEngine engine = new MapperEngine(context);
130:
131: engine.load("SharedTagService",
132: SharedTagService.class.getName());
133: engine.link("*", "SharedTagService");
134:
135: ProtocolHandler handler = HandlerFactory
136: .getInstance(engine);
137:
138: mSimplewebConnection = ConnectionFactory
139: .getConnection(handler);
140: } catch (Exception exc) {
141: throw new RegainException(
142: "Creating simpleweb server failed", exc);
143: }
144: }
145:
146: mLog.info("Listening on port " + port + "...");
147: try {
148: mCurrentSocket = new ServerSocket(port);
149: } catch (IOException exc) {
150: throw new RegainException("Creating socket for port "
151: + port + " failed.", exc);
152: }
153: mSimplewebConnection.connect(mCurrentSocket);
154: }
155: }
156:
157: }
|