Skip to content

Commit

Permalink
test: fix test class and register view servlet with a context in OSGi
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Anisimov committed Nov 30, 2020
1 parent a2da02b commit 48c9003
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public class ViewTestServlet extends VaadinServlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
viewLocator = new ViewClassLocator(getService().getClassLoader());
if (getService() != null) {
viewLocator = new ViewClassLocator(getService().getClassLoader());
}
}

static ViewClassLocator getViewLocator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import java.util.Dictionary;
import java.util.Hashtable;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.http.NamespaceException;
import org.osgi.service.http.context.ServletContextHelper;
import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;

import com.vaadin.flow.server.VaadinServletConfiguration;
Expand All @@ -41,13 +46,41 @@ public static class OsgiResourceRgistration {

}

private static final class CustomContextHelper
extends ServletContextHelper {

private CustomContextHelper(Bundle bundle) {
super(bundle);
}
}

private static class CustomContextHelperFactory
implements ServiceFactory<ServletContextHelper> {

@Override
public ServletContextHelper getService(Bundle bundle,
ServiceRegistration<ServletContextHelper> registration) {
return new CustomContextHelper(bundle);
}

@Override
public void ungetService(Bundle bundle,
ServiceRegistration<ServletContextHelper> registration,
ServletContextHelper service) {
// no op
}

}

@VaadinServletConfiguration(productionMode = false)
private static class FixedViewServlet extends ViewTestServlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);

getService().setClassLoader(getClass().getClassLoader());
if (getService() != null) {
getService().setClassLoader(getClass().getClassLoader());
}
}
}

Expand All @@ -57,7 +90,9 @@ private static class FixedRouterServlet extends RouterTestServlet {
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);

getService().setClassLoader(getClass().getClassLoader());
if (getService() != null) {
getService().setClassLoader(getClass().getClassLoader());
}
}
}

Expand All @@ -69,7 +104,9 @@ private static class FixedProductionModeViewServlet
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);

getService().setClassLoader(getClass().getClassLoader());
if (getService() != null) {
getService().setClassLoader(getClass().getClassLoader());
}
}
}

Expand All @@ -80,7 +117,9 @@ private static class FixedProductionModeTimingDataViewServlet
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);

getService().setClassLoader(getClass().getClassLoader());
if (getService() != null) {
getService().setClassLoader(getClass().getClassLoader());
}
}
}

Expand All @@ -106,6 +145,13 @@ void activate() throws NamespaceException {
new FixedProductionModeTimingDataViewServlet(),
createProperties("/view-production-timing/*"));

registerPlainJsResource(context);

String contextName = registerCustomContext(context);
registerCustomContextServlet(context, contextName);
}

private void registerPlainJsResource(BundleContext context) {
Hashtable<String, Object> properties = new Hashtable<>();
properties.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_RESOURCE_PATTERN,
"/plain-script.js");
Expand All @@ -115,6 +161,28 @@ void activate() throws NamespaceException {
new OsgiResourceRgistration(), properties);
}

private String registerCustomContext(BundleContext context) {
Dictionary<String, String> contextProps = new Hashtable<String, String>();
String contextName = "test-context";
contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME,
contextName);
contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH,
"/custom-test-context");
context.registerService(ServletContextHelper.class,
new CustomContextHelperFactory(), contextProps);
return contextName;
}

private void registerCustomContextServlet(BundleContext context,
String contextName) {
Hashtable<String, Object> properties = createProperties("/view/*");
properties.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT,
"(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "="
+ contextName + ")");
context.registerService(Servlet.class, new FixedViewServlet(),
properties);
}

private Hashtable<String, Object> createProperties(String mapping) {
Hashtable<String, Object> properties = new Hashtable<>();
properties.put(
Expand Down

0 comments on commit 48c9003

Please sign in to comment.