FileDocCategorySizeDatePackage
TorrentFolderWatcher.javaAPI DocAzureus 3.0.3.48688Mon Jul 09 13:32:02 BST 2007com.aelitis.azureus.core.helpers

TorrentFolderWatcher

public class TorrentFolderWatcher extends Object
Watches a folder for new torrents and imports them. NOTE: Folder-to-watch and other watching params are taken from a global config option right now, so starting multiple instances of TorrentFolderWatcher is useless as currently coded.

Fields Summary
private static final LogIDs
LOGID
private static final String
PARAMID_FOLDER
private org.gudy.azureus2.core3.global.GlobalManager
global_manager
private boolean
running
private final ArrayList
to_delete
protected AEMonitor
this_mon
private FilenameFilter
filename_filter
private org.gudy.azureus2.core3.config.ParameterListener
param_listener
private final Thread
watch_thread
Constructors Summary
public TorrentFolderWatcher(org.gudy.azureus2.core3.global.GlobalManager _global_manager)
Start a folder watcher, which will auto-import torrents via the given manager.

param
global_manager


	               	 
	   
		this.global_manager = _global_manager;

		if (COConfigurationManager.getBooleanParameter(PARAMID_FOLDER)) {
			running = true;
			watch_thread.setDaemon(true);
			watch_thread.setPriority(Thread.MIN_PRIORITY);
			watch_thread.start();
		}

		COConfigurationManager.addParameterListener(PARAMID_FOLDER, param_listener);
	
Methods Summary
public voiddestroy()
Stop and terminate this folder importer watcher.

		running = false;
		global_manager = null;
		COConfigurationManager.removeParameterListener(PARAMID_FOLDER,
				param_listener);
	
private voidimportAddedFiles()


		try {
			this_mon.enter();

			if (!running)
				return;

			boolean save_torrents = COConfigurationManager
					.getBooleanParameter("Save Torrent Files");

			String torrent_save_path = COConfigurationManager
					.getStringParameter("General_sDefaultTorrent_Directory");

			int start_state = COConfigurationManager
					.getBooleanParameter("Start Watched Torrents Stopped")
					? DownloadManager.STATE_STOPPED : DownloadManager.STATE_QUEUED;

			String folder_path = COConfigurationManager
					.getStringParameter("Watch Torrent Folder Path");

			String data_save_path = COConfigurationManager
					.getStringParameter("Default save path");

			File folder = null;

			if (folder_path != null && folder_path.length() > 0) {
				folder = new File(folder_path);
				if (!folder.isDirectory()) {
					if (!folder.exists()) {
						FileUtil.mkdirs(folder);
					}
					if (!folder.isDirectory()) {
						if (Logger.isEnabled())
							Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR,
									"[Watch Torrent Folder Path] " + "does not exist or "
											+ "is not a dir"));
						folder = null;
					}
				}
			}

			if (folder == null) {
				if (Logger.isEnabled())
					Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR,
							"[Watch Torrent Folder Path] not configured"));
				return;
			}

			File f = null;
			if (data_save_path != null && data_save_path.length() > 0) {
				f = new File(data_save_path);
				
				// Path is not an existing directory.
				if (!f.isDirectory()) {
					if (!f.exists()) {FileUtil.mkdirs(f);}
					
					// If path is still not a directory, abort.
					if (!f.isDirectory()) {
						if (Logger.isEnabled()) {
							Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR,
									"[Default save path] does not exist or is not a dir"));
						}
						Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_ERROR,
								"[Default save path] does not exist or is not a dir"));
						return;
					}
				}
			}
			
			// If we get here, and this is true, then data_save_path isn't valid.
			if (f == null){
				if (Logger.isEnabled()) {
 					Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR,
						"[Default save path] needs to be set for auto-.torrent-import to work"));
				}
				Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_ERROR,
						"[Default save path] needs to be set for auto-.torrent-import to work"));
			}
			
			// if we are saving torrents to the same location as we import them from
			// then we can't assume that its safe to delete the torrent after import! 

			if (torrent_save_path.length() == 0
					|| torrent_save_path.equals(folder_path)
					|| !new File(torrent_save_path).isDirectory()) {

				save_torrents = false;
			}

			//delete torrents from the previous import run

			for (int i = 0; i < to_delete.size(); i++) {

				TOTorrent torrent = (TOTorrent) to_delete.get(i);

				try {
					TorrentUtils.delete(torrent);

				} catch (Throwable e) {

					Debug.printStackTrace(e);
				}
			}

			to_delete.clear();

			String[] currentFileList = folder.list(filename_filter);
			if (currentFileList == null) {
				Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR,
						"There was a problem trying to get a listing of torrents from " + folder));
				return;
			}

			for (int i = 0; i < currentFileList.length; i++) {

				File file = new File(folder, currentFileList[i]);

				// make sure we've got a valid torrent file before proceeding

				try {

					TOTorrent torrent = TorrentUtils.readFromFile(file, false);

					if (global_manager.getDownloadManager(torrent) != null) {

						if (Logger.isEnabled())
							Logger.log(new LogEvent(LOGID, file.getAbsolutePath()
									+ " is already being downloaded"));

						// we can't touch the torrent file as it is (probably) 
						// being used for the download

					} else {
						
						byte[] hash = null;
						try {
							hash = torrent.getHash();
						} catch (Exception e) { }

						if (!save_torrents) {

							File imported = new File(folder, file.getName() + ".imported");

							TorrentUtils.move(file, imported);

							global_manager.addDownloadManager(imported.getAbsolutePath(), hash,
									data_save_path, start_state, true);

						} else {

							global_manager.addDownloadManager(file.getAbsolutePath(), hash,
									data_save_path, start_state, true);

							// add torrent for deletion, since there will be a 
							// saved copy elsewhere
							to_delete.add(torrent);
						}

						if (Logger.isEnabled())
							Logger.log(new LogEvent(LOGID, "Auto-imported "
									+ file.getAbsolutePath()));
					}

				} catch (Throwable e) {

					Debug.out("Failed to auto-import torrent file '"
							+ file.getAbsolutePath() + "' - "
							+ Debug.getNestedExceptionMessage(e));
					Debug.printStackTrace(e);
				}
			}
		} finally {
			this_mon.exit();
		}