001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.geronimo.kernel.util;
018
019 import java.util.List;
020 import java.lang.ref.WeakReference;
021 import java.util.ArrayList;
022
023
024 public class ClassLoaderRegistry {
025 private final static List<WeakReference> list = new ArrayList<WeakReference>();
026
027 public synchronized static List getList(){
028 List<ClassLoader> ret = new ArrayList<ClassLoader>();
029 for(int i=0;i<list.size();i++)
030 if(list.get(i) != null) {
031 ret.add((ClassLoader)list.get(i).get());
032 }
033 else {
034 list.remove(i);
035 }
036 return ret;
037 }
038
039 public synchronized static boolean add(ClassLoader cloader){
040 if(contains(cloader)) {
041 return false;
042 }
043 return list.add(new WeakReference<ClassLoader>(cloader));
044 }
045
046 public synchronized static boolean contains(ClassLoader cloader){
047 for(int i=0;i<list.size();i++) {
048 WeakReference wk = list.get(i);
049 if(wk.get() == null) {
050 list.remove(i);
051 }
052 else if(wk.get().equals(cloader))
053 return true;
054 }
055 return false;
056 }
057
058 public synchronized static boolean remove(ClassLoader cloader){
059 boolean result = false;
060 for(int i=0;i<list.size();i++){
061 WeakReference wk = list.get(i);
062 if(wk.get() == null)list.remove(i);
063 else if(wk.get().equals(cloader)){
064 list.remove(i);
065 result = true;
066 }
067 }
068 return result;
069 }
070 }