View Javadoc
1   /*
2    * Copyright (c) 2012-2023, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.jdbc;
31  
32  import java.io.PrintWriter;
33  import java.sql.Connection;
34  import java.sql.DriverManager;
35  import java.sql.SQLException;
36  import java.util.logging.Logger;
37  import javax.sql.DataSource;
38  import lombok.EqualsAndHashCode;
39  import lombok.ToString;
40  
41  /**
42   * Data source when all you have is a URL.
43   *
44   * @since 0.19.0
45   */
46  @ToString
47  @EqualsAndHashCode(of = "url")
48  public final class UrlSource implements DataSource {
49  
50      /**
51       * The URL.
52       */
53      private final transient String url;
54  
55      /**
56       * Public ctor.
57       * @param jdbc The JDBC URL.
58       */
59      public UrlSource(final String jdbc) {
60          this.url = jdbc;
61      }
62  
63      @Override
64      public Connection getConnection() {
65          try {
66              return DriverManager.getConnection(this.url);
67          } catch (final SQLException ex) {
68              throw new IllegalArgumentException(ex);
69          }
70      }
71  
72      @Override
73      public Connection getConnection(final String username,
74          final String password) {
75          return this.getConnection();
76      }
77  
78      @Override
79      public PrintWriter getLogWriter() {
80          throw new UnsupportedOperationException("#getLogWriter()");
81      }
82  
83      @Override
84      public void setLogWriter(final PrintWriter writer) {
85          throw new UnsupportedOperationException("#setLogWriter()");
86      }
87  
88      @Override
89      public void setLoginTimeout(final int seconds) {
90          throw new UnsupportedOperationException("#setLoginTimeout()");
91      }
92  
93      @Override
94      public int getLoginTimeout() {
95          throw new UnsupportedOperationException("#getLoginTimeout()");
96      }
97  
98      @Override
99      public Logger getParentLogger() {
100         throw new UnsupportedOperationException("#getParentLogger()");
101     }
102 
103     @Override
104     public <T> T unwrap(final Class<T> iface) {
105         throw new UnsupportedOperationException("#unwrap()");
106     }
107 
108     @Override
109     public boolean isWrapperFor(final Class<?> iface) {
110         throw new UnsupportedOperationException("#isWrapperFor()");
111     }
112 
113 }