/*
 * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.controller.web;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.opendaylight.controller.sal.utils.ServiceHelper;
import org.opendaylight.controller.usermanager.IUserManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ControllerUISessionManager implements HttpSessionListener {

    private static final Logger logger = LoggerFactory
            .getLogger(ControllerUISessionManager.class);

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        ((HttpSessionListener) getUserManagerRef().getSessionManager())
                .sessionCreated(se);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        ((HttpSessionListener) getUserManagerRef().getSessionManager())
                .sessionDestroyed(se);
    }

    private IUserManager getUserManagerRef() {
        IUserManager userManager = (IUserManager) ServiceHelper
                .getGlobalInstance(IUserManager.class, this);
        if (userManager != null) {
            return userManager;
        } else {
            logger.error("UserManager Ref is null. ");
            throw new RuntimeException("UserManager Ref is null. ");
        }
    }

}

@@ -743,29 +743,16 @@
 rFactory
-%0A            
 .getLogg
@@ -870,186 +870,148 @@
     
-((HttpSessionListener) getUserManagerRef().getSessionManager())%0A                .sessionCreated(se
+IUserManager userManager = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, this
 );%0A    
-%7D%0A%0A
     
-@Override%0A    public void sessionDestroyed(HttpSessionEvent se) %7B%0A
+if (userManager != null) %7B%0A    
     
@@ -1037,20 +1037,17 @@
 stener) 
-getU
+u
 serManag
@@ -1048,21 +1048,16 @@
 rManager
-Ref()
 .getSess
@@ -1073,98 +1073,227 @@
 r())
-%0A                .sessionDestroyed(se);%0A    %7D%0A%0A    private IUserManager getUserManagerRef(
+.sessionCreated(se);%0A        %7D else %7B%0A            logger.warn(%22User Manager is currently unavailable. Unable to register UI session.%22);%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void sessionDestroyed(HttpSessionEvent se
 ) %7B%0A
@@ -1359,25 +1359,8 @@
 lper
-%0A                
 .get
@@ -1452,26 +1452,84 @@
     
-return userManager
+((HttpSessionListener) userManager.getSessionManager()).sessionDestroyed(se)
 ;%0A  
@@ -1566,27 +1566,27 @@
 ger.
-error
+warn
 (%22User
+ 
 Manager 
 Ref 
@@ -1585,90 +1585,63 @@
 ger 
-Ref is null. %22);%0A            throw new RuntimeException(%22UserManager Ref is null. 
+is currently unavailable. Unable to destroy UI session.
 %22);%0A

Remove unnecessary exception throws