Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.spi / src / main / java / org / gvsig / fmap / dal / feature / spi / DataStoreProviderToFeatureStoreProviderFactoryWrapper.java @ 40559

History | View | Annotate | Download (3.76 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.feature.spi;
25

    
26
import java.lang.reflect.Constructor;
27

    
28
import org.gvsig.fmap.dal.DataParameters;
29
import org.gvsig.fmap.dal.DataStoreProvider;
30
import org.gvsig.fmap.dal.exception.InitializeException;
31
import org.gvsig.fmap.dal.feature.FeatureStoreProviderFactory;
32
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
33
import org.gvsig.tools.dynobject.DynObject;
34

    
35
public class DataStoreProviderToFeatureStoreProviderFactoryWrapper extends
36
                AbstractFeatureStoreProviderFactory implements FeatureStoreProviderFactory {
37

    
38
        private Class providerClass = null;
39
        private Class parametersClass = null;
40

    
41
        public DataStoreProviderToFeatureStoreProviderFactoryWrapper(String name, String description,
42
                        Class provider, Class parametersClass) {
43
                super(name, description);
44
                this.providerClass = provider;
45
                this.parametersClass = parametersClass;
46
        }
47

    
48
        public DataStoreProvider createProvider(DataParameters parameters,
49
                        DataStoreProviderServices providerServices)
50
                        throws InitializeException {
51
                try {
52
                        Class[] argsTypes = new Class[] { DataParameters.class,
53
                                        DataStoreProviderServices.class };
54
                        Object[] args = new Object[] { parameters, providerServices };
55

    
56
                        Constructor contructor;
57
                        contructor = findConstructor(providerClass, argsTypes);
58
                        return (DataStoreProvider) contructor.newInstance(args);
59
                } catch (Throwable e) {
60
                        throw new InitializeException(e);
61
                }
62
        }
63

    
64
        public final DynObject createParameters() {
65
                try {
66
                        return (DynObject) parametersClass.newInstance();
67
                } catch (Exception e) {
68
                        throw new RuntimeException(e);
69
                }
70
        }
71
        
72
        protected Constructor findConstructor(Class clazz, Class[] types)
73
                        throws SecurityException, NoSuchMethodException {
74
                try {
75
                        return clazz.getConstructor(types);
76
                } catch (NoSuchMethodException e) {
77
                        // Nothing to do
78
                }
79

    
80
                // search for the required constructor
81
                Constructor[] constrs = clazz.getConstructors();
82
                boolean allMatch;
83
                for (int i = 0; i < constrs.length; i++) {
84
                        Class[] paramTypes = constrs[i].getParameterTypes();
85
                        if (paramTypes.length == types.length) { // a general candidate
86
                                allMatch = true;
87
                                for (int j = 0; j < paramTypes.length; j++) {
88
                                        if (!isParameterCompatible(types[j], paramTypes[j])) {
89
                                                allMatch = false;
90
                                                break;
91
                                        }
92
                                }
93
                                if (allMatch) {
94
                                        return constrs[i];
95
                                }
96

    
97
                        }
98
                }
99
                StringBuffer strb = new StringBuffer();
100
                strb.append(clazz.getName());
101
                strb.append('(');
102
                if (types.length > 0) {
103
                        for (int i = 0; i < types.length - 1; i++) {
104
                                strb.append(types[i].getName());
105
                                strb.append(',');
106
                        }
107
                        strb.append(types[types.length - 1].getName());
108
                }
109
                strb.append(')');
110
                throw new NoSuchMethodException(strb.toString());
111
        }
112

    
113
        private boolean isParameterCompatible(Class current, Class defined) {
114
                if (current == null) {
115
                        return !defined.isPrimitive();
116
                } else {
117
                        return current.isAssignableFrom(defined);
118
                }
119
        }
120
}