View Javadoc

1   /*$Id: QuerierPluginFactory.java,v 1.2 2005/03/21 18:45:55 mch Exp $
2    * Created on 24-Sep-2003
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid
7    * Software License version 1.2, a copy of which has been included
8    * with this distribution in the LICENSE.txt file.
9    *
10   **/
11  package org.astrogrid.dataservice.queriers;
12  
13  import java.lang.reflect.Constructor;
14  import java.lang.reflect.InvocationTargetException;
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  import org.astrogrid.cfg.ConfigFactory;
18  
19  /*** Manages a single query; this is the root for a chain of possible chain
20   * of plugins, translators, etc. It makes the right kind of querier for the job,
21   * and fields any things that are not directly
22   * to do with the actual querying process
23   *
24   */
25  public class QuerierPluginFactory {
26     
27     private static final Log log = LogFactory.getLog(QuerierPluginFactory.class);
28     
29     /*** Key to which plugin (which sublcass of Querier) to use */
30     public static final String QUERIER_PLUGIN_KEY = "datacenter.querier.plugin";
31     
32      /***
33      * Creates a plugin from the configuration file details for the given querier
34      */
35     public static QuerierPlugin createPlugin(Querier aQuerier) throws QuerierPluginException {
36        return instantiatePlugin(aQuerier);
37     }
38     
39     
40     
41     /*** Instantiates the class with the given name.  This is useful for things
42      * such as 'plugins', where a class name might be given in a configuration file.
43      * Rather messily throws Throwable because anything might have
44      * gone wrong in the constructor.
45      */
46     public static Object instantiate(String classname) throws Throwable {
47        
48        try {
49           Class qClass = Class.forName(classname);
50  
51           /* NWW - interesting bug here.
52            original code used class.newInstance(); this method doesn't declare it throws InvocationTargetException,
53            however, this exception _is_ thrown if an exception is thrown by the constructor (as is often the case at the moment)
54            worse, as InvocatioinTargetException is a checked exception, the compiler rejects code with a catch clause for
55            invocationTargetExcetpion - as it thinks it cannot be thrown.
56            this means the exception boils out of the code, and is unstoppable - dodgy
57            work-around - use the equivalent methods on java.lang.reflect.Constructor - which do throw the correct exceptions */
58  
59           Constructor constr = qClass.getConstructor(new Class[] { });
60           return constr.newInstance(new Object[] { } );
61           
62        } catch (InvocationTargetException e) {
63           // interested in the root cause here - invocation target is just a wrapper, and not meaningful in itself.
64           throw e.getCause();
65        }
66     }
67     
68     /*** Instantiates the querier given in the configuration file
69      */
70     public static QuerierPlugin instantiatePlugin(Querier querier) throws QuerierPluginException {
71        
72        //'default' org.astrogrid.datacenter.queriers.test.PrecannedPlugin should be given in default config, not here.
73        String querierClass = ConfigFactory.getCommonConfig().getString(QUERIER_PLUGIN_KEY);
74  
75        try {
76           Class qClass = Class.forName(querierClass);
77  
78           /* NWW - interesting bug here.
79            original code used class.newInstance(); this method doesn't declare it throws InvocationTargetException,
80            however, this exception _is_ thrown if an exception is thrown by the constructor (as is often the case at the moment)
81            worse, as InvocatioinTargetException is a checked exception, the compiler rejects code with a catch clause for
82            invocationTargetExcetpion - as it thinks it cannot be thrown.
83            this means the exception boils out of the code, and is unstoppable - dodgy
84            work-around - use the equivalent methods on java.lang.reflect.Constructor - which do throw the correct exceptions */
85  
86  //         Constructor constr = qClass.getConstructor(new Class[] {Querier.class});
87  //         return (QuerierPlugin) constr.newInstance(new Object[] {querier});
88           Constructor constr = qClass.getConstructor(new Class[] {});
89           return (QuerierPlugin) constr.newInstance(new Object[] {});
90        }
91        catch (ClassNotFoundException cnfe) {
92           throw new QuerierPluginException("Server not configured properly: plugin '"+querierClass+"' not found", cnfe);
93        }
94        catch (ClassCastException cce) {
95           throw new QuerierPluginException("Server not configured properly: plugin '"+querierClass+"' is not a Querier subclass", cce);
96        }
97        catch (InvocationTargetException e) {
98           throw new QuerierPluginException("Querier '"+querierClass+"' constructor failed: "+e, e.getCause());
99        }
100       catch (Exception e) {
101          throw new QuerierPluginException("Could not load Querier '"+querierClass+"': "+e,e);
102       }
103    }
104    
105    
106    
107 }
108 
109 /*
110  $Log: QuerierPluginFactory.java,v $
111  Revision 1.2  2005/03/21 18:45:55  mch
112  Naughty big lump of changes
113 
114  Revision 1.1.1.1  2005/02/17 18:37:35  mch
115  Initial checkin
116 
117  Revision 1.1.1.1  2005/02/16 17:11:24  mch
118  Initial checkin
119 
120  Revision 1.5  2004/10/18 13:11:30  mch
121  Lumpy Merge
122 
123  Revision 1.4.2.1  2004/10/15 19:59:05  mch
124  Lots of changes during trip to CDS to improve int test pass rate
125 
126  Revision 1.4  2004/10/08 17:14:22  mch
127  Clearer separation of metadata and querier plugins, and improvements to VoResource plugin mechanisms
128 
129  Revision 1.3  2004/10/08 15:17:20  mch
130  Removed unnecessary imports
131 
132  Revision 1.2  2004/09/28 16:38:05  mch
133  Removed 4.1 interface
134 
135  Revision 1.1  2004/09/28 15:02:13  mch
136  Merged PAL and server packages
137 
138  Revision 1.2  2004/03/13 23:38:46  mch
139  Test fixes and better front-end JSP access
140 
141  Revision 1.1  2004/03/12 04:45:26  mch
142  It05 MCH Refactor
143 
144  */