public ServerLocation getNextOpServerLocation() { ServerLocation retVal = null; Connection conn = (Connection) (threadLocalConnections ? localConnection.get() : null); if (conn == null || conn.isDestroyed()) { conn = connectionManager.borrowConnection(serverTimeout); retVal = conn.getServer(); this.connectionManager.returnConnection(conn); } else { retVal = conn.getServer(); } return retVal; }
/** * Test hook that returns a list of ServerLocation instances. Each ServerLocation describes a * redundant server. An empty list is returned if we have no redundant servers. */ public List<ServerLocation> getRedundants() { List result = Collections.emptyList(); if (this.queueManager != null) { QueueManager.QueueConnections cons = this.queueManager.getAllConnections(); List<Connection> backupCons = cons.getBackups(); if (backupCons.size() > 0) { result = new ArrayList(backupCons.size()); Iterator<Connection> it = backupCons.iterator(); while (it.hasNext()) { Connection con = it.next(); result.add(con.getServer()); } } } return result; }
/** * Execute the given op on the given connection. * * @param con the connection to do the execution on * @param op the operation to execute * @return the result of execution if any; null if not */ @Override public Object executeOn(Connection con, Op op) { authenticateIfRequired(con.getServer(), op); return executor.executeOn(con, op); }
@Override public Object attempt(Connection cnx) throws Exception { if (cnx.getServer().getRequiresCredentials()) { return super.attempt(cnx); } else { return null; } }
/** * Test hook that returns a string consisting of the host name and port of the primary server. * Null is returned if we have no primary. */ public ServerLocation getPrimary() { ServerLocation result = null; if (this.queueManager != null) { QueueManager.QueueConnections cons = this.queueManager.getAllConnections(); Connection con = cons.getPrimary(); result = con.getServer(); } return result; }
/** * Test hook that returns a list of strings. Each string consists of the host name and port of a * redundant server. An empty list is returned if we have no redundant servers. */ public List<String> getRedundantNames() { List result = Collections.emptyList(); if (this.queueManager != null) { QueueManager.QueueConnections cons = this.queueManager.getAllConnections(); List<Connection> backupCons = cons.getBackups(); if (backupCons.size() > 0) { result = new ArrayList(backupCons.size()); Iterator<Connection> it = backupCons.iterator(); while (it.hasNext()) { Connection con = it.next(); ServerLocation sl = con.getServer(); result.add(sl.getHostName() + sl.getPort()); } } } return result; }
if (primary != null && !map.containsKey(primary.getServer())) { Long userId = (Long) AuthenticateUserOp.executeOn(primary.getServer(), this, userAttributes.getCredentials()); if (userId != null) { map.put(primary.getServer(), userId); for (int i = 0; i < backups.size(); i++) { Connection conn = backups.get(i); if (!map.containsKey(conn.getServer())) { Long userId = (Long) AuthenticateUserOp.executeOn(conn.getServer(), this, userAttributes.getCredentials()); if (userId != null) { map.put(conn.getServer(), userId);
this.connectionManager.activate(conn); borrow = false; if (!conn.getServer().equals(server)) {
/** * Process the security information in a response from the server. If the server sends a security * "part" we must process it so all subclasses should allow this method to be invoked. * * @see ServerConnection#updateAndGetSecurityPart() */ protected void processSecureBytes(Connection cnx, Message message) throws Exception { if (cnx.getServer().getRequiresCredentials()) { if (!message.isSecureMode()) { // This can be seen during shutdown if (logger.isTraceEnabled(LogMarker.BRIDGE_SERVER_VERBOSE)) { logger.trace(LogMarker.BRIDGE_SERVER_VERBOSE, "Response message from {} for {} has no secure part.", cnx, this); } return; } byte[] partBytes = message.getSecureBytes(); if (partBytes == null) { if (logger.isDebugEnabled()) { logger.debug("Response message for {} has no bytes in secure part.", this); } return; } byte[] bytes = ((ConnectionImpl) cnx).decryptBytes(partBytes); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes)); cnx.setConnectionID(dis.readLong()); } }
private Connection getMockedConnection(ServerLocation serverLocation, Endpoint endpoint) throws Exception { /* * Mock the connection to throw a RuntimeException() when connection.Execute() is called, * so that we attempt to notify listeners in the exception handling logic in * OpExecutorImpl.executeWithPossibleReAuthentication() */ final Connection connection = mock(PooledConnection.class); doReturn(serverLocation).when(connection).getServer(); doReturn(endpoint).when(connection).getEndpoint(); doThrow(new RuntimeException()).when(connection).execute(any()); return connection; }
private void authenticateMultiuser(PoolImpl pool, Connection conn, UserAttributes ua) { try { Long userId = (Long) AuthenticateUserOp.executeOn(conn.getServer(), pool, ua.getCredentials()); if (userId != null) { ua.setServerToId(conn.getServer(), userId); if (logger.isDebugEnabled()) { logger.debug("OpExecutorImpl.execute() - multiuser mode - authenticated this user on {}", conn); } } } catch (ServerConnectivityException sce) { Throwable cause = sce.getCause(); if (cause instanceof SocketException || cause instanceof EOFException || cause instanceof IOException || cause instanceof BufferUnderflowException || cause instanceof CancelException || (sce.getMessage() != null && (sce.getMessage().indexOf("Could not create a new connection to server") != -1 || sce.getMessage().indexOf("socket timed out on client") != -1 || sce.getMessage().indexOf("connection was asynchronously destroyed") != -1))) { throw new ServerConnectivityException("Connection error while authenticating user"); } else { throw sce; } } }
private void authenticateIfRequired(Connection conn, Op op) { if (!conn.getServer().getRequiresCredentials()) { return; UserAttributes ua = UserAttributes.userAttributes.get(); if (ua != null) { if (!ua.getServerToId().containsKey(conn.getServer())) { authenticateMultiuser(this.pool, conn, ua); if (conn.getServer().getUserId() == -1) { Connection connImpl = this.connectionManager.getConnection(conn); conn.getServer().setUserId((Long) AuthenticateUserOp.executeOn(connImpl, this.pool)); if (logger.isDebugEnabled()) { logger.debug(
private void authenticateIfRequired(Connection conn) { cancelCriterion.checkCancelInProgress(null); if (!pool.isUsedByGateway() && !pool.getMultiuserAuthentication()) { ServerLocation server = conn.getServer(); if (server.getRequiresCredentials()) { if (server.getUserId() == -1) { Long uniqueID = (Long) AuthenticateUserOp.executeOn(conn, pool); server.setUserId(uniqueID); if (logger.isDebugEnabled()) { logger.debug("CFI.authenticateIfRequired() Completed authentication on {}", conn); } } } } }
@Override public Object executeOnPrimary(Op op) { if (queueManager == null) { throw new SubscriptionNotEnabledException(); } HashSet attemptedPrimaries = new HashSet(); while (true) { Connection primary = queueManager.getAllConnections().getPrimary(); try { return executeWithPossibleReAuthentication(primary, op); } catch (Exception e) { boolean finalAttempt = !attemptedPrimaries.add(primary.getServer()); handleException(e, primary, 0, finalAttempt); // we shouldn't reach this code, but just in case if (finalAttempt) { throw new ServerConnectivityException("Tried the same primary server twice.", e); } } } }
private QueueConnectionImpl initializeQueueConnection(Connection connection, boolean isPrimary, ClientUpdater failedUpdater) { QueueConnectionImpl queueConnection = null; FailureTracker failureTracker = denyList.getFailureTracker(connection.getServer()); try { ClientUpdater updater = factory.createServerToClientConnection(connection.getEndpoint(), this, isPrimary, failedUpdater); if (updater != null) { queueConnection = new QueueConnectionImpl(this, connection, updater, failureTracker); } else { logger.warn("unable to create a subscription connection to server {}", connection.getEndpoint()); } } catch (Exception e) { if (logger.isDebugEnabled()) { logger.debug("error creating subscription connection to server {}", connection.getEndpoint(), e); } } if (queueConnection == null) { failureTracker.addFailure(); connection.destroy(); } return queueConnection; }
e.getMessage(), e); boolean finalAttempt = !attemptedPrimaries.add(primary.getServer()); handleException(e, primary, 0, finalAttempt); primary = queueManager.getAllConnections().getPrimary();
bytes = (byte[]) part.getObject(); if (bytes.length == 0) { cnx.getServer().setRequiresCredentials(false); } else { cnx.getServer().setRequiresCredentials(true); byte[] decrypted = ((ConnectionImpl) cnx).decryptBytes(bytes); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(decrypted)); return new Object[] {cnx.getServer(), userId}; } else { return userId;
private Object executeWithPossibleReAuthentication(Connection conn, Op op) throws Exception { try { return conn.execute(op); } catch (ServerConnectivityException sce) { Throwable cause = sce.getCause(); if ((cause instanceof AuthenticationRequiredException && "User authorization attributes not found.".equals(cause.getMessage())) || sce.getMessage().contains("Connection error while authenticating user")) { // (ashetkar) Need a cleaner way of doing above check. // 2nd exception-message above is from AbstractOp.sendMessage() PoolImpl pool = (PoolImpl) PoolManagerImpl.getPMI().find(this.endpointManager.getPoolName()); if (!pool.getMultiuserAuthentication()) { Connection connImpl = this.connectionManager.getConnection(conn); conn.getServer().setUserId((Long) AuthenticateUserOp.executeOn(connImpl, this)); return conn.execute(op); } else { UserAttributes ua = UserAttributes.userAttributes.get(); if (ua != null) { authenticateMultiuser(pool, conn, ua); } return conn.execute(op); } } else { throw sce; } } }
@Override protected void sendMessage(Connection cnx) throws Exception { HeapDataOutputStream hdos = new HeapDataOutputStream(Version.CURRENT); byte[] secureBytes = null; hdos.writeLong(cnx.getConnectionID()); Object userId = UserAttributes.userAttributes.get().getServerToId().get(cnx.getServer()); if (userId == null) { // This will ensure that this op is retried on another server, unless // the retryCount is exhausted. Fix for Bug 41501 throw new ServerConnectivityException("Connection error while authenticating user"); } hdos.writeLong((Long) userId); try { secureBytes = ((ConnectionImpl) cnx).encryptBytes(hdos.toByteArray()); } finally { hdos.close(); } getMessage().setSecurePart(secureBytes); getMessage().send(false); }