|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.openqa.selenium.grid.sessionmap.jdbc; |
| 19 | + |
| 20 | +import static org.openqa.selenium.grid.data.SessionClosedEvent.SESSION_CLOSED; |
| 21 | + |
| 22 | +import org.openqa.selenium.Capabilities; |
| 23 | +import org.openqa.selenium.ImmutableCapabilities; |
| 24 | +import org.openqa.selenium.NoSuchSessionException; |
| 25 | +import org.openqa.selenium.events.EventBus; |
| 26 | +import org.openqa.selenium.grid.config.Config; |
| 27 | +import org.openqa.selenium.grid.config.ConfigException; |
| 28 | +import org.openqa.selenium.grid.data.Session; |
| 29 | +import org.openqa.selenium.grid.log.LoggingOptions; |
| 30 | +import org.openqa.selenium.grid.server.EventBusOptions; |
| 31 | +import org.openqa.selenium.grid.sessionmap.SessionMap; |
| 32 | +import org.openqa.selenium.internal.Require; |
| 33 | +import org.openqa.selenium.json.Json; |
| 34 | +import org.openqa.selenium.remote.SessionId; |
| 35 | +import org.openqa.selenium.remote.tracing.Tracer; |
| 36 | + |
| 37 | +import java.io.Closeable; |
| 38 | +import java.net.URI; |
| 39 | +import java.net.URISyntaxException; |
| 40 | +import java.sql.Connection; |
| 41 | +import java.sql.PreparedStatement; |
| 42 | +import java.sql.ResultSet; |
| 43 | +import java.sql.SQLException; |
| 44 | +import java.util.logging.Logger; |
| 45 | + |
| 46 | + |
| 47 | +public class JdbcBackedSessionMap extends SessionMap implements Closeable { |
| 48 | + |
| 49 | + private static final Json JSON = new Json(); |
| 50 | + private static final Logger LOG = Logger.getLogger(JdbcBackedSessionMap.class.getName()); |
| 51 | + private static final String TABLE_NAME = "sessions_map"; |
| 52 | + private static final String SESSION_ID_COL = "session_ids"; |
| 53 | + private static final String SESSION_CAPS_COL = "session_caps"; |
| 54 | + private static final String SESSION_URI_COL = "session_uri"; |
| 55 | + private final EventBus bus; |
| 56 | + private final Connection connection; |
| 57 | + |
| 58 | + |
| 59 | + public JdbcBackedSessionMap(Tracer tracer, Connection jdbcConnection, EventBus bus) { |
| 60 | + super(tracer); |
| 61 | + |
| 62 | + Require.nonNull("JDBC Connection Object", jdbcConnection); |
| 63 | + this.bus = Require.nonNull("Event bus", bus); |
| 64 | + |
| 65 | + this.connection = jdbcConnection; |
| 66 | + this.bus.addListener(SESSION_CLOSED, event -> { |
| 67 | + SessionId id = event.getData(SessionId.class); |
| 68 | + remove(id); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + public static SessionMap create(Config config) { |
| 73 | + Tracer tracer = new LoggingOptions(config).getTracer(); |
| 74 | + EventBus bus = new EventBusOptions(config).getEventBus(); |
| 75 | + |
| 76 | + JdbcSessionMapOptions sessionMapOptions = new JdbcSessionMapOptions(config); |
| 77 | + |
| 78 | + Connection connection; |
| 79 | + |
| 80 | + try { |
| 81 | + connection = sessionMapOptions.getJdbcConnection(); |
| 82 | + } catch (SQLException e) { |
| 83 | + throw new ConfigException(e); |
| 84 | + } |
| 85 | + |
| 86 | + return new JdbcBackedSessionMap(tracer, connection, bus); |
| 87 | + } |
| 88 | + @Override |
| 89 | + public boolean add(Session session) { |
| 90 | + Require.nonNull("Session to add", session); |
| 91 | + |
| 92 | + try { |
| 93 | + return insertSessionStatement(session).executeUpdate() >= 1; |
| 94 | + |
| 95 | + } catch (SQLException e) { |
| 96 | + throw new JdbcException(e); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public Session get(SessionId id) throws NoSuchSessionException { |
| 102 | + Require.nonNull("Session ID", id); |
| 103 | + |
| 104 | + URI uri = null; |
| 105 | + Capabilities caps = null; |
| 106 | + String rawUri = null; |
| 107 | + |
| 108 | + try (ResultSet sessions = readSessionStatement(id).executeQuery()){ |
| 109 | + if (!sessions.next()) { |
| 110 | + throw new NoSuchSessionException("Unable to find..."); |
| 111 | + } |
| 112 | + |
| 113 | + rawUri = sessions.getString(SESSION_URI_COL); |
| 114 | + String rawCapabilities = sessions.getString(SESSION_CAPS_COL); |
| 115 | + |
| 116 | + caps = rawCapabilities == null ? |
| 117 | + new ImmutableCapabilities() : |
| 118 | + JSON.toType(rawCapabilities, Capabilities.class); |
| 119 | + try { |
| 120 | + uri = new URI(rawUri); |
| 121 | + } catch (URISyntaxException e) { |
| 122 | + throw new NoSuchSessionException(String.format("Unable to convert session id (%s) to uri: %s", id, rawUri), e); |
| 123 | + } |
| 124 | + |
| 125 | + return new Session(id, uri, caps); |
| 126 | + } catch (SQLException e) { |
| 127 | + throw new JdbcException(e); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void remove(SessionId id) { |
| 133 | + Require.nonNull("Session ID", id); |
| 134 | + |
| 135 | + try { |
| 136 | + getDeleteSqlForSession(id).executeUpdate(); |
| 137 | + } catch (SQLException e) { |
| 138 | + throw new JdbcException(e.getMessage()); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void close() { |
| 144 | + try { |
| 145 | + connection.close(); |
| 146 | + } catch (SQLException e) { |
| 147 | + LOG.warning("SQL exception while closing JDBC Connection:" + e.getMessage()); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private PreparedStatement insertSessionStatement(Session session) throws SQLException { |
| 152 | + PreparedStatement insertStatement = connection.prepareStatement(String.format("insert into %1$s (%2$s, %3$s, %4$s) values (?, ?, ?)", |
| 153 | + TABLE_NAME, |
| 154 | + SESSION_ID_COL, |
| 155 | + SESSION_URI_COL, |
| 156 | + SESSION_CAPS_COL)); |
| 157 | + |
| 158 | + insertStatement.setString(1, session.getId().toString()); |
| 159 | + insertStatement.setString(2, session.getUri().toString()); |
| 160 | + insertStatement.setString(3, JSON.toJson(session.getCapabilities())); |
| 161 | + |
| 162 | + return insertStatement; |
| 163 | + } |
| 164 | + |
| 165 | + private PreparedStatement readSessionStatement(SessionId sessionId) throws SQLException { |
| 166 | + PreparedStatement getSessionsStatement = connection.prepareStatement(String.format("select * from %1$s where %2$s = ?", |
| 167 | + TABLE_NAME, |
| 168 | + SESSION_ID_COL)); |
| 169 | + |
| 170 | + getSessionsStatement.setMaxRows(1); |
| 171 | + getSessionsStatement.setString(1, sessionId.toString()); |
| 172 | + |
| 173 | + return getSessionsStatement; |
| 174 | + } |
| 175 | + |
| 176 | + private PreparedStatement getDeleteSqlForSession(SessionId sessionId) throws SQLException{ |
| 177 | + PreparedStatement deleteSessionStatement = connection.prepareStatement(String.format("delete from %1$s where %2$s = ?", |
| 178 | + TABLE_NAME, |
| 179 | + SESSION_ID_COL)); |
| 180 | + |
| 181 | + deleteSessionStatement.setString(1, sessionId.toString()); |
| 182 | + |
| 183 | + return deleteSessionStatement; |
| 184 | + |
| 185 | + } |
| 186 | +} |
0 commit comments