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

org.jwall.apache.httpd.ApacheLocal Maven / Gradle / Ivy

The newest version!
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *   Copyright (C) 2010-2014 Christian Bockermann 
 *    
 *   This file is part of the jwall.org apache-config library. The apache-config library is
 *   a parsing library to handle Apache HTTPD configuration files.
 *
 *   More information and documentation for the jwall-tools can be found at
 *   
 *                      http://www.jwall.org/apache-config
 *   
 *   This program is free software; you can redistribute it and/or modify it under
 *   the terms of the GNU General Public License as published by the Free Software
 *   Foundation; either version 3 of the License, or (at your option) any later version.
 *   
 *   This program is distributed in the hope that it will be useful, but WITHOUT ANY
 *   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 *   FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 *   You should have received a copy of the GNU General Public License along with this 
 *   program; if not, see .
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package org.jwall.apache.httpd;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.logging.Logger;



/**
 * 

* This class is a wrapper implementation to gain java access to the * Apache web server. Mainly this class does provide some mechanisms * to guess the right location of the Apache configuration and the * associated files and can be used to make calls to the apachectl * script. *

* @author Christian Bockermann <[email protected]> * @version $Id$ */ public class ApacheLocal implements Apache { private static Logger log = Logger.getLogger( ApacheLocal.class.getName() ); /** Globally accessible version information */ static { System.setProperty("org.apache.httpd.version", "0.1"); } /** This references an instance that can be shared */ private static Apache sharedInstance = null; /** This instance contains all references to the config, the control-script, etc. */ ApacheLayout layout = null; /** * This constructor will initialize some basic structures by guessing * the binary location, configuration files, etc. */ public ApacheLocal(){ layout = ApacheLayout.guess(); } /** * Create a new instance of the Apache controller class by using the settings * provided within the layout object. * * @param layout The settings to be used to control the apache instance. */ public ApacheLocal( ApacheLayout layout ){ this.layout = layout; } /* (non-Javadoc) * @see org.apache.httpd.Apache#getHttpdConfig() */ public File getHttpdConfig(){ return layout.getHttpdConfig(); } /* (non-Javadoc) * @see org.apache.httpd.Apache#start() */ public String start() throws ApacheError { try { log.finest("Calling \"" + layout.apachectl + " start\""); Process p = Runtime.getRuntime().exec( layout.apachectl.getAbsolutePath()+" start"); int ret = p.waitFor(); StringBuffer s = new StringBuffer(); InputStream result; if( ret != 0 ) result = p.getErrorStream(); else result = p.getInputStream(); BufferedReader r = new BufferedReader( new InputStreamReader( result ) ); int ch = r.read(); while( ch >= 0 ){ s.append( (char) ch ); ch = r.read(); } r.close(); if(ret != 0) throw new ApacheError( s.toString() ); //"Start of apache failed, return_code: "+ret); log.finest( "ApacheCtl start returned: " + s.toString() ); return s.toString(); } catch (Exception e ){ e.printStackTrace(); throw new ApacheError( e.getMessage() ); } } /* (non-Javadoc) * @see org.apache.httpd.Apache#stop() */ public String stop() throws Exception { Process p = Runtime.getRuntime().exec( layout.apachectl+" stop"); int ret = p.waitFor(); InputStream result; if( ret != 0 ) result = p.getErrorStream(); else result = p.getInputStream(); BufferedReader r = new BufferedReader( new InputStreamReader( result ) ); StringBuffer s = new StringBuffer(); String line = r.readLine(); while( line != null && r.ready() ) s.append( line + "\n" ); r.close(); if(ret != 0) throw new Exception("Fehler beim Apache-Stop: returnCode="+ret); log.finest( "ApacheCtl stop returned: " + s.toString() ); return s.toString(); } /* (non-Javadoc) * @see org.apache.httpd.Apache#restart() */ public void restart() throws Exception { if( layout.apachectl.exists() ){ Process p = Runtime.getRuntime().exec(layout.apachectl.getAbsolutePath()+" restart"); int ret = p.waitFor(); InputStream result; if( ret != 0 ) result = p.getErrorStream(); else result = p.getInputStream(); BufferedReader r = new BufferedReader( new InputStreamReader( result ) ); StringBuffer s = new StringBuffer(); String line = r.readLine(); while( line != null && r.ready() ) s.append( line + "\n" ); r.close(); if(ret != 0) throw new Exception("" + s.toString() ); } } /* (non-Javadoc) * @see org.apache.httpd.Apache#configCheck() */ public String configCheck() throws Exception { try { String exec = layout.httpd.getAbsolutePath() + " -t -DDUMP_VHOSTS"; log.fine( "Executing: " + exec ); Process p = Runtime.getRuntime().exec( exec ); int status = p.waitFor(); InputStream result; //result = p.getInputStream(); result = p.getErrorStream(); log.fine("return-status: " + status ); BufferedReader r = new BufferedReader( new InputStreamReader( result ) ); StringBuffer s = new StringBuffer(); //int ret = p.waitFor(); boolean b = false; // = ret == 0; String line = null; b = false; do { line =r.readLine(); if( line != null ) s.append( line + "\n" ); if( line != null && !b ){ b = line.indexOf("Syntax OK") >= 0; } } while ( line != null ); return s.toString(); } catch (Exception e) { e.printStackTrace(); return null; } } /** * Return a shared instance of an Apache object. * * @return A globally shared instance. */ public static Apache getInstance(){ if( sharedInstance == null ){ sharedInstance = new ApacheLocal(); } return sharedInstance; } public static void main( String args[] ) throws Exception{ for( String arg : args ){ if( arg.equalsIgnoreCase( "-v" ) || arg.equals( "--version" ) ){ System.out.print( System.getProperty( "org.apache.httpd.version" ) ); System.err.print( System.getProperty( "org.apache.httpd.version" ) ); System.exit(0); } } Apache web = ApacheLocal.getInstance(); log.info("Checking configuration: " + web.configCheck() ); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy