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 / DataStoreProviderToFeatureStoreProviderFactoryWrapper.java
History | View | Annotate | Download (4.64 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
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 | 42891 | jjdelcerro | package org.gvsig.fmap.dal.impl; |
25 | 40435 | jjdelcerro | |
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 | 42891 | jjdelcerro | import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProviderFactory; |
32 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
33 | import org.gvsig.tools.dynobject.DynObject; |
||
34 | 45037 | jjdelcerro | import org.slf4j.Logger; |
35 | import org.slf4j.LoggerFactory; |
||
36 | 40435 | jjdelcerro | |
37 | 45037 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
38 | public class DataStoreProviderToFeatureStoreProviderFactoryWrapper |
||
39 | extends AbstractFeatureStoreProviderFactory {
|
||
40 | 40435 | jjdelcerro | |
41 | 45037 | jjdelcerro | private static final Logger LOGGER = LoggerFactory.getLogger(DataStoreProviderToFeatureStoreProviderFactoryWrapper.class); |
42 | 40435 | jjdelcerro | |
43 | 45037 | jjdelcerro | private Class providerClass = null; |
44 | private Class parametersClass = null; |
||
45 | 40435 | jjdelcerro | |
46 | 45037 | jjdelcerro | public DataStoreProviderToFeatureStoreProviderFactoryWrapper(String name, String description, |
47 | Class provider, Class parametersClass) { |
||
48 | super(name, description);
|
||
49 | this.providerClass = provider;
|
||
50 | this.parametersClass = parametersClass;
|
||
51 | LOGGER.warn("Creating wrapper for old style FeatureStoreProvider (" + providerClass.getName() + ")."); |
||
52 | } |
||
53 | 40435 | jjdelcerro | |
54 | 45037 | jjdelcerro | @Override
|
55 | public DataStoreProvider createProvider(DataParameters parameters,
|
||
56 | DataStoreProviderServices providerServices) |
||
57 | throws InitializeException {
|
||
58 | try {
|
||
59 | Class[] argsTypes = new Class[]{DataParameters.class, |
||
60 | DataStoreProviderServices.class}; |
||
61 | Object[] args = new Object[]{parameters, providerServices}; |
||
62 | 40435 | jjdelcerro | |
63 | 45037 | jjdelcerro | Constructor contructor;
|
64 | contructor = findConstructor(providerClass, argsTypes); |
||
65 | return (DataStoreProvider) contructor.newInstance(args);
|
||
66 | } catch (Throwable e) { |
||
67 | throw new InitializeException(e); |
||
68 | } |
||
69 | } |
||
70 | 40435 | jjdelcerro | |
71 | 45037 | jjdelcerro | @Override
|
72 | public final DynObject createParameters() { |
||
73 | try {
|
||
74 | return (DynObject) parametersClass.newInstance();
|
||
75 | } catch (Exception e) { |
||
76 | throw new RuntimeException(e); |
||
77 | } |
||
78 | } |
||
79 | 40435 | jjdelcerro | |
80 | 45037 | jjdelcerro | protected Constructor findConstructor(Class clazz, Class[] types) |
81 | throws SecurityException, NoSuchMethodException { |
||
82 | try {
|
||
83 | return clazz.getConstructor(types);
|
||
84 | } catch (NoSuchMethodException e) { |
||
85 | // Nothing to do
|
||
86 | } |
||
87 | 40435 | jjdelcerro | |
88 | 45037 | jjdelcerro | // search for the required constructor
|
89 | Constructor[] constrs = clazz.getConstructors(); |
||
90 | boolean allMatch;
|
||
91 | for (Constructor constr : constrs) { |
||
92 | Class[] paramTypes = constr.getParameterTypes(); |
||
93 | if (paramTypes.length == types.length) {
|
||
94 | // a general candidate
|
||
95 | allMatch = true;
|
||
96 | for (int j = 0; j < paramTypes.length; j++) { |
||
97 | if (!isParameterCompatible(types[j], paramTypes[j])) {
|
||
98 | allMatch = false;
|
||
99 | break;
|
||
100 | } |
||
101 | } |
||
102 | if (allMatch) {
|
||
103 | return constr;
|
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | StringBuilder strb = new StringBuilder(); |
||
108 | strb.append(clazz.getName()); |
||
109 | strb.append('(');
|
||
110 | if (types.length > 0) { |
||
111 | for (int i = 0; i < types.length - 1; i++) { |
||
112 | strb.append(types[i].getName()); |
||
113 | strb.append(',');
|
||
114 | } |
||
115 | strb.append(types[types.length - 1].getName());
|
||
116 | } |
||
117 | strb.append(')');
|
||
118 | throw new NoSuchMethodException(strb.toString()); |
||
119 | } |
||
120 | |||
121 | private boolean isParameterCompatible(Class current, Class defined) { |
||
122 | if (current == null) { |
||
123 | return !defined.isPrimitive();
|
||
124 | } else {
|
||
125 | return current.isAssignableFrom(defined);
|
||
126 | } |
||
127 | } |
||
128 | 40435 | jjdelcerro | } |