Skip to content

Commit

Permalink
refactoring: App classpath resources should be access via
Browse files Browse the repository at this point in the history
ResourceProvider (#9278)

Fixes #9269
  • Loading branch information
Denis committed Mar 16, 2021
1 parent 7185b0b commit 6976fbd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -31,6 +32,8 @@
import org.slf4j.LoggerFactory;

import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.di.Lookup;
import com.vaadin.flow.di.ResourceProvider;
import com.vaadin.flow.function.DeploymentConfiguration;
import com.vaadin.flow.internal.AnnotationReader;
import com.vaadin.flow.internal.Pair;
Expand Down Expand Up @@ -106,7 +109,7 @@ public TemplateData getTemplateContent(
}

String url = dependency.getUrl();
String source = getSourcesFromTemplate(tag, url);
String source = getSourcesFromTemplate(service, tag, url);
if (source == null) {
try {
source = getSourcesFromStats(service, url);
Expand Down Expand Up @@ -171,6 +174,8 @@ private boolean dependencyHasTagName(Dependency dependency, String tag) {
/**
* Finds the JavaScript sources for given tag.
*
* @param service
* the related Vaadin service
* @param tag
* the value of the {@link com.vaadin.flow.component.Tag}
* annotation, e.g. `my-component`
Expand All @@ -182,9 +187,20 @@ private boolean dependencyHasTagName(Dependency dependency, String tag) {
* @return the .js source which declares given custom element, or null if no
* such source can be found.
*/
protected String getSourcesFromTemplate(String tag, String url) {
InputStream content = getClass().getClassLoader()
.getResourceAsStream(url);
protected String getSourcesFromTemplate(VaadinService service, String tag,
String url) {
Lookup lookup = service.getContext().getAttribute(Lookup.class);
ResourceProvider resourceProvider = lookup
.lookup(ResourceProvider.class);
InputStream content = null;
try {
URL appResource = resourceProvider.getApplicationResource(service,
url);
content = appResource == null ? null : appResource.openStream();
} catch (IOException exception) {
getLogger().warn("Coudln't get resource for the template '{}'", url,
exception);
}
if (content != null) {
getLogger().debug(
"Found sources from the tag '{}' in the template '{}'", tag,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.HasElement;
import com.vaadin.flow.component.UI;
Expand Down Expand Up @@ -58,10 +63,6 @@
import com.vaadin.flow.shared.ui.LoadMode;
import com.vaadin.flow.theme.AbstractTheme;
import com.vaadin.flow.theme.ThemeDefinition;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;

import elemental.json.Json;
import elemental.json.JsonObject;
Expand Down Expand Up @@ -461,26 +462,26 @@ public static Optional<Class<?>> resolvePageConfigurationHolder(UI ui,
// found" target
return resolveRouteNotFoundNavigationTarget(
ui.getSession().getService().getContext())
.map(errorNavigationTarget -> {
/*
* {@code resolveTopParentLayout} is theoretically the
* correct way to get the parent layout. But in fact it does
* work for non route targets.
*/
List<Class<? extends RouterLayout>> layouts = RouteUtil
.getParentLayoutsForNonRouteTarget(
errorNavigationTarget);
if (layouts.isEmpty()) {
return errorNavigationTarget;
} else {
return layouts.get(layouts.size() - 1);
}
});
.map(errorNavigationTarget -> {
/*
* {@code resolveTopParentLayout} is theoretically
* the correct way to get the parent layout. But in
* fact it does work for non route targets.
*/
List<Class<? extends RouterLayout>> layouts = RouteUtil
.getParentLayoutsForNonRouteTarget(
errorNavigationTarget);
if (layouts.isEmpty()) {
return errorNavigationTarget;
} else {
return layouts.get(layouts.size() - 1);
}
});
}

/*
* NOTE: this code doesn't belong in this class, but is just
* c/p from Router to avoid adding new API to 2.1.
* NOTE: this code doesn't belong in this class, but is just c/p from Router
* to avoid adding new API to 2.1.
*/
private static Optional<Class<? extends Component>> resolveRouteNotFoundNavigationTarget(
VaadinContext context) {
Expand Down

0 comments on commit 6976fbd

Please sign in to comment.