root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / spi / BaseFeatureStoreProviderFactory.java @ 36208
History | View | Annotate | Download (2.98 KB)
1 |
package org.gvsig.fmap.dal.feature.spi; |
---|---|
2 |
|
3 |
import java.lang.reflect.Constructor; |
4 |
import java.util.List; |
5 |
|
6 |
import org.gvsig.fmap.dal.DataParameters; |
7 |
import org.gvsig.fmap.dal.DataStoreProvider; |
8 |
import org.gvsig.fmap.dal.exception.InitializeException; |
9 |
import org.gvsig.fmap.dal.feature.FeatureStoreProviderFactory; |
10 |
import org.gvsig.fmap.dal.spi.AbstractDataStoreProviderFactory; |
11 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
12 |
import org.gvsig.tools.dynobject.DynObject; |
13 |
|
14 |
public class BaseFeatureStoreProviderFactory extends |
15 |
AbstractDataStoreProviderFactory implements FeatureStoreProviderFactory {
|
16 |
|
17 |
private Class providerClass = null; |
18 |
private Class parametersClass = null; |
19 |
|
20 |
public BaseFeatureStoreProviderFactory(String name, String description, |
21 |
Class provider, Class parametersClass) { |
22 |
super(name, description);
|
23 |
this.providerClass = provider;
|
24 |
this.parametersClass = parametersClass;
|
25 |
} |
26 |
|
27 |
public DataStoreProvider createProvider(DataParameters parameters,
|
28 |
DataStoreProviderServices providerServices) |
29 |
throws InitializeException {
|
30 |
try {
|
31 |
Class[] argsTypes = new Class[] { DataParameters.class, |
32 |
DataStoreProviderServices.class }; |
33 |
Object[] args = new Object[] { parameters, providerServices }; |
34 |
|
35 |
Constructor contructor;
|
36 |
contructor = findConstructor(providerClass, argsTypes); |
37 |
return (DataStoreProvider) contructor.newInstance(args);
|
38 |
} catch (Throwable e) { |
39 |
throw new InitializeException(e); |
40 |
} |
41 |
} |
42 |
|
43 |
public final DynObject createParameters() { |
44 |
try {
|
45 |
return (DynObject) parametersClass.newInstance();
|
46 |
} catch (Exception e) { |
47 |
throw new RuntimeException(e); |
48 |
} |
49 |
} |
50 |
|
51 |
protected Constructor findConstructor(Class clazz, Class[] types) |
52 |
throws SecurityException, NoSuchMethodException { |
53 |
try {
|
54 |
return clazz.getConstructor(types);
|
55 |
} catch (NoSuchMethodException e) { |
56 |
// Nothing to do
|
57 |
} |
58 |
|
59 |
// search for the required constructor
|
60 |
Constructor[] constrs = clazz.getConstructors(); |
61 |
boolean allMatch;
|
62 |
for (int i = 0; i < constrs.length; i++) { |
63 |
Class[] paramTypes = constrs[i].getParameterTypes(); |
64 |
if (paramTypes.length == types.length) { // a general candidate |
65 |
allMatch = true;
|
66 |
for (int j = 0; j < paramTypes.length; j++) { |
67 |
if (!isParameterCompatible(types[j], paramTypes[j])) {
|
68 |
allMatch = false;
|
69 |
break;
|
70 |
} |
71 |
} |
72 |
if (allMatch) {
|
73 |
return constrs[i];
|
74 |
} |
75 |
|
76 |
} |
77 |
} |
78 |
StringBuffer strb = new StringBuffer(); |
79 |
strb.append(clazz.getName()); |
80 |
strb.append('(');
|
81 |
if (types.length > 0) { |
82 |
for (int i = 0; i < types.length - 1; i++) { |
83 |
strb.append(types[i].getName()); |
84 |
strb.append(',');
|
85 |
} |
86 |
strb.append(types[types.length - 1].getName());
|
87 |
} |
88 |
strb.append(')');
|
89 |
throw new NoSuchMethodException(strb.toString()); |
90 |
} |
91 |
|
92 |
private boolean isParameterCompatible(Class current, Class defined) { |
93 |
if (current == null) { |
94 |
return !defined.isPrimitive();
|
95 |
} else {
|
96 |
return current.isAssignableFrom(defined);
|
97 |
} |
98 |
} |
99 |
|
100 |
public List getSupportedGeometries() { |
101 |
return null; |
102 |
} |
103 |
|
104 |
public int allowMultipleGeometryTypes() { |
105 |
return UNKNOWN;
|
106 |
} |
107 |
|
108 |
} |