CCM NG: StaticThemeProvider now uses the Reflections library to list the themes embedded into the ear/war/jar. Looks like that is the only reliable way the list resources in an archive...

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@5096 8810af33-2d31-482b-a856-94f89814c4df
jensp 2017-11-01 18:04:05 +00:00
parent 7099453a09
commit 47ee7c1817
3 changed files with 127 additions and 100 deletions

View File

@ -27,6 +27,7 @@ import org.reflections.Reflections;
import org.reflections.scanners.ResourcesScanner; import org.reflections.scanners.ResourcesScanner;
import org.reflections.util.ClasspathHelper; import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder; import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -35,6 +36,7 @@ import java.io.OutputStream;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -60,10 +62,13 @@ public class StaticThemeProvider implements ThemeProvider {
StaticThemeProvider.class); StaticThemeProvider.class);
private static final String THEMES_DIR = "/themes"; private static final String THEMES_DIR = "/themes";
private static final String THEME_MANIFEST_JSON = THEMES_DIR private static final String THEMES_PACKAGE = "themes";
+ "/%s/theme.json"; private static final String THEME_MANIFEST_JSON_PATH = THEMES_DIR
private static final String THEME_MANIFEST_XML = THEMES_DIR + "/%s/theme.json";
+ "/%s/theme.xml"; private static final String THEME_MANIFEST_XML_PATH = THEMES_DIR
+ "/%s/theme.xml";
private static final String THEME_MANIFEST_JSON = "theme.json";
private static final String THEME_MANIFEST_XML = "theme.xml";
@Inject @Inject
private ThemeManifestUtil manifestUtil; private ThemeManifestUtil manifestUtil;
@ -71,116 +76,104 @@ public class StaticThemeProvider implements ThemeProvider {
@Override @Override
public List<ThemeInfo> getThemes() { public List<ThemeInfo> getThemes() {
LOGGER.debug("Retrieving static themes...");
final Reflections reflections = new Reflections( final Reflections reflections = new Reflections(
new ConfigurationBuilder() new ConfigurationBuilder()
.setUrls(ClasspathHelper.forPackage("themes")) .setUrls(ClasspathHelper.forPackage(""))
.setScanners(new ResourcesScanner())); .setScanners(new ResourcesScanner()
final Set<String> resources = reflections // .filterResultsBy(new FilterBuilder()
.getResources(Pattern.compile("themes/.*/theme.json")); // .include(THEMES_PACKAGE)
LOGGER.debug("Found resources:"); // .include(THEMES_PACKAGE + "/([\\w\\d\\s\\.]*)/theme.json")
for (final String resource : resources) { // .include(THEMES_PACKAGE + "/([\\w\\d\\s\\.]*)/theme.xml")
LOGGER.debug("\t{}", resource); // .exclude(THEMES_PACKAGE + "(.*)/(.*)")
} // )
));
final URL themesUrl = StaticThemeProvider.class.getResource(THEMES_DIR); final Set<String> jsonThemes = reflections
.getResources(Pattern.compile(THEME_MANIFEST_JSON));
if (themesUrl == null) { final Set<String> xmlThemes = reflections
throw new UnexpectedErrorException( .getResources(Pattern.compile(THEME_MANIFEST_XML));
"Static themes directory does not" final List<String> themes = new ArrayList<>();
+ "exist in class path. Something is wrong."); themes.addAll(jsonThemes
}
File directory;
try {
directory = new File(themesUrl.toURI());
} catch (URISyntaxException ex) {
throw new UnexpectedErrorException(ex);
} catch (IllegalArgumentException ex) {
directory = null;
}
final List<String> themeDirs = new ArrayList<>();
if (directory != null && directory.exists()) {
final String[] files = directory.list();
for (final String file : files) {
themeDirs.add(file);
}
} else {
final String jarPath = themesUrl
.getFile()
.replaceFirst("[.]jar[!].*", ".jar")
.replaceFirst("file:", "");
final JarFile jarFile;
try {
jarFile = new JarFile(jarPath);
} catch (IOException ex) {
throw new UnexpectedErrorException(ex);
}
final Enumeration<JarEntry> jarEntries = jarFile.entries();
while (jarEntries.hasMoreElements()) {
final JarEntry jarEntry = jarEntries.nextElement();
final String jarEntryName = jarEntry.getName();
if (jarEntryName.startsWith("themes")
&& jarEntryName.length() > "themes/".length()
&& jarEntryName.endsWith("/")) {
themeDirs.add(jarEntryName);
}
}
}
return themeDirs
.stream() .stream()
.filter(dirPath -> isThemeDir(dirPath)) .filter(themePackage -> {
.map(dirPath -> loadThemeManifest(dirPath)) return themePackage
.matches(THEMES_PACKAGE + "/([\\w\\d\\s\\.])*/theme.json");
})
// .map(themePackage -> {
// return themePackage
// .substring((THEMES_PACKAGE + "/").length(),
// ("/" + THEME_MANIFEST_JSON).length() - 1);
// })
.collect(Collectors.toList()));
themes.addAll(xmlThemes
.stream()
.filter(themePackage -> {
return themePackage
.matches(THEMES_PACKAGE + "/([\\w\\d\\s\\.])*/theme.xml");
})
// .map(themePackage -> {
// return themePackage
// .substring((THEMES_PACKAGE + "/").length(),
// ("/" + THEME_MANIFEST_XML).length() - 1);
// })
.collect(Collectors.toList()));
Collections.sort(themes);
LOGGER.debug("Found static themes:");
themes.forEach(theme -> LOGGER.debug("\t{}", theme));
for (final String theme : themes) {
final InputStream inputStream = StaticThemeProvider.class
.getResourceAsStream(String.format("/%s", theme));
final ThemeManifest manifest = manifestUtil
.loadManifest(inputStream,
theme);
LOGGER.debug("Got manifest: {}", Objects.toString(manifest));
}
return themes
.stream()
.map(theme -> loadThemeManifest(theme))
.map(manifest -> generateThemeInfo(manifest)) .map(manifest -> generateThemeInfo(manifest))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
private boolean isThemeDir(final String dirPath) { // private boolean isThemeDir(final String dirPath) {
//
// Objects.requireNonNull(dirPath);
//
// final URL manifestJsonUrl = StaticThemeProvider.class.getResource(
// String.format(THEME_MANIFEST_JSON_PATH, dirPath));
// final URL manifestXmlUrl = StaticThemeProvider.class.getResource(
// String.format(THEME_MANIFEST_XML_PATH, dirPath));
//
// return (manifestJsonUrl != null) || (manifestXmlUrl != null);
// }
private ThemeManifest loadThemeManifest(final String manifestPath) {
Objects.requireNonNull(dirPath); Objects.requireNonNull(manifestPath);
final URL manifestJsonUrl = StaticThemeProvider.class.getResource( final String pathToManifest;
String.format(THEME_MANIFEST_JSON, dirPath)); if (manifestPath.startsWith("/")) {
final URL manifestXmlUrl = StaticThemeProvider.class.getResource( pathToManifest = manifestPath;
String.format(THEME_MANIFEST_XML, dirPath));
return (manifestJsonUrl != null) || (manifestXmlUrl != null);
}
private ThemeManifest loadThemeManifest(final String dirPath) {
Objects.requireNonNull(dirPath);
final URL manifestJsonUrl = StaticThemeProvider.class.getResource(
String.format(THEME_MANIFEST_JSON, dirPath));
final URL manifestXmlUrl = StaticThemeProvider.class.getResource(
String.format(THEME_MANIFEST_XML, dirPath));
final URL manifestUrl;
if (manifestJsonUrl != null) {
manifestUrl = manifestJsonUrl;
} else if (manifestXmlUrl != null) {
manifestUrl = manifestXmlUrl;
} else { } else {
throw new IllegalArgumentException(String pathToManifest = String.format("/%s", manifestPath);
.format("Path \"%s\" does not point to a valid theme manifest.",
dirPath));
} }
final ThemeManifest themeManifest; final ThemeManifest manifest;
try (final InputStream inputStream = manifestUrl.openStream()) { try (final InputStream inputStream = StaticThemeProvider.class
.getResourceAsStream(pathToManifest)) {
themeManifest = manifestUtil.loadManifest(inputStream, manifest = manifestUtil.loadManifest(inputStream, manifestPath);
manifestUrl.toString());
} catch (IOException ex) { } catch (IOException ex) {
throw new UnexpectedErrorException(ex); throw new UnexpectedErrorException(ex);
} }
return themeManifest; return manifest;
} }
private ThemeInfo generateThemeInfo(final ThemeManifest manifest) { private ThemeInfo generateThemeInfo(final ThemeManifest manifest) {
@ -206,12 +199,31 @@ public class StaticThemeProvider implements ThemeProvider {
Objects.requireNonNull(theme); Objects.requireNonNull(theme);
if (isThemeDir(theme)) { final String manifestJsonPath = String.format("/" + THEMES_PACKAGE
return Optional.of(generateThemeInfo(loadThemeManifest(theme))); + "%s/"
} else { + THEME_MANIFEST_JSON,
return Optional.empty(); theme);
final String manifestXmlPath = String.format("/" + THEMES_PACKAGE
+ "%s/"
+ THEME_MANIFEST_XML,
theme);
final URL manifestJsonUrl = StaticThemeProvider.class
.getResource(manifestJsonPath);
final URL manifestXmlUrl = StaticThemeProvider.class
.getResource(manifestXmlPath);
if (manifestJsonUrl != null) {
return Optional
.of(generateThemeInfo(loadThemeManifest(manifestJsonPath)));
} }
if (manifestXmlUrl != null) {
return Optional
.of(generateThemeInfo(loadThemeManifest(manifestXmlPath)));
}
return Optional.empty();
} }
@Override @Override
@ -220,8 +232,21 @@ public class StaticThemeProvider implements ThemeProvider {
Objects.requireNonNull(theme); Objects.requireNonNull(theme);
return isThemeDir(theme); final String manifestJsonPath = String.format("/" + THEMES_PACKAGE
+ "%s/"
+ THEME_MANIFEST_JSON,
theme);
final String manifestXmlPath = String.format("/" + THEMES_PACKAGE
+ "%s/"
+ THEME_MANIFEST_XML,
theme);
final URL manifestJsonUrl = StaticThemeProvider.class
.getResource(manifestJsonPath);
final URL manifestXmlUrl = StaticThemeProvider.class
.getResource(manifestXmlPath);
return manifestJsonUrl != null || manifestXmlUrl != null;
} }
@Override @Override

View File

@ -0,0 +1 @@