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

History | View | Annotate | Download (6.3 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
        context.addServlet(VCSGisAuthenticateServlet.class, "/authenticate");
97

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

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

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

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

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

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

    
176
}