00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package org.objectweb.proactive.core.component.collectiveitfs;
00032
00033 import java.util.ArrayList;
00034 import java.util.Enumeration;
00035 import java.util.Hashtable;
00036
00037 import org.objectweb.proactive.ActiveObjectCreationException;
00038 import org.objectweb.proactive.ProActive;
00039 import org.objectweb.proactive.core.node.NodeException;
00040
00048 public class GatherFuturesHandlerPool {
00049 static int created = 0;
00050 static int reused = 0;
00051 static int passivated = 0;
00052 static int terminated = 0;
00053 public ArrayList<GatherFuturesHandler> pool;
00054 private static GatherFuturesHandlerPool instance = null;
00055
00056 public static GatherFuturesHandlerPool instance() {
00057 if (instance == null) {
00058 instance = new GatherFuturesHandlerPool();
00059 }
00060 return instance;
00061 }
00062
00063 private GatherFuturesHandlerPool() {
00064 expirationTime = 10000;
00065 locked = new Hashtable<GatherFuturesHandler, Long>();
00066 unlocked = new Hashtable<GatherFuturesHandler, Long>();
00067 }
00068
00069 private long expirationTime;
00070 private Hashtable<GatherFuturesHandler, Long> locked;
00071 private Hashtable<GatherFuturesHandler, Long> unlocked;
00072
00073 GatherFuturesHandler create()
00074 throws ActiveObjectCreationException, NodeException {
00075
00076 return (GatherFuturesHandler) ProActive.newActive(GatherFuturesHandler.class.getName(),
00077 new Object[] { });
00078 }
00079
00080 boolean validate(GatherFuturesHandler handler) {
00081 return true;
00082 }
00083
00084 void expire(GatherFuturesHandler handler) {
00085 ProActive.terminateActiveObject(handler, false);
00086 }
00087
00088 public synchronized GatherFuturesHandler borrowFuturesHandler()
00089 throws ActiveObjectCreationException, NodeException {
00090 long now = System.currentTimeMillis();
00091 GatherFuturesHandler handler;
00092 if (unlocked.size() > 0) {
00093 Enumeration<GatherFuturesHandler> e = unlocked.keys();
00094 while (e.hasMoreElements()) {
00095 handler = (GatherFuturesHandler) e.nextElement();
00096 if ((now - ((Long) unlocked.get(handler)).longValue()) > expirationTime) {
00097
00098 unlocked.remove(handler);
00099 expire(handler);
00100 handler = null;
00101 } else {
00102 if (validate(handler)) {
00103 unlocked.remove(handler);
00104 locked.put(handler, new Long(now));
00105
00106 return (handler);
00107 } else {
00108
00109 unlocked.remove(handler);
00110 expire(handler);
00111 handler = null;
00112 }
00113 }
00114 }
00115 }
00116
00117 handler = create();
00118 locked.put(handler, new Long(now));
00119 return (handler);
00120 }
00121
00122 public synchronized void returnFuturesHandler(GatherFuturesHandler handler) {
00123 locked.remove(handler);
00124 handler.passivate();
00125 unlocked.put(handler, new Long(System.currentTimeMillis()));
00126 }
00127
00128 }