📄 packageresource.java
字号:
String absolutePath = scope.getResource("").toExternalForm(); File basedir; URI uri; try { uri = new URI(absolutePath); } catch (URISyntaxException e) { throw new RuntimeException(e); } try { basedir = new File(uri); } catch (IllegalArgumentException e) { log.debug("Can't construct the uri as a file: " + absolutePath); // if this is thrown then the path is not really a // file. but could be a zip. String jarZipPart = uri.getSchemeSpecificPart(); // lowercased for testing if jar/zip, but leave the real // filespec unchanged String lowerJarZipPart = jarZipPart.toLowerCase(); int index = lowerJarZipPart.indexOf(".zip"); if (index == -1) { index = lowerJarZipPart.indexOf(".jar"); } if (index == -1) { throw e; } String filename = jarZipPart.substring(0, index + 4); // 4 = // len of ".jar" or ".zip" log.debug("trying the filename: " + filename + " to load as a zip/jar."); JarFile jarFile = new JarFile(filename, false); scanJarFile(scope, pattern, recurse, resources, packageRef, jarFile); return (PackageResource[])resources.toArray(new PackageResource[resources .size()]); } if (!basedir.isDirectory()) { throw new IllegalStateException("unable to read resources from directory " + basedir); } } } } catch (IOException e) { throw new WicketRuntimeException(e); } return (PackageResource[])resources.toArray(new PackageResource[resources.size()]); } /** * Gets a non-localized resource for a given set of criteria. Only one resource will be loaded * for the same criteria. * * @param scope * This argument will be used to get the class loader for loading the package * resource, and to determine what package it is in. Typically this is the calling * class/ the class in which you call this method * @param path * The path to the resource * @return The resource */ public static PackageResource get(final Class scope, final String path) { return get(scope, path, null, null); } /** * Gets the resource for a given set of criteria. Only one resource will be loaded for the same * criteria. * * @param scope * This argument will be used to get the class loader for loading the package * resource, and to determine what package it is in. Typically this is the class in * which you call this method * @param path * The path to the resource * @param locale * The locale of the resource * @param style * The style of the resource (see {@link org.apache.wicket.Session}) * @return The resource */ public static PackageResource get(final Class scope, final String path, final Locale locale, final String style) { final SharedResources sharedResources = Application.get().getSharedResources(); PackageResource resource = (PackageResource)sharedResources.get(scope, path, locale, style, true); if (resource == null) { resource = new PackageResource(scope, path, locale, style); sharedResources.add(scope, path, locale, style, resource); } return resource; } /* removed in 2.0 */ private static void scanJarFile(Class scope, Pattern pattern, boolean recurse, final List resources, String packageRef, JarFile jf) { Enumeration enumeration = jf.entries(); while (enumeration.hasMoreElements()) { JarEntry je = (JarEntry)enumeration.nextElement(); String name = je.getName(); if (name.startsWith(packageRef)) { name = name.substring(packageRef.length() + 1); if (pattern.matcher(name).matches() && (recurse || (name.indexOf('/') == -1))) { // we add the entry as a package resource resources.add(get(scope, name, null, null)); } } } } /** The path to the resource */ private final String absolutePath; /** The resource's locale */ private Locale locale; /** The path this resource was created with. */ private final String path; /** The scoping class, used for class loading and to determine the package. */ private final String scopeName; /** The resource's style */ private final String style; /** * Hidden constructor. * * @param scope * This argument will be used to get the class loader for loading the package * resource, and to determine what package it is in * @param path * The path to the resource * @param locale * The locale of the resource * @param style * The style of the resource */ protected PackageResource(final Class scope, final String path, final Locale locale, final String style) { // Convert resource path to absolute path relative to base package absolutePath = Packages.absolutePath(scope, path); IPackageResourceGuard guard = Application.get().getResourceSettings() .getPackageResourceGuard(); if (!guard.accept(scope, path)) { throw new PackageResourceBlockedException("package resource " + absolutePath + " may not be accessed"); } scopeName = scope.getName(); this.path = path; this.locale = locale; this.style = style; if (locale != null) { /* * Get the resource stream so that the real locale that could be resolved is set. * Silently ignore not resolvable resources as we are not serving the resource for now */ getResourceStream(false); // Invalidate it again so that it won't hold up resources invalidate(); } } /** * Gets the absolute path of the resource. * * @return the absolute resource path */ public final String getAbsolutePath() { return absolutePath; } /** * Gets the locale. * * @return The Locale of this package resource */ public final Locale getLocale() { return locale; } /** * Gets the path this resource was created with. * * @return the path */ public final String getPath() { return path; } /** * @return Gets the resource for the component. */ public IResourceStream getResourceStream() { return getResourceStream(true); } /** * @return Gets the resource for the component. * @param failOnError * throw an AbortException when resource does not exist */ public IResourceStream getResourceStream(boolean failOnError) { // Locate resource IResourceStream resourceStream = Application.get().getResourceSettings() .getResourceStreamLocator().locate(getScope(), absolutePath, style, locale, null); // Check that resource was found if (resourceStream == null) { if (!failOnError) { // Do not abort the request, as we are not yet serving the resource return null; } String msg = "Unable to find package resource [path = " + absolutePath + ", style = " + style + ", locale = " + locale + "]"; log.warn(msg); if (RequestCycle.get() instanceof WebRequestCycle) { throw new AbortWithWebErrorCodeException(HttpServletResponse.SC_NOT_FOUND, msg); } else { throw new AbortException(); } } locale = resourceStream.getLocale(); if (resourceStream != null) { lastModifiedTime = resourceStream.lastModifiedTime(); lastModifiedTimeUpdate = System.currentTimeMillis(); } return resourceStream; } /** * Gets the scoping class, used for class loading and to determine the package. * * @return the scoping class */ public final Class getScope() { return Classes.resolveClass(scopeName); } /** * Gets the style. * * @return the style */ public final String getStyle() { return style; } private transient Time lastModifiedTime = null; private transient long lastModifiedTimeUpdate = 0; /** * Returns the last modified time of resource * * @return last modified time or null if the time can not be determined */ public Time lastModifiedTime() { if (lastModifiedTimeUpdate == 0 || lastModifiedTimeUpdate < System.currentTimeMillis() - 5 * (1000 * 60)) { lastModifiedTime = getResourceStream().lastModifiedTime(); lastModifiedTimeUpdate = System.currentTimeMillis(); } return lastModifiedTime; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -