/** * Copyright (c) 1996-2006 Cafesoft, LLC. All Rights Reserved. * * This software is the confidential and proprietary information of * Cafesoft, LLC. ("Confidential Information"). You shall not disclose * such Confidential Information and shall use it only in accordance * with the terms of the license agreement you entered into with Cafesoft. * * CAFESOFT MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, * OR NON-INFRINGEMENT. CAFESOFT SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE * OR ITS DERIVATIVES. */ package examples.session; import com.cafesoft.cams.Config; import com.cafesoft.cams.ConfigException; import com.cafesoft.cams.session.ManagedSession; import com.cafesoft.cams.session.ManagedSessionEvent; import com.cafesoft.cams.session.ManagedSessionEventType; import com.cafesoft.cams.session.ManagedSessionEventHandler; import com.cafesoft.cams.auth.SubjectUtils; import com.cafesoft.core.log.Logger; import java.util.Properties; import java.sql.Connection; import java.sql.Driver; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; /** * Demonstrates a basic handler for ManagedSessionEvents * that queries user-specific data from a relational database via JDBC and * inserts corresponding attributes into a Cams ManagedSession. * Configuration parameters are: * * * * @version $Revision: 1.6 $ $Date: 2006/08/30 18:31:07 $ */ public class UserAttributeManagedSessionEventHandler implements ManagedSessionEventHandler { /** The Config object provided at initialization */ private Config config; /** The flag that enables/disables DEBUG-level messages. */ private boolean debug = false; /** Logs messages. */ private Logger logger; /** The loaded JDBC Driver. */ private Driver driver; /** The loaded JDBC username/password Connection Properties. */ private Properties jdbcProp; /** * Initialize the ManagedSessionEventHandler. * * @param config a Config object that provides access to configuration * parameters and a runtime Context. */ public void initialize(Config config) throws ConfigException { this.config = config; this.logger = config.getContext().getLogger(); // Check for required configuration parameters String jdbcDriver = config.getInitParameter("jdbcDriver"); if ((jdbcDriver == null) || "".equals(jdbcDriver.trim())) throw new ConfigException("Missing required parameter 'jdbcDriver'"); String url = config.getInitParameter("url"); if ((url == null) || "".equals(url.trim())) throw new ConfigException("Missing required parameter 'url'"); String sql = config.getInitParameter("sql"); if ((sql == null) || "".equals(sql.trim())) throw new ConfigException("Missing required parameter 'sql'"); String nameSpace = config.getInitParameter("nameSpace"); if ((nameSpace == null) || "".equals(nameSpace.trim())) throw new ConfigException("Missing required parameter 'nameSpace'"); String attributeName = config.getInitParameter("attributeName"); if ((attributeName == null) || "".equals(attributeName.trim())) throw new ConfigException("Missing required parameter 'attributeName'"); // Make sure the JDBC Driver loads try { this.driver = (Driver)Class.forName(jdbcDriver).newInstance(); } catch (Exception e) { throw new ConfigException( "Error loading jdbcDriver: " + jdbcDriver, e); } // Create the JDBC Connection Properties (user/password) // so we don't have to do it for every Session created this.jdbcProp = new Properties(); jdbcProp.put("user", config.getInitParameter("user", "")); jdbcProp.put("password", config.getInitParameter("password", "")); } /** * Enable or disable debugging. * * @param enable if true, enable debugging, else disable debugging. * @return the previous value of the debug flag, which is useful for * restoring the debug state if temporarily changed. */ public boolean setDebug(boolean enable) { boolean prevDebug = this.debug; this.debug = enable; return prevDebug; } /** * Handle a ManagedSessionEvent. * * @param event the ManagedSessionEvent to be handled. */ public void handleManagedSessionEvent(ManagedSessionEvent event) { if(debug) { logger.info(this, "Handling ManagedSessionEvent: class=" + getClass().getName()); } // If the session was just created, then add an attribute. if (event.getEventType() == ManagedSessionEventType.SESSION_CREATED) { // Get the user's name ManagedSession session = event.getManagedSession(); String userName = SubjectUtils.getUserName(session.getSubject()); Connection c = null; PreparedStatement s = null; ResultSet r = null; try { // Connect to the database c = driver.connect(config.getInitParameter("url"), jdbcProp); // Execute and process the SQL query s = c.prepareStatement(config.getInitParameter("sql")); s.setString(1, userName); r = s.executeQuery(); if (r.next()) { // Get the account balance for the user String value = r.getString(1); // Persist the value into the session object session.putAttribute(config.getInitParameter("nameSpace"), config.getInitParameter("attributeName"), value); } else { logger.info(this, "Attribute value for '" + userName + "' does not exist"); } } catch (SQLException e) { logger.error(this, "error querying attribute value for " + userName + e); } finally { try { if (r != null) r.close(); if (s != null) s.close(); if (c != null) c.close(); } catch (SQLException e) { logger.error(this, "error closing database connection " + e); } } } if(debug) { logger.debug(this, "Handled ManagedSessionEvent: class=" + this.getClass().getName()); } } } // End of class: UserAttributeManagedSessionEventHandler