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     */
020    package org.dllearner.server;
021    
022    import java.io.BufferedReader;
023    import java.io.File;
024    import java.io.IOException;
025    import java.io.InputStreamReader;
026    import java.net.InetSocketAddress;
027    import java.util.concurrent.ExecutorService;
028    import java.util.concurrent.Executors;
029    
030    import javax.xml.ws.Endpoint;
031    
032    import org.apache.log4j.ConsoleAppender;
033    import org.apache.log4j.FileAppender;
034    import org.apache.log4j.Level;
035    import org.apache.log4j.Logger;
036    import org.apache.log4j.SimpleLayout;
037    
038    import com.sun.net.httpserver.HttpContext;
039    import 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     */
048    public 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                    Endpoint endpoint = Endpoint.create(new DLLearnerWS());
104                    // Endpoint endpoint = Endpoint.create(new CustomDataClass());
105                    HttpContext context = server.createContext("/services");
106                    endpoint.publish(context);
107                    // Endpoint endpoint = Endpoint.publish(url, new DLLearnerWS());
108    
109                    System.out.println("OK.");
110    
111                    if(interactive) {
112                            System.out.println("Type \"exit\" to terminate web service.");
113                            boolean terminate = false;
114                            String inputString = "";
115                            do {
116                                    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
117            
118                                    try {
119                                            inputString = input.readLine();
120                                    } catch (IOException e) {
121                                            e.printStackTrace();
122                                    }
123            
124                                    if (inputString.equals("exit"))
125                                            terminate = true;
126            
127                            } while (!terminate);
128            
129                            System.out.print("Stopping web service ... ");
130                            endpoint.stop();
131            
132                            server.stop(1);
133                            threads.shutdown();
134                            System.out.println("OK.");
135                    }
136    
137            }
138    
139    }