List of usage examples for java.lang.ref SoftReference get
public T get()
From source file:org.orbeon.oxf.util.SoftReferenceObjectPool.java
/** * <p>Borrow an object from the pool. If there are no idle instances available in the pool, the configured * factory's {@link PoolableObjectFactory#makeObject()} method is invoked to create a new instance.</p> * * <p>All instances are {@link PoolableObjectFactory#activateObject(Object) activated} and * {@link PoolableObjectFactory#validateObject(Object) validated} before being returned by this * method. If validation fails or an exception occurs activating or validating an idle instance, * the failing instance is {@link PoolableObjectFactory#destroyObject(Object) destroyed} and another * instance is retrieved from the pool, validated and activated. This process continues until either the * pool is empty or an instance passes validation. If the pool is empty on activation or * it does not contain any valid instances, the factory's <code>makeObject</code> method is used * to create a new instance. If the created instance either raises an exception on activation or * fails validation, <code>NoSuchElementException</code> is thrown. Exceptions thrown by <code>MakeObject</code> * are propagated to the caller; but other than <code>ThreadDeath</code> or <code>VirtualMachineError</code>, * exceptions generated by activation, validation or destroy methods are swallowed silently.</p> * * @throws NoSuchElementException if a valid object cannot be provided * @throws IllegalStateException if invoked on a {@link #close() closed} pool * @throws Exception if an exception occurs creating a new instance * @return a valid, activated object instance *//*w ww.j a va2 s .c om*/ @Override public synchronized T borrowObject() throws Exception { assertOpen(); T obj = null; boolean newlyCreated = false; while (null == obj) { if (_pool.isEmpty()) { if (null == _factory) { throw new NoSuchElementException(); } else { newlyCreated = true; obj = _factory.makeObject(); } } else { SoftReference<T> ref = _pool.remove(_pool.size() - 1); obj = ref.get(); ref.clear(); // prevent this ref from being enqueued with refQueue. } if (null != _factory && null != obj) { try { _factory.activateObject(obj); if (!_factory.validateObject(obj)) { throw new Exception("ValidateObject failed"); } } catch (Throwable t) { PoolUtils.checkRethrow(t); try { _factory.destroyObject(obj); } catch (Throwable t2) { PoolUtils.checkRethrow(t2); // Swallowed } finally { obj = null; } if (newlyCreated) { throw new NoSuchElementException( "Could not create a validated object, cause: " + t.getMessage()); } } } } _numActive++; return obj; }
From source file:smartparking.smartparking.util.ImageDownloader.java
/** * @param url The URL of the image that will be retrieved from the cache. * @return The cached bitmap or null if it was not found. *//*from ww w .j av a 2 s .c o m*/ private Bitmap getBitmapFromCache(String url) { // First try the hard reference cache synchronized (sHardBitmapCache) { final Bitmap bitmap = sHardBitmapCache.get(url); if (bitmap != null) { // Bitmap found in hard cache // Move element to first position, so that it is removed last sHardBitmapCache.remove(url); sHardBitmapCache.put(url, bitmap); return bitmap; } } // Then try the soft reference cache SoftReference<Bitmap> bitmapReference = sSoftBitmapCache.get(url); if (bitmapReference != null) { final Bitmap bitmap = bitmapReference.get(); if (bitmap != null) { // Bitmap found in soft cache return bitmap; } else { // Soft reference has been Garbage Collected sSoftBitmapCache.remove(url); } } return null; }
From source file:org.mumod.util.ImageManager.java
public Bitmap get(String url) { SoftReference<Bitmap> ref; Bitmap bitmap;/*w w w .j a v a 2s . c om*/ // Look in memory first. synchronized (this) { ref = mCache.get(url); } if (ref != null) { bitmap = ref.get(); if (bitmap != null) { return bitmap; } } // Now try file. bitmap = lookupFile(url); if (bitmap != null) { synchronized (this) { mCache.put(url, new SoftReference<Bitmap>(bitmap)); } return bitmap; } if (MustardApplication.DEBUG) Log.i(TAG, "Image is missing: " + url); return null; }
From source file:com.guang.eunormia.common.cache.DefaultLocalCache.java
@Override public V put(K key, V value, int TTL) { SoftReference<V> result = getCache(key).put(key, new SoftReference<V>(value)); Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.SECOND, TTL); expiryCache.put(key, calendar.getTime().getTime()); return result == null ? null : result.get(); }
From source file:ma.glasnost.orika.test.perf.MultiThreadedTestCase.java
/** * Since the contract for SoftReference states that all soft references will * be cleared by the garbage collector before OOME is thrown, we allocate * dummy bytes until we reach OOME.// w ww .j a v a 2 s . c o m */ private void forceClearSoftAndWeakReferences() { SoftReference<Object> checkReference = new SoftReference<Object>(new Object()); Assert.assertNotNull(checkReference.get()); try { List<byte[]> byteBucket = new ArrayList<byte[]>(); for (int i = 0; i < Integer.MAX_VALUE; ++i) { int available = (int) Math.min((long) Integer.MAX_VALUE, Runtime.getRuntime().maxMemory()); byteBucket.add(new byte[available]); } } catch (Throwable e) { // Ignore OME; soft references should now have been cleared Assert.assertNull(checkReference.get()); } }
From source file:codelovers.test.androidyoutubeplayer.ImageDownloader.java
/** * @param url The URL of the image that will be retrieved from the cache. * @return The cached bitmap or null if it was not found. *//*w ww. j ava 2s. c om*/ private Bitmap getBitmapFromCache(String url) { // First try the hard reference cache synchronized (mHardBitmapCache) { final Bitmap bitmap = mHardBitmapCache.get(url); if (bitmap != null) { // Bitmap found in hard cache // Move element to first position, so that it is removed last mHardBitmapCache.remove(url); mHardBitmapCache.put(url, bitmap); return bitmap; } } // Then try the soft reference cache SoftReference<Bitmap> bitmapReference = sSoftBitmapCache.get(url); if (bitmapReference != null) { final Bitmap bitmap = bitmapReference.get(); if (bitmap != null) { // Bitmap found in soft cache return bitmap; } else { // Soft reference has been Garbage Collected sSoftBitmapCache.remove(url); } } return null; }
From source file:org.teiid.olingo.web.ODataFilter.java
public void internalDoFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException, TeiidProcessingException { HttpServletRequest httpRequest = (HttpServletRequest) request; String proxyURI = this.proxyBaseURI; if (proxyURI != null) { httpRequest = new ProxyHttpServletRequest(httpRequest, proxyURI); }//from ww w . java 2 s. c o m VDBKey key = null; String vdbName = null; String version = null; String modelName = null; String uri = ((HttpServletRequest) request).getRequestURI().toString(); String fullURL = ((HttpServletRequest) request).getRequestURL().toString(); if (uri.startsWith("/odata4/static/") || uri.startsWith("/odata4/keycloak/")) { //$NON-NLS-1$ //$NON-NLS-2$ chain.doFilter(httpRequest, response); return; } String contextPath = httpRequest.getContextPath(); String baseURI = fullURL.substring(0, fullURL.indexOf(contextPath)); int endIdx = uri.indexOf('/', contextPath.length() + 1); int beginIdx = contextPath.length() + 1; if (contextPath.equals("/odata4")) { //$NON-NLS-1$ if (endIdx == -1) { throw new TeiidProcessingException(ODataPlugin.Event.TEIID16020, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16020)); } baseURI = baseURI + "/odata4"; //$NON-NLS-1$ vdbName = uri.substring(beginIdx, endIdx); int modelIdx = uri.indexOf('/', endIdx + 1); if (modelIdx == -1) { modelName = uri.substring(endIdx + 1).trim(); if (modelName.isEmpty()) { throw new TeiidProcessingException(ODataPlugin.Event.TEIID16019, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16019)); } } else { modelName = uri.substring(endIdx + 1, modelIdx); } contextPath = contextPath + "/" + vdbName + "/" + modelName; //$NON-NLS-1$ //$NON-NLS-2$ vdbName = vdbName.trim(); if (vdbName.isEmpty()) { throw new TeiidProcessingException(ODataPlugin.Event.TEIID16008, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16008)); } } else { if (this.initProperties.getProperty("vdb-name") == null) { //$NON-NLS-1$ throw new TeiidProcessingException(ODataPlugin.Event.TEIID16018, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16018)); } vdbName = this.initProperties.getProperty("vdb-name"); //$NON-NLS-1$ version = this.initProperties.getProperty("vdb-version"); //$NON-NLS-1$ if (endIdx == -1) { modelName = uri.substring(beginIdx).trim(); if (modelName.isEmpty()) { throw new TeiidProcessingException(ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16021)); } } else { modelName = uri.substring(beginIdx, endIdx); } contextPath = contextPath + "/" + modelName; //$NON-NLS-1$ } ContextAwareHttpSerlvetRequest contextAwareRequest = new ContextAwareHttpSerlvetRequest(httpRequest); contextAwareRequest.setContextPath(contextPath); httpRequest = contextAwareRequest; key = new VDBKey(vdbName, version); if (key.isAtMost()) { if (key.getVersion() != null) { throw new TeiidProcessingException(ODataPlugin.Event.TEIID16044, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16044, key)); } key = new VDBKey(vdbName, "1"); //$NON-NLS-1$ //legacy behavior, default to version 1 } SoftReference<OlingoBridge> ref = this.contextMap.get(key); OlingoBridge context = null; if (ref != null) { context = ref.get(); } if (context == null) { context = new OlingoBridge(); ref = new SoftReference<OlingoBridge>(context); this.contextMap.put(key, ref); } Client client = buildClient(key.getName(), key.getVersion(), this.initProperties); try { Connection connection = client.open(); registerVDBListener(client, connection); ODataHttpHandler handler = context.getHandler(baseURI, client, modelName); httpRequest.setAttribute(ODataHttpHandler.class.getName(), handler); httpRequest.setAttribute(Client.class.getName(), client); chain.doFilter(httpRequest, response); } catch (SQLException e) { throw new TeiidProcessingException(e); } finally { try { client.close(); } catch (SQLException e) { //ignore } } }
From source file:com.twapime.app.util.AsyncImageLoader.java
/** * @param imageUrl/*from w ww . j ava 2 s.c om*/ * @return */ private Drawable getDrawableFromCache(String imageUrl) { synchronized (hardCache) { Drawable drawable = hardCache.get(imageUrl); // if (drawable != null) { // Drawable found in hard cache // Move element to first position, so that it is removed last hardCache.remove(imageUrl); hardCache.put(imageUrl, drawable); // return drawable; } } // SoftReference<Drawable> reference = softCache.get(imageUrl); // if (reference != null) { Drawable drawable = reference.get(); // if (drawable != null) { return drawable; } } // return null; }
From source file:adapters.images.ImageDownloader.java
/** * @param url The URL of the image that will be retrieved from the cache. * @return The cached bitmap or null if it was not found. *///w w w. java2s .c o m private Bitmap getBitmapFromCache(String url) { // First try the hard reference cache synchronized (sHardBitmapCache) { final Bitmap bitmap = sHardBitmapCache.get(url); if (bitmap != null) { // Bitmap found in hard cache // Move element to first position, so that it is removed last sHardBitmapCache.remove(url); sHardBitmapCache.put(url, bitmap); return bitmap; } } // Then try the soft reference cache SoftReference<Bitmap> bitmapReference = sSoftBitmapCache.get(url); if (bitmapReference != null) { final Bitmap bitmap = bitmapReference.get(); if (bitmap != null) { // Bitmap found in soft cache return bitmap; } else { // Soft reference has been Garbage Collected sSoftBitmapCache.remove(url); } } return null; }
From source file:kr.co.WhenWhereWho3.ImageDownloader.java
/** * @param url The URL of the image that will be retrieved from the cache. * @return The cached bitmap or null if it was not found. *//* w w w . j a v a 2 s . c o m*/ private Bitmap getBitmapFromCache(String url) { // First try the hard reference cache // ( ) synchronized (sHardBitmapCache) { // final Bitmap bitmap = sHardBitmapCache.get(url); // if (bitmap != null) { // Bitmap found in hard cache // Move element to first position, so that it is removed last // sHardBitmapCache.remove(url); // . ?? // sHardBitmapCache.put(url, bitmap); return bitmap; } } // Then try the soft reference cache SoftReference<Bitmap> bitmapReference = sSoftBitmapCache.get(url); if (bitmapReference != null) { final Bitmap bitmap = bitmapReference.get(); if (bitmap != null) { // Bitmap found in soft cache return bitmap; } else { // Soft reference has been Garbage Collected sSoftBitmapCache.remove(url); } } return null; }