Statistics
| Revision:

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 @ 3315

History | View | Annotate | Download (6.22 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.eclipse.jetty.server.handler.ResourceHandler;
35
import org.gvsig.fmap.dal.DALLocator;
36
import org.gvsig.fmap.dal.DataManager;
37
import org.gvsig.fmap.dal.DataStore;
38
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
39
import org.gvsig.tools.util.HasAFile;
40
import org.gvsig.tools.util.Invocable;
41
import org.gvsig.vcsgis.lib.VCSGisLocator;
42
import org.gvsig.vcsgis.lib.VCSGisManager;
43

    
44
/**
45
 *
46
 * @author gvSIG Team
47
 */
48
public class VCSGisServerUtils {
49

    
50
    /*
51
    https://www.eclipse.org/jetty/documentation/current/
52
    
53
    */
54
    public static final String VCSGIS_CONFIG_DBPARAMS = "org.gvsig.vcsgis.server.params";
55
    
56
    public static final String PROVIDER_NAME = DataStore.H2SPATIAL_PROVIDER_NAME;
57
    public static final String DB_NAME = "vcsgisserver";
58
    public static final int SERVER_PORT = 9810;
59
    
60
    public static Server createServer(
61
            JDBCServerExplorerParameters dbparams, 
62
            String shutdownpass
63
        ) {
64
        return createServer(SERVER_PORT, dbparams, shutdownpass);
65
    }
66
    
67
    public static Server createServer(JDBCServerExplorerParameters dbparams) {
68
        return createServer(SERVER_PORT, dbparams, null);
69
    }
70
    
71
    public static Server createServer(
72
            int port, 
73
            JDBCServerExplorerParameters dbparams, 
74
            String shutdownpass
75
        ) {
76
        Server server = new Server(port);
77

    
78
        ResourceHandler resource_handler = new ResourceHandler();
79
        resource_handler.setResourceBase(".");
80
        resource_handler.setDirAllowed(true);
81
        resource_handler.setDirectoriesListed(true);
82
        
83
        @SuppressWarnings("PointlessBitwiseExpression")
84
        ServletContextHandler context = new ServletContextHandler(
85
                ServletContextHandler.NO_SECURITY | ServletContextHandler.NO_SESSIONS
86
        );
87

    
88
        context.setAttribute(VCSGIS_CONFIG_DBPARAMS, dbparams);
89
        
90
        context.addServlet(HelloServlet.class, "/hello");
91
        
92
        context.addServlet(VCSGisEntitiesServlet.class, "/entities");
93
        context.addServlet(VCSGisCommitServlet.class,   "/commit");
94
        context.addServlet(VCSGisCheckoutServlet.class, "/checkout");
95
        context.addServlet(VCSGisUpdateServlet.class,   "/update");
96

    
97
        context.addServlet(VCSGisUsersServlet.class, "/users");
98
        context.addServlet(VCSGisTopologyPlansServlet.class, "/topologyplans");
99
        
100
        context.addServlet(VCSGisRowCreateServlet.class, "/rowcreate");
101
        context.addServlet(VCSGisRowUpdateServlet.class, "/rowupdate");
102
        context.addServlet(VCSGisRowDeleteServlet.class, "/rowdelete");
103

    
104
        context.addServlet(HookTestServlet.class, "/hooktest");
105

    
106
        HandlerCollection handlers = new HandlerCollection();
107
        handlers.addHandler(context);
108
        handlers.addHandler(resource_handler);
109
        if( shutdownpass!=null ) {
110
            handlers.addHandler(new ShutdownHandler(shutdownpass));
111
        }
112
        server.setHandler(handlers);
113

    
114
        return server;
115
    }
116
    
117
    public static void setAttribute(Server server, String name, Object value) {
118
        ServletContextHandler context = (ServletContextHandler) ((HandlerCollection)(server.getHandler())).getHandlers()[0];
119
        context.setAttribute(name, value);
120
    }
121

    
122
    public static void setMessager(Server server, Invocable messager) {
123
        setAttribute(server, getMessageKey(), messager);
124
    }
125
    
126
    public static void main(String[] args) throws Exception
127
    {
128
             
129
        new DefaultLibrariesInitializer().fullInitialize();
130
        
131
        VCSGisManager manager = VCSGisLocator.getVCSGisManager();
132
        JDBCServerExplorerParameters dbparams = getDBParameters(DB_NAME);
133
        manager.initRepository(dbparams, null);
134
        
135
        Server server = createServer(
136
                SERVER_PORT, 
137
                dbparams,
138
                "pass"
139
        );
140
        server.start();
141
        server.join();
142
    }
143
    
144
    private static JDBCServerExplorerParameters getDBParameters(String dbname)  {
145
        try {
146
            DataManager dataManager = DALLocator.getDataManager();
147
            JDBCServerExplorerParameters conn = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(PROVIDER_NAME);
148
            
149
            File dbfile = getResource(String.format("test-dbs/%s",dbname));
150
            FileUtils.forceMkdir(dbfile.getParentFile());
151
            ((HasAFile)conn).setFile(dbfile);
152
            return conn;
153
        } catch (Exception ex) {
154
            return null;
155
        }
156
    }
157
    
158
    private static File getTargetFolder() throws Exception {
159
        URL url = TestUtils.class.getResource("/");
160
        File x = new File(url.toURI());
161
        File target = x.getParentFile();
162
        return target;
163
    }
164
    
165
    private static File getResource(String name) throws Exception {
166
        File x = new File(getTargetFolder(), name);
167
        return x;
168
    }
169

    
170
    public static String getMessageKey() {
171
        return "org.gvsig.vcsgis.server.lib.message";
172
    }
173
    
174

    
175
}