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.governors;
25
26 import java.lang.reflect.InvocationTargetException;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import net.sf.jasperreports.engine.JRAbstractScriptlet;
31 import net.sf.jasperreports.engine.JRException;
32 import net.sf.jasperreports.engine.JRPropertiesUtil;
33 import net.sf.jasperreports.engine.scriptlets.DefaultScriptletFactory;
34 import net.sf.jasperreports.engine.scriptlets.ScriptletFactory;
35 import net.sf.jasperreports.engine.scriptlets.ScriptletFactoryContext;
36 import net.sf.jasperreports.engine.util.JRClassLoader;
37
38 /**
39  * @author Teodor Danciu (teodord@users.sourceforge.net)
40  */

41 public final class GovernorFactory implements ScriptletFactory
42 {
43
44     private static final GovernorFactory INSTANCE = new GovernorFactory();
45     
46     private GovernorFactory()
47     {
48     }
49     
50     /**
51      * 
52      */

53     public static GovernorFactory getInstance()
54     {
55         return INSTANCE;
56     }
57
58     @Override
59     public List<JRAbstractScriptlet> getScriplets(ScriptletFactoryContext context) throws JRException
60     {
61         List<JRAbstractScriptlet> scriptlets = new ArrayList<JRAbstractScriptlet>();
62
63         boolean maxPagesEnabled = JRPropertiesUtil.getInstance(context.getJasperReportsContext()).getBooleanProperty(context.getDataset(), MaxPagesGovernor.PROPERTY_MAX_PAGES_ENABLED, true);
64         if (maxPagesEnabled)
65         {
66             int maxPages = JRPropertiesUtil.getInstance(context.getJasperReportsContext()).getIntegerProperty(context.getDataset(), MaxPagesGovernor.PROPERTY_MAX_PAGES, 0);
67             if (maxPages > 0)
68             {
69                 scriptlets.add(new MaxPagesGovernor(maxPages));
70             }
71         }
72         
73         boolean timeoutEnabled = JRPropertiesUtil.getInstance(context.getJasperReportsContext()).getBooleanProperty(context.getDataset(), TimeoutGovernor.PROPERTY_TIMEOUT_ENABLED, true);
74         if (timeoutEnabled)
75         {
76             long timeout = JRPropertiesUtil.getInstance(context.getJasperReportsContext()).getLongProperty(context.getDataset(), TimeoutGovernor.PROPERTY_TIMEOUT, 0l);
77             if (timeout > 0)
78             {
79                 scriptlets.add(new TimeoutGovernor(timeout));
80             }
81         }
82         
83         return scriptlets;
84     }
85     
86     /**
87      *
88      */

89     protected JRAbstractScriptlet getScriptlet(String scriptletClassName) throws JRException
90     {
91         JRAbstractScriptlet scriptlet = null;
92
93         try
94         {
95             Class<?> scriptletClass = JRClassLoader.loadClassForName(scriptletClassName);    
96             scriptlet = (JRAbstractScriptlet) scriptletClass.getDeclaredConstructor().newInstance();
97         }
98         catch (ClassNotFoundException e)
99         {
100             throw 
101                 new JRException(
102                     DefaultScriptletFactory.EXCEPTION_MESSAGE_KEY_CLASS_LOADING_ERROR,
103                     new Object[]{scriptletClassName}, 
104                     e);
105         }
106         catch (NoSuchMethodException | InvocationTargetException 
107             | IllegalAccessException | InstantiationException e)
108         {
109             throw 
110                 new JRException(
111                     DefaultScriptletFactory.EXCEPTION_MESSAGE_KEY_INSTANCE_LOADING_ERROR,
112                     new Object[]{scriptletClassName}, 
113                     e);
114         }
115         
116         return scriptlet;
117     }
118     
119 }
120