Java tutorial
/** * Copyright 2015 OpenSearchServer Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.opensearchserver.affinities; import io.undertow.Undertow; import java.io.File; import java.io.IOException; import java.util.HashSet; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.GnuParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.jboss.resteasy.plugins.providers.jackson.JacksonJsonpInterceptor; import org.jboss.resteasy.plugins.server.undertow.UndertowJaxrsServer; import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; import com.opensearchserver.utils.json.JacksonConfig; @ApplicationPath("/") public class Main extends Application { @Override public Set<Class<?>> getClasses() { HashSet<Class<?>> classes = new HashSet<Class<?>>(); classes.add(JacksonConfig.class); classes.add(JacksonJsonProvider.class); classes.add(JacksonJsonpInterceptor.class); classes.add(AffinitiesService.class); return classes; } public static void main(String[] args) throws IOException, ParseException { Logger.getLogger("").setLevel(Level.WARNING); Options options = new Options(); options.addOption("h", "help", false, "print this message"); options.addOption("d", "datadir", true, "Data directory"); options.addOption("p", "port", true, "TCP port"); CommandLineParser parser = new GnuParser(); CommandLine cmd = parser.parse(options, args); if (cmd.hasOption("help")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("java -jar target/oss-affinities.jar", options); return; } int port = cmd.hasOption("p") ? Integer.parseInt(cmd.getOptionValue("p")) : 9092; File dataDir = new File(System.getProperty("user.home"), "opensearchserver_affinities"); if (cmd.hasOption("d")) dataDir = new File(cmd.getOptionValue("d")); if (!dataDir.exists()) throw new IOException("The data directory does not exists: " + dataDir); if (!dataDir.isDirectory()) throw new IOException("The data directory path is not a directory: " + dataDir); AffinityList.load(dataDir); UndertowJaxrsServer server = new UndertowJaxrsServer() .start(Undertow.builder().addHttpListener(port, "0.0.0.0")); server.deploy(Main.class); } }