KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > storage > PropertyConfigurableFactory


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Core License version 1 published by ozone-db.org.
3
//
4
// Copyright (C) 2003-@year@, Leo Mekenkamp. All rights reserved.
5
//
6
// $Id: PropertyConfigurableFactory.java,v 1.1 2004/01/01 20:29:29 leomekenkamp Exp $
7

8 package org.ozoneDB.core.storage;
9
10 import java.lang.reflect.Constructor JavaDoc;
11 import java.lang.reflect.InvocationTargetException JavaDoc;
12 import java.util.Properties JavaDoc;
13 import org.ozoneDB.core.ConfigurationException;
14
15 /**
16  *
17  * @author <a HREF="mailto:leoATmekenkampD0Tcom">Leo Mekenkamp (mind the anti sp@m)</a>
18  * @version $Id: PropertyConfigurableFactory.java,v 1.1 2004/01/01 20:29:29 leomekenkamp Exp $
19  */

20 public class PropertyConfigurableFactory {
21     
22     private PropertyConfigurableFactory() {
23     }
24
25     public static PropertyConfigurable create(Class JavaDoc assignableFrom, Properties JavaDoc properties, String JavaDoc propertyName) {
26         String JavaDoc className = properties.getProperty(propertyName);
27         if (className == null) {
28             throw new ConfigurationException("could not find property: '" + propertyName + "'");
29         }
30         try {
31             Class JavaDoc classType = Class.forName(className);
32             if (!PropertyConfigurable.class.isAssignableFrom(classType)) {
33                 throw new ConfigurationException(className + " should implement " + PropertyConfigurable.class);
34             }
35             if (!assignableFrom.isAssignableFrom(classType)) {
36                 throw new ConfigurationException(className + " should implement " + assignableFrom);
37             }
38             Constructor JavaDoc constructor = classType.getConstructor(new Class JavaDoc[] {Properties JavaDoc.class, String JavaDoc.class});
39             PropertyConfigurable result = (PropertyConfigurable) constructor.newInstance(new Object JavaDoc[] {properties, propertyName});
40             return result;
41         } catch (ClassNotFoundException JavaDoc e) {
42             throw new ConfigurationException(e);
43         } catch (NoSuchMethodException JavaDoc e) {
44             throw new ConfigurationException(className + " does not follow PropertyConfigurable constructor contract", e);
45         } catch (InstantiationException JavaDoc e) {
46             throw new ConfigurationException(e);
47         } catch (IllegalAccessException JavaDoc e) {
48             throw new ConfigurationException(e);
49         } catch (InvocationTargetException JavaDoc e) {
50             throw new ConfigurationException(e);
51         }
52     }
53     
54 }
55         
56  
Popular Tags