From dae8307b3fe6d4de2babddf3aeebaaa75e06bcaa Mon Sep 17 00:00:00 2001 From: James Muehlner Date: Tue, 23 Jul 2013 23:03:32 -0700 Subject: [PATCH] Ticker #263: Set up dependency injection for ConnectionGroupService. --- .../mysql/MySQLAuthenticationProvider.java | 4 +- .../net/auth/mysql/MySQLConnectionGroup.java | 7 + .../mysql/service/ConnectionGroupService.java | 421 ++---------------- 3 files changed, 35 insertions(+), 397 deletions(-) diff --git a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/MySQLAuthenticationProvider.java b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/MySQLAuthenticationProvider.java index 1c5c28fe7..0bdcdc9e9 100644 --- a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/MySQLAuthenticationProvider.java +++ b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/MySQLAuthenticationProvider.java @@ -56,12 +56,13 @@ import net.sourceforge.guacamole.net.auth.mysql.dao.SystemPermissionMapper; import net.sourceforge.guacamole.net.auth.mysql.dao.UserMapper; import net.sourceforge.guacamole.net.auth.mysql.dao.UserPermissionMapper; import net.sourceforge.guacamole.net.auth.mysql.properties.MySQLGuacamoleProperties; +import net.sourceforge.guacamole.net.auth.mysql.service.ConnectionGroupService; import net.sourceforge.guacamole.net.auth.mysql.service.ConnectionService; import net.sourceforge.guacamole.net.auth.mysql.service.PasswordEncryptionService; import net.sourceforge.guacamole.net.auth.mysql.service.PermissionCheckService; +import net.sourceforge.guacamole.net.auth.mysql.service.SHA256PasswordEncryptionService; import net.sourceforge.guacamole.net.auth.mysql.service.SaltService; import net.sourceforge.guacamole.net.auth.mysql.service.SecureRandomSaltService; -import net.sourceforge.guacamole.net.auth.mysql.service.SHA256PasswordEncryptionService; import net.sourceforge.guacamole.net.auth.mysql.service.UserService; import net.sourceforge.guacamole.properties.GuacamoleProperties; import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; @@ -172,6 +173,7 @@ public class MySQLAuthenticationProvider implements AuthenticationProvider { bind(PasswordEncryptionService.class).to(SHA256PasswordEncryptionService.class); bind(PermissionCheckService.class); bind(ConnectionService.class); + bind(ConnectionGroupService.class); bind(UserService.class); bind(ActiveConnectionSet.class).toInstance(activeConnectionSet); diff --git a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/MySQLConnectionGroup.java b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/MySQLConnectionGroup.java index 18b042fa7..f4ec4be7c 100644 --- a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/MySQLConnectionGroup.java +++ b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/MySQLConnectionGroup.java @@ -44,6 +44,7 @@ import net.sourceforge.guacamole.net.auth.AbstractConnectionGroup; import net.sourceforge.guacamole.net.auth.Connection; import net.sourceforge.guacamole.net.auth.ConnectionGroup; import net.sourceforge.guacamole.net.auth.Directory; +import net.sourceforge.guacamole.net.auth.mysql.service.ConnectionGroupService; import net.sourceforge.guacamole.protocol.GuacamoleClientInformation; /** @@ -71,6 +72,12 @@ public class MySQLConnectionGroup extends AbstractConnectionGroup { * A Directory of connection groups that have this connection group as a parent. */ private Directory connectionGroupDirectory; + + /** + * Service managing connection groups. + */ + @Inject + private ConnectionGroupService connectionGroupService; /** * Service for managing connection groups. diff --git a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/ConnectionGroupService.java b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/ConnectionGroupService.java index 7e1949ead..c969d9459 100644 --- a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/ConnectionGroupService.java +++ b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/ConnectionGroupService.java @@ -37,420 +37,49 @@ package net.sourceforge.guacamole.net.auth.mysql.service; * * ***** END LICENSE BLOCK ***** */ -import com.google.common.collect.Lists; import com.google.inject.Inject; import com.google.inject.Provider; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import net.sourceforge.guacamole.GuacamoleClientException; -import net.sourceforge.guacamole.GuacamoleException; import net.sourceforge.guacamole.net.GuacamoleSocket; -import net.sourceforge.guacamole.net.InetGuacamoleSocket; -import net.sourceforge.guacamole.net.auth.mysql.ActiveConnectionSet; -import net.sourceforge.guacamole.net.auth.mysql.MySQLConnection; -import net.sourceforge.guacamole.net.auth.mysql.MySQLConnectionRecord; -import net.sourceforge.guacamole.net.auth.mysql.MySQLGuacamoleSocket; -import net.sourceforge.guacamole.net.auth.mysql.dao.ConnectionHistoryMapper; -import net.sourceforge.guacamole.net.auth.mysql.dao.ConnectionMapper; -import net.sourceforge.guacamole.net.auth.mysql.dao.ConnectionParameterMapper; -import net.sourceforge.guacamole.net.auth.mysql.model.Connection; -import net.sourceforge.guacamole.net.auth.mysql.model.ConnectionExample; -import net.sourceforge.guacamole.net.auth.mysql.model.ConnectionHistory; -import net.sourceforge.guacamole.net.auth.mysql.model.ConnectionHistoryExample; -import net.sourceforge.guacamole.net.auth.mysql.model.ConnectionParameter; -import net.sourceforge.guacamole.net.auth.mysql.model.ConnectionParameterExample; -import net.sourceforge.guacamole.net.auth.mysql.properties.MySQLGuacamoleProperties; -import net.sourceforge.guacamole.properties.GuacamoleProperties; -import net.sourceforge.guacamole.protocol.ConfiguredGuacamoleSocket; +import net.sourceforge.guacamole.net.auth.mysql.MySQLConnectionGroup; +import net.sourceforge.guacamole.net.auth.mysql.dao.ConnectionGroupMapper; import net.sourceforge.guacamole.protocol.GuacamoleClientInformation; -import net.sourceforge.guacamole.protocol.GuacamoleConfiguration; -import org.apache.ibatis.session.RowBounds; /** * Service which provides convenience methods for creating, retrieving, and - * manipulating connections. + * manipulating connection groups. * - * @author Michael Jumper, James Muehlner + * @author James Muehlner */ public class ConnectionGroupService { - - /** - * DAO for accessing connections. - */ - @Inject - private ConnectionMapper connectionDAO; - - /** - * DAO for accessing connection parameters. - */ - @Inject - private ConnectionParameterMapper connectionParameterDAO; - - /** - * DAO for accessing connection history. - */ - @Inject - private ConnectionHistoryMapper connectionHistoryDAO; - - /** - * Provider which creates MySQLConnections. - */ - @Inject - private Provider mySQLConnectionProvider; - - /** - * Provider which creates MySQLGuacamoleSockets. - */ - @Inject - private Provider mySQLGuacamoleSocketProvider; - - /** - * Set of all currently active connections. - */ - @Inject - private ActiveConnectionSet activeConnectionSet; - + /** * Service managing users. */ @Inject private UserService userService; + + /** + * Service managing connections. + */ + @Inject + private ConnectionService connectionService; + + /** + * DAO for accessing connection groups. + */ + @Inject + private ConnectionGroupMapper connectionGroupDAO; /** - * Retrieves the connection having the given name from the database. - * - * @param name The name of the connection to return. - * @param userID The ID of the user who queried this connection. - * @return The connection having the given name, or null if no such - * connection could be found. + * Provider which creates MySQLConnectionGroups. */ - public MySQLConnection retrieveConnection(String name, int userID) { - - // Query connection by connection identifier (name) - ConnectionExample example = new ConnectionExample(); - example.createCriteria().andConnection_nameEqualTo(name); - List connections = - connectionDAO.selectByExample(example); - - // If no connection found, return null - if(connections.isEmpty()) - return null; - - // Assert only one connection found - assert connections.size() == 1 : "Multiple connections with same name."; - - // Otherwise, return found connection - return toMySQLConnection(connections.get(0), userID); + @Inject + private Provider mysqlConnectionGroupProvider; + public GuacamoleSocket connect(MySQLConnectionGroup aThis, + GuacamoleClientInformation info, int userID) { + throw new UnsupportedOperationException("Not yet implemented"); } - - /** - * Retrieves the connection having the given ID from the database. - * - * @param id The ID of the connection to retrieve. - * @param userID The ID of the user who queried this connection. - * @return The connection having the given ID, or null if no such - * connection was found. - */ - public MySQLConnection retrieveConnection(int id, int userID) { - - // Query connection by ID - Connection connection = connectionDAO.selectByPrimaryKey(id); - - // If no connection found, return null - if(connection == null) - return null; - - // Otherwise, return found connection - return toMySQLConnection(connection, userID); - } - - /** - * Retrieves a translation map of connection names to their corresponding - * IDs. - * - * @param ids The IDs of the connections to retrieve the names of. - * @return A map containing the names of all connections and their - * corresponding IDs. - */ - public Map translateNames(List ids) { - - // If no IDs given, just return empty map - if (ids.isEmpty()) - return Collections.EMPTY_MAP; - - // Map of all names onto their corresponding IDs. - Map names = new HashMap(); - - // Get all connections having the given IDs - ConnectionExample example = new ConnectionExample(); - example.createCriteria().andConnection_idIn(ids); - List connections = connectionDAO.selectByExample(example); - - // Produce set of names - for (Connection connection : connections) - names.put(connection.getConnection_name(), - connection.getConnection_id()); - - return names; - - } - - /** - * Retrieves a map of all connection names for the given IDs. - * - * @param ids The IDs of the connections to retrieve the names of. - * @return A map containing the names of all connections and their - * corresponding IDs. - */ - public Map retrieveNames(Collection ids) { - - // If no IDs given, just return empty map - if (ids.isEmpty()) - return Collections.EMPTY_MAP; - - // Map of all names onto their corresponding IDs. - Map names = new HashMap(); - - // Get all connections having the given IDs - ConnectionExample example = new ConnectionExample(); - example.createCriteria().andConnection_idIn(Lists.newArrayList(ids)); - List connections = connectionDAO.selectByExample(example); - - // Produce set of names - for (Connection connection : connections) - names.put(connection.getConnection_id(), - connection.getConnection_name()); - - return names; - - } - - /** - * Convert the given database-retrieved Connection into a MySQLConnection. - * The parameters of the given connection will be read and added to the - * MySQLConnection in the process. - * - * @param connection The connection to convert. - * @param userID The user who queried this connection. - * @return A new MySQLConnection containing all data associated with the - * specified connection. - */ - private MySQLConnection toMySQLConnection(Connection connection, int userID) { - - // Build configuration - GuacamoleConfiguration config = new GuacamoleConfiguration(); - - // Query parameters for configuration - ConnectionParameterExample connectionParameterExample = new ConnectionParameterExample(); - connectionParameterExample.createCriteria().andConnection_idEqualTo(connection.getConnection_id()); - List connectionParameters = - connectionParameterDAO.selectByExample(connectionParameterExample); - - // Set protocol - config.setProtocol(connection.getProtocol()); - - // Set all values for all parameters - for (ConnectionParameter parameter : connectionParameters) - config.setParameter(parameter.getParameter_name(), - parameter.getParameter_value()); - - // Create new MySQLConnection from retrieved data - MySQLConnection mySQLConnection = mySQLConnectionProvider.get(); - mySQLConnection.init( - connection.getConnection_id(), - connection.getConnection_name(), - config, - retrieveHistory(connection.getConnection_id()), - userID - ); - - return mySQLConnection; - - } - - /** - * Retrieves the history of the connection having the given ID. - * - * @param connectionID The ID of the connection to retrieve the history of. - * @return A list of MySQLConnectionRecord documenting the history of this - * connection. - */ - public List retrieveHistory(int connectionID) { - - // Retrieve history records relating to given connection ID - ConnectionHistoryExample example = new ConnectionHistoryExample(); - example.createCriteria().andConnection_idEqualTo(connectionID); - - // We want to return the newest records first - example.setOrderByClause("start_date DESC"); - - // Set the maximum number of history records returned to 100 - RowBounds rowBounds = new RowBounds(0, 100); - - // Retrieve all connection history entries - List connectionHistories = - connectionHistoryDAO.selectByExampleWithRowbounds(example, rowBounds); - - // Convert history entries to connection records - List connectionRecords = new ArrayList(); - Set userIDSet = new HashSet(); - for(ConnectionHistory history : connectionHistories) { - userIDSet.add(history.getUser_id()); - } - - // Get all the usernames for the users who are in the history - Map usernameMap = userService.retrieveUsernames(userIDSet); - - // Create the new ConnectionRecords - for(ConnectionHistory history : connectionHistories) { - Date startDate = history.getStart_date(); - Date endDate = history.getEnd_date(); - String username = usernameMap.get(history.getUser_id()); - MySQLConnectionRecord connectionRecord = new MySQLConnectionRecord(startDate, endDate, username); - connectionRecords.add(connectionRecord); - } - - return connectionRecords; - } - - /** - * Create a MySQLGuacamoleSocket using the provided connection. - * - * @param connection The connection to use when connecting the socket. - * @param info The information to use when performing the connection - * handshake. - * @param userID The ID of the user who is connecting to the socket. - * @return The connected socket. - * @throws GuacamoleException If an error occurs while connecting the - * socket. - */ - public MySQLGuacamoleSocket connect(MySQLConnection connection, - GuacamoleClientInformation info, int userID) - throws GuacamoleException { - - // If the given connection is active, and multiple simultaneous - // connections are not allowed, disallow connection - if(GuacamoleProperties.getProperty( - MySQLGuacamoleProperties.MYSQL_DISALLOW_SIMULTANEOUS_CONNECTIONS, false) - && activeConnectionSet.isActive(connection.getConnectionID())) - throw new GuacamoleClientException("Cannot connect. This connection is in use."); - - // Get guacd connection information - String host = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_HOSTNAME); - int port = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_PORT); - - // Get socket - GuacamoleSocket socket = new ConfiguredGuacamoleSocket( - new InetGuacamoleSocket(host, port), - connection.getConfiguration(), info - ); - - // Mark this connection as active - int historyID = activeConnectionSet.openConnection(connection.getConnectionID(), userID); - - // Return new MySQLGuacamoleSocket - MySQLGuacamoleSocket mySQLGuacamoleSocket = mySQLGuacamoleSocketProvider.get(); - mySQLGuacamoleSocket.init(socket, connection.getConnectionID(), historyID); - return mySQLGuacamoleSocket; - - } - - /** - * Creates a new connection having the given name and protocol. - * - * @param name The name to assign to the new connection. - * @param protocol The protocol to assign to the new connection. - * @param userID The ID of the user who created this connection. - * @return A new MySQLConnection containing the data of the newly created - * connection. - */ - public MySQLConnection createConnection(String name, String protocol, int userID) { - - // Initialize database connection - Connection connection = new Connection(); - connection.setConnection_name(name); - connection.setProtocol(protocol); - - // Create connection - connectionDAO.insert(connection); - return toMySQLConnection(connection, userID); - - } - - /** - * Deletes the connection having the given ID from the database. - * @param id The ID of the connection to delete. - */ - public void deleteConnection(int id) { - connectionDAO.deleteByPrimaryKey(id); - } - - /** - * Updates the connection in the database corresponding to the given - * MySQLConnection. - * - * @param mySQLConnection The MySQLConnection to update (save) to the - * database. This connection must already exist. - */ - public void updateConnection(MySQLConnection mySQLConnection) { - - // Populate connection - Connection connection = new Connection(); - connection.setConnection_id(mySQLConnection.getConnectionID()); - connection.setConnection_name(mySQLConnection.getIdentifier()); - connection.setProtocol(mySQLConnection.getConfiguration().getProtocol()); - - // Update the connection in the database - connectionDAO.updateByPrimaryKeySelective(connection); - - } - - /** - * Get the names of all the connections defined in the system. - * - * @return A Set of names of all the connections defined in the system. - */ - public Set getAllConnectionNames() { - - // Set of all present connection names - Set names = new HashSet(); - - // Query all connection names - List connections = - connectionDAO.selectByExample(new ConnectionExample()); - for (Connection connection : connections) - names.add(connection.getConnection_name()); - - return names; - - } - - /** - * Get the connection IDs of all the connections defined in the system. - * - * @return A list of connection IDs of all the connections defined in the system. - */ - public List getAllConnectionIDs() { - - // Set of all present connection IDs - List connectionIDs = new ArrayList(); - - // Query all connection IDs - List connections = - connectionDAO.selectByExample(new ConnectionExample()); - for (Connection connection : connections) - connectionIDs.add(connection.getConnection_id()); - - return connectionIDs; - - } - + + }