All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.osmdroid.server.jdk.TileServer Maven / Gradle / Ivy

There is a newer version: 6.1.20
Show newest version
package org.osmdroid.server.jdk;

import java.util.ArrayList;
import java.util.List;

import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;

/**
 * This is a simple command list web server (jetty based) that starts up a rest endpoint that serves map tiles.
 * 
 * By default it will attempt to start up on port 80.
 * @author Alex O'Ree
 */
public class TileServer {

     static int port = 80;
     static String ENDPOINT_ADDRESS;

     public static void main(String[] args) throws Exception {
          System.out.println("This will listen on port 80 by default for web traffic (on all IP addresses)");
          System.out.println("Usage");
          System.out.println("jar -jar <...with-dependencies.jar> ");
          if (args.length > 0) {
               try {
                    port = Integer.parseInt(args[0]);
               } catch (Exception ex) {
                    ex.printStackTrace();
               }
          }
          ENDPOINT_ADDRESS = "http://0.0.0.0:" + port + "/";
          
          System.out.println("Attempting to bind to " + ENDPOINT_ADDRESS);
          startServer();


          if (System.console()!=null) {
               System.out.println("Server started at " + ENDPOINT_ADDRESS + " press enter to stop.");
               System.console().readLine();
          } else {
               System.out.println("Server started at " + ENDPOINT_ADDRESS + " press Ctrl-C to stop.");
               while (true){
                    Thread.sleep(5000);
               }
          }
          server.stop();
          server.destroy();

     }

     private static Server server;
     static TileFetcher instance = null;

     /**
      * this files up a CXF based Jetty server to host tile rest service
      * @throws Exception 
      */
     private static void startServer() throws Exception {
          JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
          sf.setResourceClasses(TileFetcher.class);

          List providers = new ArrayList();
          // add custom providers if any
          providers.add(new org.apache.cxf.jaxrs.provider.JAXBElementProvider());
          providers.add(new org.apache.cxf.jaxrs.provider.json.JSONProvider());
          sf.setProviders(providers);

          sf.setResourceProvider(TileFetcher.class,
                  new SingletonResourceProvider(new TileFetcher(), true));
          sf.setAddress(ENDPOINT_ADDRESS);

          server = sf.create();

     }
}