svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libTools / src / org / gvsig / tools / extensionpoint / impl / DefaultExtensionPointManager.java @ 27500
History | View | Annotate | Download (5.89 KB)
1 |
package org.gvsig.tools.extensionpoint.impl; |
---|---|
2 |
|
3 |
import java.lang.reflect.Constructor; |
4 |
import java.lang.reflect.InvocationTargetException; |
5 |
import java.util.Arrays; |
6 |
import java.util.Collections; |
7 |
import java.util.HashMap; |
8 |
import java.util.Iterator; |
9 |
import java.util.List; |
10 |
import java.util.Map; |
11 |
|
12 |
import org.gvsig.tools.extensionpoint.ExtensionBuilder; |
13 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
14 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
15 |
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension; |
16 |
|
17 |
public class DefaultExtensionPointManager implements ExtensionPointManager { |
18 |
|
19 |
private static DefaultExtensionPointManager manager = null; |
20 |
|
21 |
private Map extensionsPoints; |
22 |
|
23 |
public DefaultExtensionPointManager() {
|
24 |
this.extensionsPoints = new HashMap(); |
25 |
} |
26 |
|
27 |
public static DefaultExtensionPointManager getManager() { |
28 |
if (manager == null) { |
29 |
manager = new DefaultExtensionPointManager();
|
30 |
} |
31 |
return manager;
|
32 |
} |
33 |
|
34 |
public Object create(Class cls) throws InstantiationException, |
35 |
IllegalAccessException {
|
36 |
Object obj = null; |
37 |
|
38 |
if (cls == null) { |
39 |
return null; |
40 |
} |
41 |
obj = cls.newInstance(); |
42 |
return obj;
|
43 |
} |
44 |
|
45 |
public Object create(Class cls, Map args) throws SecurityException, |
46 |
NoSuchMethodException, IllegalArgumentException, |
47 |
InstantiationException, IllegalAccessException, |
48 |
InvocationTargetException {
|
49 |
Object obj = null; |
50 |
Constructor create = null; |
51 |
Class[] types = new Class[1]; |
52 |
Object[] argsx = new Object[1]; |
53 |
|
54 |
if (cls == null) { |
55 |
return null; |
56 |
} |
57 |
types[0] = Map.class; |
58 |
argsx[0] = args;
|
59 |
create = this.findConstructor(cls, types);
|
60 |
obj = create.newInstance(argsx); |
61 |
return this.createObject(cls, argsx); |
62 |
} |
63 |
|
64 |
public Object create(Class cls, Object[] args) throws SecurityException, |
65 |
NoSuchMethodException, IllegalArgumentException, |
66 |
InstantiationException, IllegalAccessException, |
67 |
InvocationTargetException {
|
68 |
|
69 |
return this.createObject(cls, args); |
70 |
|
71 |
} |
72 |
|
73 |
/**
|
74 |
* @param cls
|
75 |
* @param types
|
76 |
* @return
|
77 |
* @throws NoSuchMethodException
|
78 |
* @throws SecurityException
|
79 |
*/
|
80 |
private Constructor findConstructor(Class clazz, Class[] types) |
81 |
throws SecurityException, NoSuchMethodException { |
82 |
try {
|
83 |
return clazz.getConstructor(types);
|
84 |
} catch (NoSuchMethodException e) { |
85 |
//Nothing to do
|
86 |
} |
87 |
|
88 |
// search for the required constructor
|
89 |
Constructor[] constrs = clazz.getConstructors(); |
90 |
boolean allMatch;
|
91 |
for (int i = 0; i < constrs.length; i++) { |
92 |
Class[] paramTypes = constrs[i].getParameterTypes(); |
93 |
if (paramTypes.length == types.length) { // a general candidate |
94 |
allMatch = true;
|
95 |
for (int j = 0; j < paramTypes.length; j++) { |
96 |
if (!isParameterCompatible(types[j], paramTypes[j])) {
|
97 |
allMatch = false;
|
98 |
break;
|
99 |
} |
100 |
} |
101 |
if (allMatch) {
|
102 |
return constrs[i];
|
103 |
} |
104 |
|
105 |
} |
106 |
} |
107 |
throw new NoSuchMethodException(); |
108 |
} |
109 |
|
110 |
private boolean isParameterCompatible(Class current, Class defined) { |
111 |
if (current == null) { |
112 |
return !defined.isPrimitive();
|
113 |
} else {
|
114 |
return defined.isAssignableFrom(current);
|
115 |
} |
116 |
} |
117 |
|
118 |
public ExtensionPoint create(String name, String description) { |
119 |
return new DefaultExtensionPoint(this, name, description); |
120 |
} |
121 |
|
122 |
public boolean add(ExtensionPoint extensionPoint) { |
123 |
ExtensionPoint original = (ExtensionPoint) extensionsPoints |
124 |
.get(extensionPoint.getName()); |
125 |
if (original == null) { |
126 |
extensionsPoints.put(extensionPoint.getName(), extensionPoint); |
127 |
} else {
|
128 |
for (Iterator iterator = extensionPoint.iterator(); iterator |
129 |
.hasNext();) { |
130 |
Extension extension = (Extension) iterator.next(); |
131 |
if (extension.getBuilder() != null) { |
132 |
original.append(extension.getName(), extension |
133 |
.getDescription(), extension.getBuilder()); |
134 |
} else {
|
135 |
original.append(extension.getName(), extension |
136 |
.getDescription(), extension.getExtension()); |
137 |
} |
138 |
} |
139 |
} |
140 |
return true; |
141 |
} |
142 |
|
143 |
public synchronized ExtensionPoint add(String name, String description) { |
144 |
ExtensionPoint ep = (ExtensionPoint) this.extensionsPoints.get(name);
|
145 |
if (ep == null) { |
146 |
ep = new DefaultExtensionPoint(this, name, description); |
147 |
this.extensionsPoints.put(ep.getName(), ep);
|
148 |
} |
149 |
return ep;
|
150 |
} |
151 |
|
152 |
public ExtensionPoint add(String name) { |
153 |
return add(name, null); |
154 |
} |
155 |
|
156 |
public ExtensionPoint.Extension add(String name, String description, |
157 |
String extName,
|
158 |
String extDescription, Class extension) { |
159 |
ExtensionPoint ep = add(name, description); |
160 |
return ep.append(extName, extDescription, extension);
|
161 |
} |
162 |
|
163 |
public ExtensionPoint.Extension add(String name, String description, |
164 |
String extName,
|
165 |
String extDescription, ExtensionBuilder builder) {
|
166 |
ExtensionPoint ep = add(name, description); |
167 |
return ep.append(extName, extDescription, builder);
|
168 |
} |
169 |
|
170 |
public ExtensionPoint get(String name) { |
171 |
return (ExtensionPoint) this.extensionsPoints.get(name); |
172 |
} |
173 |
|
174 |
public int getCount() { |
175 |
return this.extensionsPoints.size(); |
176 |
} |
177 |
|
178 |
public boolean has(String name) { |
179 |
return this.extensionsPoints.get(name) != null; |
180 |
} |
181 |
|
182 |
public Iterator interator() { |
183 |
return this.extensionsPoints.values().iterator(); |
184 |
} |
185 |
|
186 |
public List getNames() { |
187 |
String[] names = (String[]) this.extensionsPoints.keySet().toArray(); |
188 |
Arrays.sort(names);
|
189 |
return Collections.unmodifiableList(Arrays.asList(names)); |
190 |
} |
191 |
|
192 |
public Object createObject(Class klass, Object[] args) |
193 |
throws SecurityException, NoSuchMethodException, |
194 |
IllegalArgumentException, InstantiationException, |
195 |
IllegalAccessException, InvocationTargetException { |
196 |
Object obj = null; |
197 |
Constructor create = null; |
198 |
Class[] types = new Class[args.length]; |
199 |
|
200 |
if (klass == null) { |
201 |
return null; |
202 |
} |
203 |
for (int n = 0; n < args.length; n++) { |
204 |
Object arg = args[n];
|
205 |
if (arg != null) { |
206 |
types[n] = arg.getClass(); |
207 |
} else {
|
208 |
types[n] = null;
|
209 |
} |
210 |
} |
211 |
create = this.findConstructor(klass, types);
|
212 |
|
213 |
obj = create.newInstance(args); |
214 |
return obj;
|
215 |
|
216 |
} |
217 |
|
218 |
|
219 |
} |