KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joseki > server > module > Loader


1 /*
2  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 package org.joseki.server.module;
7
8 import org.apache.commons.logging.* ;
9
10 import org.joseki.vocabulary.JosekiModule ;
11 import org.joseki.util.PrintUtils ;
12
13 import com.hp.hpl.jena.rdf.model.* ;
14 import com.hp.hpl.jena.shared.* ;
15
16 /**
17  * Load classes and instantiate new objects based on loadable classes.
18  * Understands the RDF properties for naming and initializing a new instance.
19  *
20  * @author Andy Seaborne
21  * @version $Id: Loader.java,v 1.9 2004/04/30 14:13:14 andy_seaborne Exp $
22  */

23
24 public class Loader
25 {
26     private static Log logger = LogFactory.getLog(Loader.class.getName());
27
28     protected static ClassLoader JavaDoc classLoader = chooseClassLoader() ;
29
30     public Loader()
31     {
32     }
33  
34     public Loadable loadAndInstantiate(Resource bindingResource, Class JavaDoc expectedType)
35     {
36         logger.debug("Attempt to load: "+PrintUtils.fmt(bindingResource)) ;
37         
38         Resource implementation = null ;
39         try
40         {
41             // Alternative: pass in a top level resource and do ...
42
// There can be many bindings
43
//bindingResource = thing.getProperty(JosekiModule.binding).getResource() ;
44

45             // Should be only one.
46
implementation = bindingResource
47                                 .getProperty(JosekiModule.implementation)
48                                 .getResource() ;
49             logger.trace("Implementation: "+PrintUtils.fmt(implementation)) ;
50
51         } catch (JenaException ex)
52         {
53             logger.warn("Binding/Implementation structure incorrect") ;
54             return null ;
55         }
56         catch (NullPointerException JavaDoc nullEx)
57         {
58             logger.warn("No definition for "+PrintUtils.fmt(bindingResource)) ;
59             return null ;
60         }
61
62         String JavaDoc className = "<<unset>>" ;
63         try {
64             className = implementation.getRequiredProperty(JosekiModule.className).getString();
65             if ( className == null )
66             {
67                 // This should not happen as we used "getRequiredProperty"
68
logger.warn("Class name not found" ) ;
69                 return null ;
70             }
71             logger.trace("Class name: "+className) ;
72         } catch (PropertyNotFoundException noPropEx)
73         {
74             logger.warn("No property 'className'") ;
75             return null ;
76         }
77         
78         try {
79             logger.trace("Load module: " + className);
80             Class JavaDoc classObj = classLoader.loadClass(className);
81             if ( classObj == null )
82             {
83                 logger.warn("Null return from classloader");
84                 return null ;
85             }
86             logger.debug("Loaded: "+className) ;
87             
88             if ( ! Loadable.class.isAssignableFrom(classObj) )
89             {
90                 logger.warn(className + " does not support interface Loadable" ) ;
91                 return null;
92             }
93
94             Loadable module = (Loadable)classObj.newInstance();
95             logger.trace("New Instance created") ;
96             
97             Statement s = bindingResource.getProperty(JosekiModule.interface_) ;
98             if ( s == null || s.getResource() == null)
99             {
100                 logger.warn("No 'joseki:interface' property or value not a resource for "+PrintUtils.fmt(bindingResource)) ;
101                 return null ;
102             }
103             
104             String JavaDoc uriInterface = s.getResource().getURI() ;
105             
106             if ( uriInterface == null || ! module.getInterfaceURI().equals(uriInterface) )
107             {
108                 if ( uriInterface == null )
109                 {
110                     logger.warn("No declared interface URI : expected "+module.getInterfaceURI()) ;
111                     return null ;
112                 }
113                 logger.warn("Mismatch between expected and actual operation URIs: "+
114                         "Expected: "+uriInterface+" : Actual: "+module.getInterfaceURI() ) ;
115                 return null ;
116             }
117
118             if ( expectedType != null && ! expectedType.isInstance(module) )
119             {
120                 logger.warn(" " + className + " is not of class "+expectedType.getName()) ;
121                 return null;
122             }
123
124             // Looks good - now initialize it.
125
module.init(bindingResource, implementation) ;
126
127             //logger.debug(" Class: " + className);
128
logger.debug("Module: " + uriInterface) ;
129             logger.debug(" Implementation: "+className);
130             return module;
131         }
132         catch (Exception JavaDoc ex)
133         {
134             logger.warn(" Problems loading class " + className + " : " + ex + ": " + ex.getMessage());
135             ex.printStackTrace(System.out) ;
136             return null;
137         }
138     }
139     
140     static private ClassLoader JavaDoc chooseClassLoader()
141     {
142         ClassLoader JavaDoc classLoader = null ;
143     
144         if ( classLoader == null )
145         {
146             // Find our classloader - one that uses the /WEB-INF/lib and classes directory.
147
classLoader = Thread.currentThread().getContextClassLoader();
148             if ( classLoader != null )
149                 logger.trace("Using thread classloader") ;
150         }
151         
152 // if (classLoader == null)
153
// {
154
// classLoader = this.getClass().getClassLoader();
155
// if ( classLoader != null )
156
// logger.trace("Using 'this' classloader") ;
157
// }
158

159         if ( classLoader == null )
160         {
161             classLoader = ClassLoader.getSystemClassLoader() ;
162             if ( classLoader != null )
163                 logger.trace("Using system classloader") ;
164         }
165         
166         if ( classLoader == null )
167             logger.warn("Failed to find a classloader") ;
168         return classLoader ;
169     }
170     
171
172 }
173
174
175 /*
176  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
177  * All rights reserved.
178  *
179  * Redistribution and use in source and binary forms, with or without
180  * modification, are permitted provided that the following conditions
181  * are met:
182  * 1. Redistributions of source code must retain the above copyright
183  * notice, this list of conditions and the following disclaimer.
184  * 2. Redistributions in binary form must reproduce the above copyright
185  * notice, this list of conditions and the following disclaimer in the
186  * documentation and/or other materials provided with the distribution.
187  * 3. The name of the author may not be used to endorse or promote products
188  * derived from this software without specific prior written permission.
189  *
190  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
191  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
192  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
193  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
194  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
195  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
196  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
197  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
198  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
199  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
200  */

201
Popular Tags