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  
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.api.util.RiceUtilities;
28  import org.kuali.rice.test.launch.JettyLauncher;
29  
30  
31  
32  
33  
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      public static Config getWebappConfig(int port) {
41          return WEBAPP_CONFIGS.get(port);
42      }
43  
44      
45  
46  
47      public static enum ConfigMode {
48          
49  
50  
51          NONE,
52          
53  
54  
55          OVERRIDE,
56          
57  
58  
59          MERGE
60      }
61  
62      
63  
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 JettyLauncher jettyServer;
72  
73  	public JettyServerLifecycle() {
74  		this(8080, null);
75  	}
76  
77  	public JettyServerLifecycle(int port) {
78  		this(port, null, null);
79  	}
80  
81  	public JettyServerLifecycle(int port, String contextName) {
82  		this(port, contextName, null);
83  	}
84  
85  	public JettyServerLifecycle(int port, String contextName, String relativeWebappRoot) {
86  		jettyServer = new JettyLauncher(port, contextName, relativeWebappRoot);
87          jettyServer.setFailOnContextFailure(true);
88  		jettyServer.setTestMode(testMode);
89  	}
90  
91      public void setTestMode(boolean t) {
92          this.testMode = t;
93      }
94  
95      public boolean isTestMode() {
96          return testMode;
97      }
98  
99      public ConfigMode getConfigMode() {
100         return this.configMode;
101     }
102 
103     public void setConfigMode(ConfigMode configMode) {
104         this.configMode = configMode;
105     }
106 
107     public boolean isAddWebappResourceLoaders() {
108         return this.addWebappResourceLoaders;
109     }
110 
111     public void setAddWebappResourceLoaders(boolean addWebappResourceLoaders) {
112         this.addWebappResourceLoaders = addWebappResourceLoaders;
113     }
114 
115 	public boolean isStarted() {
116 		return started;
117 	}
118 
119 	public void start() throws Exception {
120 	    try {
121 	        jettyServer.start();
122 
123 	    } catch (RuntimeException re) {
124 	        
125 	        if (RiceUtilities.findExceptionInStack(re, BindException.class) != null) {
126 	            LOG.error("JettyServerLifecycle encountered BindException on port: " + jettyServer.getPort() + "; check logs for test junit.framework.Assert.failures or and the config for duplicate port specifications.");
127 	        }
128 	        throw re;
129 	    }
130 
131 	    ClassLoader webappClassLoader = jettyServer.getContext().getClassLoader();
132 	    if (addWebappResourceLoaders) {
133 	        ResourceLoader rl = GlobalResourceLoader.getResourceLoader(webappClassLoader);
134             if (rl == null) {
135                 throw new RuntimeException("Could not find resource loader for workflow test harness web app for: " + webappClassLoader);
136             }
137             
138             GlobalResourceLoader.addResourceLoader(rl);
139 	    }
140 	    
141         org.kuali.rice.core.api.config.property.Config webappConfig = ConfigContext.getConfig(webappClassLoader);
142 	    WEBAPP_CONFIGS.put(new Integer(jettyServer.getPort()), webappConfig);
143 	    if (ConfigMode.OVERRIDE == configMode) {
144             
145 
146 
147             ConfigContext.overrideConfig(Thread.currentThread().getContextClassLoader(), webappConfig);
148         } else if (ConfigMode.MERGE == configMode) {
149             Config curCtxConfig = ConfigContext.getCurrentContextConfig();
150             if (webappConfig != null) {
151                 curCtxConfig.putProperties(webappConfig.getProperties());
152                 curCtxConfig.putObjects(webappConfig.getObjects());
153             }
154         }
155 
156 		started = true;
157 	}
158 
159 	public void stop() throws Exception {
160 	    LOG.info("Shutting down jetty: " + jettyServer);
161 	    try {
162 	    	if (jettyServer != null && jettyServer.isStarted()) {
163 	    		jettyServer.stop();
164 	    		WEBAPP_CONFIGS.remove(jettyServer.getPort());
165 	    	}
166 	    } catch (Exception e) {
167 	        LOG.error("Error shutting down Jetty " + jettyServer.getContextName(), e);
168 	    }
169 		started = false;
170 	}
171 }