Coverage Report

##### Coverage Report - org.webmacro.util.Instantiator

Classes in this File | Line Coverage | Branch Coverage | Complexity
Instantiator |
0% | 0/61
0% | 0/34
| 5.166666666666667;5.167

1 |  |
     /*
2 |  |
      * Copyright (C) 1998-2005 Semiotek Inc.  All Rights Reserved.
3 |  |
      *
4 |  |
      * Redistribution and use in source and binary forms, with or without
5 |  |
      * modification, are permitted under the terms of either of the following
6 |  |
      * Open Source licenses:
7 |  |
      *
8 |  |
      * The GNU General Public License, version 2, or any later version, as
9 |  |
      * published by the Free Software Foundation
10 |  |
      * (http://www.fsf.org/copyleft/gpl.html);
11 |  |
      *
12 |  |
      *  or
13 |  |
      *
14 |  |
      * The Semiotek Public License (http://webmacro.org/LICENSE.)
15 |  |
      *
16 |  |
      * This software is provided "as is", with NO WARRANTY, not even the
17 |  |
      * implied warranties of fitness to purpose, or merchantability. You
18 |  |
      * assume all risks and liabilities associated with its use.
19 |  |
      *
20 |  |
      * See www.webmacro.org for more information on the WebMacro project.
21 |  |
      */
22 |  |
23 |  |
     package org.webmacro.util;
24 |  |
25 |  |
     import java.util.Arrays;
26 |  |
     import java.util.Collections;
27 |  |
     import java.util.List;
28 |  |
29 |  |
     import org.slf4j.Logger;
30 |  |
     import org.slf4j.LoggerFactory;
31 |  |
     import org.webmacro.Broker;
32 |  |
     import org.webmacro.WebMacroException;
33 |  |
34 |  |
     /**
35 |  |
      * Utility class for loading and instantiating classes subject
36 |  |
      * to package restrictions specified by the AllowedPackages property,
37 |  |
      * and using implicit package names specified by the ImpliedPackages
38 |  |
      * configuration property.
39 |  |
      *
40 |  |
      * Used by BeanDirective and SetpropsDirective.
41 |  |
      *
42 |  |
      * NOTE If AllowedPackages is empty then all packages are allowed.
43 |  |
      *
44 |  |
      * @author Keats Kirsch
45 |  |
      * @see org.webmacro.directive.BeanDirective
46 |  |
      * @see org.webmacro.directive.SetpropsDirective
47 |  |
      */
48 |  |
     final public class Instantiator
49 |  |
     {
50 | 0 |
         static Logger _log =  LoggerFactory.getLogger(Instantiator.class);
51 |  |
52 |  |
        private static final String IMPLIED_PACKAGES = "ImpliedPackages";
53 |  |
54 |  |
        private static final String ALLOWED_PACKAGES = "AllowedPackages";
55 |  |
56 |  |
        private static final String DEPRECATED_IMPLIED_PACKAGES = "BeanDirective.ImpliedPackages";
57 |  |
58 |  |
        private static final String DEPRECATED_ALLOWED_PACKAGES = "BeanDirective.AllowedPackages";
59 |  |
60 |  |
        private static final String INSTANTIATOR_KEY = "org.webmacro.util.Instantiator";
61 |  |
62 |  |
        private List _impliedPackages;
63 |  |
64 |  |
        private List _allowedPackages;
65 |  |
66 |  |
        private Broker _broker;
67 |  |
68 |  |
        private Instantiator(Broker b)
69 | 0 |
        {
70 | 0 |
           _broker = b;
71 | 0 |
           String s = b.getSetting(IMPLIED_PACKAGES);
72 | 0 |
           if (s == null)
73 |  |
           {
74 | 0 |
              s = b.getSetting(DEPRECATED_IMPLIED_PACKAGES);
75 | 0 |
              if (s != null)
76 |  |
              {
77 | 0 |
                 _log.warn(
78 |  |
                          "The configuration parameter \""
79 |  |
                                   + DEPRECATED_IMPLIED_PACKAGES
80 |  |
                                   + "\" has been deprecated! Use \""
81 |  |
                                   + IMPLIED_PACKAGES + "\" instead.");
82 |  |
              }
83 |  |
           }
84 | 0 |
           if (s == null)
85 |  |
           {
86 | 0 |
              _impliedPackages = Collections.EMPTY_LIST;
87 |  |
           }
88 |  |
           else
89 |  |
           {
90 | 0 |
              _impliedPackages = Arrays.asList(org.webmacro.servlet.TextTool.split(
91 |  |
                       s, ","));
92 |  |
           }
93 |  |
94 |  |
           // get allowed packages setting
95 | 0 |
           s = b.getSetting(ALLOWED_PACKAGES);
96 | 0 |
           if (s == null)
97 |  |
           {
98 | 0 |
              s = b.getSetting(DEPRECATED_ALLOWED_PACKAGES);
99 | 0 |
              if (s != null)
100 |  |
              {
101 | 0 |
                 _log.warn(
102 |  |
                          "The configuration parameter \""
103 |  |
                                   + DEPRECATED_ALLOWED_PACKAGES
104 |  |
                                   + "\" has been deprecated! Use \""
105 |  |
                                   + ALLOWED_PACKAGES + "\" instead.");
106 |  |
              }
107 |  |
           }
108 | 0 |
           if (s == null)
109 |  |
           {
110 | 0 |
              _allowedPackages = Collections.EMPTY_LIST;
111 |  |
           }
112 |  |
           else
113 |  |
           {
114 | 0 |
              _allowedPackages = Arrays.asList(org.webmacro.servlet.TextTool.split(
115 |  |
                       s, ","));
116 |  |
           }
117 | 0 |
        }
118 |  |
119 |  |
        public List getImpliedPackages()
120 |  |
        {
121 | 0 |
           return _impliedPackages;
122 |  |
        }
123 |  |
124 |  |
        public List getAllowedPackages()
125 |  |
        {
126 | 0 |
           return _allowedPackages;
127 |  |
        }
128 |  |
129 |  |
        public Class classForName(String className) throws WebMacroException
130 |  |
        {
131 | 0 |
           Class c = null;
132 | 0 |
           if (className.indexOf('.') >= 0) // it is a fully specified class name
133 |  |
           {
134 |  |
              try
135 |  |
              {
136 | 0 |
                 c = _broker.classForName(className);
137 |  |
              }
138 | 0 |
              catch (ClassNotFoundException e)
139 |  |
              {
140 | 0 |
                      throw new WebMacroException("Unable to load class " + className, e);
141 | 0 |
              }
142 |  |
           }
143 |  |
           else
144 |  |
           {  // try with implied packages prepended
145 | 0 |
              ClassNotFoundException exception = null;
146 |  |
147 | 0 |
              for (int i = 0; i < _impliedPackages.size(); i++)
148 |  |
              {
149 | 0 |
                 String s = (String) _impliedPackages.get(i);
150 |  |
                 try
151 |  |
                 {
152 | 0 |
                    c = _broker.classForName(s + "." + className);
153 | 0 |
                    break;
154 |  |
                 }
155 | 0 |
                 catch (ClassNotFoundException e)
156 |  |
                 {
157 | 0 |
                         exception = e;
158 |  |
                 }
159 |  |
              }
160 | 0 |
              if (c == null) {
161 | 0 |
                      if (exception == null)
162 | 0 |
                              throw new WebMacroException("Unable to load class " + className +
163 |  |
                                              ", property " + IMPLIED_PACKAGES + " contains " +
164 |  |
                                              _impliedPackages.size() + " items");
165 |  |
                      else
166 | 0 |
                              throw new WebMacroException("Unable to load class " + className, exception);
167 |  |
              }
168 |  |
           }
169 |  |
170 | 0 |
           if (!_allowedPackages.isEmpty())
171 |  |
           {
172 |  |
              // check if class is in a permitted package
173 | 0 |
              String pkg = c.getPackage().getName();
174 | 0 |
              if (!_allowedPackages.contains(pkg))
175 |  |
              {
176 | 0 |
                 throw new WebMacroException(
177 |  |
                          "You are not permitted to load classes from this package ("
178 |  |
                                   + pkg + ").  Check the \"" + ALLOWED_PACKAGES
179 |  |
                                   + "\" parameter in the WebMacro configuration.");
180 |  |
              }
181 |  |
           }
182 |  |
           // else allowed packages not specified so all allowed
183 |  |
184 | 0 |
           return c;
185 |  |
        }
186 |  |
187 |  |
        public Object instantiate(Class c, Object[] args) throws Exception
188 |  |
        {
189 | 0 |
           Object o = null;
190 | 0 |
           if (args == null)
191 |  |
           {
192 | 0 |
              o = c.newInstance();
193 |  |
           }
194 |  |
           else
195 |  |
           {
196 | 0 |
              java.lang.reflect.Constructor[] cons = c.getConstructors();
197 | 0 |
              for (int i = 0; i < cons.length; i++)
198 |  |
              {
199 | 0 |
                 if (cons[i].getParameterTypes().length == args.length)
200 |  |
                 {
201 |  |
                    // try to instantiate using this constructor
202 |  |
                    try
203 |  |
                    {
204 | 0 |
                       o = cons[i].newInstance(args);
205 | 0 |
                       break; // if successful, we're done!
206 |  |
                    }
207 | 0 |
                    catch (Exception e)
208 |  |
                    {
209 |  |
                       // ignore
210 |  |
                    }
211 |  |
                 }
212 |  |
              }
213 | 0 |
              if (o == null)
214 |  |
              {
215 | 0 |
                 throw new InstantiationException(
216 |  |
                          "Unable to construct object of type " + c.getName()
217 |  |
                                   + " using the supplied arguments: "
218 |  |
                                   + java.util.Arrays.asList(args).toString());
219 |  |
              }
220 |  |
           }
221 | 0 |
           return o;
222 |  |
        }
223 |  |
224 |  |
        synchronized public static Instantiator getInstance(Broker b)
225 |  |
        {
226 | 0 |
           Instantiator instantiator = (Instantiator) b
227 |  |
                    .getBrokerLocal(INSTANTIATOR_KEY);
228 | 0 |
           if (instantiator == null)
229 |  |
           {
230 | 0 |
              instantiator = new Instantiator(b);
231 | 0 |
              b.setBrokerLocal(INSTANTIATOR_KEY, instantiator);
232 |  |
           }
233 | 0 |
           return instantiator;
234 |  |
        }
235 |  |
     }
236 |  |