Revision 24251
branches/v2_0_0_prep/libraries/libTools/src-test/org/gvsig/tools/extensionPoint/TestExtensionPoint.java | ||
---|---|---|
28 | 28 |
import java.lang.reflect.InvocationTargetException; |
29 | 29 |
import java.util.Iterator; |
30 | 30 |
|
31 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
|
32 |
|
|
31 | 33 |
import junit.framework.TestCase; |
32 | 34 |
|
33 | 35 |
public class TestExtensionPoint extends TestCase { |
branches/v2_0_0_prep/libraries/libTools/src-test/org/gvsig/tools/extensionPoint/TestExtensionPoints.java | ||
---|---|---|
27 | 27 |
|
28 | 28 |
import java.util.Iterator; |
29 | 29 |
|
30 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
|
31 |
import org.gvsig.tools.extensionpoint.ExtensionPoints; |
|
32 |
import org.gvsig.tools.extensionpoint.ExtensionPointsSingleton; |
|
33 |
|
|
30 | 34 |
import junit.framework.TestCase; |
31 | 35 |
|
32 | 36 |
public class TestExtensionPoints extends TestCase { |
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/locator/AbstractLocator.java | ||
---|---|---|
30 | 30 |
import java.util.Map; |
31 | 31 |
import java.util.Set; |
32 | 32 |
|
33 |
import org.gvsig.tools.extensionPoint.ExtensionPoint;
|
|
34 |
import org.gvsig.tools.extensionPoint.ExtensionPoints;
|
|
35 |
import org.gvsig.tools.extensionPoint.ExtensionPointsSingleton;
|
|
33 |
import org.gvsig.tools.extensionpoint.ExtensionPoint;
|
|
34 |
import org.gvsig.tools.extensionpoint.ExtensionPoints;
|
|
35 |
import org.gvsig.tools.extensionpoint.ExtensionPointsSingleton;
|
|
36 | 36 |
|
37 | 37 |
/** |
38 | 38 |
* Locator implementation based on the use of the ExtensionPoints. |
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/locator/LocatorObjectFactory.java | ||
---|---|---|
26 | 26 |
*/ |
27 | 27 |
package org.gvsig.tools.locator; |
28 | 28 |
|
29 |
import org.gvsig.tools.extensionPoint.IExtensionBuilder;
|
|
29 |
import org.gvsig.tools.extensionpoint.IExtensionBuilder;
|
|
30 | 30 |
|
31 | 31 |
/** |
32 | 32 |
* Interface for factories of objects to be registered in a Locator. |
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/extensionpoint/ExtensionPointManager.java | ||
---|---|---|
1 |
package org.gvsig.tools.extensionpoint; |
|
2 |
|
|
3 |
import java.util.Iterator; |
|
4 |
import java.util.List; |
|
5 |
|
|
6 |
public interface ExtensionPointManager { |
|
7 |
|
|
8 |
/** |
|
9 |
* Crea un nuevo punto de extension y nos lo debuelve. El nuevo punto de |
|
10 |
* extension no se a?ade al registro. |
|
11 |
* |
|
12 |
* @param name |
|
13 |
* , nombre del punto de extension a crear. |
|
14 |
* @param description |
|
15 |
* , descripcion del punto de extension a crear. |
|
16 |
* @return el punto de extension creado. |
|
17 |
*/ |
|
18 |
public ExtensionPoint create(String name, String description); |
|
19 |
|
|
20 |
/** |
|
21 |
* A?ade el punto de extension indicado al registro de puntos de extension. |
|
22 |
* Si el punto de extension ya existiese, lo actualia sustituyendo el |
|
23 |
* anterior por el nuevo. |
|
24 |
* |
|
25 |
* @param extensionPoint |
|
26 |
* , punto de extension a a?adir al registro. |
|
27 |
* @return true si todo va bien, false si no. |
|
28 |
*/ |
|
29 |
public boolean add(ExtensionPoint extensionPoint); |
|
30 |
|
|
31 |
/** |
|
32 |
* Obtiene el punto de extension asociado al nombre indicado. |
|
33 |
* |
|
34 |
* @param name |
|
35 |
* , nombre del punto de extension que queremos obtener. |
|
36 |
* @return el punto de extension requerido. |
|
37 |
*/ |
|
38 |
public ExtensionPoint get(String name); |
|
39 |
|
|
40 |
/** |
|
41 |
* Comprueba si un punto de extension existe. |
|
42 |
* |
|
43 |
* @return true si el punto de extension "name" existe. |
|
44 |
*/ |
|
45 |
public boolean has(String name); |
|
46 |
|
|
47 |
/** |
|
48 |
* Obtiene el numero de puntos de extension existentes. |
|
49 |
* |
|
50 |
* @return |
|
51 |
*/ |
|
52 |
public int getCount(); |
|
53 |
|
|
54 |
/** |
|
55 |
* Obtiene un iterador sobre los puntos de extension existentes. |
|
56 |
* |
|
57 |
* @return |
|
58 |
*/ |
|
59 |
public Iterator interator(); |
|
60 |
|
|
61 |
/** |
|
62 |
* Obtiene la lista de nombres de los puntos de extension existentes. |
|
63 |
* |
|
64 |
* @return |
|
65 |
*/ |
|
66 |
public List getNames(); |
|
67 |
|
|
68 |
/** |
|
69 |
* Metodo de utilidad para facilitar la creacion de puntos de extension. |
|
70 |
* Comprueba si el punto de extension "name" no existe y en ese caso lo crea |
|
71 |
* y a?ade. |
|
72 |
* |
|
73 |
* @param nam |
|
74 |
* , nombre del punto de extension |
|
75 |
* @param description |
|
76 |
* , descripcion del punto de extension |
|
77 |
* @return true si todo va bien, false si no |
|
78 |
*/ |
|
79 |
public ExtensionPoint add(String name, String description); |
|
80 |
|
|
81 |
|
|
82 |
} |
|
0 | 83 |
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/extensionpoint/ExtensionBuilder.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Gobernment (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
|
|
23 |
/* |
|
24 |
* AUTHORS (In addition to CIT): |
|
25 |
*/ |
|
26 |
package org.gvsig.tools.extensionpoint; |
|
27 |
|
|
28 |
import java.lang.reflect.Constructor; |
|
29 |
import java.lang.reflect.InvocationTargetException; |
|
30 |
import java.util.Map; |
|
31 |
|
|
32 |
/** |
|
33 |
* Clase de utilidad usada para crear las extensiones. |
|
34 |
* |
|
35 |
* Esta clase presenta un par de metodos estaticos para |
|
36 |
* permitir crear un objeto a partir de una clase. |
|
37 |
* |
|
38 |
* @author jjdelcerro |
|
39 |
* |
|
40 |
*/ |
|
41 |
public abstract class ExtensionBuilder implements IExtensionBuilder { |
|
42 |
|
|
43 |
/** |
|
44 |
* Crea un objeto de la clase indicada. |
|
45 |
* |
|
46 |
* @param cls Clase de la que crear la instancia |
|
47 |
* @return |
|
48 |
* |
|
49 |
* @throws InstantiationException |
|
50 |
* @throws IllegalAccessException |
|
51 |
*/ |
|
52 |
public static Object create(Class cls) throws InstantiationException, IllegalAccessException { |
|
53 |
Object obj = null; |
|
54 |
|
|
55 |
if( cls == null ) { |
|
56 |
return null; |
|
57 |
} |
|
58 |
obj = cls.newInstance(); |
|
59 |
return obj; |
|
60 |
} |
|
61 |
|
|
62 |
/** |
|
63 |
* Crea un objeto de la clase indicada. |
|
64 |
* |
|
65 |
* Crea un objeto de la clase indicada pasandole al constructor |
|
66 |
* los argumentos indicados en <i>args</i>. |
|
67 |
* <br> |
|
68 |
* @param cls Clase de la que crear la instancia |
|
69 |
* @param args Argumentos que pasar al constructor. |
|
70 |
* @return |
|
71 |
* |
|
72 |
* @throws SecurityException |
|
73 |
* @throws NoSuchMethodException |
|
74 |
* @throws IllegalArgumentException |
|
75 |
* @throws InstantiationException |
|
76 |
* @throws IllegalAccessException |
|
77 |
* @throws InvocationTargetException |
|
78 |
*/ |
|
79 |
public static Object create(Class cls, Object [] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { |
|
80 |
Object obj = null; |
|
81 |
Constructor create = null; |
|
82 |
Class [] types = new Class[args.length]; |
|
83 |
|
|
84 |
if( cls == null ) { |
|
85 |
return null; |
|
86 |
} |
|
87 |
for( int n=0 ; n<args.length ; n++ ) { |
|
88 |
Object arg = args[n]; |
|
89 |
types[n] = arg.getClass(); |
|
90 |
} |
|
91 |
create = cls.getConstructor(types); |
|
92 |
obj = create.newInstance(args); |
|
93 |
return obj; |
|
94 |
} |
|
95 |
/** |
|
96 |
* Crea un objeto de la clase indicada. |
|
97 |
* |
|
98 |
* Crea un objeto de la clase indicada pasandole al constructor |
|
99 |
* un como argumento un Map.. |
|
100 |
* <br> |
|
101 |
* @param cls Clase de la que crear la instancia |
|
102 |
* @param args Map a pasar como argumento al constructor. |
|
103 |
* @return |
|
104 |
* |
|
105 |
* @throws SecurityException |
|
106 |
* @throws NoSuchMethodException |
|
107 |
* @throws IllegalArgumentException |
|
108 |
* @throws InstantiationException |
|
109 |
* @throws IllegalAccessException |
|
110 |
* @throws InvocationTargetException |
|
111 |
*/ |
|
112 |
public static Object create(Class cls, Map args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { |
|
113 |
Object obj = null; |
|
114 |
Constructor create = null; |
|
115 |
Class [] types = new Class[1]; |
|
116 |
Object [] argsx = new Object[1]; |
|
117 |
|
|
118 |
if( cls == null ) { |
|
119 |
return null; |
|
120 |
} |
|
121 |
types[0] = Map.class; |
|
122 |
argsx[0] = args; |
|
123 |
create = cls.getConstructor(types); |
|
124 |
obj = create.newInstance(argsx); |
|
125 |
return obj; |
|
126 |
} |
|
127 |
} |
|
0 | 128 |
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/extensionpoint/ExtensionPoint.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Gobernment (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
|
|
23 |
/* |
|
24 |
* AUTHORS (In addition to CIT): |
|
25 |
*/ |
|
26 |
package org.gvsig.tools.extensionpoint; |
|
27 |
|
|
28 |
import java.lang.reflect.InvocationTargetException; |
|
29 |
import java.security.KeyException; |
|
30 |
import java.util.*; |
|
31 |
|
|
32 |
/** |
|
33 |
* Esta clase permite registrar extensiones para un punto de extension. |
|
34 |
* <br> |
|
35 |
* <br> |
|
36 |
* La clase se comporta como un Map que mantiene el orden de insercion |
|
37 |
* de los elementos, para que puedan ser recorridos en ese orden. |
|
38 |
* <br> |
|
39 |
* Ademas de registrar las extensiones para un punto de extension, presenta |
|
40 |
* metodos para facilitar la creacion de la extension. |
|
41 |
* <br> |
|
42 |
* A la hora de registrar una extension, mediante el metodo <i>put</i>, |
|
43 |
* podremos suministrarle una clase o una instancia que implemente el |
|
44 |
* interface IExtensionBuilder. Si le suministramos una clase, cuando |
|
45 |
* queramos crear la extension mediante el metodo <i>create</i>, se creara |
|
46 |
* una instancia de la clase y se retornara. Si lo que se suministro fue |
|
47 |
* una instancia que implementa el interface IExtensionBuilder, se invocara |
|
48 |
* al metodo <i>create</i> de esta para crear la extension. |
|
49 |
* <br> |
|
50 |
* @author jjdelcerro |
|
51 |
* |
|
52 |
*/ |
|
53 |
public class ExtensionPoint extends LinkedHashMap { |
|
54 |
|
|
55 |
private static final long serialVersionUID = -5908427725588553371L; |
|
56 |
|
|
57 |
private String name; |
|
58 |
private String description; |
|
59 |
private Hashtable extensionDescriptions = new Hashtable(); |
|
60 |
private Hashtable aliases = new Hashtable(); |
|
61 |
|
|
62 |
/** |
|
63 |
* Construye un punto de extension. |
|
64 |
* <br> |
|
65 |
* @param extensionPointName Nombre del punto de extension. |
|
66 |
*/ |
|
67 |
public ExtensionPoint(String extensionPointName) { |
|
68 |
this.name = extensionPointName; |
|
69 |
} |
|
70 |
|
|
71 |
/** |
|
72 |
* Construye un punto de extension. |
|
73 |
* <br> |
|
74 |
* @param extensionPointName Nombre del punto de extension |
|
75 |
* @param description Descripcion del punto de extension |
|
76 |
*/ |
|
77 |
public ExtensionPoint(String extensionPointName, String description) { |
|
78 |
this.name = extensionPointName; |
|
79 |
this.description = description; |
|
80 |
} |
|
81 |
|
|
82 |
/** |
|
83 |
* Retorna el nombre de punto de extension. |
|
84 |
* <br> |
|
85 |
* @return Nombre del punto de extension |
|
86 |
*/ |
|
87 |
public String getName() { |
|
88 |
return this.name; |
|
89 |
} |
|
90 |
|
|
91 |
/** |
|
92 |
* Retorna la descripcion asociada al punto de extension. |
|
93 |
* <br> |
|
94 |
* @return descripcion del punto de extension |
|
95 |
*/ |
|
96 |
public String getDescription() { |
|
97 |
return this.description; |
|
98 |
} |
|
99 |
|
|
100 |
/** |
|
101 |
* Asocia una descripcion al punto de extension. |
|
102 |
* <br> |
|
103 |
* |
|
104 |
* @param description |
|
105 |
*/ |
|
106 |
public void setDescripcion(String description) { |
|
107 |
this.description = description; |
|
108 |
} |
|
109 |
|
|
110 |
/** |
|
111 |
* Retorna la descripcion asociada a una extension. |
|
112 |
* <br> |
|
113 |
* @param key |
|
114 |
* <br> |
|
115 |
* @return descripcion del punto de extension |
|
116 |
*/ |
|
117 |
public String getExtensionDescription(String key) { |
|
118 |
return (String)this.extensionDescriptions.get(key); |
|
119 |
} |
|
120 |
|
|
121 |
/** |
|
122 |
* Asocia una descripcion a una extension registrada. |
|
123 |
* <br> |
|
124 |
* |
|
125 |
* @param key Nombre de la extension |
|
126 |
* @param description |
|
127 |
*/ |
|
128 |
public void setExtensionDescription(String key,String description) { |
|
129 |
if (this.containsKey(key)) { |
|
130 |
this.extensionDescriptions.put(key,description); |
|
131 |
} |
|
132 |
} |
|
133 |
|
|
134 |
/** |
|
135 |
* A?ade una extension con su descripcion asociada |
|
136 |
* |
|
137 |
* @param key clave de la extension |
|
138 |
* @param decription descripcion de la extension |
|
139 |
* @param value extension |
|
140 |
*/ |
|
141 |
|
|
142 |
public Object put(String key, String description,Object value) { |
|
143 |
this.extensionDescriptions.put(key,description); |
|
144 |
return super.put(key,value); |
|
145 |
} |
|
146 |
|
|
147 |
/** |
|
148 |
* A?ade una extension antes de la indicada con beforeKey, |
|
149 |
* con su descripcion asociada |
|
150 |
* |
|
151 |
* @param key clave de la extension |
|
152 |
* @param decription descripcion de la extension |
|
153 |
* @param value extension |
|
154 |
*/ |
|
155 |
|
|
156 |
public Object insert(String beforeKey, String key, String description,Object value) { |
|
157 |
boolean mover = false; |
|
158 |
Map tmp = new LinkedHashMap(); |
|
159 |
|
|
160 |
for (Iterator i = this.entrySet().iterator(); i.hasNext(); ) { |
|
161 |
Map.Entry e = (Map.Entry)i.next(); |
|
162 |
if ( e.getKey().equals(beforeKey) ) { |
|
163 |
mover = true; |
|
164 |
} |
|
165 |
if( mover ) { |
|
166 |
tmp.put(e.getKey(), e.getValue()); |
|
167 |
} |
|
168 |
} |
|
169 |
for (Iterator i = tmp.keySet().iterator(); i.hasNext(); ) { |
|
170 |
String key1 = (String)i.next(); |
|
171 |
this.remove(key1); |
|
172 |
} |
|
173 |
if ( description!= null ) { |
|
174 |
this.extensionDescriptions.put(key,description); |
|
175 |
} |
|
176 |
Object returnValue = super.put(key,value); |
|
177 |
this.putAll(tmp); |
|
178 |
return returnValue; |
|
179 |
} |
|
180 |
|
|
181 |
/** |
|
182 |
* Crea una extension. |
|
183 |
* <br> |
|
184 |
* Dado un nombre de extension asociada a este punto de extension, crea |
|
185 |
* el objeto registrado para manejar la extension. |
|
186 |
* <br> |
|
187 |
* Si el objeto registrado para esa extension implementa el interface |
|
188 |
* <i>IExtensionBuilder</i>, se invoca al metodo create para crear la instancia |
|
189 |
* de la extension. |
|
190 |
* <br> |
|
191 |
* Si no implementa este interface, debera ser una clase, y se creara una |
|
192 |
* instancia de esa clase. |
|
193 |
* <br> |
|
194 |
* @param name Nombre de la extension a crear. |
|
195 |
* @return La instancia creada de la extension. |
|
196 |
* |
|
197 |
* @throws InstantiationException |
|
198 |
* @throws IllegalAccessException |
|
199 |
*/ |
|
200 |
public Object create(String name) throws InstantiationException, IllegalAccessException { |
|
201 |
Object extension = this.get(name); |
|
202 |
if (extension == null) { |
|
203 |
extension = this.get(this.aliases.get(name)); |
|
204 |
} |
|
205 |
|
|
206 |
if( extension instanceof IExtensionBuilder ) { |
|
207 |
return ((IExtensionBuilder)extension).create(); |
|
208 |
} |
|
209 |
return ExtensionBuilder.create((Class) extension); |
|
210 |
} |
|
211 |
|
|
212 |
/** |
|
213 |
* Crea una extension. |
|
214 |
* <br> |
|
215 |
* Dado un nombre de extension asociada a este punto de extension, crea |
|
216 |
* el objeto registrado para manejar la extension. |
|
217 |
* <br> |
|
218 |
* A la hora de crear la instancia de la extension, le pasara los parametros |
|
219 |
* indicados en <i>args</i>. |
|
220 |
* <br> |
|
221 |
* Debido a que los argumentos se pasan como un array de objetos, no es posible |
|
222 |
* pasar al constructor de la extension parametros de tipos basicos como <i>int</i> |
|
223 |
* o <i>long</i>. Se deberan pasar como objetos y existir un constructor de la clase |
|
224 |
* que los pueda recibir de esta manera. |
|
225 |
* <br> |
|
226 |
* @param name Nombre de la extension a crear. |
|
227 |
* @param args Array de objetos a pasar como parametros en la construccion de la instancia de la extension. |
|
228 |
* @return La instancia creada de la extension. |
|
229 |
* |
|
230 |
* @throws SecurityException |
|
231 |
* @throws NoSuchMethodException |
|
232 |
* @throws IllegalArgumentException |
|
233 |
* @throws InstantiationException |
|
234 |
* @throws IllegalAccessException |
|
235 |
* @throws InvocationTargetException |
|
236 |
*/ |
|
237 |
public Object create(String name, Object [] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { |
|
238 |
Object extension = this.get(name); |
|
239 |
if (extension == null) { |
|
240 |
extension = this.get(this.aliases.get(name)); |
|
241 |
} |
|
242 |
|
|
243 |
if( extension instanceof IExtensionBuilder ) { |
|
244 |
return ((IExtensionBuilder)extension).create(args); |
|
245 |
} |
|
246 |
return ExtensionBuilder.create((Class) extension, args); |
|
247 |
} |
|
248 |
|
|
249 |
public Object create(String name, Map args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { |
|
250 |
Object extension = this.get(name); |
|
251 |
if (extension == null) { |
|
252 |
extension = this.get(this.aliases.get(name)); |
|
253 |
} |
|
254 |
|
|
255 |
if( extension instanceof IExtensionBuilder ) { |
|
256 |
return ((IExtensionBuilder)extension).create(args); |
|
257 |
} |
|
258 |
return ExtensionBuilder.create((Class) extension, args); |
|
259 |
} |
|
260 |
|
|
261 |
/** |
|
262 |
* Crea un alias para una extension registrada. |
|
263 |
* <br> |
|
264 |
* @param item Nombre de la extension registrada. |
|
265 |
* @param alias alias a a?adir. |
|
266 |
* |
|
267 |
**/ |
|
268 |
public void addAlias(String item, String alias) throws KeyException{ |
|
269 |
if (!this.containsKey(item)) { |
|
270 |
throw new KeyException(item); |
|
271 |
} |
|
272 |
this.aliases.put(alias,item); |
|
273 |
} |
|
274 |
} |
|
0 | 275 |
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/extensionpoint/ExtensionPoints.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Gobernment (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
|
|
23 |
/* |
|
24 |
* AUTHORS (In addition to CIT): |
|
25 |
*/ |
|
26 |
package org.gvsig.tools.extensionpoint; |
|
27 |
|
|
28 |
import java.util.TreeMap; |
|
29 |
|
|
30 |
/** |
|
31 |
* Clase para registro de puntos de extension. |
|
32 |
* <br> |
|
33 |
* <br> |
|
34 |
* |
|
35 |
* @author jjdelcerro |
|
36 |
*/ |
|
37 |
public class ExtensionPoints extends TreeMap { |
|
38 |
|
|
39 |
|
|
40 |
private static final long serialVersionUID = -798417910971607414L; |
|
41 |
|
|
42 |
/** |
|
43 |
* Evita que se a?adan elementos que no son puntos de extension. |
|
44 |
* <br> |
|
45 |
* <br> |
|
46 |
* Aunque la clase se comporta como un <i>Map</i>, no esta permitido |
|
47 |
* a?adir a esta objetos que no sean de la clase <i>ExtensionPoint</i>. |
|
48 |
* Si intentamos a?adir un elemento que no sea de esta clase, se disparara |
|
49 |
* una excepcion ClassCastException. |
|
50 |
* <br> |
|
51 |
* <br> |
|
52 |
* @see java.util.Map#put(java.lang.Object, java.lang.Object) |
|
53 |
*/ |
|
54 |
public Object put(Object key, Object value) throws ClassCastException { |
|
55 |
throw new ClassCastException(); |
|
56 |
} |
|
57 |
|
|
58 |
/** |
|
59 |
* A?ade un punto de extension al registro de puntos de extension. |
|
60 |
* <br> |
|
61 |
* <br> |
|
62 |
* Mediante este metodo puede a?adir un punto de extension al registro |
|
63 |
* de puntos de extension, llevandose detras todas las extensiones que |
|
64 |
* esten registradas en el. |
|
65 |
* <br> |
|
66 |
* <br> |
|
67 |
* En caso de que ya existiese un punto de extension con el nombre dado, |
|
68 |
* a?adira a este las extensiones del punto de extension suministrado. |
|
69 |
* <br> |
|
70 |
* <br> |
|
71 |
* @param value Punto de extension a registrar |
|
72 |
* @return |
|
73 |
* |
|
74 |
*/ |
|
75 |
public Object put(ExtensionPoint value) { |
|
76 |
return put(value.getName(),value); |
|
77 |
} |
|
78 |
|
|
79 |
/** |
|
80 |
* A?ade un punto de extension al registro de puntos de extension. |
|
81 |
* <br> |
|
82 |
* <br> |
|
83 |
* Mediante este metodo puede a?adir un punto de extension al registro |
|
84 |
* de puntos de extension, llevandose detras todas las extensiones que |
|
85 |
* esten registradas en el. |
|
86 |
* <br> |
|
87 |
* <br> |
|
88 |
* En caso de que ya existiese un punto de extension con el nombre dado, |
|
89 |
* a?adira a este las extensiones del punto de extension suministrado. |
|
90 |
* <br> |
|
91 |
* <br> |
|
92 |
* Cuando se a?ade un punto de extension, es imprescindible que <i>key</i> y |
|
93 |
* el nombre del punto de extension que se este a?adiendo coincidan. |
|
94 |
* <br> |
|
95 |
* <br> |
|
96 |
* @param key Nombre del punto de extension |
|
97 |
* @param value Punto de extension a registrar |
|
98 |
* @return |
|
99 |
* |
|
100 |
*/ |
|
101 |
public Object put(String key, ExtensionPoint value) { |
|
102 |
if ( !value.getName().equals(key) ) { |
|
103 |
throw new IllegalArgumentException (); |
|
104 |
} |
|
105 |
ExtensionPoint n = (ExtensionPoint)super.get(key); |
|
106 |
if( n == null ) { |
|
107 |
return super.put(key,value); |
|
108 |
} |
|
109 |
// Como estamos actualizando un punto de extension, a?adimos a este las |
|
110 |
// extensiones del que nos acaban de suministrar. |
|
111 |
n.putAll(value); |
|
112 |
return value; |
|
113 |
} |
|
114 |
|
|
115 |
/** |
|
116 |
* Registra una extension en un punto de extension. |
|
117 |
* <br> |
|
118 |
* <br> |
|
119 |
* Mediante este metodo puede registrar sobre un punto de extension |
|
120 |
* una extension. La extension esta identificada mediante un nombre |
|
121 |
* unico, y una clase que se usara para manejar la extension o una |
|
122 |
* clase que contruira el objeto que maneje la extension. |
|
123 |
* <br> |
|
124 |
* <br> |
|
125 |
* Si ya existe en el punto de extension indicado por <i>extensionPointName</i> |
|
126 |
* una extension con el nombre <i>name</i>, esta sera sustituida por la |
|
127 |
* nueva extension. |
|
128 |
* <br> |
|
129 |
* @param extensionPointName Nombre del punto de extension |
|
130 |
* @param name Nombre o identificador de la extension |
|
131 |
* @param data Clase que implementa la extension o que la construye. |
|
132 |
* |
|
133 |
* |
|
134 |
*/ |
|
135 |
public void add(String extensionPointName, String name, Object data) { |
|
136 |
ExtensionPoint extensionPoint = (ExtensionPoint)super.get(extensionPointName); |
|
137 |
if( extensionPoint == null ) { |
|
138 |
extensionPoint = new ExtensionPoint(extensionPointName); |
|
139 |
super.put(extensionPoint.getName(), extensionPoint); |
|
140 |
} |
|
141 |
|
|
142 |
extensionPoint.put(name, data); |
|
143 |
} |
|
144 |
|
|
145 |
/** |
|
146 |
* Registra una extension en un punto de extension. |
|
147 |
* <br> |
|
148 |
* <br> |
|
149 |
* Mediante este metodo puede registrar sobre un punto de extension |
|
150 |
* una extension. La extension esta identificada mediante un nombre |
|
151 |
* unico, y una clase que se usara para manejar la extension o una |
|
152 |
* clase que contruira el objeto que maneje la extension. |
|
153 |
* <br> |
|
154 |
* <br> |
|
155 |
* Si ya existe en el punto de extension indicado por <i>extensionPointName</i> |
|
156 |
* una extension con el nombre <i>name</i>, esta sera sustituida por la |
|
157 |
* nueva extension. |
|
158 |
* <br> |
|
159 |
* @param extensionPointName Nombre del punto de extension |
|
160 |
* @param name Nombre o identificador de la extension |
|
161 |
* @param description descripcion de la extension. |
|
162 |
* @param data Clase que implementa la extension o que la construye. |
|
163 |
* |
|
164 |
* |
|
165 |
*/ |
|
166 |
public void add(String extensionPointName, String name, String description, Object data) { |
|
167 |
ExtensionPoint extensionPoint = (ExtensionPoint)super.get(extensionPointName); |
|
168 |
if( extensionPoint == null ) { |
|
169 |
extensionPoint = new ExtensionPoint(extensionPointName); |
|
170 |
super.put(extensionPoint.getName(), extensionPoint); |
|
171 |
} |
|
172 |
|
|
173 |
extensionPoint.put(name,description, data); |
|
174 |
} |
|
175 |
|
|
176 |
} |
|
0 | 177 |
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/extensionpoint/ExtensionPointsSingleton.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Gobernment (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
|
|
23 |
/* |
|
24 |
* AUTHORS (In addition to CIT): |
|
25 |
*/ |
|
26 |
package org.gvsig.tools.extensionpoint; |
|
27 |
|
|
28 |
public class ExtensionPointsSingleton extends ExtensionPoints { |
|
29 |
|
|
30 |
private static final long serialVersionUID = -630976693542039111L; |
|
31 |
|
|
32 |
private static ExtensionPoints extensionPoints = new ExtensionPointsSingleton(); |
|
33 |
|
|
34 |
private ExtensionPointsSingleton() { |
|
35 |
super(); |
|
36 |
} |
|
37 |
|
|
38 |
public static ExtensionPoints getInstance() { |
|
39 |
return ExtensionPointsSingleton.extensionPoints; |
|
40 |
} |
|
41 |
} |
|
0 | 42 |
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/extensionpoint/IExtensionBuilder.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Gobernment (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
|
|
23 |
/* |
|
24 |
* AUTHORS (In addition to CIT): |
|
25 |
*/ |
|
26 |
package org.gvsig.tools.extensionpoint; |
|
27 |
|
|
28 |
import java.util.Map; |
|
29 |
|
|
30 |
/** |
|
31 |
* Interface utilizado para indicar al registro de extensiones |
|
32 |
* que no se trata de una clase lo que hey registrado, si no |
|
33 |
* una instancia de un objeto a usar para crear la extension. |
|
34 |
* |
|
35 |
* |
|
36 |
* @author jjdelcerro |
|
37 |
* |
|
38 |
*/ |
|
39 |
public interface IExtensionBuilder { |
|
40 |
/** |
|
41 |
* Crea una instancia de la extension y la retorna. |
|
42 |
* <br> |
|
43 |
* @return |
|
44 |
*/ |
|
45 |
public Object create(); |
|
46 |
|
|
47 |
/** |
|
48 |
* Crea una instancia de la extension y la retorna. |
|
49 |
* <br> |
|
50 |
* En <i>args</i> recibira la lista de argumeentos a utilizar |
|
51 |
* para crear la extension. |
|
52 |
* <br> |
|
53 |
* @param args |
|
54 |
* @return |
|
55 |
*/ |
|
56 |
public Object create(Object [] args); |
|
57 |
|
|
58 |
public Object create(Map args); |
|
59 |
} |
|
0 | 60 |
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/extensionpoint/package.html | ||
---|---|---|
1 |
<html> |
|
2 |
<body> |
|
3 |
<p> |
|
4 |
Este paquete expone un mecanismo para registro de clases. |
|
5 |
</p> |
|
6 |
<p> |
|
7 |
Permite registrar clases o factorias de clases que luego pueden ser |
|
8 |
recuperadas para construir instancias. |
|
9 |
</p> |
|
10 |
<p> |
|
11 |
La finalidad de este registro es el manejo de puntos de extension a una |
|
12 |
aplicacion. Una aplicacion declara o registra con un nombre los puntos de |
|
13 |
extension que va a tener. Cada punto de extension puede tener registradas una |
|
14 |
o mas extensiones. Cuando se quiere a?adir una extension a la aplicacion, se |
|
15 |
registra la clase o factoria que gestiona esa extension para el punto de extension |
|
16 |
que se desee. |
|
17 |
</p> |
|
18 |
<p> |
|
19 |
Veamos esto con un ejemplo. |
|
20 |
</p> |
|
21 |
<p> |
|
22 |
Supongamos que queremos a?adir un punto de extension a la aplicacion gvSIG, que |
|
23 |
permita asignar un mecanismo de presentacion de la informacion asociada a la |
|
24 |
herramienta de "informacion" especializada segun el tipo de tema sobre el que se |
|
25 |
esta trabajando. |
|
26 |
</p> |
|
27 |
<p> |
|
28 |
Lo primero que tendriamos que hacer es darle un nombre al punto de extension. |
|
29 |
Lo llamaremos "InfoByPoint". Para esto, la aplicacion que valla a utilizar |
|
30 |
las extensiones que se registren en este punto deberia hacer lo siguiente: |
|
31 |
</p> |
|
32 |
<pre> |
|
33 |
ExtensionPoint infoByPoint = new ExtensionPoint("InfoByPoint","Registra las distintas extensiones que se pueden a?adir al 'InfoByPoint'"); |
|
34 |
ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance(); |
|
35 |
|
|
36 |
extensionPoints.put(infoByPoint); |
|
37 |
</pre> |
|
38 |
<p> |
|
39 |
Con esto creamos un punto de extension de nombre "InfoByPoint", recogemos la |
|
40 |
instancia del registro de extensiones e insertamos el nuevo punto de extenstion |
|
41 |
en el.<br> |
|
42 |
Normalmente esta operacion, en el contexto de gvSIG, se realizaria en la |
|
43 |
inicializacion de la extension de andami en la que vallamos a a?adir |
|
44 |
la herramienta de informacion. |
|
45 |
</p> |
|
46 |
<p> |
|
47 |
Las extensiones a registrar en el "InfoByPoint" podrian consistir un un |
|
48 |
JPanel que gestione la presentacion a usar para el tema. |
|
49 |
</p> |
|
50 |
<p> |
|
51 |
Luego, desde la parte de la aplicacion que necesite a?adir nueva funcionalidad |
|
52 |
en este punto de extension, se deberia a?adir la extension. Por ejemplo |
|
53 |
en la extension de andami de WMS, se podria a?adir a "InfoByPoint" la posibilidad |
|
54 |
de usar una forma especial de presentacion. Podria hacerse: |
|
55 |
</p> |
|
56 |
<pre> |
|
57 |
ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance(); |
|
58 |
extensionPoints.add("InfoByPoint","WMS",PanelQueGestionaLaExtension); |
|
59 |
</pre> |
|
60 |
<p> |
|
61 |
Donde "PanelQueGestionaLaExtension" sera el JPanel que gestiona la extension para |
|
62 |
"InfoByPoint" de WMS. |
|
63 |
</p> |
|
64 |
<p> |
|
65 |
Si quieran acceder a la extension de nombre "WMS" |
|
66 |
se haria: |
|
67 |
</p> |
|
68 |
<pre> |
|
69 |
ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance(); |
|
70 |
ExtensionPoint infoByPoint = (ExtensionPoint)extensionPoints.get("InfoByPoint"); |
|
71 |
Object ext = infoByPoint.create("WMS"); |
|
72 |
</pre> |
|
73 |
<p> |
|
74 |
Y esto nos devolberia un objeto JPanel que gestiona la extension "WMS" |
|
75 |
para el "InfoByPoint". |
|
76 |
</p> |
|
77 |
<p> |
|
78 |
Si quisiesemos recorrer las distintas extensiones de ese punto podriamos |
|
79 |
hacerlo asi: |
|
80 |
</p> |
|
81 |
<pre> |
|
82 |
ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance(); |
|
83 |
ExtensionPoint infoByPoint = (ExtensionPoint)extensionPoints.get("InfoByPoint"); |
|
84 |
Iterator infoByPoint =infoByPoint.keySet().iterator(); |
|
85 |
while( i.hasNext() ) { |
|
86 |
String nombre = (String)i.next(); |
|
87 |
... |
|
88 |
// Y para crear los JPanel asociados a la extension... |
|
89 |
Object panel = infoByPoint.create(nombre); |
|
90 |
... |
|
91 |
} |
|
92 |
</pre> |
|
93 |
<p> |
|
94 |
Ademas de registrar clases en un punto de extension, se pueden registrar |
|
95 |
instancias que implementen el interface de IExtensionBuilder. En este caso, |
|
96 |
cuando se invoque al metodo "create" del punto de extension, en lugar |
|
97 |
de crear una instancia, como no tiene la clase, este invocara al metodo |
|
98 |
create del objeto que ha sido registrado. |
|
99 |
</p> |
|
100 |
|
|
101 |
<p> |
|
102 |
Podemos encontrar un ejemplo de esto en la extension de JDBC para el |
|
103 |
catalogo. Como no existe una capa especifica para las capas JDBC, en lugar |
|
104 |
de registrar en el punto de extension una clase "capa JDBC", se registra |
|
105 |
una clase que implementa el interface IExtensionBuilder, que en su |
|
106 |
metodo create construye una capa vectorial y la inicializa de la forma |
|
107 |
apropiada para funcionar con la fuente de datos de JDBC. |
|
108 |
</p> |
|
109 |
<p> |
|
110 |
Hay que tener en cuenta que para un punto de extension dado, deberia ser |
|
111 |
trasparente que se registren en el clases o instancias que contruyen las clases. |
|
112 |
E incluso que es posible mezclar en un punto de extension los dos |
|
113 |
mecanismos, como es el caso del catalogo. |
|
114 |
</p> |
|
115 |
</body> |
|
116 |
</html> |
|
0 | 117 |
Also available in: Unified diff