1 package org.astrogrid.common.registry;
2
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.util.Properties;
8
9 import org.astrogrid.common.creator.Creator;
10 import org.astrogrid.common.creator.CreatorException;
11
12 /***
13 * @author peter.shillan <mailto:gps@roe.ac.uk />
14 */
15 public class Registry {
16
17 private Properties registry;
18 private Creator creator;
19
20 public Registry(Properties registry) {
21 this(registry, new Creator());
22 }
23
24 public Registry(String filename) throws FileNotFoundException, IOException {
25 this(new FileInputStream(filename));
26 }
27
28 public Registry(InputStream inStream) throws IOException {
29 this(new Properties());
30 registry.load(inStream);
31 }
32
33 public Registry(Properties registry, Creator creator) {
34 this.registry = registry;
35 this.creator = creator;
36 }
37
38 public Object newInstance(String property) throws CreatorException {
39 return newInstance(property, (Object[]) null);
40 }
41
42 public Object newInstance(String property, Object[] parameters) throws CreatorException {
43 Object result = null;
44
45 String className = registry.getProperty(property);
46 if(className != null && className.length() > 0) {
47 result = creator.newInstance(className, parameters);
48 }
49
50 return result;
51 }
52
53 public Object newInstance(String property, Class clazz) throws CreatorException, ClassCastException {
54 return newInstance(property, null, clazz);
55 }
56
57 public Object newInstance(String property, Object[] parameters, Class clazz) throws CreatorException, ClassCastException {
58 Object result = newInstance(property, parameters);
59
60 if(!clazz.isInstance(result)) {
61 throw new ClassCastException();
62 }
63
64 return result;
65 }
66
67 }