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