extensions/net.sf.basedb.torrent/trunk/src/main/net/sf/basedb/clients/torrent/service/TorrentServiceController.java

Code
Comments
Other
Rev Date Author Line
1246 21 Oct 10 nicklas 1 /**
1246 21 Oct 10 nicklas 2   $Id$
1246 21 Oct 10 nicklas 3
1246 21 Oct 10 nicklas 4   Copyright (C) 2010 Nicklas Nordborg
1246 21 Oct 10 nicklas 5
1246 21 Oct 10 nicklas 6   This file is part of Bittorent download service for BASE.
1246 21 Oct 10 nicklas 7   Available at http://baseplugins.thep.lu.se/
1246 21 Oct 10 nicklas 8
1246 21 Oct 10 nicklas 9   BASE is free software; you can redistribute it and/or
1246 21 Oct 10 nicklas 10   modify it under the terms of the GNU General Public License
1246 21 Oct 10 nicklas 11   as published by the Free Software Foundation; either version 2
1246 21 Oct 10 nicklas 12   of the License, or (at your option) any later version.
1246 21 Oct 10 nicklas 13
1246 21 Oct 10 nicklas 14   BASE is distributed in the hope that it will be useful,
1246 21 Oct 10 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
1246 21 Oct 10 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1246 21 Oct 10 nicklas 17   GNU General Public License for more details.
1246 21 Oct 10 nicklas 18
1246 21 Oct 10 nicklas 19   You should have received a copy of the GNU General Public License
1246 21 Oct 10 nicklas 20   along with this program; if not, write to the Free Software
1246 21 Oct 10 nicklas 21   Foundation, Inc., 59 Temple Place - Suite 330,
1246 21 Oct 10 nicklas 22   Boston, MA  02111-1307, USA.
1246 21 Oct 10 nicklas 23 */
1246 21 Oct 10 nicklas 24 package net.sf.basedb.clients.torrent.service;
1246 21 Oct 10 nicklas 25
1246 21 Oct 10 nicklas 26 import java.net.URL;
1246 21 Oct 10 nicklas 27 import java.util.Properties;
1246 21 Oct 10 nicklas 28
1246 21 Oct 10 nicklas 29 import org.slf4j.Logger;
1246 21 Oct 10 nicklas 30 import org.slf4j.LoggerFactory;
1246 21 Oct 10 nicklas 31
1246 21 Oct 10 nicklas 32 import net.sf.basedb.clients.web.extensions.service.ServiceControllerAction;
1246 21 Oct 10 nicklas 33 import net.sf.basedb.core.ConfigurationException;
1246 21 Oct 10 nicklas 34
1246 21 Oct 10 nicklas 35 /**
1246 21 Oct 10 nicklas 36   Action for hooking into the BASE extensions system. This action
1246 21 Oct 10 nicklas 37   is used to control a single {@link TorrentService} instance. Actions 
1246 21 Oct 10 nicklas 38   are created by {@link TorrentServiceControllerFactory}.
1246 21 Oct 10 nicklas 39   
1246 21 Oct 10 nicklas 40   @author Nicklas
1246 21 Oct 10 nicklas 41   @since 1.0
1246 21 Oct 10 nicklas 42   @see TorrentServiceControllerFactory
1246 21 Oct 10 nicklas 43   @see TorrentService
1246 21 Oct 10 nicklas 44 */
1246 21 Oct 10 nicklas 45 public class TorrentServiceController 
1246 21 Oct 10 nicklas 46   implements ServiceControllerAction 
1246 21 Oct 10 nicklas 47 {
1246 21 Oct 10 nicklas 48   private static final Logger log = LoggerFactory.getLogger(TorrentServiceController.class);
1246 21 Oct 10 nicklas 49
1246 21 Oct 10 nicklas 50   private final String configFile;
1246 21 Oct 10 nicklas 51   private TorrentService service;
1246 21 Oct 10 nicklas 52   
1246 21 Oct 10 nicklas 53   /**
1246 21 Oct 10 nicklas 54     Creates a new service controller action for controlling
1246 21 Oct 10 nicklas 55     a single ftp server service
1246 21 Oct 10 nicklas 56     @param configFile The path to configuration file (classpath based),
1246 21 Oct 10 nicklas 57       if null the default (/torrent.properties) is used
1246 21 Oct 10 nicklas 58   */
1246 21 Oct 10 nicklas 59   public TorrentServiceController(String configFile)
1246 21 Oct 10 nicklas 60   {
1246 21 Oct 10 nicklas 61     this.configFile = configFile == null ? "/torrent.properties" : configFile;
1246 21 Oct 10 nicklas 62   }
1246 21 Oct 10 nicklas 63   
1246 21 Oct 10 nicklas 64   /*
1246 21 Oct 10 nicklas 65     From the ServiceControllerAction interface
1246 21 Oct 10 nicklas 66     -------------------------------------------
1246 21 Oct 10 nicklas 67   */
1246 21 Oct 10 nicklas 68   @Override
1246 21 Oct 10 nicklas 69   public boolean isRunning() 
1246 21 Oct 10 nicklas 70   {
1246 21 Oct 10 nicklas 71     return service != null && service.isRunning();
1246 21 Oct 10 nicklas 72   }
1246 21 Oct 10 nicklas 73
1246 21 Oct 10 nicklas 74   @Override
1246 21 Oct 10 nicklas 75   public void start() 
1246 21 Oct 10 nicklas 76   {
1246 21 Oct 10 nicklas 77     try
1246 21 Oct 10 nicklas 78     {
1246 21 Oct 10 nicklas 79       getService().start();
1246 21 Oct 10 nicklas 80     }
1246 21 Oct 10 nicklas 81     catch (RuntimeException ex)
1246 21 Oct 10 nicklas 82     {
1246 21 Oct 10 nicklas 83       service = null;
1246 21 Oct 10 nicklas 84       throw ex;
1246 21 Oct 10 nicklas 85     }
1246 21 Oct 10 nicklas 86   }
1246 21 Oct 10 nicklas 87
1246 21 Oct 10 nicklas 88   @Override
1246 21 Oct 10 nicklas 89   public void stop() 
1246 21 Oct 10 nicklas 90   {
1246 21 Oct 10 nicklas 91     if (service != null)
1246 21 Oct 10 nicklas 92     {
1246 21 Oct 10 nicklas 93       service.stop();
1246 21 Oct 10 nicklas 94       service = null;
1246 21 Oct 10 nicklas 95     }
1246 21 Oct 10 nicklas 96   }
1246 21 Oct 10 nicklas 97   // ----------------------------------------------
1246 21 Oct 10 nicklas 98
1246 21 Oct 10 nicklas 99   /*
1246 21 Oct 10 nicklas 100     From the Object class
1246 21 Oct 10 nicklas 101     ---------------------------------------------
1246 21 Oct 10 nicklas 102   */
1246 21 Oct 10 nicklas 103   @Override
1246 21 Oct 10 nicklas 104   public String toString() 
1246 21 Oct 10 nicklas 105   {
1246 21 Oct 10 nicklas 106     return "Bittorrent Download Service";
1246 21 Oct 10 nicklas 107   }
1246 21 Oct 10 nicklas 108   // ----------------------------------------------
1246 21 Oct 10 nicklas 109   
1246 21 Oct 10 nicklas 110   /**
1246 21 Oct 10 nicklas 111     Get or create the {@link TorrentService}. A controller can only be associated
1246 21 Oct 10 nicklas 112     with a single bittorrent service.
1246 21 Oct 10 nicklas 113   */
1246 21 Oct 10 nicklas 114   private TorrentService getService()
1246 21 Oct 10 nicklas 115   {
1246 21 Oct 10 nicklas 116     if (service == null)
1246 21 Oct 10 nicklas 117     {
1246 21 Oct 10 nicklas 118       log.info("Initializing Bittorrent Download Service");
1246 21 Oct 10 nicklas 119       try
1246 21 Oct 10 nicklas 120       {
1246 21 Oct 10 nicklas 121         log.debug("configuration file=" + configFile);
1246 21 Oct 10 nicklas 122         URL config = TorrentService.class.getResource(configFile);
1246 21 Oct 10 nicklas 123         if (config == null)
1246 21 Oct 10 nicklas 124         {
1246 21 Oct 10 nicklas 125           throw new ConfigurationException("Can't find the configuration file. " +
1246 21 Oct 10 nicklas 126               "Make sure '" + configFile + "' is in the CLASSPATH.");
1246 21 Oct 10 nicklas 127         }
1246 21 Oct 10 nicklas 128         Properties properties = new Properties();
1246 21 Oct 10 nicklas 129         properties.load(config.openStream());
1246 21 Oct 10 nicklas 130         service = TorrentService.getInstance(properties);
1246 21 Oct 10 nicklas 131         log.info("Bittorrent Download Service initialized successfully");
1246 21 Oct 10 nicklas 132       }
1246 21 Oct 10 nicklas 133       catch (RuntimeException ex)
1246 21 Oct 10 nicklas 134       {
1246 21 Oct 10 nicklas 135         log.error("Could not initialize Bittorrent Download Service", ex);
1246 21 Oct 10 nicklas 136         throw ex;
1246 21 Oct 10 nicklas 137       }
1246 21 Oct 10 nicklas 138       catch (Exception ex)
1246 21 Oct 10 nicklas 139       {
1246 21 Oct 10 nicklas 140         log.error("Could not initialize Bittorrent Download Service", ex);
1246 21 Oct 10 nicklas 141         throw new RuntimeException(ex);
1246 21 Oct 10 nicklas 142       }
1246 21 Oct 10 nicklas 143     }
1246 21 Oct 10 nicklas 144     return service;
1246 21 Oct 10 nicklas 145   }
1246 21 Oct 10 nicklas 146
1246 21 Oct 10 nicklas 147 }