org/sonews/storage/StorageManager.java
author chris <chris@marvin>
Wed Jul 22 14:04:05 2009 +0200 (2009-07-22)
changeset 3 2fdc9cc89502
permissions -rw-r--r--
sonews/1.0.0
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package org.sonews.storage;
    20 
    21 /**
    22  *
    23  * @author Christian Lins
    24  * @since sonews/1.0
    25  */
    26 public final class StorageManager
    27 {
    28 
    29   private static StorageProvider provider;
    30 
    31   public static Storage current()
    32     throws StorageBackendException
    33   {
    34     synchronized(StorageManager.class)
    35     {
    36       if(provider == null)
    37       {
    38         return null;
    39       }
    40       else
    41       {
    42         return provider.storage(Thread.currentThread());
    43       }
    44     }
    45   }
    46 
    47   public static StorageProvider loadProvider(String pluginClassName)
    48   {
    49     try
    50     {
    51       Class<?> clazz = Class.forName(pluginClassName);
    52       Object   inst  = clazz.newInstance();
    53       return (StorageProvider)inst;
    54     }
    55     catch(Exception ex)
    56     {
    57       System.err.println(ex);
    58       return null;
    59     }
    60   }
    61 
    62   /**
    63    * Sets the current storage provider.
    64    * @param provider
    65    */
    66   public static void enableProvider(StorageProvider provider)
    67   {
    68     synchronized(StorageManager.class)
    69     {
    70       if(StorageManager.provider != null)
    71       {
    72         disableProvider();
    73       }
    74       StorageManager.provider = provider;
    75     }
    76   }
    77 
    78   /**
    79    * Disables the current provider.
    80    */
    81   public static void disableProvider()
    82   {
    83     synchronized(StorageManager.class)
    84     {
    85       provider = null;
    86     }
    87   }
    88 
    89 }