/* * Copyright © 2004-2007 by SAP AG. * All Rights Reserved. * * SAP, R/3, mySAP, mySAP.com, xApps, xApp, SAP NetWeaver, and other SAP * products and services mentioned herein as well as their respective logos are * trademarks or registered trademarks of SAP AG in Germany and in several other * countries all over the world. All other product and service names mentioned * are the trademarks of their respective companies. Data contained in this * document serves informational purposes only. National product specifications * may vary. * * These materials are subject to change without notice. These materials are * provided by SAP AG and its affiliated companies (SAP Group) for informational * purposes only, without representation or warranty of any kind, and SAP Group * shall not be liable for errors or omissions with respect to the materials. * The only warranties for SAP Group products and services are those that are * set forth in the express warranty statements accompanying such products and * services, if any. Nothing herein should be construed as constituting an * additional warranty. * */ package com.sap.mdm.examples; import com.sap.mdm.commands.AuthenticateRepositorySessionCommand; import com.sap.mdm.commands.CommandException; import com.sap.mdm.commands.CreateRepositorySessionCommand; import com.sap.mdm.commands.DestroySessionCommand; import com.sap.mdm.net.ConnectionAccessor; import com.sap.mdm.net.ConnectionException; import com.sap.mdm.net.SimpleConnectionFactory; import com.sap.mdm.server.RepositoryIdentifier; import com.sap.tc.logging.ConsoleLog; import com.sap.tc.logging.Location; import com.sap.tc.logging.Severity; /** * Represents an authenticated user session from the API to an MDM repository. * * Source code for * AuthenticatedRepositorySession.java */ public class AuthenticatedRepositorySession { /** State of the session */ public int state = STATE_NOT_CONNECTED; /** Initial state: Not (yet) even connected */ public static final int STATE_NOT_CONNECTED = 0; /** Connected, but no session established yet */ public static final int STATE_CONNECTED = 1; /** Connected, session established, but not yet authenticated */ public static final int STATE_SESSION_ESTABLISHED = 2; /** Ready state: Authenticated user session is established and ready to be used */ public static final int STATE_SESSION_AUTHENTICATED = 3; /** Session identifier, to be used in the setSession() method of the command classes. */ private String session; private ConnectionAccessor connection = null; private RepositoryIdentifier repId; private Settings settings; /** reference to logging location * @see com.sap.tc.logging.Location */ private static final Location trace = Location.getLocation(AuthenticatedRepositorySession.class); /** * Prepare session creation using default settings */ public AuthenticatedRepositorySession() throws ConnectionException { this.settings = new Settings(); connection = SimpleConnectionFactory.getInstance(settings.server, settings.connectionSource); state = STATE_CONNECTED; } /** * Prepare session creation from specific settings * @param settings use these parameters for session creation and authentication */ public AuthenticatedRepositorySession(Settings settings) throws ConnectionException { this.settings = settings; connection = SimpleConnectionFactory.getInstance(settings.server, settings.connectionSource); state = STATE_CONNECTED; } public AuthenticatedRepositorySession(Settings settings, ConnectionAccessor connection) { this.settings = settings; this.connection = connection; state=STATE_CONNECTED; } /** * Create and authenticate a new user session to an MDM repository. * @throws ConnectionException is propagated from the API * @throws CommandException is propagated from the API */ public void create() throws ConnectionException, CommandException { final String METHOD = "create()"; trace.entering(METHOD); if(connection==null || state!=STATE_CONNECTED) { throw new ConnectionException("Create() called without being connected."); } repId = new RepositoryIdentifier(settings.repository, settings.dbServer, settings.dbmsType); CreateRepositorySessionCommand createRepositorySessionCommand = new CreateRepositorySessionCommand(connection); createRepositorySessionCommand.setRepositoryIdentifier(repId); createRepositorySessionCommand.execute(); session = createRepositorySessionCommand.getRepositorySession(); state = STATE_SESSION_ESTABLISHED; AuthenticateRepositorySessionCommand authenticateRepositorySessionCommand = new AuthenticateRepositorySessionCommand(connection); authenticateRepositorySessionCommand.setSession(session); authenticateRepositorySessionCommand.setUserName(settings.username); authenticateRepositorySessionCommand.setUserPassword(settings.password); authenticateRepositorySessionCommand.execute(); state = STATE_SESSION_AUTHENTICATED; trace.exiting(METHOD); } /** * Destroy the session and free reserved resources. * In addition the connection is closed. * This method should be called as soon as a session is not needed any longer. * @throws CommandException is propagated from the API */ public void destroy() throws CommandException { if(state == STATE_SESSION_AUTHENTICATED || state == STATE_SESSION_ESTABLISHED) { if(session != null) { DestroySessionCommand destroySessionCommand = new DestroySessionCommand(connection); destroySessionCommand.setSession(session); destroySessionCommand.execute(); session = null; } } state = STATE_CONNECTED; } /** * Gets the connection to the MDM server created within the create() method. * @return ConnectionAccessor */ public ConnectionAccessor getConnection() { return (ConnectionAccessor) connection; } /** * Gets the session id. * @return authenticated user session */ public String getSession() { return session; } /** * Return the session state in human-readable format. * @see java.lang.Object#toString() */ public String toString() { StringBuffer buf = new StringBuffer(200); buf.append("session:......................"); buf.append(session); buf.append("\n"); buf.append("state:........................"); switch(state) { case STATE_NOT_CONNECTED: buf.append("Not connected"); break; case STATE_CONNECTED: buf.append("Connected"); break; case STATE_SESSION_ESTABLISHED: buf.append("Session established"); break; case STATE_SESSION_AUTHENTICATED: buf.append("Session authenticated"); break; } buf.append("\n"); return buf.toString(); } /** * Simple main method for testing purposes. * @param args are not supported */ public static void main(String[] args) { Location mainTrace = Location.getLocation(""); ConsoleLog log = new ConsoleLog(); mainTrace.setEffectiveSeverity(Severity.ALL); mainTrace.addLog(log); AuthenticatedRepositorySession authenticatedRepositorySession = null; try { authenticatedRepositorySession = new AuthenticatedRepositorySession(); } catch (ConnectionException e) { // Something went wrong during connecting to the MDM repository // We can not recover from this, write info and exit. e.printStackTrace(); System.exit(1); } System.out.println(authenticatedRepositorySession); try { // Try to create an authenticated user session. authenticatedRepositorySession.create(); } catch (ConnectionException e) { // Something went wrong during creating the connection. // We can not recover from this, write info and exit. e.printStackTrace(); System.exit(1); } catch (CommandException e) { // Something went wrong during command execution (e.g. authentication or establishing the session) // We can not recover from this, write info and exit. e.printStackTrace(); System.exit(1); } System.out.println(authenticatedRepositorySession); try { authenticatedRepositorySession.destroy(); } catch (CommandException e) { // Something went wrong during destroying the session (e.g. wrong session identifier used) // Error can not really be handled, inform the user. e.printStackTrace(); System.exit(1); } System.out.println(authenticatedRepositorySession); // Return with success System.exit(0); } }