Author: echatellier Date: 2010-01-05 18:07:54 +0100 (Tue, 05 Jan 2010) New Revision: 1728 Modified: trunk/changelog.txt trunk/src/main/java/org/nuiton/util/war/JettyLauncher.java trunk/src/main/java/org/nuiton/util/war/WinstoneLauncher.java Log: Use display-name from web.xml file as server name Modified: trunk/changelog.txt =================================================================== --- trunk/changelog.txt 2010-01-04 08:24:40 UTC (rev 1727) +++ trunk/changelog.txt 2010-01-05 17:07:54 UTC (rev 1728) @@ -1,12 +1,12 @@ -1.1.2 xxx 200912xx +1.1.3 xxx 201001xx + * Use display-name from web.xml file as server name - * Add generic war launcher (winstone based) +1.1.2 desbois 20091223 + * Add generic war launcher (winstone & jetty based) * Add new uncompress method with exclusion filters * [FEATURE] Force application configuration properties to be written sorted * [FEATURE] Add sed and grep method on FileUtil --- - 1.1.1 chemit 20090903 * [FEATURE] #39 add a filterVersions method in VersionUtil Modified: trunk/src/main/java/org/nuiton/util/war/JettyLauncher.java =================================================================== --- trunk/src/main/java/org/nuiton/util/war/JettyLauncher.java 2010-01-04 08:24:40 UTC (rev 1727) +++ trunk/src/main/java/org/nuiton/util/war/JettyLauncher.java 2010-01-05 17:07:54 UTC (rev 1728) @@ -31,6 +31,9 @@ import java.awt.event.MouseListener; import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.StringWriter; import java.net.JarURLConnection; import java.net.URI; import java.net.URISyntaxException; @@ -45,6 +48,9 @@ import org.eclipse.jetty.server.handler.HandlerCollection; import org.eclipse.jetty.server.nio.SelectChannelConnector; import org.eclipse.jetty.webapp.WebAppContext; +import org.xml.sax.ContentHandler; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.XMLReaderFactory; /** * War main class launcher (jetty based). @@ -70,6 +76,9 @@ /** Default port. */ protected int port = 8888; + /** Server name. */ + protected String serverName; + /** * Main method (used by war in manifest). * @@ -78,12 +87,62 @@ */ public static void main(String[] args) throws Exception { JettyLauncher launcher = new JettyLauncher(); + launcher.readInformation(); launcher.startServer(args); launcher.installSystemTray(); launcher.openBrowser(); } /** + * Parse WEB-INF/web.xml file and get server display name. + * + * @since 1.1.3 + */ + protected void readInformation() { + InputStream stream = JettyLauncher.class.getResourceAsStream("/WEB-INF/web.xml"); + + if (stream != null) { + String content = readAsString(stream); + if (content != null) { + int first = content.indexOf("<display-name>"); + if (first >= 0) { + serverName = content.substring(first + 14, content.indexOf("</display-name>")); + System.out.println("Using server name : " + serverName); + } + } + } + + // if none read, set default + if (serverName == null || serverName.isEmpty()) { + serverName = "Server"; + } + } + + /** + * Read input stream as string. + * + * Code from commons io. + * + * @param stream stream to read + * @return content as string + * @since 1.1.3 + */ + protected String readAsString(InputStream stream) { + InputStreamReader reader = new InputStreamReader(stream); + StringWriter sw = new StringWriter(); + char[] buffer = new char[4096]; + int n = 0; + try { + while (-1 != (n = reader.read(buffer))) { + sw.write(buffer, 0, n); + } + } catch (IOException e) { + e.printStackTrace(); + } + return sw.toString(); + } + + /** * Launch servlet container. * * @param args args @@ -98,7 +157,7 @@ URL classFile = JettyLauncher.class.getClassLoader().getResource(fqnLauncherFile); System.out.println(" - using classFile : " + classFile); - // strange following line seams also work for jpnl launch + // strange following line seams also work for jnlp launch File me = new File(((JarURLConnection) classFile.openConnection()).getJarFile().getName()); System.out.println(" - using warfile file : " + me); @@ -166,7 +225,7 @@ System.out.println("No favicon.{png|jpg} found, skip systray installation"); } else { Image image = new ImageIcon(imageURL).getImage(); - TrayIcon icon = new TrayIcon(image, "Server", menu); + TrayIcon icon = new TrayIcon(image, serverName, menu); icon.setImageAutoSize(true); icon.addMouseListener(this); Modified: trunk/src/main/java/org/nuiton/util/war/WinstoneLauncher.java =================================================================== --- trunk/src/main/java/org/nuiton/util/war/WinstoneLauncher.java 2010-01-04 08:24:40 UTC (rev 1727) +++ trunk/src/main/java/org/nuiton/util/war/WinstoneLauncher.java 2010-01-05 17:07:54 UTC (rev 1728) @@ -31,6 +31,9 @@ import java.awt.event.MouseListener; import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.StringWriter; import java.net.JarURLConnection; import java.net.URI; import java.net.URISyntaxException; @@ -64,6 +67,9 @@ /** Server URI. */ protected URI serverUri; + /** Server name. */ + protected String serverName; + /** * Main method (used by war in manifest). * @@ -72,12 +78,62 @@ */ public static void main(String[] args) throws IOException { WinstoneLauncher launcher = new WinstoneLauncher(); + launcher.readInformation(); launcher.startServer(args); launcher.installSystemTray(); launcher.openBrowser(); } /** + * Parse WEB-INF/web.xml file and get server display name. + * + * @since 1.1.3 + */ + protected void readInformation() { + InputStream stream = WinstoneLauncher.class.getResourceAsStream("/WEB-INF/web.xml"); + + if (stream != null) { + String content = readAsString(stream); + if (content != null) { + int first = content.indexOf("<display-name>"); + if (first >= 0) { + serverName = content.substring(first + 14, content.indexOf("</display-name>")); + System.out.println("Using server name : " + serverName); + } + } + } + + // if none read, set default + if (serverName == null || serverName.isEmpty()) { + serverName = "Server"; + } + } + + /** + * Read input stream as string. + * + * Code from commons io. + * + * @param stream stream to read + * @return content as string + * @since 1.1.3 + */ + protected String readAsString(InputStream stream) { + InputStreamReader reader = new InputStreamReader(stream); + StringWriter sw = new StringWriter(); + char[] buffer = new char[4096]; + int n = 0; + try { + while (-1 != (n = reader.read(buffer))) { + sw.write(buffer, 0, n); + } + } catch (IOException e) { + e.printStackTrace(); + } + return sw.toString(); + } + + /** * Launch servlet container. * * @param args args @@ -92,7 +148,7 @@ URL classFile = WinstoneLauncher.class.getClassLoader().getResource(fqnLauncherFile); System.out.println(" - using classFile : " + classFile); - // strange following line seams also work for jpnl launch + // strange following line seams also work for jnlp launch File me = new File(((JarURLConnection) classFile.openConnection()).getJarFile().getName()); System.out.println(" - using warfile file : " + me); @@ -161,7 +217,7 @@ System.out.println("No favicon.{png|jpg} found, skip systray installation"); } else { Image image = new ImageIcon(imageURL).getImage(); - TrayIcon icon = new TrayIcon(image, "Server", menu); + TrayIcon icon = new TrayIcon(image, serverName, menu); icon.setImageAutoSize(true); icon.addMouseListener(this);