All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.bonita.identity.impl.InMemoryIdentityService Maven / Gradle / Ivy

/**
 * Copyright (C) 2007  Bull S. A. S.
 * Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois
 * 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
 * version 2.1 of the License.
 * 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
 * program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA  02110-1301, USA.
 **/
package org.ow2.bonita.identity.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.ow2.bonita.identity.CommitException;
import org.ow2.bonita.identity.GroupNotFoundException;
import org.ow2.bonita.identity.GroupOp;
import org.ow2.bonita.identity.IdentityServiceOp;
import org.ow2.bonita.identity.Membership;
import org.ow2.bonita.identity.UserNotFoundException;
import org.ow2.bonita.identity.UserOp;

/**
 * @author "Pierre Vigneras"
 * @date Nov 27, 2007
 */
public class InMemoryIdentityService implements IdentityServiceOp {
  public static final String ROOT_ID = new Integer(0).toString();
  static {
    GroupImpl.setRoot(ROOT_ID);
  }

  protected int next;

  protected Map users;

  protected Map groupOps;

  public InMemoryIdentityService() {
    init();
  }

  protected void init() {
    this.next = 1;
    this.users = new HashMap();
    this.groupOps = new HashMap();
  }

  protected GroupImpl freshGroup() {
    return new GroupImpl();
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.ow2.bonita.identity.IdentityServiceOp#createGroup(org.ow2.bonita.identity
   * .GroupOp)
   */
  public String createGroup(final GroupOp parent) {
    final String id;
    final GroupImpl group;
    synchronized (groupOps) {
      id = new Integer(next++).toString();
      group = freshGroup();
      group.setId(id);
      group.setParent(parent);
      groupOps.put(id, group);
    }
    return id;
  }

  public String createGroup() {
    return createGroup(GroupImpl.root());
  }

  public void deleteGroup(String id) throws GroupNotFoundException {
    synchronized (groupOps) {
      if (groupOps.get(id) == null) {
        throw new GroupNotFoundException(id);
      }
      groupOps.remove(id);
    }
  }

  protected UserImpl freshUser() {
    return new UserImpl();
  }

  public String createUser() {
    final String id;
    final UserImpl user;
    synchronized (users) {
      id = new Integer(next++).toString();
      user = freshUser();
      user.setId(id);
      users.put(id, user);
    }
    return id;
  }

  public void deleteUser(String id) throws UserNotFoundException {
    synchronized (users) {
      if (users.get(id) == null) {
        throw new UserNotFoundException(id);
      }
      users.remove(id);
    }
  }

  public boolean modifyGroup(GroupOp group) throws GroupNotFoundException {
    synchronized (groupOps) {
      if (groupOps.get(group.getId()) == null) {
        throw new GroupNotFoundException(group);
      }
      groupOps.put(group.getId(), group);
    }
    return true;
  }

  public boolean modifyUser(UserOp user) throws UserNotFoundException {
    synchronized (users) {
      if (users.get(user.getId()) == null) {
        throw new UserNotFoundException(user);
      }
      users.put(user.getId(), user);
    }
    return true;
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.ow2.bonita.identity.IdentityServiceOp#getAllGroups()
   */
  public Collection getAllGroups() {
    return groupOps.values();
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.ow2.bonita.identity.IdentityServiceOp#getAllUsers()
   */
  public Collection getAllUsers() {
    return users.values();
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.ow2.bonita.identity.IdentityServiceOp#getGroup(java.lang.Object)
   */
  public GroupOp getGroup(String id) throws GroupNotFoundException {
    synchronized (groupOps) {
      final GroupOp group = groupOps.get(id);
      if (group == null) {
        throw new GroupNotFoundException(id);
      }
      return group;
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.ow2.bonita.identity.IdentityServiceOp#getUser(java.lang.Object)
   */
  public UserOp getUser(String id) throws UserNotFoundException {
    synchronized (users) {
      final UserOp user = users.get(id);
      if (user == null) {
        throw new UserNotFoundException(id);
      }
      return user;
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.ow2.bonita.identity.IdentityServiceOp#setMembership(org.ow2.bonita.
   * identity.UserOp, org.ow2.bonita.identity.GroupOp)
   */
  public Membership setMembership(final UserOp user, final GroupOp group) throws UserNotFoundException, GroupNotFoundException {
    synchronized (groupOps) {
      if (groupOps.get(group.getId()) == null) {
        throw new GroupNotFoundException(group);
      }
      synchronized (users) {
        if (users.get(user.getId()) == null) {
          throw new UserNotFoundException(user);
        }
        if (!(user instanceof UserImpl && group instanceof GroupImpl)) {
          throw new IllegalArgumentException(
              "user should be an instance of the class "
                  + UserImpl.class.getName()
                  + " and group an instance of the class "
                  + GroupImpl.class.getName());
        }
        final UserImpl u = (UserImpl) user;
        final GroupImpl g = (GroupImpl) group;
        final MembershipImpl membership = new MembershipImpl(u, g);
        setMembership(membership);
        return membership;
      }
    }
  }

  protected void setMembership(final MembershipImpl membership) {
    final UserImpl user = (UserImpl) membership.getUser();
    Set memberships = user.getMemberships();
    memberships = new HashSet(memberships);
    memberships.add(membership);
    user.setMemberships(memberships);
    final GroupImpl group = (GroupImpl) membership.getGroup();
    memberships = group.getMemberships();
    memberships = new HashSet(memberships);
    memberships.add(membership);
    group.setMemberships(memberships);
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.ow2.bonita.identity.IdentityServiceOp#commit()
   */
  public void commit() throws CommitException {
    // Nothing to do, we are in memory!
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.ow2.bonita.identity.IdentityServiceOp#authenticateUser(java.lang.String
   * , java.lang.String)
   */
  public String authenticateUser(String cname, String password) {

    return null;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy