summaryrefslogtreecommitdiff
path: root/java/src
diff options
context:
space:
mode:
authorBernard Normier <bernard@zeroc.com>2015-11-14 12:46:26 -0500
committerBernard Normier <bernard@zeroc.com>2015-11-14 12:46:26 -0500
commit0c054b830dc525d97257ef59cebd80f249d65586 (patch)
tree619d3228029838e9440100dca2057e30aabe7347 /java/src
parentMoved AbstractMutex and Cache (diff)
downloadice-0c054b830dc525d97257ef59cebd80f249d65586.tar.bz2
ice-0c054b830dc525d97257ef59cebd80f249d65586.tar.xz
ice-0c054b830dc525d97257ef59cebd80f249d65586.zip
Moved Java Cache, Store, FileLock from IceUtil to Freeze
Diffstat (limited to 'java/src')
-rw-r--r--java/src/Ice/src/main/java/IceUtil/Cache.java298
-rw-r--r--java/src/Ice/src/main/java/IceUtil/FileLockException.java71
-rw-r--r--java/src/Ice/src/main/java/IceUtil/Store.java25
-rw-r--r--java/src/Ice/src/main/java/IceUtilInternal/FileLock.java169
4 files changed, 0 insertions, 563 deletions
diff --git a/java/src/Ice/src/main/java/IceUtil/Cache.java b/java/src/Ice/src/main/java/IceUtil/Cache.java
deleted file mode 100644
index 8c60c7bef15..00000000000
--- a/java/src/Ice/src/main/java/IceUtil/Cache.java
+++ /dev/null
@@ -1,298 +0,0 @@
-// **********************************************************************
-//
-// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved.
-//
-// This copy of Ice is licensed to you under the terms described in the
-// ICE_LICENSE file included in this distribution.
-//
-// **********************************************************************
-
-package IceUtil;
-
-/**
- * An abstraction to efficiently maintain a cache, without holding
- * a lock on the entire cache while objects are being loaded from
- * their backing store. This class is useful mainly to implement
- * evictors, such as used by Freeze.
- *
- * @see Store
- * @see Freeze.Evictor
- **/
-
-public class Cache
-{
- /**
- * Initialize a cache using the specified backing store.
- **/
- public Cache(Store store)
- {
- _store = store;
- }
-
- /**
- * Return the value stored for the given key from the cache.
- *
- * @param key The key for the object to look up in the cache.
- *
- * @return If the cache contains an entry for the key, the return value
- * is the object corresponding to the key; otherwise, the return value
- * is null. <code>getIfPinned</code> does not call {@link Store#load}.
- *
- * @see Store#load
- **/
- public Object
- getIfPinned(Object key)
- {
- synchronized(_map)
- {
- CacheValue val = _map.get(key);
- return val == null ? null : val.obj;
- }
- }
-
- /**
- * Removes the entry for the given key from the cache.
- *
- * @param key The key for the entry to remove.
- *
- * @return If the cache contains an entry for the key, the
- * return value is the corresponding object; otherwise, the
- * return value is <code>null</code>.
- **/
- public Object
- unpin(Object key)
- {
- synchronized(_map)
- {
- CacheValue val = _map.remove(key);
- return val == null ? null : val.obj;
- }
- }
-
- /**
- * Removes all entries from the cache.
- **/
- public void
- clear()
- {
- synchronized(_map)
- {
- _map.clear();
- }
- }
-
- /**
- * Returns the number of entries in the cache.
- *
- * @return The number of entries.
- **/
- public int
- size()
- {
- synchronized(_map)
- {
- return _map.size();
- }
- }
-
- /**
- * Adds a key-value pair to the cache.
- * This version of <code>pin</code> does not call {@link Store#load} to retrieve
- * an entry from backing store if an entry for the given key is not yet in the cache. This
- * is useful to add a newly-created object to the cache.
- *
- * @param key The key for the entry.
- * @param o The value for the entry.
- * @return If the cache already contains an entry with the given key, the entry is
- * unchanged and <code>pin(Object, Object)</code> returns the original value for the entry; otherwise,
- * the entry is added and <code>pin(Object, Object)</code> returns <code>null</code>.
- **/
- public Object
- pin(Object key, Object o)
- {
- synchronized(_map)
- {
- CacheValue existingVal = _map.put(key, new CacheValue(o));
- if(existingVal != null)
- {
- _map.put(key, existingVal);
- return existingVal.obj;
- }
- else
- {
- return null;
- }
- }
- }
-
- /**
- * Returns an object from the cache.
- * If no entry with the given key is in the cache, <code>pin</code> calls
- * {@link Store#load} to retrieve the corresponding value (if any) from the
- * backing store.
- *
- * @param key The key for the entry to retrieve.
- * @return Returns the value for the corresponding key if the cache
- * contains an entry for the key. Otherwise, <code>pin(Object)</code> calls
- * {@link Store#load} and the return value is whatever is returned by
- * <code>load</code>; if <code>load</code> throws an exception, that exception
- * is thrown by <code>pin(Object)</code>.
- **/
- public Object
- pin(Object key)
- {
- return pinImpl(key, null);
- }
-
- /**
- * Adds a key-value pair to the cache.
- * @param key The key for the entry.
- * @param newObj The value for the entry.
- * @return If the cache already contains an entry for the given key,
- * <code>putIfAbsent</code> returns the original value for that key.
- * If no entry is for the given key is in the cache, <code>putIfAbsent</code>
- * calls {@link Store#load} to retrieve the corresponding entry (if any) from
- * the backings store and returns the value returned by <code>load</code>.
- * If the cache does not contain an entry for the given key and <code>load</code>
- * does not return a value for the key, <code>putIfAbsent</code> adds the new entry
- * and returns <code>null</code>.
- **/
- public Object
- putIfAbsent(Object key, Object newObj)
- {
- return pinImpl(key, newObj);
- }
-
- static private class CacheValue
- {
- CacheValue()
- {
- }
-
- CacheValue(Object obj)
- {
- this.obj = obj;
- }
-
- Object obj = null;
- java.util.concurrent.CountDownLatch latch = null;
- }
-
- private Object
- pinImpl(Object key, Object newObj)
- {
- for(;;)
- {
- CacheValue val = null;
- java.util.concurrent.CountDownLatch latch = null;
-
- synchronized(_map)
- {
- val = _map.get(key);
- if(val == null)
- {
- val = new CacheValue();
- _map.put(key, val);
- }
- else
- {
- if(val.obj != null)
- {
- return val.obj;
- }
- if(val.latch == null)
- {
- //
- // The first queued thread creates the latch
- //
- val.latch = new java.util.concurrent.CountDownLatch(1);
- }
- latch = val.latch;
- }
- }
-
- if(latch != null)
- {
- try
- {
- latch.await();
- }
- catch(InterruptedException e)
- {
- // Ignored
- }
-
- //
- // val could be stale now, e.g. some other thread pinned and unpinned the
- // object while we were waiting.
- // So start over.
- //
- continue;
- }
- else
- {
- Object obj;
- try
- {
- obj = _store.load(key);
- }
- catch(RuntimeException e)
- {
- synchronized(_map)
- {
- _map.remove(key);
- latch = val.latch;
- val.latch = null;
- }
- if(latch != null)
- {
- latch.countDown();
- assert latch.getCount() == 0;
- }
- throw e;
- }
-
- synchronized(_map)
- {
- if(obj != null)
- {
- val.obj = obj;
- }
- else
- {
- if(newObj == null)
- {
- //
- // pin() did not find the object
- //
-
- //
- // The waiting threads will have to call load() to see by themselves.
- //
- _map.remove(key);
- }
- else
- {
- //
- // putIfAbsent() inserts key/newObj
- //
- val.obj = newObj;
- }
- }
-
- latch = val.latch;
- val.latch = null;
- }
- if(latch != null)
- {
- latch.countDown();
- assert latch.getCount() == 0;
- }
- return obj;
- }
- }
- }
-
- private final java.util.Map<Object, CacheValue> _map = new java.util.HashMap<Object, CacheValue>();
- private final Store _store;
-}
diff --git a/java/src/Ice/src/main/java/IceUtil/FileLockException.java b/java/src/Ice/src/main/java/IceUtil/FileLockException.java
deleted file mode 100644
index 74d2568596f..00000000000
--- a/java/src/Ice/src/main/java/IceUtil/FileLockException.java
+++ /dev/null
@@ -1,71 +0,0 @@
-// **********************************************************************
-//
-// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved.
-//
-// This copy of Ice is licensed to you under the terms described in the
-// ICE_LICENSE file included in this distribution.
-//
-// **********************************************************************
-
-package IceUtil;
-
-public class FileLockException extends RuntimeException implements Cloneable
-{
- public FileLockException()
- {
- }
-
- public FileLockException(String path)
- {
- this.path = path;
- }
-
- public FileLockException(Throwable cause)
- {
- super(cause);
- }
-
- public FileLockException(String path, Throwable cause)
- {
- super(cause);
- this.path = path;
- }
-
- @Override
- public FileLockException clone()
- {
- FileLockException c = null;
- try
- {
- c = (FileLockException)super.clone();
- }
- catch(CloneNotSupportedException ex)
- {
- assert false;
- }
- return c;
- }
-
- public String
- ice_name()
- {
- return "IceUtil::FileLockException";
- }
-
- @Override
- public String
- toString()
- {
- java.io.StringWriter sw = new java.io.StringWriter();
- java.io.PrintWriter pw = new java.io.PrintWriter(sw);
- IceUtilInternal.OutputBase out = new IceUtilInternal.OutputBase(pw);
- out.setUseTab(false);
- out.print(getClass().getName());
- out.inc();
- IceInternal.ValueWriter.write(this, out);
- pw.flush();
- return sw.toString();
- }
-
- public String path;
-}
diff --git a/java/src/Ice/src/main/java/IceUtil/Store.java b/java/src/Ice/src/main/java/IceUtil/Store.java
deleted file mode 100644
index 63f45783660..00000000000
--- a/java/src/Ice/src/main/java/IceUtil/Store.java
+++ /dev/null
@@ -1,25 +0,0 @@
-// **********************************************************************
-//
-// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved.
-//
-// This copy of Ice is licensed to you under the terms described in the
-// ICE_LICENSE file included in this distribution.
-//
-// **********************************************************************
-
-package IceUtil;
-
-/**
- * A base interface for retrieving persistent objects from a backing store.
- **/
-
-public interface Store
-{
- /**
- * Instantiate an object, initializing its state from a backing store.
- *
- * @param key The database key for the object to instantiate.
- *
- **/
- Object load(Object key);
-}
diff --git a/java/src/Ice/src/main/java/IceUtilInternal/FileLock.java b/java/src/Ice/src/main/java/IceUtilInternal/FileLock.java
deleted file mode 100644
index e2049fb1457..00000000000
--- a/java/src/Ice/src/main/java/IceUtilInternal/FileLock.java
+++ /dev/null
@@ -1,169 +0,0 @@
-// **********************************************************************
-//
-// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved.
-//
-// This copy of Ice is licensed to you under the terms described in the
-// ICE_LICENSE file included in this distribution.
-//
-// **********************************************************************
-
-package IceUtilInternal;
-
-import java.io.File;
-import java.io.RandomAccessFile;
-import java.nio.channels.FileChannel;
-import java.lang.reflect.Method;
-
-public final class FileLock
-{
- //
- // The constructor opens the given file (eventually creating it)
- // and acquires a lock on the file or throws FileLockException if
- // the file couldn't be locked.
- //
- // If the lock can be acquired, the Java VM name is written to the
- // file.
- //
- public FileLock(String path)
- {
- _file = new File(path);
-
- FileChannel channel;
- try
- {
- _randFile = new RandomAccessFile(_file, "rw");
- channel = _randFile.getChannel();
- }
- catch(java.io.FileNotFoundException e)
- {
- throw new IceUtil.FileLockException(path);
- }
-
- java.nio.channels.FileLock lock;
- try
- {
- lock = channel.tryLock();
- }
- catch(Exception ex)
- {
- throw new IceUtil.FileLockException(path, ex);
- }
-
- if(lock == null)
- {
- throw new IceUtil.FileLockException(path);
- }
-
- //
- // In Windows we don't write the process pid to the file, as is not posible
- // to read the file from other process while it is locked here.
- //
- if(!System.getProperty("os.name").startsWith("Windows"))
- {
- try
- {
- //
- // Java doesn't provide get pid operation. This code
- // writes the Java VM name instead of the pid.
- //
- // The output is JVM dependent. With the Sun
- // implementation it's `pid@hostname'
- //
- try
- {
- //
- // We access java.lang.management classes using reflection
- // because these classes are not available with Android
- // Dalvik JVM.
- //
- if(!System.getProperty("java.vm.name").startsWith("Dalvik"))
- {
- Class<?> fC = IceInternal.Util.findClass("java.lang.management.ManagementFactory", null);
- Class<?> mC = IceInternal.Util.findClass("java.lang.management.RuntimeMXBean", null);
-
- Method getRuntimeMXBean = fC.getDeclaredMethod("getRuntimeMXBean", (Class<?>[])null);
-
- Method getName = mC.getDeclaredMethod("getName", (Class<?>[])null);
-
- Object mxBean = getRuntimeMXBean.invoke(null);
- _randFile.writeUTF((String)getName.invoke(mxBean));
- }
- else
- {
- //
- // In Android with Dalvik we can use android.os.Process to get the
- // process pid. That is done using reflection because it is specific
- // for Android Dalvik VM.
- //
- Class<?> pC = IceInternal.Util.findClass("android.os.Process", null);
- Method myPid = pC.getDeclaredMethod("myPid", (Class<?>[])null);
- _randFile.writeUTF(((Integer)myPid.invoke(null)).toString());
- }
- }
- catch(NoSuchMethodException ex)
- {
- }
- catch(IllegalAccessException ex)
- {
- }
- catch(java.lang.reflect.InvocationTargetException ex)
- {
- }
- //
- // Don't close _randFile here or the lock will be released. It is called
- // during release see comments there.
- //
- }
- catch(java.io.IOException ex)
- {
- release();
- throw new IceUtil.FileLockException(path, ex);
- }
- }
- }
-
- //
- // Remove the lock if it is owned by the class instance.
- //
- public void release()
- {
- if(System.getProperty("os.name").startsWith("Windows"))
- {
- if(_randFile != null)
- {
- //
- // In Windows we need to close the file handler before
- // we try to delete the file. Note that the call to close
- // also release the file lock.
- //
- try
- {
- _randFile.close();
- }
- catch(java.io.IOException ex)
- {
- }
- _randFile = null;
- }
- }
- //
- // on UNIX the call to delete remove the file and that
- // release the lock.
- //
- // In Windows the call to delete will success if at that point
- // the file has not been opened by any process.
- //
- if(_file != null)
- {
- _file.delete();
- _file = null;
- }
- }
-
- //
- // We need to keep these refrences to delete the lock file during release
- //
- private File _file;
- private RandomAccessFile _randFile;
-
-}