svn-gvsig-desktop / tags / v1_0_2_RELEASE / libraries / libIverUtiles / src / com / iver / utiles / extensionPoints / ExtensionPoint.java @ 11432
History | View | Annotate | Download (7.54 KB)
1 |
package com.iver.utiles.extensionPoints; |
---|---|
2 |
|
3 |
import java.lang.reflect.InvocationTargetException; |
4 |
import java.security.KeyException; |
5 |
import java.util.Hashtable; |
6 |
import java.util.Iterator; |
7 |
import java.util.LinkedHashMap; |
8 |
import java.util.Map; |
9 |
|
10 |
|
11 |
/**
|
12 |
* Esta clase permite registrar extensiones para un punto de extension.
|
13 |
* <br>
|
14 |
* <br>
|
15 |
* La clase se comporta como un Map que mantiene el orden de insercion
|
16 |
* de los elementos, para que puedan ser recorridos en ese orden.
|
17 |
* <br>
|
18 |
* Ademas de registrar las extensiones para un punto de extension, presenta
|
19 |
* metodos para facilitar la creacion de la extension.
|
20 |
* <br>
|
21 |
* A la hora de registrar una extension, mediante el petodo <i>put</i>,
|
22 |
* podremos suministrarle una clase o una instancia que implemente el
|
23 |
* interface IExtensionBuilder. Si le suministramos una clase, cuando
|
24 |
* queramos crear la extension mediante el metodo <i>create</i>, se creara
|
25 |
* una instancia de la clase y se retornara. Si lo que se suministro fue
|
26 |
* una instancia que implementa el interface IExtensionBuilder, se invocara
|
27 |
* al metodo <i>create</i> de esta para crear la extension.
|
28 |
* <br>
|
29 |
* @author jjdelcerro
|
30 |
*
|
31 |
*/
|
32 |
public class ExtensionPoint extends LinkedHashMap { |
33 |
|
34 |
private static final long serialVersionUID = -5908427725588553371L; |
35 |
|
36 |
private String name; |
37 |
private String description; |
38 |
private Hashtable extensionDescriptions = new Hashtable(); |
39 |
private Hashtable aliases = new Hashtable(); |
40 |
|
41 |
/**
|
42 |
* Construye un punto de extension.
|
43 |
* <br>
|
44 |
* @param extensionPointName Nombre del punto de extension.
|
45 |
*/
|
46 |
public ExtensionPoint(String extensionPointName) { |
47 |
this.name = extensionPointName;
|
48 |
} |
49 |
|
50 |
/**
|
51 |
* Construye un punto de extension.
|
52 |
* <br>
|
53 |
* @param extensionPointName Nombre del punto de extension
|
54 |
* @param description Descripcion del punto de extension
|
55 |
*/
|
56 |
public ExtensionPoint(String extensionPointName, String description) { |
57 |
this.name = extensionPointName;
|
58 |
this.description = description;
|
59 |
} |
60 |
|
61 |
/**
|
62 |
* Retorna el nombre de punto de extension.
|
63 |
* <br>
|
64 |
* @return Nombre del punto de extension
|
65 |
*/
|
66 |
public String getName() { |
67 |
return this.name; |
68 |
} |
69 |
|
70 |
/**
|
71 |
* Retorna la descripcion asociada al punto de extension.
|
72 |
* <br>
|
73 |
* @return descripcion del punto de extension
|
74 |
*/
|
75 |
public String getDescription() { |
76 |
return this.description; |
77 |
} |
78 |
|
79 |
/**
|
80 |
* Asocia una descripcion al punto de extension.
|
81 |
* <br>
|
82 |
*
|
83 |
* @param description
|
84 |
*/
|
85 |
public void setDescripcion(String description) { |
86 |
this.description = description;
|
87 |
} |
88 |
|
89 |
/**
|
90 |
* Retorna la descripcion asociada a una extension.
|
91 |
* <br>
|
92 |
* @param key
|
93 |
* <br>
|
94 |
* @return descripcion del punto de extension
|
95 |
*/
|
96 |
public String getExtensionDescription(String key) { |
97 |
return (String)this.extensionDescriptions.get(key); |
98 |
} |
99 |
|
100 |
/**
|
101 |
* Asocia una descripcion a una extension registrada.
|
102 |
* <br>
|
103 |
*
|
104 |
* @param key Nombre de la extension
|
105 |
* @param description
|
106 |
*/
|
107 |
public void setExtensionDescription(String key,String description) { |
108 |
if (this.containsKey(key)) { |
109 |
this.extensionDescriptions.put(key,description);
|
110 |
} |
111 |
} |
112 |
|
113 |
/**
|
114 |
* A?ade una extension con su descripcion asociada
|
115 |
*
|
116 |
* @param key clave de la extension
|
117 |
* @param decription descripcion de la extension
|
118 |
* @param value extension
|
119 |
*/
|
120 |
|
121 |
public Object put(String key, String description,Object value) { |
122 |
this.extensionDescriptions.put(key,description);
|
123 |
return super.put(key,value); |
124 |
} |
125 |
|
126 |
/**
|
127 |
* A?ade una extension antes de la indicada con beforeKey,
|
128 |
* con su descripcion asociada
|
129 |
*
|
130 |
* @param key clave de la extension
|
131 |
* @param decription descripcion de la extension
|
132 |
* @param value extension
|
133 |
*/
|
134 |
|
135 |
public Object insert(String beforeKey, String key, String description,Object value) { |
136 |
boolean mover = false; |
137 |
Map tmp = new LinkedHashMap(); |
138 |
|
139 |
for (Iterator i = this.entrySet().iterator(); i.hasNext(); ) { |
140 |
Map.Entry e = (Map.Entry)i.next(); |
141 |
if ( e.getKey().equals(beforeKey) ) {
|
142 |
mover = true;
|
143 |
} |
144 |
if( mover ) {
|
145 |
tmp.put(e.getKey(), e.getValue()); |
146 |
} |
147 |
} |
148 |
for (Iterator i = tmp.keySet().iterator(); i.hasNext(); ) { |
149 |
String key1 = (String)i.next(); |
150 |
this.remove(key1);
|
151 |
} |
152 |
if ( description!= null ) { |
153 |
this.extensionDescriptions.put(key,description);
|
154 |
} |
155 |
Object returnValue = super.put(key,value); |
156 |
this.putAll(tmp);
|
157 |
return returnValue;
|
158 |
} |
159 |
|
160 |
/**
|
161 |
* Crea una extension.
|
162 |
* <br>
|
163 |
* Dado un nombre de extension asociada a este punto de extension, crea
|
164 |
* el objeto registrado para manejar la extension.
|
165 |
* <br>
|
166 |
* Si el objeto registrado para esa extension implementa el interface
|
167 |
* <i>IExtensionBuilder</i>, se invoca al metodo create para crear la instancia
|
168 |
* de la extension.
|
169 |
* <br>
|
170 |
* Si no implementa este interface, debera ser una clase, y se creara una
|
171 |
* instancia de esa clase.
|
172 |
* <br>
|
173 |
* @param name Nombre de la extension a crear.
|
174 |
* @return La instancia creada de la extension.
|
175 |
*
|
176 |
* @throws InstantiationException
|
177 |
* @throws IllegalAccessException
|
178 |
*/
|
179 |
public Object create(String name) throws InstantiationException, IllegalAccessException { |
180 |
Object extension = this.get(name); |
181 |
if (extension == null) { |
182 |
extension = this.get(this.aliases.get(name)); |
183 |
} |
184 |
|
185 |
if( extension instanceof IExtensionBuilder ) { |
186 |
return ((IExtensionBuilder)extension).create();
|
187 |
} |
188 |
return ExtensionBuilder.create((Class) extension); |
189 |
} |
190 |
|
191 |
/**
|
192 |
* Crea una extension.
|
193 |
* <br>
|
194 |
* Dado un nombre de extension asociada a este punto de extension, crea
|
195 |
* el objeto registrado para manejar la extension.
|
196 |
* <br>
|
197 |
* A la hora de crear la instanacia de la extension, le pasara los parametros
|
198 |
* indicados en <i>args</i>.
|
199 |
* <br>
|
200 |
* Debido a que los argumentos se pasan como un array de objetos, no es posible
|
201 |
* pasar al constructor de la extension parametros de tipos basicos como <i>int</i>
|
202 |
* o <i>long</i>. Se deberan pasar como objetos y existir un constructor de la clase
|
203 |
* que los pueda recibir de esta manera.
|
204 |
* <br>
|
205 |
* @param name Nombre de la extension a crear.
|
206 |
* @param args Array de objetos a pasar como parametros en la construccion de la instancia de la extension.
|
207 |
* @return La instancia creada de la extension.
|
208 |
*
|
209 |
* @throws SecurityException
|
210 |
* @throws NoSuchMethodException
|
211 |
* @throws IllegalArgumentException
|
212 |
* @throws InstantiationException
|
213 |
* @throws IllegalAccessException
|
214 |
* @throws InvocationTargetException
|
215 |
*/
|
216 |
public Object create(String name, Object [] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { |
217 |
Object extension = this.get(name); |
218 |
if (extension == null) { |
219 |
extension = this.get(this.aliases.get(name)); |
220 |
} |
221 |
|
222 |
if( extension instanceof IExtensionBuilder ) { |
223 |
return ((IExtensionBuilder)extension).create(args);
|
224 |
} |
225 |
return ExtensionBuilder.create((Class) extension, args); |
226 |
} |
227 |
|
228 |
public Object create(String name, Map args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { |
229 |
Object extension = this.get(name); |
230 |
if (extension == null) { |
231 |
extension = this.get(this.aliases.get(name)); |
232 |
} |
233 |
|
234 |
if( extension instanceof IExtensionBuilder ) { |
235 |
return ((IExtensionBuilder)extension).create(args);
|
236 |
} |
237 |
return ExtensionBuilder.create((Class) extension, args); |
238 |
} |
239 |
|
240 |
/**
|
241 |
* Crea un alias para una extension registrada.
|
242 |
* <br>
|
243 |
* @param item Nombre de la extension registrada.
|
244 |
* @param alias alias a a?adir.
|
245 |
*
|
246 |
**/
|
247 |
public void addAlias(String item, String alias) throws KeyException{ |
248 |
if (!this.containsKey(item)) { |
249 |
throw new KeyException(item); |
250 |
} |
251 |
this.aliases.put(alias,item);
|
252 |
} |
253 |
|
254 |
|
255 |
|
256 |
} |