1 /*
2 * JasperReports - Free Java Reporting Library.
3 * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4 * http://www.jaspersoft.com
5 *
6 * Unless you have purchased a commercial license agreement from Jaspersoft,
7 * the following license terms apply:
8 *
9 * This program is part of JasperReports.
10 *
11 * JasperReports is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * JasperReports is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23 */
24 package net.sf.jasperreports.engine.util;
25
26 import java.awt.Font;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.ObjectInputStream;
30 import java.io.ObjectStreamClass;
31
32 import net.sf.jasperreports.engine.JasperReportsContext;
33 import net.sf.jasperreports.engine.fonts.FontUtil;
34
35 /**
36 * A subclass of {@link ObjectInputStream} that uses
37 * {@link Thread#getContextClassLoader() the context class loader} to resolve
38 * classes encountered in the input stream.
39 *
40 * @author Lucian Chirita (lucianc@users.sourceforge.net)
41 */
42 public class ContextClassLoaderObjectInputStream extends ObjectInputStream
43 {
44 private final JasperReportsContext jasperReportsContext;
45
46 /**
47 * Creates an object input stream that reads data from the specified
48 * {@link InputStream}.
49 *
50 * @param in the input stream to read data from
51 * @throws IOException
52 * @see ObjectInputStream#ObjectInputStream(InputStream)
53 */
54 public ContextClassLoaderObjectInputStream(JasperReportsContext jasperReportsContext, InputStream in) throws IOException
55 {
56 super(in);
57
58 this.jasperReportsContext = jasperReportsContext;
59
60 try
61 {
62 enableResolveObject(true);
63 }
64 catch(SecurityException ex)
65 {
66 //FIXMEFONT we silence this for applets. but are there other similar situations that we need to deal with by signing jars?
67 }
68 }
69
70 /**
71 *
72 */
73 public JasperReportsContext getJasperReportsContext()
74 {
75 return jasperReportsContext;
76 }
77
78 /**
79 * Calls <code>super.resolveClass()</code> and in case this fails with
80 * {@link ClassNotFoundException} attempts to load the class using the
81 * context class loader.
82 */
83 @Override
84 protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException,
85 ClassNotFoundException
86 {
87 try
88 {
89 return super.resolveClass(desc);
90 }
91 catch (ClassNotFoundException e)
92 {
93 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
94 if (contextClassLoader == null)
95 {
96 throw e;
97 }
98
99 String name = desc.getName();
100 try
101 {
102 //attempt to load the class using the context class loader
103 return Class.forName(name, false, contextClassLoader);
104 }
105 catch (ClassNotFoundException e2)
106 {
107 //fallback to the original exception
108 throw e;
109 }
110 }
111 }
112
113
114 /**
115 * Checks to see if the object is an instance of <code>java.awt.Font</code>,
116 * and in case it is, it replaces it with the one looked up for in the font extensions.
117 */
118 @Override
119 protected Object resolveObject(Object obj) throws IOException
120 {
121 Font font = (obj instanceof Font) ? (Font)obj : null;
122
123 if (font != null)
124 {
125 // We use the font.getName() method here because the name field in the java.awt.Font class is the only font name related information that gets serialized,
126 // along with the size and style. The font.getFontName() and font.getFamily() both return runtime calculated values, which are not accurate in case of AWT fonts
127 // created at runtime through font extensions (both seem to return 'Dialog').
128 // For AWT fonts created from font extensions using the Font.createFont(int, InputStream), the name field is set to the same value as the font.getFontName(),
129 // which is the recommended method to get the name of an AWT font.
130 String fontName = font.getName();
131 // We load an instance of an AWT font, even if the specified font name is not available (ignoreMissingFont=true),
132 // because only third-party visualization packages such as JFreeChart (chart themes) store serialized java.awt.Font objects,
133 // and they are responsible for the drawing as well.
134 // Here we rely on the utility method ability to find a font by face name, not only family name. This is because font.getName() above returns an AWT font name,
135 // not a font family name.
136 Font newFont = FontUtil.getInstance(jasperReportsContext).getAwtFontFromBundles(fontName, font.getStyle(), font.getSize2D(), null, true);
137
138 if (newFont != null)
139 {
140 return newFont.deriveFont(font.getAttributes());
141 }
142 }
143
144 return obj;
145 }
146
147
148 }
149