extensions/net.sf.basedb.torrent/trunk/src/main/net/sf/basedb/clients/torrent/service/TorrentServiceControllerFactory.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 org.slf4j.Logger;
1246 21 Oct 10 nicklas 27 import org.slf4j.LoggerFactory;
1246 21 Oct 10 nicklas 28
1246 21 Oct 10 nicklas 29 import net.sf.basedb.clients.web.extensions.service.ServiceControllerAction;
1246 21 Oct 10 nicklas 30 import net.sf.basedb.util.extensions.ActionFactory;
1246 21 Oct 10 nicklas 31 import net.sf.basedb.util.extensions.InvokationContext;
1246 21 Oct 10 nicklas 32
1246 21 Oct 10 nicklas 33 /**
1246 21 Oct 10 nicklas 34   An action factory for creating {@link TorrentServiceController} actions.
1246 21 Oct 10 nicklas 35   A single factory is tied to a single {@link TorrentService}, which is
1246 21 Oct 10 nicklas 36   created lazily when it is first needed. This usually happens when
1246 21 Oct 10 nicklas 37   the {@link #getActions(InvokationContext)} method is called.
1246 21 Oct 10 nicklas 38   <p>
1246 21 Oct 10 nicklas 39   The action factory accepts the following parameters:
1246 21 Oct 10 nicklas 40   <ul>
1246 21 Oct 10 nicklas 41   <li>{@link #setConfig(String)}: The path to the configuration file. The
1246 21 Oct 10 nicklas 42     default is: <code>/torrent.properties</code>
1246 21 Oct 10 nicklas 43   </ul>
1246 21 Oct 10 nicklas 44   
1246 21 Oct 10 nicklas 45   @author Nicklas
1246 21 Oct 10 nicklas 46   @since 1.0
1246 21 Oct 10 nicklas 47 */
1246 21 Oct 10 nicklas 48 public class TorrentServiceControllerFactory 
1246 21 Oct 10 nicklas 49   implements ActionFactory<ServiceControllerAction> 
1246 21 Oct 10 nicklas 50 {
1246 21 Oct 10 nicklas 51   private static final Logger log = LoggerFactory.getLogger(TorrentServiceControllerFactory.class);
1246 21 Oct 10 nicklas 52
1246 21 Oct 10 nicklas 53   private TorrentServiceController controller;
1246 21 Oct 10 nicklas 54   private String config;
1246 21 Oct 10 nicklas 55   
1246 21 Oct 10 nicklas 56   /**
1246 21 Oct 10 nicklas 57     Creates a new factory instance.
1246 21 Oct 10 nicklas 58   */
1246 21 Oct 10 nicklas 59   public TorrentServiceControllerFactory()
1246 21 Oct 10 nicklas 60   {}
1246 21 Oct 10 nicklas 61   
1246 21 Oct 10 nicklas 62   /*
1246 21 Oct 10 nicklas 63     From the ActionFactory interface
1246 21 Oct 10 nicklas 64     -------------------------------------------
1246 21 Oct 10 nicklas 65   */
1246 21 Oct 10 nicklas 66   @Override
1246 21 Oct 10 nicklas 67   public ServiceControllerAction[] getActions(InvokationContext context)
1246 21 Oct 10 nicklas 68   {
1246 21 Oct 10 nicklas 69     return new ServiceControllerAction[] { getController() };
1246 21 Oct 10 nicklas 70   }
1246 21 Oct 10 nicklas 71
1246 21 Oct 10 nicklas 72   /**
1246 21 Oct 10 nicklas 73     Always TRUE.
1246 21 Oct 10 nicklas 74   */
1246 21 Oct 10 nicklas 75   @Override
1246 21 Oct 10 nicklas 76   public boolean prepareContext(InvokationContext context) 
1246 21 Oct 10 nicklas 77   {
1246 21 Oct 10 nicklas 78     return true;
1246 21 Oct 10 nicklas 79   }
1246 21 Oct 10 nicklas 80   // -----------------------------------------------
1246 21 Oct 10 nicklas 81
1246 21 Oct 10 nicklas 82   /**
1246 21 Oct 10 nicklas 83     Set the path to the configuration file to use. The path
1246 21 Oct 10 nicklas 84     should be a path on the CLASSPATH. 
1246 21 Oct 10 nicklas 85     @param config The path, or null to use the default path (/torrent.properties)
1246 21 Oct 10 nicklas 86   */
1246 21 Oct 10 nicklas 87   public void setConfig(String config)
1246 21 Oct 10 nicklas 88   {
1246 21 Oct 10 nicklas 89     this.config = config;
1246 21 Oct 10 nicklas 90   }
1246 21 Oct 10 nicklas 91
1246 21 Oct 10 nicklas 92   private synchronized TorrentServiceController getController()
1246 21 Oct 10 nicklas 93   {
1246 21 Oct 10 nicklas 94     if (controller == null)
1246 21 Oct 10 nicklas 95     {
1246 21 Oct 10 nicklas 96       controller = new TorrentServiceController(config);
1246 21 Oct 10 nicklas 97     }
1246 21 Oct 10 nicklas 98     return controller;
1246 21 Oct 10 nicklas 99   }
1246 21 Oct 10 nicklas 100   
1246 21 Oct 10 nicklas 101 }