Skip to content

Commit

Permalink
[PAXWEB-824] - ServletContext doesn't always get registered as a service
Browse files Browse the repository at this point in the history
partially applied patch provided by Tuomas Kiviaho
  • Loading branch information
ANierbeck committed Mar 30, 2016
1 parent 9cb2222 commit f92b12e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 31 deletions.
Expand Up @@ -31,13 +31,13 @@
import java.util.EventListener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import javax.servlet.ServletContainerInitializer;
Expand Down Expand Up @@ -66,9 +66,12 @@
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer;
import org.ops4j.pax.swissbox.core.ContextClassLoaderUtils;
import org.ops4j.pax.web.service.WebContainerConstants;
import org.ops4j.pax.web.service.WebContainerContext;
import org.ops4j.pax.web.service.jetty.internal.util.DOMJettyWebXmlParser;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.http.HttpContext;
import org.slf4j.Logger;
Expand Down Expand Up @@ -414,6 +417,32 @@ public String toString() {
public boolean addBean(Object o) {
return super.addBean(o);
}

@Override
protected void startContext() throws Exception {
super.startContext();
LOG.debug("Registering ServletContext as service. ");
BundleContext bundleContext = (BundleContext) this.attributes.get(WebContainerConstants.BUNDLE_CONTEXT_ATTRIBUTE);
Bundle bundle = bundleContext.getBundle();
Dictionary<String, String> properties = new Hashtable<String, String>();
properties.put("osgi.web.symbolicname",
bundle.getSymbolicName());

Dictionary<?, ?> headers = bundle.getHeaders();
String version = (String) headers
.get(Constants.BUNDLE_VERSION);
if (version != null && version.length() > 0) {
properties.put("osgi.web.version", version);
}

// Context servletContext = context.getServletContext();
String webContextPath = getContextPath();

properties.put("osgi.web.contextpath", webContextPath);

registerService(bundleContext, properties);
LOG.debug("ServletContext registered as service. ");
}

public class SContext extends ServletContextHandler.Context {

Expand Down Expand Up @@ -581,5 +610,4 @@ public String getMimeType(final String name) {
}

}

}
Expand Up @@ -339,30 +339,30 @@ private HttpServiceContext addContext(final ContextModel model) {
if (!serverHandler.isStarted() && !serverHandler.isStarting()) {
serverHandler.start();
}
// if the server handler is a handler collection, seems like
// jetty will not automatically
// start inner handlers. So, force the start of the created
// context
if (!context.isStarted() && !context.isStarting()) {
LOG.debug("Registering ServletContext as service. ");
Dictionary<String, String> properties = new Hashtable<String, String>();
properties.put("osgi.web.symbolicname", bundle.getSymbolicName());

Dictionary<?, ?> headers = bundle.getHeaders();
String version = (String) headers.get(Constants.BUNDLE_VERSION);
if (version != null && version.length() > 0) {
properties.put("osgi.web.version", version);
}

// Context servletContext = context.getServletContext();
String webContextPath = context.getContextPath();

properties.put("osgi.web.contextpath", webContextPath);

context.registerService(bundleContext, properties);
LOG.debug("ServletContext registered as service. ");

}
// // if the server handler is a handler collection, seems like
// // jetty will not automatically
// // start inner handlers. So, force the start of the created
// // context
// if (!context.isStarted() && !context.isStarting()) {
// LOG.debug("Registering ServletContext as service. ");
// Dictionary<String, String> properties = new Hashtable<String, String>();
// properties.put("osgi.web.symbolicname", bundle.getSymbolicName());
//
// Dictionary<?, ?> headers = bundle.getHeaders();
// String version = (String) headers.get(Constants.BUNDLE_VERSION);
// if (version != null && version.length() > 0) {
// properties.put("osgi.web.version", version);
// }
//
// // Context servletContext = context.getServletContext();
// String webContextPath = context.getContextPath();
//
// properties.put("osgi.web.contextpath", webContextPath);
//
// context.registerService(bundleContext, properties);
// LOG.debug("ServletContext registered as service. ");
//
// }
// CHECKSTYLE:OFF
} catch (Exception ignore) {
LOG.error("Could not start the servlet context for http context [" + model.getHttpContext() + "]",
Expand Down
Expand Up @@ -50,7 +50,7 @@ public class JettyServerWrapperTest {
private static final String KNOWN_CONTEXT_NAME = "TestContext";
private static final String BUNDLE_SYMBOLIC_NAME = "BundleSymbolicName";
private static final int NUMBER_OF_CONCURRENT_EXECUTIONS = 2;
private static final int REPETITIONS_OF_MULTI_THREADED_TEST = 10000;
private static final int REPETITIONS_OF_MULTI_THREADED_TEST = 1000;
@Mock
private ServerModel serverModelMock;
@Mock
Expand All @@ -72,6 +72,7 @@ public void mockIt() {
new Hashtable<String, String>());
when(bundleMock.getSymbolicName()).thenReturn(BUNDLE_SYMBOLIC_NAME);
when(bundleMock.getBundleContext()).thenReturn(bundleContextMock);
when(bundleContextMock.getBundle()).thenReturn(bundleMock);
}

@SuppressWarnings("unchecked")
Expand All @@ -82,8 +83,10 @@ public void getOrCreateContextDoesNotRegisterMultipleServletContextsForSameConte
serverModelMock, new QueuedThreadPool());
try {
jettyServerWrapperUnderTest.start();
jettyServerWrapperUnderTest.getOrCreateContext(contextModelMock);
jettyServerWrapperUnderTest.getOrCreateContext(contextModelMock);
HttpServiceContext context = jettyServerWrapperUnderTest.getOrCreateContext(contextModelMock);
context.start();
context = jettyServerWrapperUnderTest.getOrCreateContext(contextModelMock);
context.start();

verify(bundleContextMock, times(1)).registerService(
same(ServletContext.class), any(ServletContext.class),
Expand All @@ -108,8 +111,9 @@ public void run() {
//CHECKSTYLE:OFF
try {
countDownLatch.await();
jettyServerWrapperUnderTest
HttpServiceContext context = jettyServerWrapperUnderTest
.getOrCreateContext(contextModelMock);
context.start();
} catch (final InterruptedException ex) {
// ignore
} catch (final Exception ex) {
Expand All @@ -128,8 +132,11 @@ public void run() {
final boolean terminated = executor.awaitTermination(10,
TimeUnit.SECONDS);
if (exceptionInRunnable != null) {
try {
throw exceptionInRunnable;
} finally {
exceptionInRunnable = null;
throw exceptionInRunnable;
}
}
assertTrue("could not shutdown the executor within the timeout",
terminated);
Expand All @@ -150,6 +157,7 @@ public void executeMultiThreadedTestMultipleTimes() throws Throwable {
for (; i < REPETITIONS_OF_MULTI_THREADED_TEST; i++) {
getOrCreateContextDoesNotRegisterMultipleServletContextsForSameContextModelMultiThreaded();
reset(bundleContextMock);
when(bundleContextMock.getBundle()).thenReturn(bundleMock);
}
} catch (final Throwable ex) {
System.out.println("Broken in Run #" + i);
Expand Down
Expand Up @@ -141,6 +141,8 @@ protected void scanJspConfig() throws IOException, SAXException {

Collection<TaglibDescriptor> descriptors = jspConfigDescriptor.getTaglibs();
for (TaglibDescriptor descriptor : descriptors) {
if (descriptor == null)
continue;
String taglibURI = descriptor.getTaglibURI();
String resourcePath = descriptor.getTaglibLocation();
// Note: Whilst the Servlet 2.4 DTD implies that the location must
Expand Down

0 comments on commit f92b12e

Please sign in to comment.