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 @ 44644
History | View | Annotate | Download (3.73 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 | |||
35 | 43020 | jjdelcerro | public class DataStoreProviderToFeatureStoreProviderFactoryWrapper |
36 | extends AbstractFeatureStoreProviderFactory
|
||
37 | { |
||
38 | 40435 | jjdelcerro | |
39 | private Class providerClass = null; |
||
40 | private Class parametersClass = null; |
||
41 | |||
42 | public DataStoreProviderToFeatureStoreProviderFactoryWrapper(String name, String description, |
||
43 | Class provider, Class parametersClass) { |
||
44 | super(name, description);
|
||
45 | this.providerClass = provider;
|
||
46 | this.parametersClass = parametersClass;
|
||
47 | } |
||
48 | |||
49 | public DataStoreProvider createProvider(DataParameters parameters,
|
||
50 | DataStoreProviderServices providerServices) |
||
51 | throws InitializeException {
|
||
52 | try {
|
||
53 | Class[] argsTypes = new Class[] { DataParameters.class, |
||
54 | DataStoreProviderServices.class }; |
||
55 | Object[] args = new Object[] { parameters, providerServices }; |
||
56 | |||
57 | Constructor contructor;
|
||
58 | contructor = findConstructor(providerClass, argsTypes); |
||
59 | return (DataStoreProvider) contructor.newInstance(args);
|
||
60 | } catch (Throwable e) { |
||
61 | throw new InitializeException(e); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | public final DynObject createParameters() { |
||
66 | try {
|
||
67 | return (DynObject) parametersClass.newInstance();
|
||
68 | } catch (Exception e) { |
||
69 | throw new RuntimeException(e); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | protected Constructor findConstructor(Class clazz, Class[] types) |
||
74 | throws SecurityException, NoSuchMethodException { |
||
75 | try {
|
||
76 | return clazz.getConstructor(types);
|
||
77 | } catch (NoSuchMethodException e) { |
||
78 | // Nothing to do
|
||
79 | } |
||
80 | |||
81 | // search for the required constructor
|
||
82 | Constructor[] constrs = clazz.getConstructors(); |
||
83 | boolean allMatch;
|
||
84 | for (int i = 0; i < constrs.length; i++) { |
||
85 | Class[] paramTypes = constrs[i].getParameterTypes(); |
||
86 | if (paramTypes.length == types.length) { // a general candidate |
||
87 | allMatch = true;
|
||
88 | for (int j = 0; j < paramTypes.length; j++) { |
||
89 | if (!isParameterCompatible(types[j], paramTypes[j])) {
|
||
90 | allMatch = false;
|
||
91 | break;
|
||
92 | } |
||
93 | } |
||
94 | if (allMatch) {
|
||
95 | return constrs[i];
|
||
96 | } |
||
97 | |||
98 | } |
||
99 | } |
||
100 | StringBuffer strb = new StringBuffer(); |
||
101 | strb.append(clazz.getName()); |
||
102 | strb.append('(');
|
||
103 | if (types.length > 0) { |
||
104 | for (int i = 0; i < types.length - 1; i++) { |
||
105 | strb.append(types[i].getName()); |
||
106 | strb.append(',');
|
||
107 | } |
||
108 | strb.append(types[types.length - 1].getName());
|
||
109 | } |
||
110 | strb.append(')');
|
||
111 | throw new NoSuchMethodException(strb.toString()); |
||
112 | } |
||
113 | |||
114 | private boolean isParameterCompatible(Class current, Class defined) { |
||
115 | if (current == null) { |
||
116 | return !defined.isPrimitive();
|
||
117 | } else {
|
||
118 | return current.isAssignableFrom(defined);
|
||
119 | } |
||
120 | } |
||
121 | } |