2005-09-20 17:36:22 +02:00
// yacy.java
// -----------------------
2008-07-20 19:14:51 +02:00
// (C) by Michael Peter Christen; mc@yacy.net
2005-09-20 17:36:22 +02:00
// first published on http://www.yacy.net
// Frankfurt, Germany, 2004, 2005
2005-04-07 21:19:42 +02:00
//
2005-09-20 17:36:22 +02:00
// $LastChangedDate$
// $LastChangedRevision$
// $LastChangedBy$
2005-04-07 21:19:42 +02:00
//
2005-09-20 17:36:22 +02:00
// 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 2 of the License, or
// (at your option) any later version.
2005-04-07 21:19:42 +02:00
//
2005-09-20 17:36:22 +02:00
// 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.
2005-04-07 21:19:42 +02:00
//
2005-09-20 17:36:22 +02:00
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2006-06-01 01:31:46 +02:00
2012-07-22 13:18:45 +02:00
package net.yacy ;
2009-10-18 03:38:07 +02:00
2005-05-05 07:32:19 +02:00
import java.io.BufferedReader ;
import java.io.BufferedWriter ;
import java.io.File ;
import java.io.FileInputStream ;
import java.io.FileNotFoundException ;
2005-12-07 02:40:52 +01:00
import java.io.FileOutputStream ;
2005-05-05 07:32:19 +02:00
import java.io.FileWriter ;
import java.io.IOException ;
import java.io.InputStreamReader ;
import java.io.PrintWriter ;
2012-01-15 19:01:05 +01:00
import java.io.RandomAccessFile ;
import java.nio.channels.FileChannel ;
import java.nio.channels.FileLock ;
2005-05-05 07:32:19 +02:00
import java.util.Properties ;
2009-10-20 00:34:44 +02:00
import java.util.concurrent.Semaphore ;
2005-09-27 18:28:55 +02:00
2011-01-03 21:52:54 +01:00
import net.yacy.cora.date.GenericFormatter ;
2012-06-11 16:48:53 +02:00
import net.yacy.cora.lod.JenaTripleStore ;
2011-04-26 15:35:29 +02:00
import net.yacy.cora.protocol.ClientIdentification ;
2010-08-23 14:32:02 +02:00
import net.yacy.cora.protocol.RequestHeader ;
2010-08-23 00:32:39 +02:00
import net.yacy.cora.protocol.http.HTTPClient ;
2011-08-23 22:18:30 +02:00
import net.yacy.cora.sorting.Array ;
2012-09-21 15:48:16 +02:00
import net.yacy.data.Translator ;
2010-08-05 12:43:03 +02:00
import net.yacy.gui.YaCyApp ;
2010-08-05 12:49:58 +02:00
import net.yacy.gui.framework.Browser ;
2009-10-10 01:13:30 +02:00
import net.yacy.kelondro.logging.Log ;
2009-10-10 03:14:19 +02:00
import net.yacy.kelondro.util.FileUtils ;
import net.yacy.kelondro.util.Formatter ;
import net.yacy.kelondro.util.MemoryControl ;
2009-10-20 00:34:44 +02:00
import net.yacy.kelondro.util.OS ;
2011-09-25 18:59:06 +02:00
import net.yacy.peers.operation.yacyBuildProperties ;
import net.yacy.peers.operation.yacyRelease ;
import net.yacy.peers.operation.yacyVersion ;
import net.yacy.search.Switchboard ;
import net.yacy.search.SwitchboardConstants ;
2012-09-21 15:48:16 +02:00
import net.yacy.server.serverCore ;
import net.yacy.server.http.HTTPDemon ;
2012-06-22 11:39:17 +02:00
import com.google.common.io.Files ;
2005-04-07 21:19:42 +02:00
2005-08-02 21:40:29 +02:00
/ * *
2005-12-07 11:31:48 +01:00
* This is the main class of YaCy . Several threads are started from here :
2005-08-02 21:40:29 +02:00
* < ul >
* < li > one single instance of the plasmaSwitchboard is generated , which itself
* starts a thread with a plasmaHTMLCache object . This object simply counts
* files sizes in the cache and terminates them . It also generates a
* plasmaCrawlerLoader object , which may itself start some more httpc - calling
* threads to load web pages . They terminate automatically when a page has
* loaded .
* < li > one serverCore - thread is started , which implements a multi - threaded
* server . The process may start itself many more processes that handle
2006-03-05 11:07:52 +01:00
* connections . lo
2005-08-02 21:40:29 +02:00
* < li > finally , all idle - dependent processes are written in a queue in
* plasmaSwitchboard which are worked off inside an idle - sensitive loop of the
* main process . ( here )
* < / ul >
*
* On termination , the following must be done :
* < ul >
2009-12-10 00:27:26 +01:00
* < li > stop feeding of the crawling process because it otherwise fills the
2005-08-02 21:40:29 +02:00
* indexing queue .
* < li > say goodbye to connected peers and disable new connections . Don ' t wait for
* success .
* < li > first terminate the serverCore thread . This prevents that new cache
* objects are queued .
* < li > wait that the plasmaHTMLCache terminates ( it should be normal that this
* process already has terminated ) .
* < li > then wait for termination of all loader process of the
* plasmaCrawlerLoader .
* < li > work off the indexing and cache storage queue . These values are inside a
* RAM cache and would be lost otherwise .
* < li > write all settings .
* < li > terminate .
* < / ul >
* /
2005-09-20 17:36:22 +02:00
public final class yacy {
2011-07-05 08:54:05 +02:00
2005-04-07 21:19:42 +02:00
// static objects
2009-07-11 19:03:22 +02:00
public static final String vString = yacyBuildProperties . getVersion ( ) ;
2011-02-02 01:06:29 +01:00
public static float version = 0 . 1f ;
2011-07-05 08:54:05 +02:00
2009-07-11 19:03:22 +02:00
public static final String vDATE = yacyBuildProperties . getBuildDate ( ) ;
2007-07-11 01:56:25 +02:00
public static final String copyright = " [ YaCy v " + vString + " , build " + vDATE + " by Michael Christen / www.yacy.net ] " ;
public static final String hline = " ------------------------------------------------------------------------------- " ;
2010-09-10 14:47:24 +02:00
public static final Semaphore shutdownSemaphore = new Semaphore ( 0 ) ;
2011-07-05 08:54:05 +02:00
2007-05-06 10:22:18 +02:00
/ * *
2009-07-19 22:37:44 +02:00
* a reference to the { @link Switchboard } created by the
2007-05-06 10:22:18 +02:00
* { @link yacy # startup ( String , long , long ) } method .
* /
2009-07-19 22:37:44 +02:00
private static Switchboard sb = null ;
2012-06-10 10:23:35 +02:00
public static String homedir ;
2012-06-10 12:58:29 +02:00
public static File dataHome_g ;
2005-08-02 21:40:29 +02:00
/ * *
* Starts up the whole application . Sets up all datastructures and starts
* the main threads .
*
* @param homePath Root - path where all information is to be found .
2005-09-20 12:10:34 +02:00
* @param startupFree free memory at startup time , to be used later for statistics
2005-08-02 21:40:29 +02:00
* /
2011-07-05 08:54:05 +02:00
private static void startup ( final File dataHome , final File appHome , final long startupMemFree , final long startupMemTotal , final boolean gui ) {
2005-05-11 11:44:36 +02:00
try {
// start up
System . out . println ( copyright ) ;
System . out . println ( hline ) ;
2005-08-02 21:40:29 +02:00
2005-04-07 21:19:42 +02:00
// check java version
try {
2008-05-03 12:16:04 +02:00
" a " . codePointAt ( 0 ) ; // needs at least Java 1.5
2008-08-02 14:12:04 +02:00
} catch ( final NoSuchMethodError e ) {
2011-01-02 22:17:21 +01:00
System . err . println ( " STARTUP: Java Version too low. You need at least Java 1.5 to run YaCy " ) ; // TODO: is 1.6 now
2005-12-05 01:17:12 +01:00
Thread . sleep ( 3000 ) ;
2005-04-07 21:19:42 +02:00
System . exit ( - 1 ) ;
}
2011-07-05 08:54:05 +02:00
2006-10-11 20:27:38 +02:00
// ensure that there is a DATA directory, if not, create one and if that fails warn and die
2010-09-02 21:24:22 +02:00
mkdirsIfNeseccary ( dataHome ) ;
2012-06-10 12:58:29 +02:00
dataHome_g = dataHome ;
2010-09-02 21:24:22 +02:00
mkdirsIfNeseccary ( appHome ) ;
File f = new File ( dataHome , " DATA/ " ) ;
2008-08-06 21:43:12 +02:00
mkdirsIfNeseccary ( f ) ;
2011-07-05 08:54:05 +02:00
if ( ! ( f . exists ( ) ) ) {
System . err . println ( " Error creating DATA-directory in " + dataHome . toString ( ) + " . Please check your write-permission for this folder. YaCy will now terminate. " ) ;
System . exit ( - 1 ) ;
2006-10-11 20:27:38 +02:00
}
2012-06-11 16:48:53 +02:00
2012-06-10 10:23:35 +02:00
homedir = appHome . toString ( ) ;
2011-07-05 08:54:05 +02:00
2005-06-09 11:46:43 +02:00
// setting up logging
2010-09-02 21:24:22 +02:00
f = new File ( dataHome , " DATA/LOG/ " ) ;
2008-08-06 21:43:12 +02:00
mkdirsIfNeseccary ( f ) ;
2010-09-02 21:24:22 +02:00
f = new File ( dataHome , " DATA/LOG/yacy.logging " ) ;
2012-05-17 04:26:03 +02:00
final File f0 = new File ( appHome , " defaults/yacy.logging " ) ;
2011-09-05 01:28:15 +02:00
if ( ! f . exists ( ) | | f0 . lastModified ( ) > f . lastModified ( ) ) try {
2012-06-22 11:39:17 +02:00
Files . copy ( f0 , f ) ;
2011-09-05 01:28:15 +02:00
} catch ( final IOException e ) {
System . out . println ( " could not copy yacy.logging " ) ;
}
2005-09-27 18:28:55 +02:00
try {
2010-09-02 21:24:22 +02:00
Log . configureLogging ( dataHome , appHome , new File ( dataHome , " DATA/LOG/yacy.logging " ) ) ;
2008-08-02 14:12:04 +02:00
} catch ( final IOException e ) {
2010-09-02 21:24:22 +02:00
System . out . println ( " could not find logging properties in homePath= " + dataHome ) ;
2009-11-05 21:28:37 +01:00
Log . logException ( e ) ;
2005-06-13 14:01:58 +02:00
}
2010-01-10 17:10:11 +01:00
Log . logConfig ( " STARTUP " , " YaCy version: " + yacyBuildProperties . getVersion ( ) + " / " + yacyBuildProperties . getSVNRevision ( ) ) ;
2009-01-31 00:33:47 +01:00
Log . logConfig ( " STARTUP " , " Java version: " + System . getProperty ( " java.version " , " no-java-version " ) ) ;
Log . logConfig ( " STARTUP " , " Operation system: " + System . getProperty ( " os.name " , " unknown " ) ) ;
2010-09-02 21:24:22 +02:00
Log . logConfig ( " STARTUP " , " Application root-path: " + appHome ) ;
Log . logConfig ( " STARTUP " , " Data root-path: " + dataHome ) ;
2011-01-03 21:52:54 +01:00
Log . logConfig ( " STARTUP " , " Time zone: UTC " + GenericFormatter . UTCDiffString ( ) + " ; UTC+0000 is " + System . currentTimeMillis ( ) ) ;
2009-10-20 00:34:44 +02:00
Log . logConfig ( " STARTUP " , " Maximum file system path length: " + OS . maxPathLength ) ;
2011-07-05 08:54:05 +02:00
2010-09-02 21:24:22 +02:00
f = new File ( dataHome , " DATA/yacy.running " ) ;
2007-06-08 14:45:03 +02:00
if ( f . exists ( ) ) { // another instance running? VM crash? User will have to care about this
2009-01-31 00:33:47 +01:00
Log . logSevere ( " STARTUP " , " WARNING: the file " + f + " exists, this usually means that a YaCy instance is still running " ) ;
2008-08-06 21:43:12 +02:00
delete ( f ) ;
2007-06-08 14:45:03 +02:00
}
2008-08-06 21:43:12 +02:00
if ( ! f . createNewFile ( ) )
2009-01-31 00:33:47 +01:00
Log . logSevere ( " STARTUP " , " WARNING: the file " + f + " can not be created! " ) ;
2011-07-05 08:54:05 +02:00
try { new FileOutputStream ( f ) . write ( Integer . toString ( OS . getPID ( ) ) . getBytes ( ) ) ; } catch ( final Exception e ) { } // write PID
2007-06-13 22:55:48 +02:00
f . deleteOnExit ( ) ;
2012-01-15 19:01:05 +01:00
FileChannel channel = null ;
FileLock lock = null ;
try {
channel = new RandomAccessFile ( f , " rw " ) . getChannel ( ) ;
lock = channel . tryLock ( ) ; // lock yacy.running
2012-07-05 11:18:31 +02:00
} catch ( final Exception e ) { }
2011-07-05 08:54:05 +02:00
2008-08-02 14:12:04 +02:00
final String oldconf = " DATA/SETTINGS/httpProxy.conf " . replace ( " / " , File . separator ) ;
final String newconf = " DATA/SETTINGS/yacy.conf " . replace ( " / " , File . separator ) ;
2010-09-02 21:24:22 +02:00
final File oldconffile = new File ( dataHome , oldconf ) ;
2008-03-16 23:31:54 +01:00
if ( oldconffile . exists ( ) ) {
2010-09-02 21:24:22 +02:00
final File newconfFile = new File ( dataHome , newconf ) ;
2008-08-06 21:43:12 +02:00
if ( ! oldconffile . renameTo ( newconfFile ) )
2009-01-31 00:33:47 +01:00
Log . logSevere ( " STARTUP " , " WARNING: the file " + oldconffile + " can not be renamed to " + newconfFile + " ! " ) ;
2008-03-16 23:31:54 +01:00
}
2011-09-01 15:13:21 +02:00
try {
sb = new Switchboard ( dataHome , appHome , " defaults/yacy.init " . replace ( " / " , File . separator ) , newconf ) ;
} catch ( final RuntimeException e ) {
2011-10-19 01:38:19 +02:00
Log . logSevere ( " STARTUP " , " YaCy cannot start: " + e . getMessage ( ) , e ) ;
2011-09-01 15:13:21 +02:00
System . exit ( - 1 ) ;
}
2008-11-22 00:21:33 +01:00
//sbSync.V(); // signal that the sb reference was set
2011-08-25 12:35:25 +02:00
2011-08-22 19:50:03 +02:00
// switch the memory strategy
MemoryControl . setStandardStrategy ( sb . getConfigBool ( " memory.standardStrategy " , true ) ) ;
2011-07-05 08:54:05 +02:00
2005-09-20 12:10:34 +02:00
// save information about available memory at startup time
2005-09-21 02:12:37 +02:00
sb . setConfig ( " memoryFreeAfterStartup " , startupMemFree ) ;
sb . setConfig ( " memoryTotalAfterStartup " , startupMemTotal ) ;
2011-07-05 08:54:05 +02:00
2010-08-05 12:43:03 +02:00
// start gui if wanted
2011-01-28 11:54:13 +01:00
if ( gui ) YaCyApp . start ( " localhost " , ( int ) sb . getConfigLong ( " port " , 8090 ) ) ;
2011-07-05 08:54:05 +02:00
2005-06-30 00:55:37 +02:00
// hardcoded, forced, temporary value-migration
2005-09-21 02:12:37 +02:00
sb . setConfig ( " htTemplatePath " , " htroot/env/templates " ) ;
2005-08-02 21:40:29 +02:00
2009-07-12 11:38:03 +02:00
int oldRev ;
2010-08-05 12:43:03 +02:00
try {
2009-07-12 11:38:03 +02:00
oldRev = Integer . parseInt ( sb . getConfig ( " svnRevision " , " 0 " ) ) ;
2011-07-05 08:54:05 +02:00
} catch ( final NumberFormatException e ) {
2009-07-12 11:38:03 +02:00
oldRev = 0 ;
2010-08-05 12:43:03 +02:00
}
2011-07-05 08:54:05 +02:00
final int newRev = Integer . parseInt ( yacyBuildProperties . getSVNRevision ( ) ) ;
2009-07-12 11:38:03 +02:00
sb . setConfig ( " svnRevision " , yacyBuildProperties . getSVNRevision ( ) ) ;
2010-09-02 21:24:22 +02:00
sb . setConfig ( " applicationRoot " , appHome . toString ( ) ) ;
sb . setConfig ( " dataRoot " , dataHome . toString ( ) ) ;
2007-04-27 11:23:44 +02:00
yacyVersion . latestRelease = version ;
2005-08-02 21:40:29 +02:00
2005-05-11 11:44:36 +02:00
// read environment
2009-06-11 12:20:31 +02:00
final int timeout = Math . max ( 5000 , Integer . parseInt ( sb . getConfig ( " httpdTimeout " , " 5000 " ) ) ) ;
2005-08-02 21:40:29 +02:00
2005-04-07 21:19:42 +02:00
// create some directories
2010-09-02 21:24:22 +02:00
final File htRootPath = new File ( appHome , sb . getConfig ( " htRootPath " , " htroot " ) ) ;
final File htDocsPath = sb . getDataPath ( SwitchboardConstants . HTDOCS_PATH , SwitchboardConstants . HTDOCS_PATH_DEFAULT ) ;
2008-08-06 21:43:12 +02:00
mkdirIfNeseccary ( htDocsPath ) ;
2005-12-05 10:13:13 +01:00
//final File htTemplatePath = new File(homePath, sb.getConfig("htTemplatePath","htdocs"));
2005-08-02 21:40:29 +02:00
2005-08-02 02:16:19 +02:00
// create default notifier picture
2006-02-03 22:21:42 +01:00
//TODO: Use templates instead of copying images ...
2006-02-04 11:50:22 +01:00
if ( ! ( ( new File ( htDocsPath , " notifier.gif " ) ) . exists ( ) ) ) try {
2012-06-22 11:39:17 +02:00
Files . copy ( new File ( htRootPath , " env/grafics/empty.gif " ) ,
2006-02-03 22:21:42 +01:00
new File ( htDocsPath , " notifier.gif " ) ) ;
2008-08-02 14:12:04 +02:00
} catch ( final IOException e ) { }
2005-08-02 21:40:29 +02:00
2008-02-18 17:38:06 +01:00
final File htdocsReadme = new File ( htDocsPath , " readme.txt " ) ;
2009-01-31 02:06:56 +01:00
if ( ! ( htdocsReadme . exists ( ) ) ) try { FileUtils . copy ( (
2005-05-11 11:44:36 +02:00
" This is your root directory for individual Web Content \ r \ n " +
" \ r \ n " +
" Please place your html files into the www subdirectory. \ r \ n " +
" The URL of that path is either \ r \ n " +
" http://www.<your-peer-name>.yacy or \ r \ n " +
" http://<your-ip>:<your-port>/www \ r \ n " +
" \ r \ n " +
" Other subdirectories may be created; they map to corresponding sub-domains. \ r \ n " +
" This directory shares it's content with the applications htroot path, so you \ r \ n " +
" may access your yacy search page with \ r \ n " +
" http://<your-peer-name>.yacy/ \ r \ n " +
2008-08-02 14:12:04 +02:00
" \ r \ n " ) . getBytes ( ) , htdocsReadme ) ; } catch ( final IOException e ) {
2005-07-03 14:40:36 +02:00
System . out . println ( " Error creating htdocs readme: " + e . getMessage ( ) ) ;
}
2005-08-02 21:40:29 +02:00
2005-09-20 17:36:22 +02:00
final File wwwDefaultPath = new File ( htDocsPath , " www " ) ;
2008-08-06 21:43:12 +02:00
mkdirIfNeseccary ( wwwDefaultPath ) ;
2005-08-02 21:40:29 +02:00
2005-09-20 17:36:22 +02:00
final File shareDefaultPath = new File ( htDocsPath , " share " ) ;
2008-08-06 21:43:12 +02:00
mkdirIfNeseccary ( shareDefaultPath ) ;
2005-08-02 21:40:29 +02:00
2006-02-12 17:46:43 +01:00
migration . migrate ( sb , oldRev , newRev ) ;
2011-07-05 08:54:05 +02:00
2008-04-22 18:53:53 +02:00
// delete old release files
2008-08-02 14:12:04 +02:00
final int deleteOldDownloadsAfterDays = ( int ) sb . getConfigLong ( " update.deleteOld " , 30 ) ;
2009-05-02 14:12:22 +02:00
yacyRelease . deleteOldDownloads ( sb . releasePath , deleteOldDownloadsAfterDays ) ;
2011-07-05 08:54:05 +02:00
2008-05-04 14:18:00 +02:00
// set user-agent
2011-04-26 15:35:29 +02:00
HTTPClient . setDefaultUserAgent ( ClientIdentification . getUserAgent ( ) ) ;
2012-06-11 16:48:53 +02:00
2012-02-13 18:04:22 +01:00
// initial fill of the triplestore
2012-06-17 10:50:12 +02:00
File triplestore = new File ( sb . getConfig ( " triplestore " , new File ( dataHome , " DATA/TRIPLESTORE " ) . getAbsolutePath ( ) ) ) ;
mkdirIfNeseccary ( triplestore ) ;
for ( String s : triplestore . list ( ) ) {
if ( ( s . endsWith ( " .rdf " ) | | s . endsWith ( " .nt " ) ) & & ! s . equals ( " local.rdf " ) & & ! s . endsWith ( " _triplestore.rdf " ) & & ! s . startsWith ( " private_store_ " ) ) {
try {
JenaTripleStore . load ( new File ( triplestore , s ) . getAbsolutePath ( ) ) ;
} catch ( IOException e ) {
Log . logException ( e ) ;
}
}
}
if ( sb . getConfigBool ( " triplestore.persistent " , false ) ) {
File local = new File ( triplestore , " local.rdf " ) ;
if ( local . exists ( ) ) {
try {
JenaTripleStore . load ( local . getAbsolutePath ( ) ) ;
} catch ( IOException e ) {
Log . logException ( e ) ;
}
2012-06-11 16:48:53 +02:00
}
2012-02-13 18:04:22 +01:00
}
2011-07-05 08:54:05 +02:00
2005-05-11 11:44:36 +02:00
// start main threads
2011-01-28 11:54:13 +01:00
final String port = sb . getConfig ( " port " , " 8090 " ) ;
2005-05-11 11:44:36 +02:00
try {
2009-07-19 22:37:44 +02:00
final HTTPDemon protocolHandler = new HTTPDemon ( sb ) ;
2005-11-15 16:03:15 +01:00
final serverCore server = new serverCore (
2005-05-11 11:44:36 +02:00
timeout /*control socket timeout in milliseconds*/ ,
true /* block attacks (wrong protocol) */ ,
protocolHandler /*command class*/ ,
2005-09-21 02:12:37 +02:00
sb ,
2005-06-10 11:19:24 +02:00
30000 /*command max length incl. GET args*/ ) ;
2005-05-11 11:44:36 +02:00
server . setName ( " httpd: " + port ) ;
2005-07-03 14:40:36 +02:00
server . setPriority ( Thread . MAX_PRIORITY ) ;
2005-10-31 11:46:13 +01:00
server . setObeyIntermission ( false ) ;
2011-07-05 08:54:05 +02:00
2010-06-26 12:32:47 +02:00
// start the server
sb . deployThread ( " 10_httpd " , " HTTPD Server/Proxy " , " the HTTPD, used as web server and proxy " , null , server , 0 , 0 , 0 , 0 ) ;
//server.start();
2005-08-02 21:40:29 +02:00
2010-06-26 12:32:47 +02:00
// open the browser window
final boolean browserPopUpTrigger = sb . getConfig ( SwitchboardConstants . BROWSER_POP_UP_TRIGGER , " true " ) . equals ( " true " ) ;
2011-03-30 02:49:47 +02:00
if ( browserPopUpTrigger ) try {
2010-06-26 12:32:47 +02:00
final String browserPopUpPage = sb . getConfig ( SwitchboardConstants . BROWSER_POP_UP_PAGE , " ConfigBasic.html " ) ;
2012-07-10 22:59:03 +02:00
//boolean properPW = (sb.getConfig("adminAccount", "").isEmpty()) && (sb.getConfig(httpd.ADMIN_ACCOUNT_B64MD5, "").length() > 0);
2010-06-26 12:32:47 +02:00
//if (!properPW) browserPopUpPage = "ConfigBasic.html";
2011-02-14 17:15:14 +01:00
Browser . openBrowser ( ( server . withSSL ( ) ? " https " : " http " ) + " ://localhost: " + serverCore . getPortNr ( port ) + " / " + browserPopUpPage ) ;
2011-11-27 17:54:48 +01:00
} catch ( final Throwable e ) {
2011-07-05 08:54:05 +02:00
// cannot open browser. This may be normal in headless environments
//Log.logException(e);
2010-06-26 12:32:47 +02:00
}
2011-07-05 08:54:05 +02:00
2012-01-18 20:47:09 +01:00
// enable browser popup, http server is ready now
sb . tray . setReady ( ) ;
2010-06-26 12:32:47 +02:00
//regenerate Locales from Translationlist, if needed
2011-12-08 10:25:26 +01:00
final File locale_source = sb . getAppPath ( " locale.source " , " locales " ) ;
2010-06-26 12:32:47 +02:00
final String lang = sb . getConfig ( " locale.language " , " " ) ;
if ( ! lang . equals ( " " ) & & ! lang . equals ( " default " ) ) { //locale is used
String currentRev = " " ;
2005-05-27 10:36:07 +02:00
try {
2010-09-02 21:24:22 +02:00
final BufferedReader br = new BufferedReader ( new InputStreamReader ( new FileInputStream ( new File ( sb . getDataPath ( " locale.translated_html " , " DATA/LOCALE/htroot " ) , lang + " /version " ) ) ) ) ;
2010-06-26 12:32:47 +02:00
currentRev = br . readLine ( ) ;
br . close ( ) ;
} catch ( final IOException e ) {
//Error
2005-05-27 10:36:07 +02:00
}
2005-08-02 21:40:29 +02:00
2010-06-26 12:32:47 +02:00
if ( ! currentRev . equals ( sb . getConfig ( " svnRevision " , " " ) ) ) try { //is this another version?!
final File sourceDir = new File ( sb . getConfig ( " htRootPath " , " htroot " ) ) ;
2010-09-02 21:24:22 +02:00
final File destDir = new File ( sb . getDataPath ( " locale.translated_html " , " DATA/LOCALE/htroot " ) , lang ) ;
2011-12-08 10:25:26 +01:00
if ( Translator . translateFilesRecursive ( sourceDir , destDir , new File ( locale_source , lang + " .lng " ) , " html,template,inc " , " locale " ) ) { //translate it
2010-06-26 12:32:47 +02:00
//write the new Versionnumber
final BufferedWriter bw = new BufferedWriter ( new PrintWriter ( new FileWriter ( new File ( destDir , " version " ) ) ) ) ;
bw . write ( sb . getConfig ( " svnRevision " , " Error getting Version " ) ) ;
bw . close ( ) ;
2005-07-03 14:40:36 +02:00
}
2010-06-26 12:32:47 +02:00
} catch ( final IOException e ) { }
}
// initialize number formatter with this locale
Formatter . setLocale ( lang ) ;
2011-07-05 08:54:05 +02:00
2010-06-26 12:32:47 +02:00
// registering shutdown hook
Log . logConfig ( " STARTUP " , " Registering Shutdown Hook " ) ;
final Runtime run = Runtime . getRuntime ( ) ;
run . addShutdownHook ( new shutdownHookThread ( Thread . currentThread ( ) , sb ) ) ;
2005-08-02 21:40:29 +02:00
2010-06-26 12:32:47 +02:00
// save information about available memory after all initializations
//try {
sb . setConfig ( " memoryFreeAfterInitBGC " , MemoryControl . free ( ) ) ;
sb . setConfig ( " memoryTotalAfterInitBGC " , MemoryControl . total ( ) ) ;
System . gc ( ) ;
sb . setConfig ( " memoryFreeAfterInitAGC " , MemoryControl . free ( ) ) ;
sb . setConfig ( " memoryTotalAfterInitAGC " , MemoryControl . total ( ) ) ;
//} catch (ConcurrentModificationException e) {}
2011-07-05 08:54:05 +02:00
2010-06-26 12:32:47 +02:00
// wait for server shutdown
try {
sb . waitForShutdown ( ) ;
} catch ( final Exception e ) {
Log . logSevere ( " MAIN CONTROL LOOP " , " PANIC: " + e . getMessage ( ) , e ) ;
}
// shut down
2011-07-16 12:08:43 +02:00
Array . terminate ( ) ;
2010-06-26 12:32:47 +02:00
Log . logConfig ( " SHUTDOWN " , " caught termination signal " ) ;
server . terminate ( false ) ;
server . interrupt ( ) ;
server . close ( ) ;
2011-07-05 08:54:05 +02:00
2010-06-26 12:32:47 +02:00
// idle until the processes are down
if ( server . isAlive ( ) ) {
2005-05-11 11:44:36 +02:00
server . interrupt ( ) ;
2005-04-07 21:19:42 +02:00
}
2010-06-26 12:32:47 +02:00
Log . logConfig ( " SHUTDOWN " , " server has terminated " ) ;
sb . close ( ) ;
2008-08-02 14:12:04 +02:00
} catch ( final Exception e ) {
2009-01-31 00:33:47 +01:00
Log . logSevere ( " STARTUP " , " Unexpected Error: " + e . getClass ( ) . getName ( ) , e ) ;
2005-05-11 11:44:36 +02:00
//System.exit(1);
}
2012-01-15 19:01:05 +01:00
if ( lock ! = null ) lock . release ( ) ;
if ( channel ! = null ) channel . close ( ) ;
2008-08-02 14:12:04 +02:00
} catch ( final Exception ee ) {
2009-01-31 00:33:47 +01:00
Log . logSevere ( " STARTUP " , " FATAL ERROR: " + ee . getMessage ( ) , ee ) ;
2007-05-06 10:22:18 +02:00
} finally {
2005-05-11 11:44:36 +02:00
}
2012-06-11 16:48:53 +02:00
2012-06-17 10:50:12 +02:00
// save the triple store
2012-06-10 21:49:02 +02:00
if ( sb . getConfigBool ( " triplestore.persistent " , false ) ) {
2012-06-17 10:50:12 +02:00
JenaTripleStore . saveAll ( ) ;
2012-02-13 18:04:22 +01:00
}
2012-06-11 16:48:53 +02:00
2009-01-31 00:33:47 +01:00
Log . logConfig ( " SHUTDOWN " , " goodbye. (this is the last line) " ) ;
2009-06-15 23:39:24 +02:00
Log . shutdown ( ) ;
2010-09-10 14:47:24 +02:00
shutdownSemaphore . release ( 1000 ) ;
2008-09-04 23:28:00 +02:00
try {
System . exit ( 0 ) ;
2011-07-05 08:54:05 +02:00
} catch ( final Exception e ) { } // was once stopped by de.anomic.net.ftpc$sm.checkExit(ftpc.java:1790)
2005-04-07 21:19:42 +02:00
}
2005-08-02 21:40:29 +02:00
2008-08-06 21:43:12 +02:00
/ * *
* @param f
* /
2011-07-05 08:54:05 +02:00
private static void delete ( final File f ) {
2008-08-06 21:43:12 +02:00
if ( ! f . delete ( ) )
2009-01-31 00:33:47 +01:00
Log . logSevere ( " STARTUP " , " WARNING: the file " + f + " can not be deleted! " ) ;
2008-08-06 21:43:12 +02:00
}
/ * *
* @see File # mkdir ( )
* @param path
* /
private static void mkdirIfNeseccary ( final File path ) {
if ( ! ( path . exists ( ) ) )
if ( ! path . mkdir ( ) )
2009-01-31 00:33:47 +01:00
Log . logWarning ( " STARTUP " , " could not create directory " + path . toString ( ) ) ;
2008-08-06 21:43:12 +02:00
}
/ * *
* @see File # mkdirs ( )
* @param path
* /
2012-06-10 12:58:29 +02:00
public static void mkdirsIfNeseccary ( final File path ) {
2008-08-06 21:43:12 +02:00
if ( ! ( path . exists ( ) ) )
if ( ! path . mkdirs ( ) )
2009-01-31 00:33:47 +01:00
Log . logWarning ( " STARTUP " , " could not create directories " + path . toString ( ) ) ;
2008-08-06 21:43:12 +02:00
}
/ * *
2005-08-02 21:40:29 +02:00
* Loads the configuration from the data - folder .
* FIXME : Why is this called over and over again from every method , instead
* of setting the configurationdata once for this class in main ?
*
* @param mes Where are we called from , so that the errormessages can be
* more descriptive .
* @param homePath Root - path where all the information is to be found .
* @return Properties read from the configurationfile .
* /
2008-08-02 14:12:04 +02:00
private static Properties configuration ( final String mes , final File homePath ) {
2009-01-31 00:33:47 +01:00
Log . logConfig ( mes , " Application Root Path: " + homePath . toString ( ) ) ;
2005-08-02 21:40:29 +02:00
2005-04-07 21:19:42 +02:00
// read data folder
2008-08-02 14:12:04 +02:00
final File dataFolder = new File ( homePath , " DATA " ) ;
2005-04-07 21:19:42 +02:00
if ( ! ( dataFolder . exists ( ) ) ) {
2009-01-31 00:33:47 +01:00
Log . logSevere ( mes , " Application was never started or root path wrong. " ) ;
2005-04-07 21:19:42 +02:00
System . exit ( - 1 ) ;
}
2005-08-02 21:40:29 +02:00
2008-08-02 14:12:04 +02:00
final Properties config = new Properties ( ) ;
2008-08-06 21:43:12 +02:00
FileInputStream fis = null ;
try {
fis = new FileInputStream ( new File ( homePath , " DATA/SETTINGS/yacy.conf " ) ) ;
config . load ( fis ) ;
2008-08-02 14:12:04 +02:00
} catch ( final FileNotFoundException e ) {
2009-01-31 00:33:47 +01:00
Log . logSevere ( mes , " could not find configuration file. " ) ;
2005-04-07 21:19:42 +02:00
System . exit ( - 1 ) ;
2008-08-02 14:12:04 +02:00
} catch ( final IOException e ) {
2009-01-31 00:33:47 +01:00
Log . logSevere ( mes , " could not read configuration file. " ) ;
2005-04-07 21:19:42 +02:00
System . exit ( - 1 ) ;
2008-08-06 21:43:12 +02:00
} finally {
if ( fis ! = null ) {
try {
fis . close ( ) ;
2011-07-05 08:54:05 +02:00
} catch ( final IOException e ) {
2009-11-05 21:28:37 +01:00
Log . logException ( e ) ;
2008-08-06 21:43:12 +02:00
}
}
2005-04-07 21:19:42 +02:00
}
2005-08-02 21:40:29 +02:00
2005-04-07 21:19:42 +02:00
return config ;
}
2011-07-05 08:54:05 +02:00
2005-08-02 21:40:29 +02:00
/ * *
2006-05-26 14:18:12 +02:00
* Call the shutdown - page of YaCy to tell it to shut down . This method is
2005-08-02 21:40:29 +02:00
* called if you start yacy with the argument - shutdown .
*
* @param homePath Root - path where all the information is to be found .
* /
2009-06-11 13:31:26 +02:00
public static void shutdown ( final File homePath ) {
2005-04-07 21:19:42 +02:00
// start up
System . out . println ( copyright ) ;
System . out . println ( hline ) ;
2009-06-11 13:31:26 +02:00
submitURL ( homePath , " Steering.html?shutdown= " , " Terminate YaCy " ) ;
}
2005-08-02 21:40:29 +02:00
2009-06-11 13:31:26 +02:00
public static void update ( final File homePath ) {
// start up
System . out . println ( copyright ) ;
System . out . println ( hline ) ;
submitURL ( homePath , " ConfigUpdate_p.html?autoUpdate= " , " Update YaCy to most recent version " ) ;
}
2011-07-05 08:54:05 +02:00
private static void submitURL ( final File homePath , final String path , final String processdescription ) {
2009-06-11 13:31:26 +02:00
final Properties config = configuration ( " COMMAND-STEERING " , homePath ) ;
2005-08-02 21:40:29 +02:00
2005-04-07 21:19:42 +02:00
// read port
2011-01-28 11:54:13 +01:00
final int port = serverCore . getPortNr ( config . getProperty ( " port " , " 8090 " ) ) ;
2005-08-02 21:40:29 +02:00
2005-04-07 21:19:42 +02:00
// read password
2011-05-05 02:25:14 +02:00
String encodedPassword = ( String ) config . get ( SwitchboardConstants . ADMIN_ACCOUNT_B64MD5 ) ;
2005-04-07 21:19:42 +02:00
if ( encodedPassword = = null ) encodedPassword = " " ; // not defined
2005-08-02 21:40:29 +02:00
2005-04-07 21:19:42 +02:00
// send 'wget' to web interface
2009-07-19 22:37:44 +02:00
final RequestHeader requestHeader = new RequestHeader ( ) ;
requestHeader . put ( RequestHeader . AUTHORIZATION , " realm= " + encodedPassword ) ; // for http-authentify
2010-08-22 19:38:27 +02:00
// final Client con = new Client(10000, requestHeader);
2012-10-28 19:56:02 +01:00
final HTTPClient con = new HTTPClient ( ClientIdentification . getUserAgent ( ) , ClientIdentification . DEFAULT_TIMEOUT ) ;
2010-08-22 19:38:27 +02:00
con . setHeader ( requestHeader . entrySet ( ) ) ;
// ResponseContainer res = null;
2005-04-07 21:19:42 +02:00
try {
2010-08-22 19:38:27 +02:00
// res = con.GET("http://localhost:"+ port +"/" + path);
con . GETbytes ( " http://localhost: " + port + " / " + path ) ;
2005-08-02 21:40:29 +02:00
2005-04-07 21:19:42 +02:00
// read response
2010-08-22 19:38:27 +02:00
// if (res.getStatusLine().startsWith("2")) {
if ( con . getStatusCode ( ) > 199 & & con . getStatusCode ( ) < 300 ) {
2009-06-11 13:31:26 +02:00
Log . logConfig ( " COMMAND-STEERING " , " YACY accepted steering command: " + processdescription ) ;
2010-08-22 19:38:27 +02:00
// final ByteArrayOutputStream bos = new ByteArrayOutputStream(); //This is stream is not used???
// try {
// FileUtils.copyToStream(new BufferedInputStream(res.getDataAsStream()), new BufferedOutputStream(bos));
// } finally {
// res.closeStream();
// }
2005-04-07 21:19:42 +02:00
} else {
2010-08-22 19:38:27 +02:00
// Log.logSevere("COMMAND-STEERING", "error response from YACY socket: " + res.getStatusLine());
Log . logSevere ( " COMMAND-STEERING " , " error response from YACY socket: " + con . getHttpResponse ( ) . getStatusLine ( ) ) ;
2005-04-07 21:19:42 +02:00
System . exit ( - 1 ) ;
}
2008-08-02 14:12:04 +02:00
} catch ( final IOException e ) {
2009-06-11 13:31:26 +02:00
Log . logSevere ( " COMMAND-STEERING " , " could not establish connection to YACY socket: " + e . getMessage ( ) ) ;
2005-04-07 21:19:42 +02:00
System . exit ( - 1 ) ;
2010-08-22 19:38:27 +02:00
// } finally {
// // release connection
// if(res != null) {
// res.closeStream();
// }
2005-04-07 21:19:42 +02:00
}
2011-07-05 08:54:05 +02:00
2010-08-30 19:48:20 +02:00
try {
HTTPClient . closeConnectionManager ( ) ;
2011-07-05 08:54:05 +02:00
} catch ( final InterruptedException e ) {
2010-08-30 19:48:20 +02:00
e . printStackTrace ( ) ;
}
2005-08-02 21:40:29 +02:00
2005-04-07 21:19:42 +02:00
// finished
2009-06-11 13:31:26 +02:00
Log . logConfig ( " COMMAND-STEERING " , " SUCCESSFULLY FINISHED COMMAND: " + processdescription ) ;
2005-04-07 21:19:42 +02:00
}
2011-07-05 08:54:05 +02:00
2005-08-02 21:40:29 +02:00
/ * *
2005-12-11 01:25:02 +01:00
* Main - method which is started by java . Checks for special arguments or
* starts up the application .
2011-07-05 08:54:05 +02:00
*
2005-12-11 01:25:02 +01:00
* @param args
* Given arguments from the command line .
* /
2005-04-07 21:19:42 +02:00
public static void main ( String args [ ] ) {
2011-07-05 08:54:05 +02:00
2011-06-15 11:14:51 +02:00
try {
2011-07-05 08:54:05 +02:00
2011-06-15 11:14:51 +02:00
// check assertion status
//ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true);
boolean assertionenabled = false ;
2012-09-10 07:15:52 +02:00
assert ( assertionenabled = true ) = = true ; // compare to true to remove warning: "Possible accidental assignement"
2011-06-15 11:14:51 +02:00
if ( assertionenabled ) System . out . println ( " Asserts are enabled " ) ;
2011-07-05 08:54:05 +02:00
2011-06-15 11:14:51 +02:00
// check memory amount
System . gc ( ) ;
final long startupMemFree = MemoryControl . free ( ) ;
final long startupMemTotal = MemoryControl . total ( ) ;
2011-07-05 08:54:05 +02:00
2011-06-15 11:14:51 +02:00
// maybe go into headless awt mode: we have three cases depending on OS and one exception:
// windows : better do not go into headless mode
// mac : go into headless mode because an application is shown in gui which may not be wanted
// linux : go into headless mode because this does not need any head operation
// exception : if the -gui option is used then do not go into headless mode since that uses a gui
boolean headless = true ;
if ( OS . isWindows ) headless = false ;
if ( args . length > = 1 & & args [ 0 ] . toLowerCase ( ) . equals ( " -gui " ) ) headless = false ;
System . setProperty ( " java.awt.headless " , headless ? " true " : " false " ) ;
2011-07-05 08:54:05 +02:00
String s = " " ; for ( final String a : args ) s + = a + " " ;
2011-06-15 11:14:51 +02:00
yacyRelease . startParameter = s . trim ( ) ;
2011-07-05 08:54:05 +02:00
2011-06-15 11:14:51 +02:00
File applicationRoot = new File ( System . getProperty ( " user.dir " ) . replace ( '\\' , '/' ) ) ;
File dataRoot = applicationRoot ;
//System.out.println("args.length=" + args.length);
//System.out.print("args=["); for (int i = 0; i < args.length; i++) System.out.print(args[i] + ", "); System.out.println("]");
if ( ( args . length > = 1 ) & & ( args [ 0 ] . toLowerCase ( ) . equals ( " -startup " ) | | args [ 0 ] . equals ( " -start " ) ) ) {
// normal start-up of yacy
if ( args . length > 1 ) dataRoot = new File ( System . getProperty ( " user.home " ) . replace ( '\\' , '/' ) , args [ 1 ] ) ;
startup ( dataRoot , applicationRoot , startupMemFree , startupMemTotal , false ) ;
} else if ( args . length > = 1 & & args [ 0 ] . toLowerCase ( ) . equals ( " -gui " ) ) {
// start-up of yacy with gui
if ( args . length > 1 ) dataRoot = new File ( System . getProperty ( " user.home " ) . replace ( '\\' , '/' ) , args [ 1 ] ) ;
startup ( dataRoot , applicationRoot , startupMemFree , startupMemTotal , true ) ;
} else if ( ( args . length > = 1 ) & & ( ( args [ 0 ] . toLowerCase ( ) . equals ( " -shutdown " ) ) | | ( args [ 0 ] . equals ( " -stop " ) ) ) ) {
// normal shutdown of yacy
if ( args . length = = 2 ) applicationRoot = new File ( args [ 1 ] ) ;
shutdown ( applicationRoot ) ;
} else if ( ( args . length > = 1 ) & & ( args [ 0 ] . toLowerCase ( ) . equals ( " -update " ) ) ) {
// aut-update yacy
if ( args . length = = 2 ) applicationRoot = new File ( args [ 1 ] ) ;
update ( applicationRoot ) ;
} else if ( ( args . length > = 1 ) & & ( args [ 0 ] . toLowerCase ( ) . equals ( " -version " ) ) ) {
// show yacy version
System . out . println ( copyright ) ;
} else {
if ( args . length = = 1 ) applicationRoot = new File ( args [ 0 ] ) ;
startup ( dataRoot , applicationRoot , startupMemFree , startupMemTotal , false ) ;
}
} finally {
Log . shutdown ( ) ;
}
2005-04-07 21:19:42 +02:00
}
}
2005-05-11 11:44:36 +02:00
2005-08-02 21:40:29 +02:00
/ * *
* This class is a helper class whose instance is started , when the java virtual
* machine shuts down . Signals the plasmaSwitchboard to shut down .
* /
2005-07-03 14:40:36 +02:00
class shutdownHookThread extends Thread {
2010-01-11 23:18:38 +01:00
private final Switchboard sb ;
private final Thread mainThread ;
2005-08-02 21:40:29 +02:00
2009-07-19 22:37:44 +02:00
public shutdownHookThread ( final Thread mainThread , final Switchboard sb ) {
2005-09-20 17:36:22 +02:00
super ( ) ;
2005-09-21 02:12:37 +02:00
this . sb = sb ;
2005-05-11 11:44:36 +02:00
this . mainThread = mainThread ;
}
2005-08-02 21:40:29 +02:00
2012-07-05 10:23:07 +02:00
@Override
2005-07-03 14:40:36 +02:00
public void run ( ) {
2005-05-11 11:44:36 +02:00
try {
2005-09-21 02:12:37 +02:00
if ( ! this . sb . isTerminated ( ) ) {
2009-01-31 00:33:47 +01:00
Log . logConfig ( " SHUTDOWN " , " Shutdown via shutdown hook. " ) ;
2005-08-02 21:40:29 +02:00
2005-05-11 11:44:36 +02:00
// sending the yacy main thread a shutdown signal
2009-01-31 00:33:47 +01:00
Log . logFine ( " SHUTDOWN " , " Signaling shutdown to the switchboard. " ) ;
2009-12-08 15:25:51 +01:00
this . sb . terminate ( " shutdown hook " ) ;
2005-08-02 21:40:29 +02:00
2005-05-11 11:44:36 +02:00
// waiting for the yacy thread to finish execution
2009-01-31 00:33:47 +01:00
Log . logFine ( " SHUTDOWN " , " Waiting for main thread to finish. " ) ;
2006-09-20 12:13:23 +02:00
if ( this . mainThread . isAlive ( ) & & ! this . sb . isTerminated ( ) ) {
this . mainThread . join ( ) ;
}
2005-05-11 11:44:36 +02:00
}
2008-08-02 14:12:04 +02:00
} catch ( final Exception e ) {
2009-01-31 00:33:47 +01:00
Log . logSevere ( " SHUTDOWN " , " Unexpected error. " + e . getClass ( ) . getName ( ) , e ) ;
2005-05-11 11:44:36 +02:00
}
}
2005-09-22 20:54:36 +02:00
}