Statistics
| Revision:

root / trunk / libraries / libIverUtiles / src / com / iver / utiles / PostProcessSupport.java @ 6620

History | View | Annotate | Download (6.88 KB)

1
/*
2
 * Created on 24-ago-2005
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 * 
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 * 
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *  
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 * 
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
 *  
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 * 
34
 *    or
35
 * 
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 * 
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
package com.iver.utiles;
45

    
46
import java.lang.reflect.InvocationTargetException;
47
import java.lang.reflect.Method;
48
import java.util.ArrayList;
49

    
50
/**
51
 * @author fjp
52
 *
53
 * Clase que recibe las llamadas que tiene que hacer cuando termine
54
 * la lectura de proyecto, por ejemplo. Recibir?a algo as? como Clase,
55
 * Nombre de funci?n a invocar, Par?metros y Prioridad.
56
 * Lo guarda en un array, ordena por prioridad y empieza a invocar.
57
 */
58
public class PostProcessSupport
59
{
60
    private static ArrayList callList = new ArrayList();
61
    private static class Call 
62
    {
63
        private Object obj;
64
        private Method method;
65
        private Object[] parameters;
66
        private int priority;
67
        public Call(Object obj, String strMethod, Object[] parameters, int priority)
68
        {
69
            this.obj = obj;
70
            this.parameters = parameters;
71
            Class[] classParams = new Class[parameters.length];
72
            for (int i=0; i< parameters.length; i++)
73
                classParams[i] = parameters[i].getClass();
74
            try {
75
                method = obj.getClass().getMethod(strMethod, classParams);
76
            } catch (SecurityException e) {
77
                // TODO Auto-generated catch block
78
                e.printStackTrace();
79
            } catch (NoSuchMethodException e) {
80
                // Resulta que con getMethod no pilla bien el superinterface VectorialLegend, 
81
                // as? que lo hacemos manual
82
                Method[] list = obj.getClass().getMethods();
83
                for (int i=0; i< list.length; i++)
84
                {
85
                    // System.err.println(list[i].toString());
86
                    if (list[i].getName().compareTo(strMethod) == 0)
87
                    {
88
                        boolean encontrado = true;
89
                        Class[] params = list[i].getParameterTypes();
90
                        // En cada par?metro del m?todo, miramos si 
91
                        // el par?metro correspondiente implementa la interfaz
92
                        // adecuada.
93
                        for (int j=0; j < params.length; j++) // for de los par?metros del m?todo.
94
                        {
95
                                // Miramos primero si es de la clase que buscamos, y si no lo es, pasamos
96
                                // a revisar sus interfaces.
97
                                boolean bParamOK = false;
98
                                if (params[j].getName().compareTo(parameters[j].getClass().getName()) == 0)
99
                                {
100
                                        bParamOK = true;
101
                                        continue;
102
                                }
103
                            // Interfaces que implementa el par?metro j-?simo que le pasamos.
104
                            Class[] theInterfaces = parameters[j].getClass().getInterfaces();
105
                            
106
                            for (int k=0; k< theInterfaces.length; k++)
107
                            {
108
                                // Si alguno de estos interfaces cuadra con lo que
109
                                // espera el m?todo, podemos comprobar el siguiente par?metro.
110
                                // Si al salir del for externo, encontrado sigue siendo true, 
111
                                // hemos encontrado el m?todo que busc?bamos.
112
                                if (theInterfaces[k].getName().compareTo(params[j].getName()) == 0)
113
                                {
114
                                    bParamOK = true;
115
                                    break;
116
                                }
117
                            }
118
                            if (bParamOK == false)
119
                            {
120
                                encontrado = false;
121
                                break; // Vamos a por otro m?todo
122
                            }
123
                        }
124
                        if (encontrado)
125
                        {
126
                            method = list[i];
127
                            return;                            
128
                        }
129
                    }
130
                }
131
                e.printStackTrace();
132
            }
133
        }
134
        
135
        public Object executeMethod() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
136
        {
137
            return method.invoke(obj,parameters);
138
        }
139
    }
140
    public static void addToPostProcess(Object obj, String strMethod, Object[] parameters, int priority)
141
    {
142
        Call newCall = new Call(obj, strMethod, parameters, priority); 
143
        callList.add(newCall);
144
    }
145
    public static void addToPostProcess(Object obj, String strMethod, Object parameter, int priority)
146
    {
147
        Object[] parameters = new Object[1];
148
        parameters[0] = parameter;
149
        Call newCall = new Call(obj, strMethod, parameters, priority);
150
        callList.add(newCall);
151
    }
152
    
153
    private static void orderByPriority()
154
    {
155
        // TODO
156
    }
157
    public static void executeCalls()
158
    {
159
        // TODO: Primero deber?amos ordenar por prioridad
160
        // por ahora no lo hago.
161
        orderByPriority();
162
        
163
        for (int i=0; i < callList.size(); i++)
164
        {
165
            Call call = (Call) callList.get(i);
166
            try {
167
                call.executeMethod();
168
            } catch (IllegalArgumentException e) {
169
                // TODO Auto-generated catch block
170
                e.printStackTrace();
171
            } catch (IllegalAccessException e) {
172
                // TODO Auto-generated catch block
173
                e.printStackTrace();
174
            } catch (InvocationTargetException e) {
175
                // TODO Auto-generated catch block
176
                e.printStackTrace();
177
            }
178
        }
179
        clearList();
180
    }
181
    public static void clearList()
182
    {
183
        callList.clear();
184
    }
185
}