1 /* 2 Licensed under the Apache License, Version 2.0 (the "License"); 3 you may not use this file except in compliance with the License. 4 You may obtain a copy of the License at 5 6 http://www.apache.org/licenses/LICENSE-2.0 7 8 Unless required by applicable law or agreed to in writing, software 9 distributed under the License is distributed on an "AS IS" BASIS, 10 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 See the License for the specific language governing permissions and 12 limitations under the License. 13 */ 14 package uk.nhs.interoperability.client.samples; 15 16 import java.io.IOException; 17 import java.util.HashMap; 18 import java.util.Map; 19 20 import winstone.Launcher; 21 22 /** 23 * 24 * Creates a standalone server for the war - i.e. 25 * provides the entry point for an executable 26 * war file.<br/><br/> 27 * 28 * For information on how to get this to work and 29 * integration with Maven see:<br/><br/> 30 * 31 * http://winstone.sourceforge.net/#embedding (for winstone server starting), and<br/> 32 * http://internna.blogspot.co.uk/2011/08/step-by-step-executable-war-files.html 33 * for details of maven integration (the example integration is with Jetty) 34 * 35 * @author Michael Odling-Smee 36 * @author Nicholas Jones 37 * @since 0.1 38 * 39 */ 40 public class StandaloneServer { 41 42 /** 43 * Instantiates a new standalone server. 44 */ 45 private StandaloneServer() { 46 } 47 48 /** 49 * Entry point for starting the Standalone Server 50 * from the executable war. 51 * 52 * Reference to the executable war (itk-samples.war), 53 * note this means that you can only run the war 54 * from within the directory<br/><br/> 55 * 56 * <code>java -jar itk-samples.war</code> will work<br/><br> 57 * 58 * <code>java -jar path/to/war/itk-samples.war</code> won't work<br/><br> 59 * 60 * @param args the arguments 61 */ 62 public static void main(String[] args) { 63 System.out.println("Starting standalone server... "); 64 StandaloneServer standaloneServer = new StandaloneServer(); 65 standaloneServer.startUp(args); 66 } 67 68 private void startUp(String[] args) { 69 Launcher winstone = null; 70 try { 71 Map<String, String> containerArgs = new HashMap<String, String>(); 72 73 /* 74 * Tell it to use itself as the war file. The 75 * way this works is that Winstone (in the jar 76 * path in the itk-samples.war archive) starts 77 * and then loads a warfile - which happens to 78 * be the same archive 79 */ 80 containerArgs.put("warfile", "itk-samples.war"); 81 82 /* 83 * Need to provide a prefix so that configuration 84 * is consistent with when it is deployed as a 85 * normal war within an appserver. If this prefix 86 * is changed then the routing configuration 87 * for callbacks etc. will have to be updated 88 * accordingly 89 */ 90 containerArgs.put("prefix", "/itk-samples"); 91 92 /* 93 * Set the HTTP listener port 94 */ 95 containerArgs.put("httpPort", "8080"); 96 97 /* 98 * We ship with JSPs so need to indicate 99 * that Winstone should use Jasper 100 * to compile the JSPs 101 */ 102 containerArgs.put("useJasper", "true"); 103 104 /* 105 * Allow command line usage to override 106 */ 107 for (int i = 0; i < args.length; i++) { 108 String currentArg = args[i]; 109 //Check if this is a winstone style directive 110 if (currentArg.startsWith("--")) { 111 String key = currentArg.substring(2, currentArg.indexOf("=")); 112 String value = currentArg.substring(currentArg.indexOf("=") + 1); 113 containerArgs.put(key, value); 114 } 115 } 116 117 /* 118 * Initialise the logging and launch 119 * Winstone 120 */ 121 Launcher.initLogger(containerArgs); 122 winstone = new Launcher(containerArgs); 123 124 /* 125 * Need to block whilst Winstone 126 * is running 127 */ 128 while (winstone.isRunning()) { 129 try { 130 Thread.sleep(1000); 131 } catch (InterruptedException e) { 132 /* 133 * Shutdown winstone is the 134 * user presses Cntrl + C or 135 * similar 136 */ 137 winstone.shutdown(); 138 } 139 } 140 } catch (IOException e) { 141 e.printStackTrace(); 142 } finally { 143 if (winstone != null && winstone.isRunning()) { 144 /* 145 * Belt and braces - to make sure winstone is 146 * shut down if any exception bubbles all the way 147 * to the top 148 */ 149 winstone.shutdown(); 150 } 151 } 152 } 153 154 }