001/** 002 * Copyright (C) 2007-2008, Jens Lehmann 003 * 004 * This file is part of DL-Learner. 005 * 006 * DL-Learner is free software; you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation; either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * DL-Learner is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 018 * 019 */ 020package org.dllearner.server; 021 022import java.io.BufferedReader; 023import java.io.File; 024import java.io.IOException; 025import java.io.InputStreamReader; 026import java.net.InetSocketAddress; 027import java.util.concurrent.ExecutorService; 028import java.util.concurrent.Executors; 029 030import javax.xml.ws.Endpoint; 031 032import org.apache.log4j.ConsoleAppender; 033import org.apache.log4j.FileAppender; 034import org.apache.log4j.Level; 035import org.apache.log4j.Logger; 036import org.apache.log4j.SimpleLayout; 037 038import com.sun.net.httpserver.HttpContext; 039import com.sun.net.httpserver.HttpServer; 040 041/** 042 * Starts the DL-Learner web service. 043 * 044 * @author Jens Lehmann 045 * @author Sebastian Hellmann 046 * 047 */ 048public class DLLearnerWSStart { 049 050 /** 051 * DL-Learner web service startup method. 052 * 053 * @param args 054 * --non-interactive starts the web service in a mode, where 055 * it does not wait for user input, i.e. it cannot be terminated 056 * using exit. Use this in conjunction with nohup. 057 */ 058 public static void main(String[] args) { 059 060 // "interactive" means that the web service waits for the 061 // user to type "exit" and exit gracefully; it 062 // non-interactive mode, the web service is started and has 063 // to be terminated externally (e.g. killing its process); 064 // when using nohup, please use noninteractive mode 065 boolean interactive = true; 066 if (args.length > 0 && args[0].equals("--non-interactive")) { 067 interactive = false; 068 } 069 070 // create web service logger 071 SimpleLayout layout = new SimpleLayout(); 072 ConsoleAppender consoleAppender = new ConsoleAppender(layout); 073 Logger logger = Logger.getRootLogger(); 074 075 FileAppender fileAppenderNormal = null; 076 File f = new File("log/sparql.txt"); 077 try { 078 fileAppenderNormal = new FileAppender(layout, "log/log.txt", false); 079 f.delete(); 080 f.createNewFile(); 081 } catch (IOException e) { 082 e.printStackTrace(); 083 } 084 085 logger.removeAllAppenders(); 086 logger.addAppender(consoleAppender); 087 logger.addAppender(fileAppenderNormal); 088 logger.setLevel(Level.INFO); 089 090 InetSocketAddress isa = new InetSocketAddress("localhost", 8181); 091 HttpServer server = null; 092 try { 093 server = HttpServer.create(isa, 0); 094 } catch (IOException e1) { 095 e1.printStackTrace(); 096 } 097 ExecutorService threads = Executors.newFixedThreadPool(10); 098 server.setExecutor(threads); 099 server.start(); 100 101 System.out.print("Starting DL-Learner web service at http://" + isa.getHostName() + ":" 102 + isa.getPort() + "/services ... "); 103 DLLearnerWS ws = new DLLearnerWS(); 104 Endpoint endpoint = Endpoint.create(ws); 105 ws.getKnowledgeSources(); 106 // Endpoint endpoint = Endpoint.create(new CustomDataClass()); 107 HttpContext context = server.createContext("/services"); 108 endpoint.publish(context); 109 // Endpoint endpoint = Endpoint.publish(url, new DLLearnerWS()); 110 111 System.out.println("OK."); 112 113 if(interactive) { 114 System.out.println("Type \"exit\" to terminate web service."); 115 boolean terminate = false; 116 String inputString = ""; 117 do { 118 BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); 119 120 try { 121 inputString = input.readLine(); 122 } catch (IOException e) { 123 e.printStackTrace(); 124 } 125 126 if (inputString.equals("exit")) 127 terminate = true; 128 129 } while (!terminate); 130 131 System.out.print("Stopping web service ... "); 132 endpoint.stop(); 133 134 server.stop(1); 135 threads.shutdown(); 136 System.out.println("OK."); 137 } 138 139 } 140 141}