comparison graal/com.oracle.max.base/src/com/sun/max/util/Registry.java @ 3733:e233f5660da4

Added Java files from Maxine project.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Sat, 17 Dec 2011 19:59:18 +0100
parents
children bc8527f3071c
comparison
equal deleted inserted replaced
3732:3e2e8b8abdaf 3733:e233f5660da4
1 /*
2 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.sun.max.util;
24
25 import java.util.*;
26
27 import com.sun.max.program.*;
28
29 /**
30 * The {@code Registry} class implements a type of configuration mechanism
31 * that allows a short string name (called an alias) to refer to a class name.
32 * The short name can be used to quickly look up an internally registered class and
33 * instantiate it (assuming the target class as a public constructor which takes
34 * no parameters). If the alias is not registered, the registry will try to use
35 * the alias as a fully-qualified Java class and load it using reflection.
36 */
37 public class Registry<C> {
38
39 protected final boolean loadClass;
40 protected final Class<C> classClass;
41 protected final Map<String, Class<? extends C>> classMap;
42 protected final Map<String, C> objectMap;
43 protected final Map<String, String> stringMap;
44
45 public Registry(Class<C> classType, boolean loadClass) {
46 this.loadClass = loadClass;
47 this.classClass = classType;
48 this.classMap = new HashMap<String, Class<? extends C>>();
49 this.objectMap = new HashMap<String, C>();
50 this.stringMap = new HashMap<String, String>();
51 }
52
53 public void registerObject(String alias, C object) {
54 objectMap.put(alias, object);
55 }
56
57 public void registerClass(String alias, Class<? extends C> classType) {
58 classMap.put(alias, classType);
59 }
60
61 public void registerClass(String alias, String className) {
62 stringMap.put(alias, className);
63 }
64
65 public C getInstance(String alias) {
66 return getInstance(alias, true);
67 }
68
69 public C getInstance(String alias, boolean fatal) {
70 final C object = objectMap.get(alias);
71 if (object != null) {
72 return object;
73 }
74 Class<? extends C> classRef = classMap.get(alias);
75 String className = alias;
76 try {
77 if (classRef == null) {
78 className = stringMap.get(alias);
79 if (className != null) {
80 classRef = Class.forName(className).asSubclass(classClass);
81 } else if (loadClass) {
82 classRef = Class.forName(alias).asSubclass(classClass);
83 } else {
84 return genError(fatal, "cannot find alias", alias, className);
85 }
86 }
87 className = classRef.getName();
88 return classRef.newInstance();
89 } catch (ClassNotFoundException e) {
90 return genError(fatal, "cannot find class", alias, className);
91 } catch (InstantiationException e) {
92 return genError(fatal, "cannot instantiate class", alias, className);
93 } catch (IllegalAccessException e) {
94 return genError(fatal, "cannot instantiate class", alias, className);
95 } catch (ClassCastException e) {
96 return genError(fatal, "not a subclass of " + classClass.getName(), alias, className);
97 }
98 }
99
100 public Iterable<String> getAliases() {
101 final LinkedList<String> lista = new LinkedList<String>();
102 lista.addAll(objectMap.keySet());
103 lista.addAll(classMap.keySet());
104 lista.addAll(stringMap.keySet());
105 return lista;
106 }
107
108 private C genError(boolean fatal, String message, String alias, String className) {
109 if (!fatal) {
110 return null;
111 }
112 String mstr = message + ": " + alias;
113 if (className != null) {
114 mstr = mstr + "(" + className + ")";
115 }
116 throw ProgramError.unexpected(mstr);
117 }
118
119 public static <T> Registry<T> newRegistry(Class<T> cl) {
120 return new Registry<T>(cl, true);
121 }
122 }