KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > design > JRAbstractJavaCompiler


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
29 /*
30  * Contributors:
31  * Peter Severin - peter_p_s@users.sourceforge.net
32  */

33 package net.sf.jasperreports.engine.design;
34
35 import java.io.Serializable JavaDoc;
36 import java.util.Map JavaDoc;
37
38 import org.apache.commons.collections.ReferenceMap;
39
40 import net.sf.jasperreports.engine.JRException;
41 import net.sf.jasperreports.engine.fill.JREvaluator;
42 import net.sf.jasperreports.engine.util.JRClassLoader;
43
44
45 /**
46  * @author Teodor Danciu (teodord@users.sourceforge.net)
47  * @version $Id: JRAbstractJavaCompiler.java 1289 2006-06-13 14:54:52 +0300 (Tue, 13 Jun 2006) teodord $
48  */

49 public abstract class JRAbstractJavaCompiler extends JRAbstractCompiler
50 {
51
52     // @JVM Crash workaround
53
// Reference to the loaded class class in a per thread map
54
private static ThreadLocal JavaDoc classFromBytesRef = new ThreadLocal JavaDoc();
55
56
57     private static Object JavaDoc CLASS_CACHE_NULL_KEY = new Object JavaDoc();
58     private static Map JavaDoc classCache = new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.SOFT);
59
60     
61     protected JRAbstractJavaCompiler(boolean needsSourceFiles)
62     {
63         super(needsSourceFiles);
64     }
65
66
67     protected JREvaluator loadEvaluator(Serializable JavaDoc compileData, String JavaDoc className) throws JRException
68     {
69         JREvaluator evaluator = null;
70
71         try
72         {
73             Class JavaDoc clazz = getClassFromCache(className);
74             if (clazz == null)
75             {
76                 clazz = JRClassLoader.loadClassFromBytes(className, (byte[]) compileData);
77                 putClassInCache(className, clazz);
78             }
79             
80             //FIXME multiple classes per thread?
81
classFromBytesRef.set(clazz);
82         
83             evaluator = (JREvaluator) clazz.newInstance();
84         }
85         catch (Exception JavaDoc e)
86         {
87             throw new JRException("Error loading expression class : " + className, e);
88         }
89         
90         return evaluator;
91     }
92     
93     
94     protected static Object JavaDoc classCacheKey()
95     {
96         ClassLoader JavaDoc contextClassLoader = Thread.currentThread().getContextClassLoader();
97         Object JavaDoc key = contextClassLoader == null ? CLASS_CACHE_NULL_KEY : contextClassLoader;
98         return key;
99     }
100
101     
102     protected static synchronized Class JavaDoc getClassFromCache(String JavaDoc className)
103     {
104         Object JavaDoc key = classCacheKey();
105         Map JavaDoc contextMap = (Map JavaDoc) classCache.get(key);
106         Class JavaDoc cachedClass = null;
107         if (contextMap != null)
108         {
109             cachedClass = (Class JavaDoc) contextMap.get(className);
110         }
111         return cachedClass;
112     }
113
114
115     protected static synchronized void putClassInCache(String JavaDoc className, Class JavaDoc loadedClass)
116     {
117         Object JavaDoc key = classCacheKey();
118         Map JavaDoc contextMap = (Map JavaDoc) classCache.get(key);
119         if (contextMap == null)
120         {
121             contextMap = new ReferenceMap(ReferenceMap.HARD, ReferenceMap.SOFT);
122             classCache.put(key, contextMap);
123         }
124         contextMap.put(className, loadedClass);
125     }
126 }
127
Popular Tags