Skip to content

Commit

Permalink
fix: servlet context listener should handle all possible Vaadin contexts
Browse files Browse the repository at this point in the history
Fixes #5
  • Loading branch information
Denis Anisimov committed Nov 30, 2020
1 parent 36652f7 commit fcaac7e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@
*/
@Component(immediate = true, service = { VaadinServiceInitListener.class,
HttpSessionListener.class,
ServletContextListener.class }, scope = ServiceScope.SINGLETON, property = HttpWhiteboardConstants.HTTP_WHITEBOARD_LISTENER
+ "=true")
ServletContextListener.class }, scope = ServiceScope.SINGLETON, property = {
HttpWhiteboardConstants.HTTP_WHITEBOARD_LISTENER + "=true",
HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT + "=(&("
+ HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME
+ "=*) (!("
+ HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME
+ "=vaadinResourcesContext.*)))" })
public class OSGiVaadinInitialization implements VaadinServiceInitListener,
HttpSessionListener, ServletContextListener {

Expand Down Expand Up @@ -187,11 +192,18 @@ public void contextInitialized(ServletContextEvent event) {
VaadinServletContext context = new VaadinServletContext(servletContext);

// ensure the lookup is set into the context
context.getAttribute(Lookup.class, () -> new OsgiLookupImpl());
Lookup lookup = context.getAttribute(Lookup.class,
() -> new OsgiLookupImpl());

Collection<Servlet> servlets = lookupAll(Servlet.class);
for (Servlet servlet : servlets) {
if (isUninitializedServlet(servlet)) {
ServletContext ctx = servlet.getServletConfig()
.getServletContext();
if (new VaadinServletContext(ctx)
.getAttribute(Lookup.class) != lookup) {
continue;
}
try {
servlet.init(servlet.getServletConfig());
} catch (ServletException e) {
Expand Down Expand Up @@ -250,7 +262,8 @@ private Optional<Bundle> getBundle(String symbolicName) {
}

private String generateUniqueContextName(String contextPath) {
String name = "vaadinContext." + sanitizeContextName(contextPath);
String name = "vaadinResourcesContext."
+ sanitizeContextName(contextPath);
Set<String> contextNames = getAvailableContextNames();
if (contextNames.contains(name)) {
int i = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.vaadin.flow.internal.ReflectTools;
import com.vaadin.flow.internal.UsageStatistics;
import com.vaadin.flow.router.HasErrorParameter;
import com.vaadin.flow.server.InvalidApplicationConfigurationException;
import com.vaadin.flow.server.startup.ClassLoaderAwareServletContainerInitializer;
import com.vaadin.flow.server.startup.DevModeInitializer;
import com.vaadin.flow.server.startup.LookupInitializer;
Expand Down Expand Up @@ -105,7 +106,22 @@ public boolean hasInitializers() {
public void addScannedClasses(
Map<Long, Collection<Class<?>>> extenderClasses) {
cachedClasses.putAll(extenderClasses);
contexts.forEach(this::resetContextInitializers);
List<InvalidApplicationConfigurationException> thrown = new ArrayList<>();
for (ServletContext context : contexts) {
try {
resetContextInitializers(context);
} catch (InvalidApplicationConfigurationException exception) {
// don't stop context initializers processing for different
// context, instead initialize all the contexts and then throw
thrown.add(exception);
}
}
// Now if there was an exception throw it.
// This is a workaround for #9417 which causes an exception being thrown
// by VaadinAppShellInitializer in our ITs
if (!thrown.isEmpty()) {
throw thrown.get(0);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2000-2020 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.osgi;

import com.vaadin.flow.uitest.ui.BaseHrefIT;

public class CustomContextBaseHrefIT extends BaseHrefIT {

@Override
protected String getTestPath() {
return "/custom-test-context" + super.getTestPath();
}
}

0 comments on commit fcaac7e

Please sign in to comment.