Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / impl / DataStoreProviderToCoverageProviderFactoryWrapper.java @ 43020

History | View | Annotate | Download (4.61 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.impl;
25

    
26
import org.gvsig.fmap.dal.ExternalStoreProviderFactory;
27
import java.lang.reflect.Constructor;
28
import org.gvsig.fmap.dal.DataFactoryUnit;
29

    
30
import org.gvsig.fmap.dal.DataParameters;
31
import org.gvsig.fmap.dal.DataStoreProvider;
32
import org.gvsig.fmap.dal.exception.InitializeException;
33
import org.gvsig.fmap.dal.spi.AbstractDataStoreProviderFactory;
34
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
35
import org.gvsig.tools.dynobject.DynObject;
36
import org.gvsig.tools.service.spi.Services;
37

    
38
public class DataStoreProviderToCoverageProviderFactoryWrapper extends
39
                AbstractDataStoreProviderFactory implements ExternalStoreProviderFactory {
40

    
41
        private Class providerClass = null;
42
        private Class parametersClass = null;
43

    
44
        public DataStoreProviderToCoverageProviderFactoryWrapper(String name, String description,
45
                        Class provider, Class parametersClass) {
46
                super(name,description);
47
                this.providerClass = provider;
48
                this.parametersClass = parametersClass;
49
        }
50

    
51
        @Override
52
        public DataFactoryUnit create(DynObject parameters, Services providerServices) {
53
            try {
54
                return this.createProvider(
55
                        (DataParameters)parameters,
56
                        (DataStoreProviderServices)providerServices
57
                );
58
            } catch (InitializeException ex) {
59
                throw  new RuntimeException(ex);
60
            }
61
        }
62

    
63
        
64
        @Override
65
        public DataStoreProvider createProvider(
66
                DataParameters parameters,
67
                DataStoreProviderServices providerServices
68
            ) throws InitializeException {
69
                try {
70
                        Class[] argsTypes = new Class[] { DataParameters.class,
71
                                        DataStoreProviderServices.class };
72
                        Object[] args = new Object[] { parameters, providerServices };
73

    
74
                        Constructor contructor;
75
                        contructor = findConstructor(providerClass, argsTypes);
76
                        return (DataStoreProvider) contructor.newInstance(args);
77
                } catch (Throwable e) {
78
                        throw new InitializeException(e);
79
                }
80
        }
81

    
82
        @Override
83
        public final DynObject createParameters() {
84
                try {
85
                        return (DynObject) parametersClass.newInstance();
86
                } catch (Exception e) {
87
                        throw new RuntimeException(e);
88
                }
89
        }
90
        
91
        protected Constructor findConstructor(Class clazz, Class[] types)
92
                        throws SecurityException, NoSuchMethodException {
93
                try {
94
                        return clazz.getConstructor(types);
95
                } catch (NoSuchMethodException e) {
96
                        // Nothing to do
97
                }
98

    
99
                // search for the required constructor
100
                Constructor[] constrs = clazz.getConstructors();
101
                boolean allMatch;
102
            for (Constructor constr : constrs) {
103
                Class[] paramTypes = constr.getParameterTypes();
104
                if (paramTypes.length == types.length) {
105
                    // a general candidate
106
                    allMatch = true;
107
                    for (int j = 0; j < paramTypes.length; j++) {
108
                        if (!isParameterCompatible(types[j], paramTypes[j])) {
109
                            allMatch = false;
110
                            break;
111
                                        }
112
                                }
113
                    if (allMatch) {
114
                        return constr;
115
                    }
116
                }
117
            }
118
                StringBuilder strb = new StringBuilder();
119
                strb.append(clazz.getName());
120
                strb.append('(');
121
                if (types.length > 0) {
122
                        for (int i = 0; i < types.length - 1; i++) {
123
                                strb.append(types[i].getName());
124
                                strb.append(',');
125
                        }
126
                        strb.append(types[types.length - 1].getName());
127
                }
128
                strb.append(')');
129
                throw new NoSuchMethodException(strb.toString());
130
        }
131

    
132
        private boolean isParameterCompatible(Class current, Class defined) {
133
                if (current == null) {
134
                        return !defined.isPrimitive();
135
                } else {
136
                        return current.isAssignableFrom(defined);
137
                }
138
        }
139

    
140
}