org/sonews/storage/impl/JDBCDatabaseProvider.java
author cli
Wed May 12 11:18:02 2010 +0200 (2010-05-12)
changeset 31 087ef6fe6a1a
parent 3 2fdc9cc89502
permissions -rw-r--r--
Group.getByName() removed. Channel.getByName() does no longer catch StorageBackendExceptions but throw them further.
chris@3
     1
/*
chris@3
     2
 *   SONEWS News Server
chris@3
     3
 *   see AUTHORS for the list of contributors
chris@3
     4
 *
chris@3
     5
 *   This program is free software: you can redistribute it and/or modify
chris@3
     6
 *   it under the terms of the GNU General Public License as published by
chris@3
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@3
     8
 *   (at your option) any later version.
chris@3
     9
 *
chris@3
    10
 *   This program is distributed in the hope that it will be useful,
chris@3
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@3
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@3
    13
 *   GNU General Public License for more details.
chris@3
    14
 *
chris@3
    15
 *   You should have received a copy of the GNU General Public License
chris@3
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@3
    17
 */
chris@3
    18
chris@3
    19
package org.sonews.storage.impl;
chris@3
    20
chris@3
    21
import java.sql.SQLException;
chris@3
    22
import java.util.Map;
chris@3
    23
import java.util.concurrent.ConcurrentHashMap;
cli@30
    24
import org.sonews.storage.Storage;
cli@30
    25
import org.sonews.storage.StorageBackendException;
cli@30
    26
import org.sonews.storage.StorageProvider;
chris@3
    27
chris@3
    28
/**
chris@3
    29
 *
chris@3
    30
 * @author Christian Lins
chris@3
    31
 * @since sonews/1.0
chris@3
    32
 */
chris@3
    33
public class JDBCDatabaseProvider implements StorageProvider
chris@3
    34
{
chris@3
    35
chris@3
    36
  protected static final Map<Thread, JDBCDatabase> instances
chris@3
    37
    = new ConcurrentHashMap<Thread, JDBCDatabase>();
chris@3
    38
chris@3
    39
  @Override
chris@3
    40
  public boolean isSupported(String uri)
chris@3
    41
  {
chris@3
    42
    throw new UnsupportedOperationException("Not supported yet.");
chris@3
    43
  }
chris@3
    44
chris@3
    45
  @Override
chris@3
    46
  public Storage storage(Thread thread)
chris@3
    47
    throws StorageBackendException
chris@3
    48
  {
chris@3
    49
    try
chris@3
    50
    {
chris@3
    51
    if(!instances.containsKey(Thread.currentThread()))
chris@3
    52
    {
chris@3
    53
      JDBCDatabase db = new JDBCDatabase();
chris@3
    54
      db.arise();
chris@3
    55
      instances.put(Thread.currentThread(), db);
chris@3
    56
      return db;
chris@3
    57
    }
chris@3
    58
    else
chris@3
    59
    {
chris@3
    60
      return instances.get(Thread.currentThread());
chris@3
    61
    }
chris@3
    62
    }
chris@3
    63
    catch(SQLException ex)
chris@3
    64
    {
chris@3
    65
      throw new StorageBackendException(ex);
chris@3
    66
    }
chris@3
    67
  }
chris@3
    68
chris@3
    69
}