com.app.server.ShutDownServer.java Source code

Java tutorial

Introduction

Here is the source code for com.app.server.ShutDownServer.java

Source

package com.app.server;

/*Copyright 2013 - 2015, Arun_Soundararajan (arun_srajan_2007@yahoo.com).and/or its affiliates.
    
All files in this repository or distribution are licensed under the
Apache License, Version 2.0 (the "License");
you may not use any files in this repository or distribution 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.*/

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

import org.apache.commons.digester3.Digester;
import org.apache.commons.digester3.binder.DigesterLoader;
import org.apache.commons.digester3.xmlrules.FromXmlRulesModule;
import org.apache.log4j.Logger;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * This class is the implemsntation for server shutdown
 * @author arun
 *
 */
public class ShutDownServer {
    static Logger log = Logger.getLogger(ShutDownServer.class);

    /**
     * @param args
     * @throws SAXException 
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException, SAXException {
        DigesterLoader serverdigesterLoader = DigesterLoader.newLoader(new FromXmlRulesModule() {

            protected void loadRules() {
                // TODO Auto-generated method stub
                try {
                    loadXMLRules(new InputSource(new FileInputStream("./config/serverconfig-rules.xml")));
                } catch (FileNotFoundException e) {
                    log.error("Could not load rules xml serverconfig-rules.xml", e);
                    // TODO Auto-generated catch block
                    //e.printStackTrace();
                }

            }
        });
        Digester serverdigester = serverdigesterLoader.newDigester();
        ServerConfig serverconfig = (ServerConfig) serverdigester
                .parse(new InputSource(new FileInputStream("./config/serverconfig.xml")));
        try {
            Socket socket = new Socket("localhost", Integer.parseInt(serverconfig.getShutdownport()));
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write("shutdown WebServer\r\n\r\n".getBytes());
            outputStream.close();
        } catch (Exception ex) {
            log.error("Could not able to create socket and write shutdown bytes", ex);
            //e1.printStackTrace();
        }

    }

}