Coverage Report - org.kuali.rice.core.web.jetty.JettyServer
 
Classes in this File Line Coverage Branch Coverage Complexity
JettyServer
0%
0/87
0%
0/22
1.63
 
 1  
 /*
 2  
  * Copyright 2007 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.core.web.jetty;
 17  
 
 18  
 import java.io.File;
 19  
 import java.lang.reflect.Field;
 20  
 
 21  
 import org.apache.commons.lang.StringUtils;
 22  
 import org.apache.commons.lang.builder.ToStringBuilder;
 23  
 import org.kuali.rice.core.api.lifecycle.Lifecycle;
 24  
 import org.mortbay.jetty.Server;
 25  
 import org.mortbay.jetty.servlet.Context;
 26  
 import org.mortbay.jetty.servlet.ServletHolder;
 27  
 import org.mortbay.jetty.webapp.WebAppContext;
 28  
 
 29  
 public class JettyServer implements Lifecycle {
 30  
         
 31  0
         private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
 32  
                         .getLogger(JettyServer.class);
 33  
         
 34  
     /**
 35  
      * The name of an attribute we set in the ServletContext to indicate to the webapp
 36  
      * that it is running within unit tests, in case it needs to alter its configuration
 37  
      * or behavior.
 38  
      */
 39  
     public static final String JETTYSERVER_TESTMODE_ATTRIB = "JETTYSERVER_TESTMODE";
 40  
 
 41  
         private int port;
 42  
         private String contextName;        
 43  
         private String relativeWebappRoot;
 44  
         private Class servletClass;
 45  
         private Server server;
 46  
         private Context context;
 47  
         private boolean failOnContextFailure;
 48  
 
 49  
         /**
 50  
          * Whether we are in test mode
 51  
          */
 52  0
         private boolean testMode = false;
 53  
 
 54  
         public JettyServer() {
 55  0
                 this(8080);
 56  0
         }
 57  
 
 58  
         public JettyServer(int port) {
 59  0
                 this(port, null, null, null);
 60  0
         }
 61  
 
 62  
         public JettyServer(int port, String contextName) {
 63  0
                 this(port, contextName, null, null);
 64  0
         }
 65  
         
 66  
         public JettyServer(int port, String contextName, String relativeWebappRoot) {
 67  0
         this(port, contextName, relativeWebappRoot, null);
 68  0
         }        
 69  
 
 70  
     public JettyServer(int port, String contextName, Class servletClass) {
 71  0
         this(port, contextName, null, servletClass);
 72  0
     }   
 73  
 
 74  0
     public JettyServer(int port, String contextName, String relativeWebappRoot, Class servletClass) {
 75  0
         this.port = port;
 76  0
         this.contextName = contextName;
 77  0
         this.relativeWebappRoot = relativeWebappRoot;
 78  0
         this.servletClass = servletClass;
 79  0
     }   
 80  
 
 81  
     public void setTestMode(boolean t) {
 82  0
             this.testMode = t;
 83  0
         }
 84  
 
 85  
         public boolean isTestMode() {
 86  0
             return testMode;
 87  
         }
 88  
 
 89  
         public Server getServer() {
 90  0
                 return server;
 91  
         }
 92  
 
 93  
         public Context getContext() {
 94  0
             return context;
 95  
         }
 96  
 
 97  
         public void start() throws Exception {
 98  0
                 server = createServer();
 99  0
                 server.start();
 100  0
                 if (isFailOnContextFailure() && contextStartupFailed()) {
 101  
                         try {
 102  0
                                 server.stop();
 103  0
                         } catch (Exception e) {
 104  0
                                 LOG.warn("Failed to stop server after web application startup failure.");
 105  0
                         }
 106  0
                         throw new Exception("Failed to startup web application context!  Check logs for specific error.");
 107  
                 }
 108  0
         }
 109  
 
 110  
         public void stop() throws Exception {
 111  0
                 server.stop(); 
 112  0
         }
 113  
 
 114  
         public boolean isStarted() {
 115  0
                 return server.isStarted();
 116  
         }
 117  
 
 118  
         protected Server createServer() {
 119  0
                 Server server = new Server(getPort());
 120  0
                 setBaseDirSystemProperty();
 121  0
                 if (useWebAppContext()) {
 122  0
                         File tmpDir = new File(System.getProperty("basedir") + "/target/jetty-tmp");
 123  0
                         tmpDir.mkdirs();
 124  0
                         if (LOG.isInfoEnabled()) {
 125  0
                                 LOG.info("WebAppRoot = " + System.getProperty("basedir") + getRelativeWebappRoot());
 126  
                         }
 127  0
                         WebAppContext webAppContext = new WebAppContext(System.getProperty("basedir") + getRelativeWebappRoot(), getContextName());
 128  0
                         webAppContext.setTempDirectory(tmpDir);
 129  0
                         webAppContext.setAttribute(JETTYSERVER_TESTMODE_ATTRIB, String.valueOf(isTestMode()));
 130  0
                         context = webAppContext;
 131  0
                         server.addHandler(context);
 132  0
                 } else {
 133  0
                         Context root = new Context(server,"/",Context.SESSIONS);
 134  0
                         root.addServlet(new ServletHolder(servletClass), getContextName());
 135  0
                         root.setAttribute(JETTYSERVER_TESTMODE_ATTRIB, String.valueOf(isTestMode()));
 136  0
                         context = root;
 137  
                 }
 138  0
                 return server;
 139  
         }
 140  
 
 141  
         protected void setBaseDirSystemProperty() {
 142  0
                 if (System.getProperty("basedir") == null) {
 143  0
                         System.setProperty("basedir", System.getProperty("user.dir"));
 144  
                 }
 145  0
         }
 146  
         
 147  
         private boolean useWebAppContext() {
 148  0
                 return StringUtils.isNotBlank(this.relativeWebappRoot);
 149  
         }
 150  
         
 151  
         /**
 152  
          * A hack for Jetty so that we can detect if context startup failed.  Jetty has no programatic
 153  
          * way available to detect if context startup failed.  Instead we have to use reflection to
 154  
          * check the value of a private variable.  See http://jira.codehaus.org/browse/JETTY-319
 155  
          * for more details on the issue.
 156  
          */
 157  
         protected boolean contextStartupFailed() throws Exception {
 158  
         /*
 159  
                  * We can only tell if the context startup failed if the server is using a WebAppContext object since the
 160  
                  * org.mortbay.jetty.servlet.Context object does not have a field named '_unavailable'
 161  
                  */
 162  0
                 if (useWebAppContext()) {
 163  0
                         Field unavailableField = context.getClass().getDeclaredField("_unavailable");
 164  0
                         unavailableField.setAccessible(true);
 165  0
                         return unavailableField.getBoolean(context);
 166  
                 }
 167  0
                 return false;
 168  
         }
 169  
         
 170  
         public String getRelativeWebappRoot() {
 171  0
                 if (relativeWebappRoot == null) {
 172  0
                         return "/sampleapp/web-root";
 173  
                 }
 174  0
                 return relativeWebappRoot;
 175  
         }
 176  
 
 177  
         public void setRelativeWebappRoot(String relativeWebappRoot) {
 178  0
                 this.relativeWebappRoot = relativeWebappRoot;
 179  0
         }
 180  
 
 181  
         public String getContextName() {
 182  0
                 if (contextName == null) {
 183  0
                         return "/SampleRiceClient";
 184  
                 }
 185  0
                 return contextName;
 186  
         }
 187  
 
 188  
         public void setContextName(String contextName) {
 189  0
                 this.contextName = contextName;
 190  0
         }
 191  
 
 192  
         public int getPort() {
 193  0
                 return port;
 194  
         }
 195  
 
 196  
         public void setPort(int port) {
 197  0
                 this.port = port;
 198  0
         }
 199  
         
 200  
         
 201  
         public boolean isFailOnContextFailure() {
 202  0
                 return this.failOnContextFailure;
 203  
         }
 204  
 
 205  
         public void setFailOnContextFailure(boolean failOnContextFailure) {
 206  0
                 this.failOnContextFailure = failOnContextFailure;
 207  0
         }
 208  
 
 209  
         public String toString() {
 210  0
             return new ToStringBuilder(this).append("port", port)
 211  
                                             .append("contextName", contextName)
 212  
                                             .append("relativeWebappRoot", relativeWebappRoot)
 213  
                                         .append("servletClass", servletClass)
 214  
                                             .toString();
 215  
         }
 216  
 
 217  
     public static void main(String[] args) {
 218  0
         int port = args.length > 0 ? Integer.parseInt(args[0]) : 8080;
 219  0
         String contextName = args.length > 1 ? args[1] : null;
 220  0
         String relativeWebappRoot = args.length > 2 ? args[2] : null;
 221  
         try {
 222  0
             new JettyServer(port, contextName, relativeWebappRoot).start();
 223  0
         } catch (Exception e) {
 224  0
             e.printStackTrace();
 225  0
         }
 226  0
     }
 227  
 }