Skip to content

Commit

Permalink
Initialize static routes properly within OSGi container (vaadin#4902)
Browse files Browse the repository at this point in the history
Properly initialize route data in OSGi
  • Loading branch information
Denis committed Dec 27, 2018
1 parent 7336025 commit 7913878
Show file tree
Hide file tree
Showing 11 changed files with 960 additions and 468 deletions.
4 changes: 4 additions & 0 deletions flow-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
<version>${osgi.core.version}</version>
<scope>provided</scope>
</dependency>

</dependencies>

<build>
Expand Down Expand Up @@ -197,6 +198,9 @@
<configuration>
<excludedGroups>${test.excludegroup}</excludedGroups>
<enableAssertions>true</enableAssertions>
<classpathDependencyExcludes>
<classpathDependencyExclude>org.osgi:org.osgi.core</classpathDependencyExclude>
</classpathDependencyExcludes>
</configuration>
</plugin>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public interface Configuration extends Serializable {
* Configure the given RouteConfiguration.
*
* @param configuration
* mutable routeConfiguration to make changes to
* mutable routeConfiguration to make changes to
*/
void configure(RouteConfiguration configuration);
}
Expand All @@ -70,9 +70,8 @@ public interface Configuration extends Serializable {
private final ReentrantLock configurationLock = new ReentrantLock(true);

/**
* The live configuration for this route registry.
* This can only be updated through {@link #configure(Configuration)} for
* concurrency reasons.
* The live configuration for this route registry. This can only be updated
* through {@link #configure(Configuration)} for concurrency reasons.
*/
private volatile RouteConfiguration routeConfiguration = new RouteConfiguration();
private volatile RouteConfiguration editing = null;
Expand All @@ -83,7 +82,7 @@ public interface Configuration extends Serializable {
* Thread-safe update of the RouteConfiguration.
*
* @param command
* command that will mutate the configuration copy.
* command that will mutate the configuration copy.
*/
protected void configure(Configuration command) {
lock();
Expand Down Expand Up @@ -189,8 +188,8 @@ private List<RouteData> getRegisteredRoutes(
List<RouteAliasData> routeAliases = new ArrayList<>();

configuration.getRoutePaths(target).stream()
.filter(route -> !route.equals(url)).forEach(
route -> routeAliases.add(new RouteAliasData(
.filter(route -> !route.equals(url))
.forEach(route -> routeAliases.add(new RouteAliasData(
getParentLayouts(configuration, target, route),
route, parameters, target)));
List<Class<? extends RouterLayout>> parentLayouts = getParentLayouts(
Expand All @@ -211,7 +210,7 @@ private List<RouteData> getRegisteredRoutes(
* even if aliases change.
*
* @param routeData
* route data to flatten.
* route data to flatten.
* @return flattened list of routes and aliases
*/
private List<RouteBaseData<?>> flattenRoutes(List<RouteData> routeData) {
Expand Down Expand Up @@ -274,7 +273,7 @@ public Optional<String> getTargetUrl(
* Append any required parameters as /{param_class} to the route.
*
* @param navigationTarget
* navigation target to generate url for
* navigation target to generate url for
* @return route with required parameters
*/
private String collectRequiredParameters(
Expand All @@ -288,9 +287,8 @@ private String collectRequiredParameters(
List<Class<?>> routeParameters = getRouteParameters(navigationTarget);

if (!routeParameters.isEmpty()) {
routeParameters.forEach(
param -> route.append("/{").append(param.getSimpleName())
.append("}"));
routeParameters.forEach(param -> route.append("/{")
.append(param.getSimpleName()).append("}"));
}
return route.toString();
}
Expand Down Expand Up @@ -328,8 +326,13 @@ public void removeRoute(String path,
if (!getConfiguration().hasRoute(path)) {
return;
}
configure(configuration -> configuration
.removeRoute(path, navigationTarget));
configure(configuration -> configuration.removeRoute(path,
navigationTarget));
}

@Override
public void clean() {
configure(RouteConfiguration::clear);
}

/**
Expand All @@ -338,11 +341,11 @@ public void removeRoute(String path,
* Note! this should only be called from a configure() for thread safety.
*
* @param path
* path for the navigation target
* path for the navigation target
* @param navigationTarget
* navigation target for given path
* navigation target for given path
* @param configuration
* mutable configuration object
* mutable configuration object
* @return the route target to which the target was added
*/
private RouteTarget addRouteToConfiguration(String path,
Expand All @@ -368,12 +371,12 @@ private RouteTarget addRouteToConfiguration(String path,
* related.
*
* @param target
* error handler target
* error handler target
* @param exceptionTargetsMap
* map of existing error handlers
* map of existing error handlers
* @throws InvalidRouteConfigurationException
* if trying to add a non related exception handler for which a
* handler already exists
* if trying to add a non related exception handler for which a
* handler already exists
*/
protected void addErrorTarget(Class<? extends Component> target,
Map<Class<? extends Exception>, Class<? extends Component>> exceptionTargetsMap) {
Expand All @@ -398,9 +401,9 @@ protected void addErrorTarget(Class<? extends Component> target,
* allowed.
*
* @param target
* target being handled
* target being handled
* @param exceptionType
* type of the handled exception
* type of the handled exception
*/
private void handleRegisteredExceptionType(
Map<Class<? extends Exception>, Class<? extends Component>> exceptionTargetsMap,
Expand All @@ -412,10 +415,10 @@ private void handleRegisteredExceptionType(
if (registered.isAssignableFrom(target)) {
exceptionTargetsMap.put(exceptionType, target);
} else if (!target.isAssignableFrom(registered)) {
String msg = String
.format("Only one target for an exception should be defined. Found '%s' and '%s' for exception '%s'",
target.getName(), registered.getName(),
exceptionType.getName());
String msg = String.format(
"Only one target for an exception should be defined. Found '%s' and '%s' for exception '%s'",
target.getName(), registered.getName(),
exceptionType.getName());
throw new InvalidRouteLayoutConfigurationException(msg);
}
}
Expand All @@ -425,16 +428,16 @@ private void handleRegisteredExceptionType(
* cause until possible exception with handler found.
*
* @param exception
* exception to get handler for
* exception to get handler for
* @return Optional containing found handler or empty if none found
*/
protected Optional<ErrorTargetEntry> searchByCause(Exception exception) {
Class<? extends Component> targetClass = routeConfiguration
.getExceptionHandlerByClass(exception.getClass());

if (targetClass != null) {
return Optional.of(new ErrorTargetEntry(targetClass,
exception.getClass()));
return Optional.of(
new ErrorTargetEntry(targetClass, exception.getClass()));
}

Throwable cause = exception.getCause();
Expand All @@ -449,14 +452,14 @@ protected Optional<ErrorTargetEntry> searchByCause(Exception exception) {
* exist.
*
* @param exception
* exception to get handler for
* exception to get handler for
* @return Optional containing found handler or empty if none found
*/
protected Optional<ErrorTargetEntry> searchBySuperType(
Throwable exception) {
Class<?> superClass = exception.getClass().getSuperclass();
while (superClass != null && Exception.class
.isAssignableFrom(superClass)) {
while (superClass != null
&& Exception.class.isAssignableFrom(superClass)) {
Class<? extends Component> targetClass = routeConfiguration
.getExceptionHandlerByClass(superClass);
if (targetClass != null) {
Expand Down
Loading

0 comments on commit 7913878

Please sign in to comment.