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