001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: ObjectPool.java 475981 2006-11-16 23:35:53Z minchau $
020     */
021    package org.apache.xml.utils;
022    
023    import java.util.ArrayList;
024    
025    import org.apache.xml.res.XMLErrorResources;
026    import org.apache.xml.res.XMLMessages;
027    
028    
029    /**
030     * Pool of object of a given type to pick from to help memory usage
031     * @xsl.usage internal
032     */
033    public class ObjectPool implements java.io.Serializable
034    {
035        static final long serialVersionUID = -8519013691660936643L;
036    
037      /** Type of objects in this pool.
038       *  @serial          */
039      private final Class objectType;
040    
041      /** Stack of given objects this points to.
042       *  @serial          */
043      private final ArrayList freeStack;
044    
045      /**
046       * Constructor ObjectPool
047       *
048       * @param type Type of objects for this pool
049       */
050      public ObjectPool(Class type)
051      {
052        objectType = type;
053        freeStack = new ArrayList();
054      }
055      
056      /**
057       * Constructor ObjectPool
058       *
059       * @param className Fully qualified name of the type of objects for this pool.
060       */
061      public ObjectPool(String className)
062      {
063        try
064        {
065          objectType = ObjectFactory.findProviderClass(
066            className, ObjectFactory.findClassLoader(), true);
067        }
068        catch(ClassNotFoundException cnfe)
069        {
070          throw new WrappedRuntimeException(cnfe);
071        }
072        freeStack = new ArrayList();
073      }
074    
075    
076      /**
077       * Constructor ObjectPool
078       *
079       *
080       * @param type Type of objects for this pool
081       * @param size Size of vector to allocate
082       */
083      public ObjectPool(Class type, int size)
084      {
085        objectType = type;
086        freeStack = new ArrayList(size);
087      }
088    
089      /**
090       * Constructor ObjectPool
091       *
092       */
093      public ObjectPool()
094      {
095        objectType = null;
096        freeStack = new ArrayList();
097      }
098    
099      /**
100       * Get an instance of the given object in this pool if available
101       *
102       *
103       * @return an instance of the given object if available or null
104       */
105      public synchronized Object getInstanceIfFree()
106      {
107    
108        // Check if the pool is empty.
109        if (!freeStack.isEmpty())
110        {
111    
112          // Remove object from end of free pool.
113          Object result = freeStack.remove(freeStack.size() - 1);
114          return result;
115        }
116    
117        return null;
118      }
119    
120      /**
121       * Get an instance of the given object in this pool 
122       *
123       *
124       * @return An instance of the given object
125       */
126      public synchronized Object getInstance()
127      {
128    
129        // Check if the pool is empty.
130        if (freeStack.isEmpty())
131        {
132    
133          // Create a new object if so.
134          try
135          {
136            return objectType.newInstance();
137          }
138          catch (InstantiationException ex){}
139          catch (IllegalAccessException ex){}
140    
141          // Throw unchecked exception for error in pool configuration.
142          throw new RuntimeException(XMLMessages.createXMLMessage(XMLErrorResources.ER_EXCEPTION_CREATING_POOL, null)); //"exception creating new instance for pool");
143        }
144        else
145        {
146    
147          // Remove object from end of free pool.
148          Object result = freeStack.remove(freeStack.size() - 1);
149    
150          return result;
151        }
152      }
153    
154      /**
155       * Add an instance of the given object to the pool  
156       *
157       *
158       * @param obj Object to add.
159       */
160      public synchronized void freeInstance(Object obj)
161      {
162    
163        // Make sure the object is of the correct type.
164        // Remove safety.  -sb
165        // if (objectType.isInstance(obj))
166        // {
167        freeStack.add(obj);
168        // }
169        // else
170        // {
171        //  throw new IllegalArgumentException("argument type invalid for pool");
172        // }
173      }
174    }