org/objectweb/proactive/core/gc/GarbageCollector.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.gc;
00032 
00033 import org.apache.log4j.Logger;
00034 
00035 import org.objectweb.proactive.core.UniqueID;
00036 import org.objectweb.proactive.core.body.AbstractBody;
00037 import org.objectweb.proactive.core.body.LocalBodyStore;
00038 import org.objectweb.proactive.core.body.UniversalBody;
00039 import org.objectweb.proactive.core.body.proxy.UniversalBodyProxy;
00040 import org.objectweb.proactive.core.util.log.Loggers;
00041 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00042 
00043 import java.util.Collection;
00044 import java.util.LinkedList;
00045 import java.util.List;
00046 import java.util.ListIterator;
00047 
00048 
00052 public class GarbageCollector {
00053     private static final Logger logger = ProActiveLogger.getLogger(Loggers.GC);
00054 
00058     private final List<Proxy> proxies;
00059 
00063     public GarbageCollector(AbstractBody body) {
00064         this.proxies = new LinkedList<Proxy>();
00065     }
00066 
00071     private void strongReferenceProxies() {
00072         int nrRemoved = 0;
00073 
00074         for (ListIterator<Proxy> iter = this.proxies.listIterator();
00075                 iter.hasNext();) {
00076             Proxy p = iter.next();
00077 
00078             if (!p.setStrong()) {
00079                 iter.remove();
00080                 nrRemoved++;
00081             }
00082         }
00083 
00084         if (nrRemoved != 0) {
00085             logger.debug("Removed " + nrRemoved + " proxies");
00086         }
00087     }
00088 
00093     private void weakReferenceProxies() {
00094         for (Proxy p : this.proxies) {
00095             p.setWeak();
00096         }
00097     }
00098 
00099     public synchronized void addProxy(UniversalBodyProxy proxy) {
00100         proxies.add(new Proxy(proxy));
00101         logger.debug("New proxy");
00102     }
00103 
00104     public static String getDgcState(UniqueID bodyID) {
00105         UniversalBody body = LocalBodyStore.getInstance().getLocalBody(bodyID);
00106 
00107         if (body == null) {
00108             logger.error("Body " + bodyID + " not found");
00109 
00110             return "Body not found";
00111         }
00112 
00113         return "DGC";
00114     }
00115 
00116     public Collection<UniversalBodyProxy> getReferences() {
00117         Collection<UniversalBodyProxy> refs = new LinkedList<UniversalBodyProxy>();
00118         strongReferenceProxies();
00119 
00120         for (Proxy p : this.proxies) {
00121             refs.add(p.getStrong());
00122         }
00123 
00124         weakReferenceProxies();
00125 
00126         return refs;
00127     }
00128 
00129     public static boolean isBuildingTopology() {
00130         return "true".equals(System.getProperty("proactive.build_topology"));
00131     }
00132 }

Generated on Mon Jan 22 15:16:08 2007 for ProActive by  doxygen 1.5.1