Coverage Report - org.kuali.rice.test.lifecycles.JettyServerLifecycle
 
Classes in this File Line Coverage Branch Coverage Complexity
JettyServerLifecycle
0%
0/62
0%
0/20
2
JettyServerLifecycle$ConfigMode
0%
0/4
N/A
2
 
 1  
 /*
 2  
  * Copyright 2007-2008 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.test.lifecycles;
 17  
 
 18  
 import java.net.BindException;
 19  
 import java.util.HashMap;
 20  
 import java.util.Map;
 21  
 import java.util.Set;
 22  
 
 23  
 import org.apache.log4j.Logger;
 24  
 import org.kuali.rice.core.api.config.property.Config;
 25  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 26  
 import org.kuali.rice.core.api.lifecycle.Lifecycle;
 27  
 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
 28  
 import org.kuali.rice.core.api.resourceloader.ResourceLoader;
 29  
 import org.kuali.rice.core.api.util.RiceUtilities;
 30  
 import org.kuali.rice.core.web.jetty.JettyServer;
 31  
 import org.mortbay.jetty.webapp.WebAppClassLoader;
 32  
 
 33  
 
 34  
 /**
 35  
  * A lifecycle for running a jetty web server.
 36  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 37  
  */
 38  
 public class JettyServerLifecycle implements Lifecycle {
 39  0
     private static final Logger LOG = Logger.getLogger(JettyServerLifecycle.class);
 40  
 
 41  0
     private static final HashMap<Integer, Config> WEBAPP_CONFIGS = new HashMap<Integer, Config>();
 42  
 
 43  
     public static Config getWebappConfig(int port) {
 44  0
         return WEBAPP_CONFIGS.get(port);
 45  
     }
 46  
 
 47  
     /**
 48  
      * Enum for dealing with the webapp's Config
 49  
      */
 50  0
     public static enum ConfigMode {
 51  
         /**
 52  
          * Do nothing
 53  
          */
 54  0
         NONE,
 55  
         /**
 56  
          * Override the Config for the context class loader
 57  
          */
 58  0
         OVERRIDE,
 59  
         /**
 60  
          * Merge the webapp's Config into the existing context class loader config
 61  
          */
 62  0
         MERGE
 63  
     }
 64  
 
 65  
     /**
 66  
      * By default we set the JettyServer to test mode
 67  
      */
 68  0
     private boolean testMode = true;
 69  
     private boolean started;
 70  0
     private ConfigMode configMode = ConfigMode.OVERRIDE;
 71  0
     private boolean addWebappResourceLoaders = true;
 72  
 
 73  
         
 74  
         protected JettyServer jettyServer;
 75  
                 
 76  
         public JettyServerLifecycle() {
 77  0
                 this(8080, null);
 78  0
         }
 79  
 
 80  
         public JettyServerLifecycle(int port) {
 81  0
                 this(port, null, null);
 82  0
         }
 83  
 
 84  
         public JettyServerLifecycle(int port, String contextName) {
 85  0
                 this(port, contextName, null);
 86  0
         }
 87  
         
 88  0
         public JettyServerLifecycle(int port, String contextName, String relativeWebappRoot) {
 89  0
                 jettyServer = new JettyServer(port, contextName, relativeWebappRoot);
 90  0
                 jettyServer.setFailOnContextFailure(true);
 91  0
                 jettyServer.setTestMode(testMode);
 92  0
         }        
 93  
         
 94  
     public void setTestMode(boolean t) {
 95  0
         this.testMode = t;
 96  0
     }
 97  
 
 98  
     public boolean isTestMode() {
 99  0
         return testMode;
 100  
     }
 101  
 
 102  
     public ConfigMode getConfigMode() {
 103  0
         return this.configMode;
 104  
     }
 105  
 
 106  
     public void setConfigMode(ConfigMode configMode) {
 107  0
         this.configMode = configMode;
 108  0
     }
 109  
 
 110  
     public boolean isAddWebappResourceLoaders() {
 111  0
         return this.addWebappResourceLoaders;
 112  
     }
 113  
 
 114  
     public void setAddWebappResourceLoaders(boolean addWebappResourceLoaders) {
 115  0
         this.addWebappResourceLoaders = addWebappResourceLoaders;
 116  0
     }
 117  
 
 118  
         public boolean isStarted() {
 119  0
                 return started;
 120  
         }
 121  
 
 122  
         public void start() throws Exception {
 123  
             try {
 124  0
                 jettyServer.start();
 125  
                 
 126  0
             } catch (RuntimeException re) {
 127  
                 // add some handling to make port conflicts more easily identified
 128  0
                 if (RiceUtilities.findExceptionInStack(re, BindException.class) != null) {
 129  0
                     LOG.error("JettyServerLifecycle encountered BindException on port: " + jettyServer.getPort() + "; check logs for test failures or and the config for duplicate port specifications.");
 130  
                 }
 131  0
                 throw re;
 132  0
             }
 133  
 
 134  0
             ClassLoader webappClassLoader = jettyServer.getContext().getClassLoader();
 135  0
             if (addWebappResourceLoaders) {
 136  0
                 ResourceLoader rl = GlobalResourceLoader.getResourceLoader(webappClassLoader);
 137  0
             if (rl == null) {
 138  0
                 throw new RuntimeException("Could not find resource loader for workflow test harness web app for: " + webappClassLoader);
 139  
             }
 140  0
             GlobalResourceLoader.addResourceLoader(rl);
 141  
             }
 142  
 
 143  
         // TODO: RICE-2.0 UPGRADE had to jump through hoops with M8 because getConfig was made private.  Uncomment the following and remove the next block
 144  
             // Config webappConfig = ConfigContext.getConfig(webappClassLoader);
 145  
 
 146  0
         Config webappConfig = null;
 147  0
         for (Map.Entry<ClassLoader, Config> configEntry : ConfigContext.getConfigs()) {
 148  0
             if (configEntry.getKey() instanceof WebAppClassLoader) {
 149  0
                 webappConfig = configEntry.getValue();
 150  
             }
 151  
         }
 152  
 
 153  0
         WEBAPP_CONFIGS.put(jettyServer.getPort(), webappConfig);
 154  0
             if (ConfigMode.OVERRIDE == configMode) {
 155  
             // this overrides the test harness classloader config with the webapp's config...
 156  0
             ConfigContext.overrideConfig(Thread.currentThread().getContextClassLoader(), webappConfig);
 157  0
         } else if (ConfigMode.MERGE == configMode) {
 158  0
             Config curCtxConfig = ConfigContext.getCurrentContextConfig();
 159  0
             if (webappConfig != null) {
 160  0
                 curCtxConfig.putProperties(webappConfig.getProperties());
 161  0
                 curCtxConfig.putObjects(webappConfig.getObjects());
 162  
             }
 163  
         }
 164  
 
 165  0
                 started = true;
 166  0
         }
 167  
 
 168  
         public void stop() throws Exception {
 169  0
             LOG.info("Shutting down jetty: " + jettyServer);
 170  
             try {
 171  0
                     if (jettyServer != null && jettyServer.isStarted()) {
 172  0
                             jettyServer.stop();
 173  0
                             WEBAPP_CONFIGS.remove(jettyServer.getPort());
 174  
                     }
 175  0
             } catch (Exception e) {
 176  0
                 LOG.error("Error shutting down Jetty " + jettyServer.getContextName() + " " + jettyServer.getRelativeWebappRoot(), e);
 177  0
             }
 178  0
                 started = false;
 179  0
         }
 180  
 }