1 /* ServiceProviderLoadingAction.java -- Action for loading plug-in services.
2    Copyright (C) 2004  Free Software Foundation
3 
4 This file is part of GNU Classpath.
5 
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20 
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25 
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37 
38 
39 package gnu.classpath;
40 
41 import java.security.PrivilegedExceptionAction;
42 
43 /**
44  * A privileged action for creating a new instance of a service
45  * provider.
46  *
47  * <p>Class loading and instantiation is encapsulated in a
48  * <code>PriviledgedAction</code> in order to restrict the loaded
49  * service providers to the {@link java.security.AccessControlContext}
50  * that was active when {@link
51  * gnu.classpath.ServiceFactory#lookupProviders(Class, ClassLoader)} was
52  * called, even though the actual loading is delayed to the time when the
53  * provider is actually needed.
54  *
55  * @author <a href="mailto:brawer@dandelis.ch">Sascha Brawer</a>
56  */
57 final class ServiceProviderLoadingAction<P>
58   implements PrivilegedExceptionAction<P>
59 {
60   /**
61    * The interface to which the loaded service provider implementation
62    * must conform.  Usually, this is a Java interface type, but it
63    * might also be an abstract class or even a concrete class.
64    */
65   private final Class<P> spi;
66 
67 
68   /**
69    * The fully qualified name of the class that gets loaded when
70    * this action is executed.
71    */
72   private final String providerName;
73 
74 
75   /**
76    * The ClassLoader that gets used for loading the service provider
77    * class.
78    */
79   private final ClassLoader loader;
80 
81 
82   /**
83    * Constructs a privileged action for loading a service provider.
84    *
85    * @param spi the interface to which the loaded service provider
86    * implementation must conform. Usually, this is a Java interface
87    * type, but it might also be an abstract class or even a concrete
88    * superclass.
89    *
90    * @param providerName the fully qualified name of the class that
91    * gets loaded when this action is executed.
92    *
93    * @param loader the ClassLoader that gets used for loading the
94    * service provider class.
95    *
96    * @throws IllegalArgumentException if <code>spi</code>,
97    * <code>providerName</code> or <code>loader</code> is
98    * <code>null</code>.
99    */
ServiceProviderLoadingAction(Class<P> spi, String providerName, ClassLoader loader)100   ServiceProviderLoadingAction(Class<P> spi, String providerName,
101                                ClassLoader loader)
102   {
103     if (spi == null || providerName == null || loader == null)
104       throw new IllegalArgumentException();
105 
106     this.spi = spi;
107     this.providerName = providerName;
108     this.loader = loader;
109   }
110 
111 
112   /**
113    * Loads an implementation class for a service provider, and creates
114    * a new instance of the loaded class by invoking its public
115    * no-argument constructor.
116    *
117    * @return a new instance of the class whose name was passed as
118    * <code>providerName</code> to the constructor.
119    *
120    * @throws ClassCastException if the service provider does not
121    * implement the <code>spi</code> interface that was passed to the
122    * constructor.
123    *
124    * @throws IllegalAccessException if the service provider class or
125    * its no-argument constructor are not <code>public</code>.
126    *
127    * @throws InstantiationException if the service provider class is
128    * <code>abstract</code>, an interface, a primitive type, an array
129    * class, or void; or if service provider class does not have a
130    * no-argument constructor; or if there some other problem with
131    * creating a new instance of the service provider.
132    */
run()133   public P run()
134     throws Exception
135   {
136     Class<P> loadedClass;
137     P serviceProvider;
138 
139     loadedClass = (Class<P>) loader.loadClass(providerName);
140     serviceProvider = loadedClass.newInstance();
141 
142     // Ensure that the loaded provider is actually implementing
143     // the service provider interface.
144     if (!spi.isInstance(serviceProvider))
145       throw new ClassCastException(spi.getName());
146 
147     return serviceProvider;
148   }
149 }
150