View Javadoc

1    /****************************************************************************
2    * Copyright (c) 2005, 2006, 2007, 2008, 2009 Imola Informatica.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the LGPL License v2.1
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/licenses/lgpl.html
7    ****************************************************************************/
8   package it.imolinfo.jbi4corba.jbi.component;
9   
10  import java.util.LinkedList;
11  
12  import javax.xml.transform.Transformer;
13  import javax.xml.transform.TransformerConfigurationException;
14  import javax.xml.transform.TransformerFactory;
15  
16  public class TransformerPool {
17      private static final TransformerFactory cTransformerFact =
18              TransformerFactory.newInstance();
19      
20      private final LinkedList<Transformer> mTransformers;
21  
22      /** Creates a new instance of TransformerPool */
23      public TransformerPool() {
24          mTransformers = new LinkedList<Transformer>();
25      }
26      
27      public TransformerPool(int size) throws TransformerConfigurationException {
28          this();
29          for (int i = 0; i < size; ++i) {
30              mTransformers.addFirst(cTransformerFact.newTransformer());
31          }
32      }
33      
34      public Transformer retrieve() throws TransformerConfigurationException {
35          Transformer transformer = null;
36          
37          synchronized(this) {
38              if (!mTransformers.isEmpty()) {
39                  transformer = mTransformers.removeFirst();
40              } else {
41                  transformer = cTransformerFact.newTransformer();
42              }
43          }
44          return transformer;
45      }
46      
47      public boolean relinquish(Transformer transformer) {
48          boolean success = false;
49          if (transformer != null) {
50              synchronized (this) {
51                  if (!mTransformers.contains(transformer)) {
52                      mTransformers.addFirst(transformer);
53                      success = true;
54                  }
55              }
56          }
57          return success;
58      }
59  }