diff options
author | Anjum Fatima | 2019-10-10 21:28:51 +0000 |
---|---|---|
committer | Anjum Fatima | 2019-10-11 16:12:52 +0000 |
commit | c7ed63f62e43b59ac17849fe85fefbccbc984e05 (patch) | |
tree | 02b5c0d71f12edaf70c568fbbcb66b4526909fc9 | |
parent | b92ebfa684857d988054b3b894a03742a60c896d (diff) | |
download | rt.equinox.bundles-c7ed63f62e43b59ac17849fe85fefbccbc984e05.tar.gz rt.equinox.bundles-c7ed63f62e43b59ac17849fe85fefbccbc984e05.tar.xz rt.equinox.bundles-c7ed63f62e43b59ac17849fe85fefbccbc984e05.zip |
Bug 551743 - [http whiteboard] NullPointerException whenI20191014-1800
org.eclipse.equinox.http.servlet.internal.Activator.context is nulled
out on shutdown
Change-Id: I389b9f5248c003ccc2b104f437769deeba7bf76a
Signed-off-by: Anjum Fatima <anjum.eclipse@gmail.com>
-rw-r--r-- | bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/Activator.java | 215 |
1 files changed, 121 insertions, 94 deletions
diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/Activator.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/Activator.java index b96d57927..70160487e 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/Activator.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/Activator.java @@ -52,16 +52,20 @@ public class Activator public static void addProxyServlet(ProxyServlet proxyServlet) { Object previousRegistration = registrations.putIfAbsent( proxyServlet, proxyServlet); + BundleContext currentContext = context; + try { + if (!(previousRegistration instanceof ServiceRegistration) && + (currentContext != null)) { + ServiceRegistration<HttpServlet> serviceRegistration = + currentContext.registerService( + HttpServlet.class, proxyServlet, + new Hashtable<String, Object>()); - if (!(previousRegistration instanceof ServiceRegistration) && - (context != null)) { - - ServiceRegistration<HttpServlet> serviceRegistration = - context.registerService( - HttpServlet.class, proxyServlet, - new Hashtable<String, Object>()); - - registrations.put(proxyServlet, serviceRegistration); + registrations.put(proxyServlet, serviceRegistration); + } + } catch (IllegalStateException ex) { + //If the currentContext is no longer valid. + return; } } @@ -82,7 +86,7 @@ public class Activator processRegistrations(); serviceTracker = new ServiceTracker<HttpServlet, HttpTuple>( - context, HttpServlet.class, this); + bundleContext, HttpServlet.class, this); serviceTracker.open(); } @@ -95,100 +99,109 @@ public class Activator public HttpTuple addingService( ServiceReference<HttpServlet> serviceReference) { - - HttpServlet httpServlet = context.getService(serviceReference); - - if (!(httpServlet instanceof ProxyServlet)) { - context.ungetService(serviceReference); + BundleContext currentContext = context; + if (currentContext == null) { return null; } - ProxyServlet proxyServlet = (ProxyServlet)httpServlet; - - ServletConfig servletConfig = proxyServlet.getServletConfig(); - ServletContext servletContext = servletConfig.getServletContext(); - - Dictionary<String, Object> serviceProperties = - new Hashtable<String, Object>(3); - - Enumeration<String> initparameterNames = - servletConfig.getInitParameterNames(); - - while (initparameterNames.hasMoreElements()) { - String name = initparameterNames.nextElement(); + try { + HttpServlet httpServlet = currentContext.getService(serviceReference); - serviceProperties.put( - name, servletConfig.getInitParameter(name)); - } + if (!(httpServlet instanceof ProxyServlet)) { + currentContext.ungetService(serviceReference); + return null; + } - if (serviceProperties.get(Constants.SERVICE_VENDOR) == null) { - serviceProperties.put( - Constants.SERVICE_VENDOR, DEFAULT_SERVICE_VENDOR); - } + ProxyServlet proxyServlet = (ProxyServlet)httpServlet; - if (serviceProperties.get(Constants.SERVICE_DESCRIPTION) == null) { - serviceProperties.put( - Constants.SERVICE_DESCRIPTION, DEFAULT_SERVICE_DESCRIPTION); - } + ServletConfig servletConfig = proxyServlet.getServletConfig(); + ServletContext servletContext = servletConfig.getServletContext(); - Object httpServiceEndpointObj = serviceProperties.get(HttpServiceRuntimeConstants.HTTP_SERVICE_ENDPOINT); + Dictionary<String, Object> serviceProperties = + new Hashtable<String, Object>(3); - if (httpServiceEndpointObj == null) { - String[] httpServiceEndpoints = getHttpServiceEndpoints( - serviceProperties, servletContext, servletConfig.getServletName()); + Enumeration<String> initparameterNames = + servletConfig.getInitParameterNames(); - serviceProperties.put( - HttpServiceRuntimeConstants.HTTP_SERVICE_ENDPOINT, - httpServiceEndpoints); - } - else { - List<String> httpServiceEndpoints = new ArrayList<String>(); + while (initparameterNames.hasMoreElements()) { + String name = initparameterNames.nextElement(); - String contextPath = servletContext.getContextPath(); + serviceProperties.put( + name, servletConfig.getInitParameter(name)); + } - for (String httpServiceEndpoint : StringPlus.from(httpServiceEndpointObj)) { - if (!httpServiceEndpoint.startsWith(Const.HTTP.concat(":")) && !httpServiceEndpoint.startsWith(contextPath)) { //$NON-NLS-1$ - httpServiceEndpoint = contextPath + httpServiceEndpoint; - } + if (serviceProperties.get(Constants.SERVICE_VENDOR) == null) { + serviceProperties.put( + Constants.SERVICE_VENDOR, DEFAULT_SERVICE_VENDOR); + } - httpServiceEndpoints.add(httpServiceEndpoint); + if (serviceProperties.get(Constants.SERVICE_DESCRIPTION) == null) { + serviceProperties.put( + Constants.SERVICE_DESCRIPTION, DEFAULT_SERVICE_DESCRIPTION); } - serviceProperties.put( - HttpServiceRuntimeConstants.HTTP_SERVICE_ENDPOINT, - httpServiceEndpoints); - } + Object httpServiceEndpointObj = serviceProperties.get(HttpServiceRuntimeConstants.HTTP_SERVICE_ENDPOINT); - // need a unique id for our service to match old HttpService HttpContext - serviceProperties.put(UNIQUE_SERVICE_ID, new Random().nextLong()); - // white board support - // determine if the system bundle context should be used: - boolean useSystemContext = Boolean.valueOf(context.getProperty(PROP_GLOBAL_WHITEBOARD)); - BundleContext trackingContext = useSystemContext ? context.getBundle(Constants.SYSTEM_BUNDLE_LOCATION).getBundleContext() : context; - HttpServiceRuntimeImpl httpServiceRuntime = new HttpServiceRuntimeImpl( - trackingContext, context, servletContext, serviceProperties); - httpServiceRuntime.open(); + if (httpServiceEndpointObj == null) { + String[] httpServiceEndpoints = getHttpServiceEndpoints( + serviceProperties, servletContext, servletConfig.getServletName()); - proxyServlet.setHttpServiceRuntimeImpl(httpServiceRuntime); + serviceProperties.put( + HttpServiceRuntimeConstants.HTTP_SERVICE_ENDPOINT, + httpServiceEndpoints); + } + else { + List<String> httpServiceEndpoints = new ArrayList<String>(); - // imperative API support; - // the http service must be registered first so we can get its service id - HttpServiceFactory httpServiceFactory = new HttpServiceFactory(httpServiceRuntime); - ServiceRegistration<?> hsfRegistration = context.registerService( - HTTP_SERVICES_CLASSES, httpServiceFactory, serviceProperties); + String contextPath = servletContext.getContextPath(); - serviceProperties.put(HttpServiceRuntimeConstants.HTTP_SERVICE_ID, Collections.singletonList(hsfRegistration.getReference().getProperty(Constants.SERVICE_ID))); + for (String httpServiceEndpoint : StringPlus.from(httpServiceEndpointObj)) { + if (!httpServiceEndpoint.startsWith(Const.HTTP.concat(":")) && !httpServiceEndpoint.startsWith(contextPath)) { //$NON-NLS-1$ + httpServiceEndpoint = contextPath + httpServiceEndpoint; + } - ServiceRegistration<HttpServiceRuntime> hsrRegistration = - context.registerService( - HttpServiceRuntime.class, httpServiceRuntime, - serviceProperties); + httpServiceEndpoints.add(httpServiceEndpoint); + } - httpServiceRuntime.setHsrRegistration(hsrRegistration); + serviceProperties.put( + HttpServiceRuntimeConstants.HTTP_SERVICE_ENDPOINT, + httpServiceEndpoints); + } - return new HttpTuple( - proxyServlet, httpServiceFactory, hsfRegistration, - httpServiceRuntime, hsrRegistration); + // need a unique id for our service to match old HttpService HttpContext + serviceProperties.put(UNIQUE_SERVICE_ID, new Random().nextLong()); + // white board support + // determine if the system bundle context should be used: + boolean useSystemContext = Boolean.valueOf(currentContext.getProperty(PROP_GLOBAL_WHITEBOARD)); + BundleContext trackingContext = useSystemContext ? currentContext.getBundle(Constants.SYSTEM_BUNDLE_LOCATION).getBundleContext() : currentContext; + HttpServiceRuntimeImpl httpServiceRuntime = new HttpServiceRuntimeImpl( + trackingContext, currentContext, servletContext, serviceProperties); + httpServiceRuntime.open(); + + proxyServlet.setHttpServiceRuntimeImpl(httpServiceRuntime); + + // imperative API support; + // the http service must be registered first so we can get its service id + HttpServiceFactory httpServiceFactory = new HttpServiceFactory(httpServiceRuntime); + ServiceRegistration<?> hsfRegistration = currentContext.registerService( + HTTP_SERVICES_CLASSES, httpServiceFactory, serviceProperties); + + serviceProperties.put(HttpServiceRuntimeConstants.HTTP_SERVICE_ID, Collections.singletonList(hsfRegistration.getReference().getProperty(Constants.SERVICE_ID))); + + ServiceRegistration<HttpServiceRuntime> hsrRegistration = + currentContext.registerService( + HttpServiceRuntime.class, httpServiceRuntime, + serviceProperties); + + httpServiceRuntime.setHsrRegistration(hsrRegistration); + + return new HttpTuple( + proxyServlet, httpServiceFactory, hsfRegistration, + httpServiceRuntime, hsrRegistration); + } catch (IllegalStateException ex) { + //If the currentContext is no longer valid. + return null; + } } public void modifiedService( @@ -200,10 +213,16 @@ public class Activator public void removedService( ServiceReference<HttpServlet> serviceReference, HttpTuple httpTuple) { - - context.ungetService(serviceReference); - - httpTuple.destroy(); + BundleContext currentContext = context; + if (currentContext != null) { + try { + currentContext.ungetService(serviceReference); + httpTuple.destroy(); + } catch (IllegalStateException ex) { + //If the currentContext is no longer valid. + return; + } + } } private String[] getHttpServiceEndpoints( @@ -304,20 +323,28 @@ public class Activator private void processRegistrations() { Iterator<Entry<ProxyServlet, Object>> iterator = registrations.entrySet().iterator(); + BundleContext currentContext = context; + if (currentContext == null) { + return; + } while (iterator.hasNext()) { Entry<ProxyServlet, Object> entry = iterator.next(); - ProxyServlet proxyServlet = entry.getKey(); Object value = entry.getValue(); - if (!(value instanceof ServiceRegistration)) { - ServiceRegistration<HttpServlet> serviceRegistration = - context.registerService( - HttpServlet.class, proxyServlet, - new Hashtable<String, Object>()); + try { + if (!(value instanceof ServiceRegistration)) { + ServiceRegistration<HttpServlet> serviceRegistration = + currentContext.registerService( + HttpServlet.class, proxyServlet, + new Hashtable<String, Object>()); - entry.setValue(serviceRegistration); + entry.setValue(serviceRegistration); + } + } catch (IllegalStateException ex) { + //If the currentContext is no longer valid. + return; } } } |