public AssetSourceImpl( ThreadLocale threadLocale, Map<String, AssetFactory> configuration, SymbolSource symbolSource, Logger logger, OperationTracker tracker, Request request) { this.configuration = configuration; this.threadLocale = threadLocale; this.symbolSource = symbolSource; this.logger = logger; this.tracker = tracker; this.request = request; Map<Class, AssetFactory> byResourceClass = CollectionFactory.newMap(); for (Map.Entry<String, AssetFactory> e : configuration.entrySet()) { String prefix = e.getKey(); AssetFactory factory = e.getValue(); Resource rootResource = factory.getRootResource(); byResourceClass.put(rootResource.getClass(), factory); prefixToRootResource.put(prefix, rootResource); } registry = StrategyRegistry.newInstance(AssetFactory.class, byResourceClass); }
private Asset getLocalizedAssetFromResource(Resource unlocalized, Locale locale) { Resource localized = locale == null ? unlocalized : unlocalized.forLocale(locale); if (localized == null || !localized.exists()) throw new RuntimeException( String.format("Unable to locate asset '%s' (the file does not exist).", unlocalized)); return getAssetForResource(localized); }
/** * Finds a localized resource. * * @param baseResource base resource, or null for classpath root * @param path path from baseResource to expected resource * @param locale locale to localize for, or null to not localize * @return resource, which may not exist */ private Resource findLocalizedResource(Resource baseResource, String path, Locale locale) { Resource unlocalized = findResource(baseResource, path); if (locale == null || !unlocalized.exists()) { return unlocalized; } return localize(unlocalized, locale); }
private Asset createAssetFromResource(Resource resource) { // The class of the resource is derived from the class of the base resource. // So we can then use the class of the resource as a key to locate the correct asset // factory. try { upgradeReadLockToWriteLock(); // Check for competing thread beat us to it (not very likely!): Asset result = TapestryInternalUtils.getAndDeref(cache, resource); if (result != null) { return result; } Class resourceClass = resource.getClass(); AssetFactory factory = registry.get(resourceClass); return factory.createAsset(resource); } finally { downgradeWriteLockToReadLock(); } }
/** * @param baseResource the base resource (or null for classpath root) that path will extend from * @param path extension path from the base resource * @return the resource, unlocalized, which may not exist (may be for a path with no actual * resource) */ private Resource findResource(Resource baseResource, String path) { assert path != null; int colonx = path.indexOf(':'); if (colonx < 0) { Resource root = baseResource != null ? baseResource : prefixToRootResource.get(AssetConstants.CLASSPATH); return root.forFile(path); } String prefix = path.substring(0, colonx); Resource root = prefixToRootResource.get(prefix); if (root == null) throw new IllegalArgumentException( String.format("Unknown prefix for asset path '%s'.", path)); return root.forFile(path.substring(colonx + 1)); }
/** * converts an image resource to an AWT image. * * @param imageResource image resource * @return AWT image */ private Image toAwtImage(Resource imageResource) { Image image = Toolkit.getDefaultToolkit().getImage(imageResource.toURL()); MediaTracker mediaTracker = new MediaTracker(new Container()); mediaTracker.addImage(image, 0); try { mediaTracker.waitForID(0); } catch (InterruptedException e) { throw new RuntimeException(e); } return image; }
private Asset getComponentAsset( ComponentResources resources, String expandedPath, Resource metaResource) { if (expandedPath.contains(":") || expandedPath.startsWith("/")) { return getAssetInLocale(resources.getBaseResource(), expandedPath, resources.getLocale()); } // So, it's relative to the component. First, check if there's a match using the 5.4 rules. if (metaResource.exists()) { return getAssetForResource(metaResource); } Resource oldStyle = findLocalizedResource(resources.getBaseResource(), expandedPath, resources.getLocale()); if (oldStyle == null || !oldStyle.exists()) { return null; } return getAssetForResource(oldStyle); }
private String buildClientURL(Resource resource) { boolean requiresDigest = cache.requiresDigest(resource); String path = resource.getPath(); if (requiresDigest) { // Resources with extensions go from foo/bar/baz.txt --> foo/bar/baz.CHECKSUM.txt int lastdotx = path.lastIndexOf('.'); path = path.substring(0, lastdotx + 1) + cache.getDigest(resource) + path.substring(lastdotx); } return path; }
private Asset getLocaleAsset(Locale locale, String jQueryUIPath) { final String prefix = String.format("%s/i18n/jquery.ui.datepicker-%s", jQueryUIPath, locale.getLanguage()); final Resource withCountryExtension = typeCoercer.coerce(String.format("%s-%s.js", prefix, locale.getCountry()), Resource.class); if (withCountryExtension.exists()) { return assetSource.getClasspathAsset(withCountryExtension.getPath()); } final Resource withLanguageExtension = typeCoercer.coerce(String.format("%s.js", prefix), Resource.class); if (withLanguageExtension.exists()) { return assetSource.getClasspathAsset(withLanguageExtension.getPath()); } return null; }
private Resource localize(Resource unlocalized, Locale locale) { Resource localized = unlocalized.forLocale(locale); return localized != null ? localized : unlocalized; }