String log4j = System.getProperty("log4j.configuration"); if(log4j == null) { log4j = _home_dir.getPath() + File.separatorChar + "log4j.xml"; File lf = new File(log4j); if(lf.canRead()) System.setProperty("log4j.configuration", lf.toURI().toASCIIString()); }
	public void run(String[] args) {		if (args.length > 0) {			if (args[0].equals("client")) {				//_classname = "org.exist.client.InteractiveClient";				_classname = "org.exist.client.InteractiveClient";				_mode = "client";			} else if (args[0].equals("standalone")) {				_classname = "org.exist.Server";				_mode = "standalone";			} else if (args[0].equals("backup")) {				_classname = "org.exist.backup.Main";				_mode = "backup";			} else if (args[0].equals("jetty")) {				//_classname = "org.mortbay.jetty.Server";				_classname = "org.exist.JettyStart";				_mode = "jetty";			} else if (args[0].equals("shutdown")) {				_classname = "org.exist.ServerShutdown";				_mode = "none";			} else {				_classname = args[0];				_mode = "none";			}			String[] nargs = new String[args.length - 1];			if (args.length > 1)				System.arraycopy(args, 1, nargs, 0, args.length - 1);			args = nargs;		} else {			_classname = "org.exist.JettyStart";			_mode = "jetty";		}		//--------------------		// detect exist.home:		//--------------------		_home_dir = getDirectory(System.getProperty("exist.home"));		if (_home_dir == null) {			// if eXist is deployed as web application, try to find WEB-INF first			File webinf = new File("WEB-INF");			if (_debug)				System.err.println("trying " + webinf.getAbsolutePath());			if (webinf.exists()) {				File jar =					new File(						webinf.getPath()							+ File.separatorChar							+ "lib"							+ File.separatorChar							+ "exist.jar");				if (jar.exists())					try {						_home_dir = webinf.getCanonicalFile();					} catch (IOException e) {					}			}		}		if (_home_dir == null) {			// failed: try exist.jar in current directory			File jar = new File("exist.jar");			if (_debug)				System.err.println("trying " + jar.getAbsolutePath());			if (jar.canRead()) {				try {					_home_dir = new File(".").getCanonicalFile();				} catch (IOException e) {				}			}		}		if (_home_dir == null) {			// failed: try ../exist.jar			File jar = new File(".." + File.separatorChar + "exist.jar");			if (_debug)				System.err.println("trying " + jar.getAbsolutePath());			if (jar.exists())				try {					_home_dir = new File("..").getCanonicalFile();				} catch (IOException e) {				}		}		// searching exist.jar failed, try conf.xml to have the configuration		// at least		if (_home_dir == null) {			// try conf.xml in current dir			File jar = new File("conf.xml");			if (_debug)				System.err.println("trying " + jar.getAbsolutePath());			if (jar.canRead()) {				try {					_home_dir = new File(".").getCanonicalFile();				} catch (IOException e) {				}			}		}		if (_home_dir == null) {			// try ../conf.xml			File jar = new File(".." + File.separatorChar + "conf.xml");			if (_debug)				System.err.println("trying " + jar.getAbsolutePath());			if (jar.exists())				try {					_home_dir = new File("..").getCanonicalFile();				} catch (IOException e) {				}		}		//TODO: more attempts here...		if (_home_dir != null) {			// if we managed to detect exist.home, store it in system property			if (_debug)				System.err.println("EXIST_HOME=" + System.getProperty("exist.home"));			System.setProperty("exist.home", _home_dir.getPath());			System.setProperty("user.dir", _home_dir.getPath());			if (_mode.equals("jetty")) {				// try to find Jetty				String _jetty_dir = null;				String _dirs[] = _home_dir.list();				for (int i = 0; i < _dirs.length; i++)					if (_dirs[i].startsWith("Jetty")) {						_jetty_dir = _dirs[i];						break;					}				if (_jetty_dir == null) {					System.err.println("ERROR: Jetty could not be found in " + _home_dir.getPath());					return;				}				System.setProperty(					"jetty.home",					_home_dir.getPath() + File.separatorChar + "Jetty-4.1.4");				args =					new String[] {						System.getProperty("jetty.home")							+ File.separatorChar							+ "etc"							+ File.separatorChar							+ "jetty.xml" };			}			// set up classpath:			// prefill existing paths in classpath_dirs...			if (_debug)				System.out.println("existing classpath = " + System.getProperty("java.class.path"));			_classpath.addClasspath(System.getProperty("java.class.path"));			// add JARs from ext and lib			// be smart about it			try {				InputStream cpcfg = null;				try {					cpcfg =						new java.io.FileInputStream(							_home_dir.getPath() + File.separatorChar + "start.config");				} catch (java.io.FileNotFoundException e) {					cpcfg = null;				}				if (cpcfg == null) {					if (_debug)						System.err.println("Configuring classpath from default resource");					cpcfg =						getClass().getClassLoader().getResourceAsStream(							"org/exist/start/start.config");				}				if (cpcfg == null) {					System.err.println("start.config not found. Bailing out.");					return;				}				configureClasspath(_home_dir.getPath(), _classpath, cpcfg, args, _mode);				cpcfg.close();			} catch (IOException e) {				e.printStackTrace();			}			// try to find javac and add it in classpaths			String java_home = System.getProperty("java.home");			if (java_home != null) {				File jdk_home = null;				try {					jdk_home = new File(java_home).getParentFile().getCanonicalFile();				} catch (IOException e) {				}				if (jdk_home != null) {					File tools_jar_file = null;					try {						tools_jar_file =							new File(jdk_home, "lib" + File.separator + "tools.jar")								.getCanonicalFile();					} catch (IOException e) {					}					if ((tools_jar_file != null) && tools_jar_file.isFile()) {						// OK, found tools.jar in java.home/../lib						// add it in						_classpath.addComponent(tools_jar_file);						if (_debug)							System.err.println("JAVAC = " + tools_jar_file);					}				}			}			// okay, classpath complete.			System.setProperty("java.class.path", _classpath.toString());			ClassLoader cl = _classpath.getClassLoader();			// clean up tempdir for Jetty...			try {				File tmpdir = new File(System.getProperty("java.io.tmpdir")).getCanonicalFile();				if (tmpdir.isDirectory()) {					System.setProperty("java.io.tmpdir", tmpdir.getPath());				}			} catch (IOException e) {			}			if (_debug)				System.err.println("TEMPDIR=" + System.getProperty("java.io.tmpdir"));			if (_debug)				System.err.println("CLASSPATH=" + _classpath.toString());			// Invoke org.mortbay.jetty.Server.main(args) using new classloader.			Thread.currentThread().setContextClassLoader(cl);			try {				invokeMain(cl, _classname, args);			} catch (Exception e) {				e.printStackTrace();			}		} else {			// if not, warn user			System.err.println("ERROR: exist.home cound not be autodetected, bailing out.");			System.err.flush();		}	}