-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CCDM: allow page configuration in client mode #7050
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ab84f05
CCDM: allow page configuration in client mode
manolo 754e006
CCDM: fix sonar errors
manolo a3a1824
CCDM: apply reviewer suggestions
manolo fa38b8a
CCDM: show an error when there is no AppShell but offending annotations
manolo bb19ed1
CCDM: use VaadinServletContext for the app-shell registry
manolo 0eb95a1
CCDM: cosmetic changes rewording jdocs, renaming classes and fixing t…
aa56d2c
CCDM: add Meta.Container to initializer types
manolo 1128c4d
CCDM: fix tests
manolo 35a9e8b
CCDM: remove `$Container` fragment when reporting multiple annotations
manolo 6bb8d10
CCDM: sonar warnings
manolo File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
flow-server/src/main/java/com/vaadin/flow/component/page/VaadinAppShell.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2000-2018 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.component.page; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* A marker class to configure the index.hml page when using client-side | ||
* bootstrapping. This class supports the following annotations that affect the | ||
* generated index.html page: | ||
* | ||
* <ul> | ||
* <li>{@link Meta}: appends an HTML {@code <meta>} tag to the bottom of the | ||
* {@code <head>} element</li> | ||
* </ul> | ||
* | ||
* <p> | ||
* There should be at max one class extending {@link VaadinAppShell} in the | ||
* application. | ||
* </p> | ||
* | ||
* <p> | ||
* NOTE: the application shell class is the only valid target for the page | ||
* configuration annotations listed above. The application would fail to start | ||
* if any of these annotations is wrongly placed on a class other than the | ||
* application shell class. | ||
* <p> | ||
* | ||
* <code> | ||
* @Meta(name = "Author", content = "Donald Duck") | ||
* public class AppShell extends VaadinAppShell { | ||
* } | ||
* </code> | ||
* | ||
* @since 3.0 | ||
*/ | ||
public abstract class VaadinAppShell implements Serializable { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
flow-server/src/main/java/com/vaadin/flow/server/startup/VaadinAppShellInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* | ||
* Copyright 2000-2018 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.server.startup; | ||
|
||
import javax.servlet.ServletContainerInitializer; | ||
import javax.servlet.ServletContext; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRegistration; | ||
import javax.servlet.annotation.HandlesTypes; | ||
import javax.servlet.annotation.WebListener; | ||
|
||
import java.io.Serializable; | ||
import java.lang.annotation.Annotation; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.slf4j.LoggerFactory; | ||
|
||
import com.vaadin.flow.component.page.Meta; | ||
import com.vaadin.flow.component.page.VaadinAppShell; | ||
import com.vaadin.flow.function.DeploymentConfiguration; | ||
import com.vaadin.flow.server.InvalidApplicationConfigurationException; | ||
import com.vaadin.flow.server.VaadinServlet; | ||
import com.vaadin.flow.server.VaadinServletContext; | ||
import com.vaadin.flow.server.startup.ServletDeployer.StubServletConfig; | ||
|
||
/** | ||
* Servlet initializer visiting {@link VaadinAppShell} configuration. | ||
* | ||
* @since 3.0 | ||
*/ | ||
@HandlesTypes({ VaadinAppShell.class, Meta.class, Meta.Container.class }) | ||
@WebListener | ||
public class VaadinAppShellInitializer implements ServletContainerInitializer, | ||
Serializable { | ||
|
||
@Override | ||
public void onStartup(Set<Class<?>> classes, ServletContext context) | ||
throws ServletException { | ||
|
||
Collection<? extends ServletRegistration> registrations = context | ||
.getServletRegistrations().values(); | ||
if (registrations.isEmpty()) { | ||
return; | ||
} | ||
|
||
DeploymentConfiguration config = StubServletConfig | ||
.createDeploymentConfiguration(context, | ||
registrations.iterator().next(), VaadinServlet.class); | ||
|
||
init(classes, context, config); | ||
} | ||
|
||
/** | ||
* Initializes the {@link VaadinAppShellRegistry} for the application. | ||
* | ||
* @param classes | ||
* a set of classes that matches the {@link HandlesTypes} set in | ||
* this class. | ||
* @param context | ||
* the servlet context. | ||
* @param config | ||
* the vaadin configuration for the application. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static void init(Set<Class<?>> classes, ServletContext context, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
DeploymentConfiguration config) { | ||
|
||
if (!config.isClientSideMode()) { | ||
return; | ||
} | ||
|
||
VaadinAppShellRegistry registry = VaadinAppShellRegistry | ||
.getInstance(new VaadinServletContext(context)); | ||
registry.reset(); | ||
|
||
if (classes.isEmpty()) { | ||
return; | ||
} | ||
|
||
List<String> offendingAnnotations = new ArrayList<>(); | ||
|
||
classes.stream() | ||
// sort classes by putting VaadinAppShell in first position | ||
.sorted((a, b) -> registry.isShell(a) ? -1 | ||
: registry.isShell(b) ? 1 : 0) | ||
.forEach(clz -> { | ||
if (registry.isShell(clz)) { | ||
registry.setShell( | ||
(Class<? extends VaadinAppShell>) clz); | ||
} else { | ||
String error = registry.validateClass(clz); | ||
if (error != null) { | ||
offendingAnnotations.add(error); | ||
} | ||
} | ||
}); | ||
|
||
if (!offendingAnnotations.isEmpty()) { | ||
if (registry.getShell() == null) { | ||
String message = String.format( | ||
VaadinAppShellRegistry.ERROR_HEADER_NO_SHELL, | ||
String.join("\n ", offendingAnnotations)); | ||
LoggerFactory.getLogger(VaadinAppShellInitializer.class) | ||
.error(message); | ||
} else { | ||
String message = String.format( | ||
VaadinAppShellRegistry.ERROR_HEADER_OFFENDING, | ||
registry.getShell(), | ||
String.join("\n ", offendingAnnotations)); | ||
throw new InvalidApplicationConfigurationException(message); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Return the set of valid annotations in a {@link VaadinAppShell} class. | ||
* This method is thought to be called from external plugins to decouple | ||
* them. | ||
* | ||
* @return | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static List<Class<? extends Annotation>> getValidAnnotations() { | ||
HandlesTypes annotation = VaadinAppShellInitializer.class | ||
.getAnnotation(HandlesTypes.class); | ||
assert annotation != null; | ||
List<Class<? extends Annotation>> ret = new ArrayList<>(); | ||
for (Class<?> clazz : annotation.value()) { | ||
if (clazz.isAnnotation()) { | ||
ret.add((Class<? extends Annotation>) clazz); | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
/** | ||
* Return the {@link VaadinAppShell} class. This method is thought to be | ||
* called from external plugins to decouple them. | ||
* | ||
* @return | ||
*/ | ||
public static List<Class<?>> getValidSupers() { | ||
return Collections.singletonList(VaadinAppShell.class); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Convert the abstract class "VaadinAppShell" into an interface.