-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CCDM: allow page configuration in client mode
- Added a `VaadinAppShell` class for configurin clientSide page - Added a `ServletContinerInitializer` for checking annotations on startup - Implement `Meta` annotations in clientSide mode
- Loading branch information
Showing
10 changed files
with
615 additions
and
5 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
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,41 @@ | ||
/* | ||
* 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 in clientSide mode. | ||
* | ||
* There should be only one class in the application extending this. | ||
* | ||
* It is mandatory to remove all annotations from other class before adding | ||
* an <code>AppShell</code> to the project. | ||
* | ||
* <p> | ||
* | ||
* <code> | ||
* | ||
* @Meta(name = "Author", content = "Donald Duck") | ||
* public class AppShell extends VaadinAppShell { | ||
* } | ||
* </code> | ||
* | ||
* @since 3.0 | ||
*/ | ||
@Meta(name = "charset", content = "UTF-8") | ||
public 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
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.ServletContextEvent; | ||
import javax.servlet.ServletContextListener; | ||
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 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.startup.ServletDeployer.StubServletConfig; | ||
|
||
/** | ||
* Servlet initializer visiting {@link VaadinAppShell} configuration. | ||
* | ||
* @since 3.0 | ||
*/ | ||
@HandlesTypes({ VaadinAppShell.class, Meta.class }) | ||
@WebListener | ||
public class VaadinAppShellInitializer implements ServletContainerInitializer, | ||
Serializable, ServletContextListener { | ||
|
||
@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); | ||
} | ||
|
||
@Override | ||
public void contextInitialized(ServletContextEvent sce) { | ||
} | ||
|
||
@Override | ||
public void contextDestroyed(ServletContextEvent sce) { | ||
} | ||
|
||
/** | ||
* 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, | ||
DeploymentConfiguration config) { | ||
|
||
if (!config.isClientSideMode()) { | ||
return; | ||
} | ||
|
||
VaadinAppShellRegistry registry = VaadinAppShellRegistry | ||
.getInstance(context); | ||
|
||
// Reset the registry | ||
registry.setShell(null, context); | ||
|
||
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, context); | ||
} else { | ||
String error = registry.validateClass(clz); | ||
if (error != null) { | ||
offendingAnnotations.add(error); | ||
} | ||
} | ||
}); | ||
|
||
if (!offendingAnnotations.isEmpty()) { | ||
String message = String.format(VaadinAppShellRegistry.ERROR_HEADER, | ||
registry.getShell().getName(), | ||
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.