SecurityFactory.java :  » MVC » cloak-mvc » com » dnc » cloak » framework » security » Java Open Source

Java Open Source » MVC » cloak mvc 
cloak mvc » com » dnc » cloak » framework » security » SecurityFactory.java
/**
 * Copyright (C) 2010 Andrew C. Love <drewncrew@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dnc.cloak.framework.security;

import java.security.Principal;
import java.util.UUID;

import javax.security.auth.login.LoginContext;

import org.apache.log4j.Logger;


public class SecurityFactory {
  
  private static final Logger logger = Logger.getLogger(SecurityFactory.class.getName());

  private static final SecurityFactory instance = new SecurityFactory();

  
  private SecurityFactory() {
    super();  
    
    if(logger.isDebugEnabled()){
      logger.debug("SecurityFactory constructor");
    }
  }

  public synchronized static SecurityFactory getInstance() {
    return instance;
  }

  public CloakPrincipal createPrincipal(String userName,  String password) throws CloakSecurityException {
    CloakPrincipal cloakPrincipal = new CloakPrincipal();
    cloakPrincipal.setName(userName);
    cloakPrincipal.setPassword(password);
    logger.debug("Created principal:" + userName );
        
    return cloakPrincipal;
  }
  
  public ISecurityContext createSecurityContext(Principal principle) throws CloakSecurityException {
    ISecurityContext securityContext = new DefaultSecurityContext(principle);
    logger.debug("Created security context for:" + principle.getName() );
    
    return securityContext;
  }
  
  public ISecurityContext createSecurityContext(LoginContext lc ) throws CloakSecurityException {
    ISecurityContext securityContext = new DefaultSecurityContext(lc);
    logger.debug("Created security context for:" + lc);
    
    return securityContext;
  }
  
  public ISessionContext createSession(LoginContext lc) throws CloakSecurityException {
    String sessionId = UUID.randomUUID().toString();
    DefaultSessionContext session = new DefaultSessionContext(sessionId);    
    logger.debug("Created session:" + lc.toString() + ":" + sessionId );
    
    return session;
  }
  
  public ISessionContext createSession(Principal principal) throws CloakSecurityException {
    String sessionId = UUID.randomUUID().toString();
    DefaultSessionContext session = new DefaultSessionContext(sessionId);    
    logger.debug("Created session:" + principal.toString() + ":" + sessionId );
    
    return session;
  }

  public ISessionContext createStatelessSession(LoginContext lc) throws CloakSecurityException {
    String sessionId = UUID.randomUUID().toString();
    DefaultSessionContext session = new DefaultSessionContext(sessionId);    
    logger.debug("Created stateless session:" + lc.toString() + ":" + sessionId );
    
    return session;
  }  
  
  public ISessionContext createStatelessSession(Principal principal) throws CloakSecurityException {
    String sessionId = UUID.randomUUID().toString();
    DefaultSessionContext session = new DefaultSessionContext(sessionId);    
    logger.debug("Created stateless session:" + principal.toString() + ":" + sessionId );
    
    return session;
  }  
  
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.