1 /* 2 * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package de.smartics.properties.impl.config.ds; 17 18 import java.util.Iterator; 19 import java.util.Locale; 20 import java.util.ServiceLoader; 21 import java.util.concurrent.CopyOnWriteArrayList; 22 23 import javax.annotation.concurrent.ThreadSafe; 24 25 import de.smartics.properties.spi.config.ds.DataSourceConnector; 26 import de.smartics.util.lang.Arg; 27 28 /** 29 * Manager for connector implementations. 30 */ 31 @ThreadSafe 32 public final class DataSourceProxyManager 33 { 34 // ********************************* Fields ********************************* 35 36 // --- constants ------------------------------------------------------------ 37 38 // --- members -------------------------------------------------------------- 39 40 /** 41 * The registry of all registered connectors. 42 */ 43 private static final CopyOnWriteArrayList<DataSourceConnector> REGISTRY = 44 new CopyOnWriteArrayList<DataSourceConnector>(); 45 46 // ****************************** Initializer ******************************* 47 48 static 49 { 50 final Iterator<DataSourceConnector> iterator = 51 ServiceLoader.load(DataSourceConnector.class).iterator(); 52 53 while (iterator.hasNext()) 54 { 55 final DataSourceConnector connector = iterator.next(); 56 register(connector); 57 } 58 } 59 60 // ****************************** Constructors ****************************** 61 62 private DataSourceProxyManager() 63 { 64 } 65 66 // ****************************** Inner Classes ***************************** 67 68 // ********************************* Methods ******************************** 69 70 // --- init ----------------------------------------------------------------- 71 72 // --- get&set -------------------------------------------------------------- 73 74 // --- business ------------------------------------------------------------- 75 76 /** 77 * Registers the given connector. A connector implementation should call this 78 * method to make itself known to data source connector manager. 79 * 80 * @param connector the new connector to be registered. 81 */ 82 public static void register(final DataSourceConnector connector) 83 { 84 synchronized (REGISTRY) 85 { 86 REGISTRY.addIfAbsent(Arg.checkNotNull("connector", connector)); 87 } 88 } 89 90 /** 91 * Returns the first matching driver for a database identified by the given 92 * {@code id}. 93 * 94 * @param databaseId the identifier of the database. 95 * @return a connector to interface with the given data source. 96 * @throws IllegalStateException if no suitable connector has been registered. 97 */ 98 public static DataSourceConnector getConnectorFor(final String databaseId) 99 throws IllegalStateException 100 { 101 final String normalized = databaseId.toLowerCase(Locale.ENGLISH); 102 for (final DataSourceConnector connector : REGISTRY) 103 { 104 if (connector.accepts(normalized)) 105 { 106 return connector; 107 } 108 } 109 110 throw new IllegalStateException(String.format( 111 "No suitable connector found for '%s'.", normalized)); 112 } 113 114 // --- object basics -------------------------------------------------------- 115 116 }