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 @ 47673
History | View | Annotate | Download (5.17 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 |
import org.slf4j.Logger; |
38 |
import org.slf4j.LoggerFactory; |
39 |
|
40 |
@SuppressWarnings("UseSpecificCatch") |
41 |
public class DataStoreProviderToCoverageProviderFactoryWrapper extends |
42 |
AbstractDataStoreProviderFactory implements ExternalStoreProviderFactory {
|
43 |
|
44 |
private static final Logger LOGGER = LoggerFactory.getLogger(DataStoreProviderToCoverageProviderFactoryWrapper.class); |
45 |
|
46 |
private Class providerClass = null; |
47 |
private Class parametersClass = null; |
48 |
|
49 |
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 |
} |
67 |
} |
68 |
|
69 |
@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 |
|
79 |
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 |
|
87 |
@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 |
|
96 |
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 |
} |
117 |
} |
118 |
if (allMatch) {
|
119 |
return constr;
|
120 |
} |
121 |
} |
122 |
} |
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 |
|
137 |
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 |
|
145 |
} |