KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > util > JRSingletonCache


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.util;
29
30 import net.sf.jasperreports.engine.JRException;
31
32 import org.apache.commons.collections.ReferenceMap;
33
34
35 /**
36  * Utility to use as a soft cache of singleton instances.
37  *
38  * @author Lucian Chirita (lucianc@users.sourceforge.net)
39  * @version $Id: JRSingletonCache.java 1245 2006-04-28 11:23:04 +0300 (Fri, 28 Apr 2006) lucianc $
40  */

41 public class JRSingletonCache
42 {
43     private final ReferenceMap cache;
44     private final Class JavaDoc itf;
45
46     /**
47      * Creates a cache of singleton instances.
48      *
49      * @param itf a interface or class that should be implemented by all classes cached by this object
50      */

51     public JRSingletonCache(Class JavaDoc itf)
52     {
53         cache = new ReferenceMap();
54         this.itf = itf;
55     }
56
57     /**
58      * Returns the singleton instance corresponding to a class.
59      * <p>
60      * The instance is first searched into the cache and created if not found.
61      * <p>
62      * The class is expected to have a no-argument constructor.
63      *
64      * @param className
65      * @return the singleton instance corresponding to a class
66      * @throws JRException
67      */

68     public synchronized Object JavaDoc getCachedInstance(String JavaDoc className) throws JRException
69     {
70         Object JavaDoc instance = cache.get(className);
71         if (instance == null)
72         {
73             try
74             {
75                 Class JavaDoc clazz = JRClassLoader.loadClassForName(className);
76                 if (itf != null && !itf.isAssignableFrom(clazz))
77                 {
78                     throw new JRException("Class \"" + className + "\" should be compatible with \"" + itf.getName() + "\"");
79                 }
80
81                 instance = clazz.newInstance();
82             }
83             catch (ClassNotFoundException JavaDoc e)
84             {
85                 throw new JRException("Class " + className + " not found.", e);
86             }
87             catch (InstantiationException JavaDoc e)
88             {
89                 throw new JRException("Error instantiating class " + className + ".", e);
90             }
91             catch (IllegalAccessException JavaDoc e)
92             {
93                 throw new JRException("Error instantiating class " + className + ".", e);
94             }
95             
96             cache.put(className, instance);
97         }
98         return instance;
99     }
100 }
101
Popular Tags