56380
|
1 |
/*
|
56475
|
2 |
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
|
56380
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
package jdk.incubator.sql2;
|
|
26 |
|
|
27 |
import java.util.ServiceLoader;
|
56397
|
28 |
import java.util.ServiceLoader.Provider;
|
56380
|
29 |
|
|
30 |
/**
|
|
31 |
* This interface supports injecting a {@link DataSourceFactory}. The SPI
|
|
32 |
* mechanism will find {@link DataSourceFactory} implementations with the
|
|
33 |
* given class name.
|
56475
|
34 |
*
|
|
35 |
* Implementations must be thread safe.
|
56380
|
36 |
*
|
|
37 |
*/
|
|
38 |
public interface DataSourceFactory {
|
|
39 |
|
|
40 |
/**
|
|
41 |
* Uses SPI to find a {@link DataSourceFactory} with the requested name or
|
|
42 |
* {@code null} if one is not found.
|
|
43 |
*
|
56797
|
44 |
* @param <T>
|
56380
|
45 |
* @param name the name of the class that implements the factory
|
|
46 |
* @return a {@link DataSourceFactory} for {@code name} or {@code null} if one
|
|
47 |
* is not found
|
|
48 |
*/
|
56797
|
49 |
public static <T extends DataSourceFactory> T newFactory(String name) {
|
56475
|
50 |
if (name == null) throw new IllegalArgumentException("DataSourceFactory name is null");
|
56797
|
51 |
return (T)ServiceLoader
|
56397
|
52 |
.load(DataSourceFactory.class)
|
|
53 |
.stream()
|
|
54 |
.filter(p -> p.type().getName().equals(name))
|
|
55 |
.findFirst()
|
|
56 |
.map(Provider::get)
|
|
57 |
.orElse(null);
|
56380
|
58 |
}
|
|
59 |
|
|
60 |
/**
|
|
61 |
* Returns a new {@link DataSource} builder.
|
|
62 |
*
|
|
63 |
* @return a {@link DataSource} builder. Not {@code null}.
|
|
64 |
*/
|
|
65 |
public DataSource.Builder builder();
|
|
66 |
|
|
67 |
}
|