1 16 19 package com.sun.org.apache.xml.internal.utils; 20 21 import java.util.Vector ; 22 23 import com.sun.org.apache.xml.internal.res.XMLErrorResources; 24 import com.sun.org.apache.xml.internal.res.XMLMessages; 25 26 27 31 public class ObjectPool implements java.io.Serializable 32 { 33 34 36 private final Class objectType; 37 38 40 private final Vector freeStack; 41 42 47 public ObjectPool(Class type) 48 { 49 objectType = type; 50 freeStack = new Vector (); 51 } 52 53 58 public ObjectPool(String className) 59 { 60 try 61 { 62 objectType = ObjectFactory.findProviderClass( 63 className, ObjectFactory.findClassLoader(), true); 64 } 65 catch(ClassNotFoundException cnfe) 66 { 67 throw new WrappedRuntimeException(cnfe); 68 } 69 freeStack = new Vector (); 70 } 71 72 73 80 public ObjectPool(Class type, int size) 81 { 82 objectType = type; 83 freeStack = new Vector (size); 84 } 85 86 90 public ObjectPool() 91 { 92 objectType = null; 93 freeStack = new Vector (); 94 } 95 96 102 public synchronized Object getInstanceIfFree() 103 { 104 105 if (!freeStack.isEmpty()) 107 { 108 109 Object result = freeStack.lastElement(); 111 112 freeStack.setSize(freeStack.size() - 1); 113 114 return result; 115 } 116 117 return null; 118 } 119 120 126 public synchronized Object getInstance() 127 { 128 129 if (freeStack.isEmpty()) 131 { 132 133 try 135 { 136 return objectType.newInstance(); 137 } 138 catch (InstantiationException ex){} 139 catch (IllegalAccessException ex){} 140 141 throw new RuntimeException (XMLMessages.createXMLMessage(XMLErrorResources.ER_EXCEPTION_CREATING_POOL, null)); } 144 else 145 { 146 147 Object result = freeStack.lastElement(); 149 150 freeStack.setSize(freeStack.size() - 1); 151 152 return result; 153 } 154 } 155 156 162 public synchronized void freeInstance(Object obj) 163 { 164 165 freeStack.addElement(obj); 170 } 176 } 177 | Popular Tags |