@Test public void protected_asset_client_URL() { ResourceCache cache = mockResourceCache(); ClasspathAssetAliasManager aliasManager = mockClasspathAssetAliasManager(); Resource r = new ClasspathResource("foo/Bar.txt"); train_requiresDigest(cache, r, true); expect(cache.getDigest(r)).andReturn("ABC123"); String expectedClientURL = "/context/asset/foo/Bar.ABC123.txt"; train_toClientURL(aliasManager, "foo/Bar.ABC123.txt", expectedClientURL); EasyMock.expectLastCall().times(2); // 2nd time is the toString() call replay(); AssetFactory factory = new ClasspathAssetFactory(cache, aliasManager); Asset asset = factory.createAsset(r); assertSame(asset.getResource(), r); assertEquals(asset.toClientURL(), expectedClientURL); assertEquals(asset.toString(), expectedClientURL); verify(); }
public void cleanCaches() { layoutCacheByMarkup.clear(); drawableCacheByMarkup.clear(); animatorCacheByMarkup.clear(); animatorCacheByResDescValue.clear(); // domAnimCache.clear(); // Menues??? valuesCacheByMarkup.clear(); }
public XMLDOMLayout getXMLDOMLayoutCacheByMarkup( String markup, String itsNatServerVersion, XMLDOMLayoutParser.LayoutType layoutType, XMLDOMParserContext xmlDOMParserContext) { // Este método DEBE ser multihilo, el objeto layoutCacheByMarkup ya lo es. // No pasa nada si por una rarísima casualidad dos Layout idénticos hacen put, quedará el // último, ten en cuenta que esto // es un caché. // Extraemos el markup sin el script de carga porque dos páginas generadas "iguales" SIEMPRE // serán diferentes a nivel // de markup en el loadInitScript porque el id cambia y algún token aleatorio, sin el // loadInitScript podemos conseguir // muchos más aciertos de cacheo y acelerar un montón al tener el parseo ya hecho. // Si el template no es generado por ItsNat server o bien el scripting está desactivado // (itsNatServerVersion puede // ser no null pues es un header), loadInitScript será null y no pasa nada markupNoLoadScript[0] // es el markup original String[] markupWithoutLoadScript = new String[1]; String loadScript = null; if (itsNatServerVersion != null && layoutType == XMLDOMLayoutParser.LayoutType.PAGE) loadScript = XMLDOMLayout.extractLoadScriptMarkup(markup, markupWithoutLoadScript); else markupWithoutLoadScript[0] = markup; XMLDOMLayout cachedXMLDOMLayout = layoutCacheByMarkup.get(markupWithoutLoadScript[0]); if (cachedXMLDOMLayout == null) { XMLDOMLayoutParser layoutParser = XMLDOMLayoutParser.createXMLDOMLayoutParser( itsNatServerVersion, layoutType, xmlDOMParserContext); cachedXMLDOMLayout = layoutParser.createXMLDOMLayout(); layoutCacheByMarkup.put( markupWithoutLoadScript[0], cachedXMLDOMLayout); // Cacheamos cuanto antes pues puede haber recursividad layoutParser.parse(markup, cachedXMLDOMLayout); if (cachedXMLDOMLayout instanceof XMLDOMLayoutPageItsNat) ((XMLDOMLayoutPageItsNat) cachedXMLDOMLayout) .setLoadInitScript( null); // Que quede claro que no se puede utilizar directamente en el cacheado // guardado } else // cachedDOMLayout != null { // Recuerda que cachedDOMLayout devuelto tiene el timestamp actualizado por el hecho de llamar // al get() } XMLDOMLayout clonedDOMLayout = cachedXMLDOMLayout .partialClone(); // Necesitamos un clone parcial porque el loadInitScript necesitamos // alojarlo en un objeto nuevo pues no puede cachearse if (clonedDOMLayout instanceof XMLDOMLayoutPageItsNat) ((XMLDOMLayoutPageItsNat) clonedDOMLayout).setLoadInitScript(loadScript); return clonedDOMLayout; }
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; }
public XMLDOMDrawable getXMLDOMDrawableCacheByMarkup( String markup, XMLDOMParserContext xmlDOMParserContext) { // Ver notas de getXMLDOMLayoutCacheByMarkup() XMLDOMDrawable cachedXMLDOMDrawable = drawableCacheByMarkup.get(markup); if (cachedXMLDOMDrawable != null) return cachedXMLDOMDrawable; cachedXMLDOMDrawable = new XMLDOMDrawable(); drawableCacheByMarkup.put( markup, cachedXMLDOMDrawable); // Cacheamos cuanto antes pues puede haber recursividad XMLDOMDrawableParser parser = XMLDOMDrawableParser.createXMLDOMDrawableParser(xmlDOMParserContext); parser.parse(markup, cachedXMLDOMDrawable); return cachedXMLDOMDrawable; }
public XMLDOMValues getXMLDOMValuesCacheByMarkup( String markup, XMLDOMParserContext xmlDOMParserContext) { // Ver notas de getXMLDOMLayoutCacheByMarkup() XMLDOMValues cachedXMLDOMValues = valuesCacheByMarkup.get(markup); if (cachedXMLDOMValues != null) { // System.out.println("CACHED cachedXMLDOMValues"); return cachedXMLDOMValues; } cachedXMLDOMValues = new XMLDOMValues(); valuesCacheByMarkup.put( markup, cachedXMLDOMValues); // Cacheamos cuanto antes pues puede haber recursividad XMLDOMValuesParser parser = XMLDOMValuesParser.createXMLDOMValuesParser(xmlDOMParserContext); parser.parse(markup, cachedXMLDOMValues); return cachedXMLDOMValues; }
@Before public void mockResourcePersister() { duplicationCache = mock(DuplicationCache.class); ResourceCache resourceCache = mock(ResourceCache.class); BatchResource batchResource = mock(BatchResource.class); when(batchResource.resource()).thenReturn(aFile); when(batchResource.snapshotId()).thenReturn(FILE_SNAPSHOT_ID); when(resourceCache.get("foo:org/foo/Bar.java")).thenReturn(batchResource); MetricFinder metricFinder = mock(MetricFinder.class); when(metricFinder.findByKey(CoreMetrics.DUPLICATIONS_DATA_KEY)) .thenReturn(CoreMetrics.DUPLICATIONS_DATA.setId(2)); duplicationPersister = new DuplicationPersister( getMyBatis(), ruleFinder, resourceCache, duplicationCache, metricFinder); }
/** * Request the given resource from this job frontend's class loader. * * @param theJobBackend Job Backend that is calling this method. * @param resourceName Resource name. * @exception IOException Thrown if an I/O error occurred. */ public synchronized void requestResource(JobBackendRef theJobBackend, String resourceName) throws IOException { // To hold resource content. byte[] content = null; // Get resource content. If resource not found, content is null. if (myResourceCache.contains(resourceName)) { // Get resource content from cache. content = myResourceCache.getNoWait(resourceName); } else { // Get resource content from class loader, save it in cache. InputStream stream = getClass().getClassLoader().getResourceAsStream(resourceName); if (stream != null) { content = new ByteSequence(stream).toByteArray(); } myResourceCache.put(resourceName, content); } // Send resource to job backend. theJobBackend.reportResource(this, resourceName, content); }
void doStart(Project rootProject) { Bucket bucket = new Bucket(rootProject); addBucket(rootProject, bucket); if (migration != null) { migration.checkIfMigrationNeeded(rootProject); } resourceCache.add(rootProject, null); currentProject = rootProject; for (Project module : rootProject.getModules()) { addModule(rootProject, module); } }
public XMLDOMAnimator getXMLDOMAnimatorCacheByMarkup( String markup, ResourceDescDynamic resourceDesc, XMLDOMParserContext xmlDOMParserContext) { // Ver notas de getXMLDOMLayoutCacheByMarkup() String resourceDescValue = resourceDesc.getResourceDescValue(); if (animatorCacheByResDescValue.get(resourceDescValue) == null) animatorCacheByResDescValue.put( resourceDescValue, resourceDesc); // Lo hacemos antes de animatorCacheByMarkup.get() de esta manera cacheamos // también en el caso raro de dos archivos con el mismo markup, por otra // parte en el caso de que ya exista se actualiza el timestamp del recurso // al hacer el get (recurso recientemente usado) XMLDOMAnimator cachedXMLDOMAnimator = animatorCacheByMarkup.get(markup); if (cachedXMLDOMAnimator != null) return cachedXMLDOMAnimator; cachedXMLDOMAnimator = new XMLDOMAnimator(); animatorCacheByMarkup.put( markup, cachedXMLDOMAnimator); // Cacheamos cuanto antes pues puede haber recursividad XMLDOMAnimatorParser parser = XMLDOMAnimatorParser.createXMLDOMAnimatorParser(xmlDOMParserContext); parser.parse(markup, cachedXMLDOMAnimator); return cachedXMLDOMAnimator; }
private Bucket doIndex(Resource resource, @Nullable Resource parentReference) { Bucket bucket = getBucket(resource); if (bucket != null) { return bucket; } if (StringUtils.isBlank(resource.getKey())) { LOG.warn("Unable to index a resource without key " + resource); return null; } Resource parent = null; if (!ResourceUtils.isLibrary(resource)) { // a library has no parent parent = (Resource) ObjectUtils.defaultIfNull(parentReference, currentProject); } Bucket parentBucket = getBucket(parent); if (parentBucket == null && parent != null) { LOG.warn("Resource ignored, parent is not indexed: " + resource); return null; } if (ResourceUtils.isProject(resource) || /* For technical projects */ ResourceUtils.isRootProject(resource)) { resource.setEffectiveKey(resource.getKey()); } else { resource.setEffectiveKey(ComponentKeys.createEffectiveKey(currentProject, resource)); } bucket = new Bucket(resource).setParent(parentBucket); addBucket(resource, bucket); Resource parentResource = parentBucket != null ? parentBucket.getResource() : null; resourceCache.add(resource, parentResource); return bucket; }
public ResourceDescDynamic getAnimatorResourceDescDynamicCacheByResourceDescValue( String resourceDescValue) { return animatorCacheByResDescValue.get(resourceDescValue); }
/** * Loads, parses and returns the resource of the specified URI, or null if not found. The parser * is defined by the loader defined in {@link ResourceCache}. * * @param cache the resource cache. Note: its loader must extend from {@link ResourceLoader}. * @param path the URI path * @param extra the extra parameter that will be passed to {@link * ResourceLoader#parse(String,File,Object)} and {@link * ResourceLoader#parse(String,URL,Object)} */ public static final <V> V get( ResourceCache<V> cache, ServletContext ctx, String path, Object extra) { // 20050905: Tom Yeh // We don't need to handle the default name if user specifies only a dir // because it is handled by the container directly // And, web developer has to specify <welcome-file> in web.xml URL url = null; if (path == null || path.length() == 0) path = "/"; else if (path.charAt(0) != '/') { if (path.indexOf("://") > 0) { try { url = new URL(path); } catch (java.net.MalformedURLException ex) { throw new SystemException(ex); } } else path = '/' + path; } if (url == null) { if (path.startsWith("/~")) { final ServletContext ctx0 = ctx; final String path0 = path; final int j = path.indexOf('/', 2); final String ctxpath; if (j >= 0) { ctxpath = "/" + path.substring(2, j); path = path.substring(j); } else { ctxpath = "/" + path.substring(2); path = "/"; } final ExtendletContext extctx = Servlets.getExtendletContext(ctx, ctxpath.substring(1)); if (extctx != null) { url = extctx.getResource(path); // if (log.debugable()) log.debug("Resolving "+path0+" to "+url); if (url == null) return null; try { return cache.get(new ResourceInfo(path, url, extra)); } catch (Throwable ex) { final IOException ioex = getIOException(ex); if (ioex == null) throw SystemException.Aide.wrap(ex); log.warningBriefly("Unable to load " + url, ioex); } return null; } ctx = ctx.getContext(ctxpath); if (ctx == null) { // failed // if (log.debugable()) log.debug("Context not found: "+ctxpath); ctx = ctx0; path = path0; // restore } } final String flnm = ctx.getRealPath(path); if (flnm != null) { try { return cache.get(new ResourceInfo(path, new File(flnm), extra)); // it is loader's job to check the existence } catch (Throwable ex) { final IOException ioex = getIOException(ex); if (ioex == null) throw SystemException.Aide.wrap(ex); log.warningBriefly("Unable to load " + flnm, ioex); } return null; } } // try url because some server uses JAR format try { if (url == null) url = ctx.getResource(path); if (url != null) return cache.get(new ResourceInfo(path, url, extra)); } catch (Throwable ex) { final IOException ioex = getIOException(ex); if (ioex == null) throw SystemException.Aide.wrap(ex); log.warningBriefly("Unable to load " + path, ioex); } return null; }