/******************************************************************************
  @header FTSessionManagerImpl
 
  @availability OS X, GNUstep
  @copyright 2004, 2005, 2006 Free Software Foundation, Inc.
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA

  <pre>
  -------------------------------------------------------------------------
  Modification history

  31.01.2005 ola     initial version
  23.08.2006 ola     license changed
  23.08.2006 ola     license changed
  -------------------------------------------------------------------------
  </pre>
******************************************************************************/

#include <FT/FTDefines.h>
#include <Encore/ECExceptions.h>
#include <FT/FTSessionManagerImpl.h>
#include <FT/FTAdministrationSessionImpl.h>
#include <FT/FTServerImpl.h>
#include <FT/FTLogging.h>

@implementation FTSessionManagerImpl

- initForServer: (FTServerImpl *) theServer {
  self = [ super init ];
  self->lock = [[NSLock alloc] init];
  self->server = [theServer retain];
  self->session_counter = 0;
  
  return self;
}


- (void) dealloc {
  [self->lock release];
  [self->server release];

  [super dealloc];
}


- (id) createSessionId {
  NSString *sessionId;
  
  [self->lock lock];
  self->session_counter++;
  [self->lock unlock];
  
  sessionId = [[NSString alloc] initWithFormat: @"@FTSessionID:%d", 
    self->session_counter];
    
  return sessionId;
}


- (id <FTSession>) loginAs: (NSString *) loginId
  withPassword: (NSString *) password {
  FTSessionImpl *toReturn = nil;
  
  if( [loginId isEqual: @"altum"] && [password isEqual: @"silentium"] ) {
    NS_DURING
      id sessionId = [self createSessionId];
      [self->lock lock];
      toReturn = [[FTAdministrationSessionImpl alloc] 
        initForSessionManager: self
	    server: self->server 
        useSessionId: sessionId ];
	NS_HANDLER
      [[FTLogging coreLog]
        error: @"Got unexpected exception %@", localException ];
      [self->lock unlock];
      [localException raise];
	NS_ENDHANDLER
    
    [self->lock unlock];
  } else {
    [[[ECPermissionDeniedException alloc] 
	  initForAccessObject: @"Access to FTSession"] raise];
  }
  
  return toReturn;
}


- reconnectSession: (id <FTSession>) aSession {
  // at present this method does nothing since we do not have a reference to
  // open sessions and the like...
  return self;
}
@end
