| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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 | |
|
| 36 | |
|
| 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 | |
|
| 49 | |
|
| 50 | 0 | public static enum ConfigMode { |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | 0 | NONE, |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | 0 | OVERRIDE, |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | 0 | MERGE |
| 63 | |
} |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 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 | |
|
| 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 | |
|
| 144 | |
|
| 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 | |
|
| 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 | |
} |