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