svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.h2spatial / org.gvsig.h2spatial.h2gis132 / org.gvsig.h2spatial.h2gis132.provider / src / main / java / org / gvsig / fmap / dal / store / h2 / H2SpatialConnectionParametersHelper.java @ 45901
History | View | Annotate | Download (4.53 KB)
1 |
package org.gvsig.fmap.dal.store.h2; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.util.Properties; |
5 |
import org.apache.commons.lang3.StringUtils; |
6 |
import org.gvsig.fmap.dal.DataParameters; |
7 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
8 |
import org.gvsig.fmap.dal.resource.db.DBParameters; |
9 |
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
10 |
|
11 |
public class H2SpatialConnectionParametersHelper { |
12 |
|
13 |
private final JDBCConnectionParameters parameters; |
14 |
|
15 |
private static final String DATABASE_FILE = "database_file"; |
16 |
private static final String MAXSECONDSIDLE = "MaxSecondsIdle"; |
17 |
private static final String SERVERPORT = "ServerPort"; |
18 |
private static final String SERVERALLOWOTHERS = "ServerAllowOthers"; |
19 |
|
20 |
|
21 |
public H2SpatialConnectionParametersHelper(JDBCConnectionParameters parameters) {
|
22 |
this.parameters = parameters;
|
23 |
} |
24 |
|
25 |
public String getUrl() { |
26 |
String url = (String) this.getDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME); |
27 |
if( StringUtils.isEmpty(url) ) {
|
28 |
url = H2SpatialUtils.getConnectionURL((H2SpatialConnectionParameters) this.parameters);
|
29 |
this.setDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME, url);
|
30 |
} |
31 |
return url;
|
32 |
} |
33 |
|
34 |
public void validate() throws ValidateDataParametersException { |
35 |
if( this.getDynValue(JDBCConnectionParameters.JDBC_DRIVER_CLASS_PARAMTER_NAME) == null ) { |
36 |
this.setDynValue(JDBCConnectionParameters.JDBC_DRIVER_CLASS_PARAMTER_NAME,
|
37 |
H2SpatialHelper.H2SPATIAL_JDBC_DRIVER |
38 |
); |
39 |
} |
40 |
if( getFile() == null ) { |
41 |
if( this.getDynValue(JDBCConnectionParameters.DBNAME_PARAMTER_NAME) == null ) { |
42 |
throw new ValidateDataParametersException(); |
43 |
} else {
|
44 |
setFile(new File((String) this.getDynValue(JDBCConnectionParameters.DBNAME_PARAMTER_NAME))); |
45 |
} |
46 |
} else {
|
47 |
String dbName = (String) this.getDynValue(JDBCConnectionParameters.DBNAME_PARAMTER_NAME); |
48 |
if( StringUtils.isEmpty(dbName) ) {
|
49 |
dbName = getDbNameFromFile(getFile()); |
50 |
this.setDynValue(
|
51 |
JDBCConnectionParameters.DBNAME_PARAMTER_NAME, |
52 |
dbName.toUpperCase() |
53 |
); |
54 |
} |
55 |
} |
56 |
if( this.getDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME) == null ) { |
57 |
String url = H2SpatialUtils.getConnectionURL((H2SpatialConnectionParameters) this.parameters); |
58 |
this.setDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME, url);
|
59 |
} |
60 |
} |
61 |
|
62 |
private Object getDynValue(String name) { |
63 |
return ((DataParameters) this.parameters).getDynValue(name); |
64 |
} |
65 |
|
66 |
private void setDynValue(String name, Object value) { |
67 |
((DataParameters) this.parameters).setDynValue(name, value);
|
68 |
} |
69 |
|
70 |
public int getMaxSecondsIdle() { |
71 |
Integer x = (Integer) this.getDynValue(MAXSECONDSIDLE); |
72 |
if( x == null ) { |
73 |
return -1; |
74 |
} |
75 |
return x;
|
76 |
} |
77 |
|
78 |
public int getServerPort() { |
79 |
Integer x = (Integer) this.getDynValue(SERVERPORT); |
80 |
if( x == null ) { |
81 |
return -1; |
82 |
} |
83 |
return x;
|
84 |
} |
85 |
|
86 |
public boolean getServerAllowOthers() { |
87 |
Boolean x = (Boolean) this.getDynValue(SERVERALLOWOTHERS); |
88 |
if( x == null ) { |
89 |
return false; |
90 |
} |
91 |
return x;
|
92 |
} |
93 |
|
94 |
public File getFile() { |
95 |
File f = (File) this.getDynValue(DATABASE_FILE); |
96 |
if( this.getDynValue(DBParameters.DBNAME_PARAMTER_NAME) == null |
97 |
&& f != null ) {
|
98 |
String dbname = getDbNameFromFile(f);
|
99 |
this.setDynValue(DBParameters.DBNAME_PARAMTER_NAME, dbname);
|
100 |
} |
101 |
return f;
|
102 |
} |
103 |
|
104 |
public void setFile(File database) { |
105 |
this.setDynValue(DATABASE_FILE, database);
|
106 |
if( this.getDynValue(DBParameters.DBNAME_PARAMTER_NAME) == null |
107 |
&& database != null ) {
|
108 |
String dbname = getDbNameFromFile(database);
|
109 |
this.setDynValue(DBParameters.DBNAME_PARAMTER_NAME, dbname);
|
110 |
} |
111 |
} |
112 |
|
113 |
public Properties getProperties() { |
114 |
Properties props = new Properties(); |
115 |
return props;
|
116 |
} |
117 |
|
118 |
private String getDbNameFromFile(File f) { |
119 |
String name = H2SpatialUtils.removeH2FileNameExtension(f.getName());
|
120 |
return name;
|
121 |
} |
122 |
|
123 |
public String getServerPortAsString() { |
124 |
int port = this.getServerPort(); |
125 |
if( port<=0 ) { |
126 |
return null; |
127 |
} |
128 |
String port_s = String.valueOf(port); |
129 |
return port_s;
|
130 |
} |
131 |
} |