gvsig-projects-pool / org.gvsig.vcsgis / trunk / org.gvsig.vcsgis / org.gvsig.vcsgis.server / org.gvsig.vcsgis.server.lib / src / main / java / org / gvsig / vcsgis / server / lib / VCSGisServerUtils.java @ 2731
History | View | Annotate | Download (5.04 KB)
1 |
/*
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2020 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, see <https://www.gnu.org/licenses/>.
|
18 |
*
|
19 |
* For any additional information, do not hesitate to contact us
|
20 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
21 |
*/
|
22 |
|
23 |
package org.gvsig.vcsgis.server.lib; |
24 |
|
25 |
import java.io.File; |
26 |
import java.net.URL; |
27 |
import org.apache.commons.io.FileUtils; |
28 |
import org.apache.commons.math.stat.inference.TestUtils; |
29 |
import org.eclipse.jetty.server.Server; |
30 |
import org.eclipse.jetty.server.handler.ShutdownHandler; |
31 |
import org.eclipse.jetty.servlet.ServletContextHandler; |
32 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
33 |
import org.eclipse.jetty.server.handler.HandlerCollection; |
34 |
import org.gvsig.fmap.dal.DALLocator; |
35 |
import org.gvsig.fmap.dal.DataManager; |
36 |
import org.gvsig.fmap.dal.DataStore; |
37 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
38 |
import org.gvsig.tools.util.HasAFile; |
39 |
import org.gvsig.vcsgis.lib.VCSGisLocator; |
40 |
import org.gvsig.vcsgis.lib.VCSGisManager; |
41 |
|
42 |
/**
|
43 |
*
|
44 |
* @author gvSIG Team
|
45 |
*/
|
46 |
public class VCSGisServerUtils { |
47 |
|
48 |
/*
|
49 |
https://www.eclipse.org/jetty/documentation/current/
|
50 |
|
51 |
*/
|
52 |
public static final String VCSGIS_CONFIG_DBPARAMS = "org.gvsig.vcsgis.server.params"; |
53 |
|
54 |
public static final String PROVIDER_NAME = DataStore.H2SPATIAL_PROVIDER_NAME; |
55 |
public static final String DB_NAME = "vcsgisserver"; |
56 |
public static final int SERVER_PORT = 9810; |
57 |
|
58 |
public static Server createServer( |
59 |
JDBCServerExplorerParameters dbparams, |
60 |
String shutdownpass
|
61 |
) { |
62 |
return createServer(SERVER_PORT, dbparams, shutdownpass);
|
63 |
} |
64 |
|
65 |
public static Server createServer(JDBCServerExplorerParameters dbparams) { |
66 |
return createServer(SERVER_PORT, dbparams, null); |
67 |
} |
68 |
|
69 |
public static Server createServer( |
70 |
int port,
|
71 |
JDBCServerExplorerParameters dbparams, |
72 |
String shutdownpass
|
73 |
) { |
74 |
Server server = new Server(port);
|
75 |
|
76 |
ServletContextHandler context = new ServletContextHandler(
|
77 |
ServletContextHandler.NO_SECURITY | ServletContextHandler.NO_SESSIONS |
78 |
); |
79 |
|
80 |
context.setAttribute(VCSGIS_CONFIG_DBPARAMS, dbparams); |
81 |
|
82 |
context.addServlet(HelloServlet.class, "/hello");
|
83 |
|
84 |
context.addServlet(VCSGisEntitiesServlet.class, "/entities");
|
85 |
context.addServlet(VCSGisCommitServlet.class, "/commit");
|
86 |
context.addServlet(VCSGisCheckoutServlet.class, "/checkout");
|
87 |
|
88 |
context.addServlet(VCSGisRowCreateServlet.class, "/rowcreate");
|
89 |
context.addServlet(VCSGisRowUpdateServlet.class, "/rowupdate");
|
90 |
context.addServlet(VCSGisRowDeleteServlet.class, "/rowdelete");
|
91 |
|
92 |
HandlerCollection handlers = new HandlerCollection();
|
93 |
handlers.addHandler(context); |
94 |
if( shutdownpass!=null ) { |
95 |
handlers.addHandler(new ShutdownHandler(shutdownpass));
|
96 |
} |
97 |
server.setHandler(handlers); |
98 |
|
99 |
return server;
|
100 |
} |
101 |
|
102 |
public static void main(String[] args) throws Exception |
103 |
{ |
104 |
|
105 |
new DefaultLibrariesInitializer().fullInitialize();
|
106 |
|
107 |
VCSGisManager manager = VCSGisLocator.getVCSGisManager(); |
108 |
JDBCServerExplorerParameters dbparams = getDBParameters(DB_NAME); |
109 |
manager.initRepository(dbparams, null);
|
110 |
|
111 |
Server server = createServer( |
112 |
SERVER_PORT, |
113 |
dbparams, |
114 |
"pass"
|
115 |
); |
116 |
server.start(); |
117 |
server.join(); |
118 |
} |
119 |
|
120 |
private static JDBCServerExplorerParameters getDBParameters(String dbname) { |
121 |
try {
|
122 |
DataManager dataManager = DALLocator.getDataManager(); |
123 |
JDBCServerExplorerParameters conn = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(PROVIDER_NAME); |
124 |
|
125 |
File dbfile = getResource(String.format("test-dbs/%s",dbname)); |
126 |
FileUtils.forceMkdir(dbfile.getParentFile()); |
127 |
((HasAFile)conn).setFile(dbfile); |
128 |
return conn;
|
129 |
} catch (Exception ex) { |
130 |
return null; |
131 |
} |
132 |
} |
133 |
|
134 |
private static File getTargetFolder() throws Exception { |
135 |
URL url = TestUtils.class.getResource("/"); |
136 |
File x = new File(url.toURI()); |
137 |
File target = x.getParentFile();
|
138 |
return target;
|
139 |
} |
140 |
|
141 |
private static File getResource(String name) throws Exception { |
142 |
File x = new File(getTargetFolder(), name); |
143 |
return x;
|
144 |
} |
145 |
|
146 |
|
147 |
|
148 |
} |