r48 - in trunk: . nuiton-web nuiton-web/src nuiton-web/src/license nuiton-web/src/main nuiton-web/src/main/java nuiton-web/src/main/java/org nuiton-web/src/main/java/org/nuiton nuiton-web/src/main/java/org/nuiton/web nuiton-web/src/main/java/org/nuiton/web/war nuiton-web/src/site nuiton-web/src/site/apt nuiton-web/src/test
Author: tchemit Date: 2011-01-26 17:07:53 +0100 (Wed, 26 Jan 2011) New Revision: 48 Url: http://nuiton.org/repositories/revision/nuiton-web/48 Log: Evolution #1201: Introduce WinstoneLauncher and JettyLauncher Evolution #1251: Introduce nuiton-web module Added: trunk/nuiton-web/ trunk/nuiton-web/LICENSE.txt trunk/nuiton-web/README.txt trunk/nuiton-web/changelog.txt trunk/nuiton-web/pom.xml trunk/nuiton-web/src/ trunk/nuiton-web/src/license/ trunk/nuiton-web/src/license/THIRD-PARTY.properties trunk/nuiton-web/src/main/ trunk/nuiton-web/src/main/java/ trunk/nuiton-web/src/main/java/org/ trunk/nuiton-web/src/main/java/org/nuiton/ trunk/nuiton-web/src/main/java/org/nuiton/web/ trunk/nuiton-web/src/main/java/org/nuiton/web/war/ trunk/nuiton-web/src/main/java/org/nuiton/web/war/JettyLauncher.java trunk/nuiton-web/src/main/java/org/nuiton/web/war/WinstoneLauncher.java trunk/nuiton-web/src/main/java/org/nuiton/web/war/package-info.java trunk/nuiton-web/src/main/resources/ trunk/nuiton-web/src/site/ trunk/nuiton-web/src/site/apt/ trunk/nuiton-web/src/site/apt/Warlauncher.apt trunk/nuiton-web/src/site/apt/index.apt trunk/nuiton-web/src/site/site_fr.xml trunk/nuiton-web/src/test/ trunk/nuiton-web/src/test/java/ trunk/nuiton-web/src/test/resources/ Modified: trunk/pom.xml Property changes on: trunk/nuiton-web ___________________________________________________________________ Added: svn:ignore + target *.log *.ipr *.iws *.iml .project .classpath .settings Added: trunk/nuiton-web/LICENSE.txt =================================================================== --- trunk/nuiton-web/LICENSE.txt (rev 0) +++ trunk/nuiton-web/LICENSE.txt 2011-01-26 16:07:53 UTC (rev 48) @@ -0,0 +1,166 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. + Property changes on: trunk/nuiton-web/LICENSE.txt ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: trunk/nuiton-web/README.txt ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/nuiton-web/changelog.txt =================================================================== --- trunk/nuiton-web/changelog.txt (rev 0) +++ trunk/nuiton-web/changelog.txt 2011-01-26 16:07:53 UTC (rev 48) @@ -0,0 +1,2 @@ +Please refer to the release note : + http://maven-site.nuiton.org/nuiton-rss/changes-report.html \ No newline at end of file Property changes on: trunk/nuiton-web/changelog.txt ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/nuiton-web/pom.xml =================================================================== --- trunk/nuiton-web/pom.xml (rev 0) +++ trunk/nuiton-web/pom.xml 2011-01-26 16:07:53 UTC (rev 48) @@ -0,0 +1,112 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <!-- ************************************************************* --> + <!-- *** POM Relationships *************************************** --> + <!-- ************************************************************* --> + + <parent> + <groupId>org.nuiton.web</groupId> + <artifactId>nuiton-web-parent</artifactId> + <version>1.0-SNAPSHOT</version> + </parent> + + <artifactId>nuiton-web</artifactId> + + <dependencies> + + <!-- Dependencies for WarLaunchers --> + + <dependency> + <groupId>org.jvnet.hudson.winstone</groupId> + <artifactId>winstone</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.eclipse.jetty.aggregate</groupId> + <artifactId>jetty-webapp</artifactId> + <scope>provided</scope> + </dependency> + + </dependencies> + + + <!-- ************************************************************* --> + <!-- *** Project Information ************************************* --> + <!-- ************************************************************* --> + + <name>Nuiton Web :: Nuiton Web</name> + <description>Usefull classes for web developpement.</description> + <inceptionYear>2011</inceptionYear> + + <!-- ************************************************************* --> + <!-- *** Build Settings ****************************************** --> + <!-- ************************************************************* --> + + <packaging>jar</packaging> + + <properties> + + <!-- extra files to include in release --> + <redmine.releaseFiles>${redmine.libReleaseFiles}</redmine.releaseFiles> + + </properties> + + <build> + + </build> + + <!-- ************************************************************* --> + <!-- *** Build Environment ************************************** --> + <!-- ************************************************************* --> + + <profiles> + + <!-- create assemblies at release time --> + <profile> + <id>assembly-profile</id> + <activation> + <property> + <name>performRelease</name> + <value>true</value> + </property> + </activation> + <build> + <defaultGoal>package</defaultGoal> + <plugins> + + <!-- launch in a release the assembly automaticly --> + <plugin> + <artifactId>maven-assembly-plugin</artifactId> + <executions> + <execution> + <id>create-assemblies</id> + <phase>package</phase> + <goals> + <goal>single</goal> + </goals> + </execution> + </executions> + <configuration> + <attach>false</attach> + <descriptorRefs> + <descriptorRef>deps</descriptorRef> + <descriptorRef>full</descriptorRef> + </descriptorRefs> + </configuration> + </plugin> + + </plugins> + + </build> + </profile> + + </profiles> + +</project> Property changes on: trunk/nuiton-web/pom.xml ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/nuiton-web/src/license/THIRD-PARTY.properties =================================================================== --- trunk/nuiton-web/src/license/THIRD-PARTY.properties (rev 0) +++ trunk/nuiton-web/src/license/THIRD-PARTY.properties 2011-01-26 16:07:53 UTC (rev 48) @@ -0,0 +1,16 @@ +# Generated by org.nuiton.license.plugin.AddThirdPartyMojo +#------------------------------------------------------------------------------- +# Already used licenses in project : +# - Apache Software License - Version 2.0 +# - BSD License +# - COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0 +# - Eclipse Public License - Version 1.0 +# - GNU Lesser General Public License version 2.1 +# - Sun Microsystems, Inc. Binary Code License Agreement for the JDK 5.0 +# - The Apache Software License, Version 2.0 +#------------------------------------------------------------------------------- +# Please fill the missing licenses for dependencies : +# +# +#Wed Jan 26 16:55:58 CET 2011 +javax.servlet--servlet-api--2.5--jar=Sun Microsystems, Inc. Binary Code License Agreement for the JDK 5.0 Property changes on: trunk/nuiton-web/src/license/THIRD-PARTY.properties ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/nuiton-web/src/main/java/org/nuiton/web/war/JettyLauncher.java =================================================================== --- trunk/nuiton-web/src/main/java/org/nuiton/web/war/JettyLauncher.java (rev 0) +++ trunk/nuiton-web/src/main/java/org/nuiton/web/war/JettyLauncher.java 2011-01-26 16:07:53 UTC (rev 48) @@ -0,0 +1,297 @@ +/* + * #%L + * Nuiton Web :: Nuiton Web + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.web.war; + +import org.eclipse.jetty.server.Connector; +import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.DefaultHandler; +import org.eclipse.jetty.server.handler.HandlerCollection; +import org.eclipse.jetty.server.nio.SelectChannelConnector; +import org.eclipse.jetty.webapp.WebAppContext; + +import javax.swing.ImageIcon; +import java.awt.AWTException; +import java.awt.Desktop; +import java.awt.Image; +import java.awt.MenuItem; +import java.awt.PopupMenu; +import java.awt.SystemTray; +import java.awt.TrayIcon; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseEvent; +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; +import java.net.URL; + +/** + * War main class launcher (jetty based). + * + * To use it : + * java -jar app-xxx.war + * + * @author chatellier + * @version $Id$ + * @since 1.0 + */ +public class JettyLauncher implements ActionListener, MouseListener { + + /** Jetty server instance. */ + protected Server jettyServer; + + /** Server URI. */ + protected URI serverUri; + + /** Default port. */ + protected int port = 8888; + + /** Server name. */ + protected String serverName; + + /** + * Main method (used by war in manifest). + * + * @param args args + * @throws Exception + */ + 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 + * @throws Exception + */ + protected void startServer(String[] args) throws Exception { + System.out.println("Starting server embedded mode..."); + + String fqnLauncherFile = JettyLauncher.class.getName().replaceAll("\\.", "/") + ".class"; + System.out.println("Search for launcher class : " + fqnLauncherFile); + + URL classFile = JettyLauncher.class.getClassLoader().getResource(fqnLauncherFile); + System.out.println(" - using classFile : " + classFile); + + // 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); + + jettyServer = new Server(); + + Connector connector = new SelectChannelConnector(); + connector.setPort(port); + jettyServer.setConnectors(new Connector[]{connector}); + + WebAppContext webappcontext = new WebAppContext(); + webappcontext.setContextPath("/"); + webappcontext.setWar(me.getAbsolutePath()); + + HandlerCollection handlers= new HandlerCollection(); + handlers.setHandlers(new Handler[]{webappcontext, new DefaultHandler()}); + jettyServer.setHandler(handlers); + + jettyServer.start(); + + // build server uri + try { + serverUri = new URI("http://localhost:" + port); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + } + + /** + * Shutdown server. + */ + protected void stopServer() { + if (jettyServer != null) { + try { + jettyServer.stop(); + } catch (Exception e) { + e.printStackTrace(); + } + System.exit(0); + } + } + + /** + * Install system tray to stop server. + */ + protected void installSystemTray() { + if (SystemTray.isSupported()) { + // build menu + PopupMenu menu = new PopupMenu(); + MenuItem browserItem = new MenuItem("Start browser"); + browserItem.addActionListener(this); + browserItem.setActionCommand("browser"); + menu.add(browserItem); + + MenuItem stopItem = new MenuItem("Stop server"); + stopItem.addActionListener(this); + stopItem.setActionCommand("stop"); + menu.add(stopItem); + + // build tray icon + URL imageURL = JettyLauncher.class.getResource("/favicon.png"); + if (imageURL == null) { + imageURL = JettyLauncher.class.getResource("/favicon.jpg"); + } + if (imageURL == null) { + System.out.println("No favicon.{png|jpg} found, skip systray installation"); + } else { + Image image = new ImageIcon(imageURL).getImage(); + TrayIcon icon = new TrayIcon(image, serverName, menu); + icon.setImageAutoSize(true); + icon.addMouseListener(this); + + // System tray + SystemTray systemTray = SystemTray.getSystemTray(); + try { + systemTray.add(icon); + } catch (AWTException ex) { + throw new RuntimeException("Can't install tray icon", ex); + } + } + } + } + + @Override + public void actionPerformed(ActionEvent e) { + if ("browser".equalsIgnoreCase(e.getActionCommand())) { + openBrowser(); + } else if ("stop".equalsIgnoreCase(e.getActionCommand())) { + stopServer(); + } + } + + /** + * Open browser. + * + * @throws IOException + */ + protected void openBrowser() { + if (Desktop.isDesktopSupported() && serverUri != null) { + Desktop desktop = Desktop.getDesktop(); + if (desktop.isSupported(Desktop.Action.BROWSE)) { + System.out.println("Opening browser at " + serverUri); + try { + desktop.browse(serverUri); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + @Override + public void mouseClicked(MouseEvent e) { + if (e.getClickCount() == 2) { + openBrowser(); + } + } + + @Override + public void mousePressed(MouseEvent e) { + + } + + @Override + public void mouseReleased(MouseEvent e) { + + } + + @Override + public void mouseEntered(MouseEvent e) { + + } + + @Override + public void mouseExited(MouseEvent e) { + + } +} Property changes on: trunk/nuiton-web/src/main/java/org/nuiton/web/war/JettyLauncher.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/nuiton-web/src/main/java/org/nuiton/web/war/WinstoneLauncher.java =================================================================== --- trunk/nuiton-web/src/main/java/org/nuiton/web/war/WinstoneLauncher.java (rev 0) +++ trunk/nuiton-web/src/main/java/org/nuiton/web/war/WinstoneLauncher.java 2011-01-26 16:07:53 UTC (rev 48) @@ -0,0 +1,294 @@ +/* + * #%L + * Nuiton Web :: Nuiton Web + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.web.war; + +import winstone.Launcher; + +import javax.swing.ImageIcon; +import java.awt.AWTException; +import java.awt.Desktop; +import java.awt.Image; +import java.awt.MenuItem; +import java.awt.PopupMenu; +import java.awt.SystemTray; +import java.awt.TrayIcon; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseEvent; +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; +import java.net.URL; +import java.util.Map; + +/** + * War main class launcher (winstone based). + * + * Based on winstone micro container. + * + * To use it : + * java -jar app-xxx.war + * + * @author chatellier + * @version $Id$ + * @since 1.0 + */ +public class WinstoneLauncher implements ActionListener, MouseListener { + + /** Winstone server instance. */ + protected Launcher winstone; + + /** Server URI. */ + protected URI serverUri; + + /** Server name. */ + protected String serverName; + + /** + * Main method (used by war in manifest). + * + * @param args args + * @throws IOException + */ + 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 + * @throws IOException + */ + protected void startServer(String[] args) throws IOException { + System.out.println("Starting server embedded mode..."); + + String fqnLauncherFile = WinstoneLauncher.class.getName().replaceAll("\\.", "/") + ".class"; + System.out.println("Search for launcher class : " + fqnLauncherFile); + + URL classFile = WinstoneLauncher.class.getClassLoader().getResource(fqnLauncherFile); + System.out.println(" - using classFile : " + classFile); + + // 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); + + // hashArgs map, initialized with command line args + Map<String, String> hashArgs = Launcher.getArgsFromCommandLine(args); + + hashArgs.put("warfile", me.getAbsolutePath()); // or any other command line args, eg port + + System.out.println(" - using args : " + hashArgs); + + Launcher.initLogger(hashArgs); + winstone = new Launcher(hashArgs); // spawns threads, so your application doesn't block + + // open browser + int port = 8080; + String configPort = hashArgs.get("httpPort"); + if (configPort != null && !configPort.isEmpty()) { + try { + port = Integer.parseInt(configPort); + } catch (NumberFormatException e) { + e.printStackTrace(); + } + } + + // build server uri + try { + serverUri = new URI("http://localhost:" + port); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + } + + /** + * Shutdown server. + */ + protected void stopServer() { + if (winstone != null) { + winstone.shutdown(); + System.exit(0); + } + } + + /** + * Install system tray to stop server. + */ + protected void installSystemTray() { + if (SystemTray.isSupported()) { + // build menu + PopupMenu menu = new PopupMenu(); + MenuItem browserItem = new MenuItem("Start browser"); + browserItem.addActionListener(this); + browserItem.setActionCommand("browser"); + menu.add(browserItem); + + MenuItem stopItem = new MenuItem("Stop server"); + stopItem.addActionListener(this); + stopItem.setActionCommand("stop"); + menu.add(stopItem); + + // build tray icon + URL imageURL = WinstoneLauncher.class.getResource("/favicon.png"); + if (imageURL == null) { + imageURL = WinstoneLauncher.class.getResource("/favicon.jpg"); + } + if (imageURL == null) { + System.out.println("No favicon.{png|jpg} found, skip systray installation"); + } else { + Image image = new ImageIcon(imageURL).getImage(); + TrayIcon icon = new TrayIcon(image, serverName, menu); + icon.setImageAutoSize(true); + icon.addMouseListener(this); + + // System tray + SystemTray systemTray = SystemTray.getSystemTray(); + try { + systemTray.add(icon); + } catch (AWTException ex) { + throw new RuntimeException("Can't install tray icon", ex); + } + } + } + } + + /* + * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) + */ + @Override + public void actionPerformed(ActionEvent e) { + if ("browser".equalsIgnoreCase(e.getActionCommand())) { + openBrowser(); + } else if ("stop".equalsIgnoreCase(e.getActionCommand())) { + stopServer(); + } + } + + /** + * Open browser. + * + * @throws IOException + */ + protected void openBrowser() { + if (Desktop.isDesktopSupported() && serverUri != null) { + Desktop desktop = Desktop.getDesktop(); + if (desktop.isSupported(Desktop.Action.BROWSE)) { + System.out.println("Opening browser at " + serverUri); + try { + desktop.browse(serverUri); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + @Override + public void mouseClicked(MouseEvent e) { + if (e.getClickCount() == 2) { + openBrowser(); + } + } + + @Override + public void mousePressed(MouseEvent e) { + + } + + @Override + public void mouseReleased(MouseEvent e) { + + } + + @Override + public void mouseEntered(MouseEvent e) { + + } + + @Override + public void mouseExited(MouseEvent e) { + + } +} Property changes on: trunk/nuiton-web/src/main/java/org/nuiton/web/war/WinstoneLauncher.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/nuiton-web/src/main/java/org/nuiton/web/war/package-info.java =================================================================== --- trunk/nuiton-web/src/main/java/org/nuiton/web/war/package-info.java (rev 0) +++ trunk/nuiton-web/src/main/java/org/nuiton/web/war/package-info.java 2011-01-26 16:07:53 UTC (rev 48) @@ -0,0 +1,34 @@ +/* + * #%L + * Nuiton Web :: Nuiton Web + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +/** + * Embedded war launchers. + * + * Currently implemented with : + * <ul> + * <li>Winstone : {@link org.nuiton.web.war.WinstoneLauncher}</li> + * <li>Jetty : {@link org.nuiton.web.war.JettyLauncher}</li> + * </ul> + */ +package org.nuiton.web.war; Property changes on: trunk/nuiton-web/src/main/java/org/nuiton/web/war/package-info.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/nuiton-web/src/site/apt/Warlauncher.apt =================================================================== --- trunk/nuiton-web/src/site/apt/Warlauncher.apt (rev 0) +++ trunk/nuiton-web/src/site/apt/Warlauncher.apt 2011-01-26 16:07:53 UTC (rev 48) @@ -0,0 +1,174 @@ +~~~ +~~ #%L +~~ Nuiton Web :: Nuiton Web +~~ +~~ $Id$ +~~ $HeadURL$ +~~ %% +~~ Copyright (C) 2011 CodeLutin +~~ %% +~~ This program is free software: you can redistribute it and/or modify +~~ it under the terms of the GNU Lesser General Public License as +~~ published by the Free Software Foundation, either version 3 of the +~~ License, or (at your option) any later version. +~~ +~~ This program is distributed in the hope that it will be useful, +~~ but WITHOUT ANY WARRANTY; without even the implied warranty of +~~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +~~ GNU General Lesser Public License for more details. +~~ +~~ You should have received a copy of the GNU General Lesser Public +~~ License along with this program. If not, see +~~ <http://www.gnu.org/licenses/lgpl-3.0.html>. +~~ #L% +~~~ + ------ + War launcher + ------ + + +War launcher + +* Features + + * Start embedded servlet container with current webapp + + * Favicon support + + * Systray with popup menu + +* Prerequisites + + By default, both jetty and winstone look for an icon named <<favicon.png>> or + <<favicon.jpg>> at webapp base. If none of this icons is found, system tray + won't work. + + In maven, by default, this file must be put in <<<src/main/webapp>>> directory. + + If <<<display-name>>> content can be read into <<<WEB-INF/web.xml>>> file, + it's used as server name (currently in systray tooltip). + +* Use Winstone war launcher + +** Maven configuration + + Add following dependencies to your project. + +------------------------------------------------ +<dependency> + <groupId>org.nuiton</groupId> + <artifactId>nuiton-utils</artifactId> + <version>1.3</version> + <scope>provided</scope> +</dependency> +<dependency> + <groupId>org.jvnet.hudson.winstone</groupId> + <artifactId>winstone</artifactId> + <version>0.9.10-hudson-16</version> + <scope>provided</scope> +</dependency> +------------------------------------------------ + + They have to be both at least in <<<provided>>> scope. + + Then, you need to add following configuration into maven-war-plugin : + +-------------------------------------------------------------------- +<plugin> + <artifactId>maven-war-plugin</artifactId> + <version>2.1-beta-1</version> + <configuration> + <archive> + <manifest> + <mainClass>org.nuiton.util.war.WinstoneLauncher</mainClass> + </manifest> + </archive> + <overlays> + <overlay> + <groupId>org.nuiton</groupId> + <artifactId>nuiton-utils</artifactId> + <type>jar</type> + <includes> + <include>**/war/Winstone*</include> + </includes> + </overlay> + <overlay> + <groupId>org.jvnet.hudson.winstone</groupId> + <artifactId>winstone</artifactId> + <type>jar</type> + </overlay> + </overlays> + </configuration> +</plugin> +-------------------------------------------------------------------- + +** References + + * Winstone home : {{http://winstone.sourceforge.net}} + + * Winstone command line options : {{http://winstone.sourceforge.net/#commandLine}} + +* Jetty based + +** Maven configuration + + Add following dependencies to your project. + +------------------------------------------------ +<dependency> + <groupId>org.nuiton</groupId> + <artifactId>nuiton-utils</artifactId> + <version>1.3</version> + <scope>provided</scope> +</dependency> +<dependency> + <groupId>org.eclipse.jetty.aggregate</groupId> + <artifactId>jetty-webapp</artifactId> + <version>7.1.0.v20100505</version> + <scope>provided</scope> +</dependency> +<dependency> + <groupId>javax.servlet</groupId> + <artifactId>servlet-api</artifactId> + <version>2.5</version> + <scope>provided</scope> +</dependency> +------------------------------------------------ + + They have to be both at least in <<<provided>>> scope. + + Then, you need to add following configuration into maven-war-plugin : + +-------------------------------------------------------------------- +<plugin> + <artifactId>maven-war-plugin</artifactId> + <version>2.1-beta-1</version> + <configuration> + <archive> + <manifest> + <mainClass>org.nuiton.util.war.JettyLauncher</mainClass> + </manifest> + </archive> + <overlays> + <overlay> + <groupId>org.nuiton</groupId> + <artifactId>nuiton-utils</artifactId> + <type>jar</type> + <includes> + <include>**/war/Jetty*</include> + </includes> + </overlay> + <overlay> + <groupId>org.eclipse.jetty.aggregate</groupId> + <artifactId>jetty-webapp</artifactId> + <type>jar</type> + </overlay> + <overlay> + <groupId>javax.servlet</groupId> + <artifactId>servlet-api</artifactId> + <type>jar</type> + </overlay> + </overlays> + </configuration> +</plugin> +-------------------------------------------------------------------- Property changes on: trunk/nuiton-web/src/site/apt/Warlauncher.apt ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/nuiton-web/src/site/apt/index.apt =================================================================== --- trunk/nuiton-web/src/site/apt/index.apt (rev 0) +++ trunk/nuiton-web/src/site/apt/index.apt 2011-01-26 16:07:53 UTC (rev 48) @@ -0,0 +1,34 @@ +~~~ +~~ #%L +~~ Nuiton Web :: Nuiton Web +~~ +~~ $Id$ +~~ $HeadURL$ +~~ %% +~~ Copyright (C) 2011 CodeLutin +~~ %% +~~ This program is free software: you can redistribute it and/or modify +~~ it under the terms of the GNU Lesser General Public License as +~~ published by the Free Software Foundation, either version 3 of the +~~ License, or (at your option) any later version. +~~ +~~ This program is distributed in the hope that it will be useful, +~~ but WITHOUT ANY WARRANTY; without even the implied warranty of +~~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +~~ GNU General Lesser Public License for more details. +~~ +~~ You should have received a copy of the GNU General Lesser Public +~~ License along with this program. If not, see +~~ <http://www.gnu.org/licenses/lgpl-3.0.html>. +~~ #L% +~~~ + ---- + Nuiton Web + ---- + ---- + 2009-08-23 + ---- + +Présentation + + A faire... Property changes on: trunk/nuiton-web/src/site/apt/index.apt ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/nuiton-web/src/site/site_fr.xml =================================================================== --- trunk/nuiton-web/src/site/site_fr.xml (rev 0) +++ trunk/nuiton-web/src/site/site_fr.xml 2011-01-26 16:07:53 UTC (rev 48) @@ -0,0 +1,62 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + #%L + Nuiton Web :: Nuiton Web + + $Id$ + $HeadURL$ + %% + Copyright (C) 2011 CodeLutin + %% + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Lesser Public License for more details. + + You should have received a copy of the GNU General Lesser Public + License along with this program. If not, see + <http://www.gnu.org/licenses/lgpl-3.0.html>. + #L% + --> + +<project name="${project.name}"> + + <bannerLeft> + <name>${project.name}</name> + <href>index.html</href> + </bannerLeft> + + <body> + + <breadcrumbs> + <item name="${project.name}" href="index.html"/> + </breadcrumbs> + + <menu ref="parent"/> + + <menu name="Utilisateur"> + <item name="Accueil" href="index.html"/> + <item name="War launcher" href="/Warlauncher.html"/> + </menu> + + <menu name="Téléchargement"> + <item + href="${repository.home.url}/org/nuiton/${project.artifactId}/${project.version}/${project.build.finalName}.jar" + name="Librairie (jar)"/> + <item + href="${repository.home.url}/org/nuiton/${project.artifactId}/${project.version}/${project.build.finalName}-javadoc.jar" + name="Javadoc (jar)"/> + <item + href="${repository.home.url}/org/nuiton/${project.artifactId}/${project.version}/${project.build.finalName}-sources.jar" + name="Sources (jar)"/> + </menu> + + <menu ref="reports"/> + + </body> +</project> Property changes on: trunk/nuiton-web/src/site/site_fr.xml ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2011-01-26 15:30:14 UTC (rev 47) +++ trunk/pom.xml 2011-01-26 16:07:53 UTC (rev 48) @@ -20,6 +20,7 @@ <version>1.0-SNAPSHOT</version> <modules> + <module>nuiton-web</module> <module>nuiton-tapestry</module> <module>nuiton-rss</module> </modules> @@ -28,6 +29,20 @@ <dependencies> + <!-- WarLaunchers --> + + <dependency> + <groupId>org.jvnet.hudson.winstone</groupId> + <artifactId>winstone</artifactId> + <version>0.9.10-hudson-24</version> + </dependency> + + <dependency> + <groupId>org.eclipse.jetty.aggregate</groupId> + <artifactId>jetty-webapp</artifactId> + <version>7.1.0.v20100505</version> + </dependency> + <!-- Tapestry --> <dependency> <groupId>org.apache.tapestry</groupId>
participants (1)
-
tchemit@users.nuiton.org