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

org.ldaptive.beans.AbstractClassDescriptor Maven / Gradle / Ivy

Go to download

Mapping, persistence, and code generation API for reading and writing POJOs to an LDAP directory

The newest version!
/* See LICENSE for licensing and NOTICE for copyright. */
package org.ldaptive.beans;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import org.ldaptive.LdapAttribute;
import org.ldaptive.LdapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Base implementation of a class descriptor. Stores a map of {@link AttributeValueMutator} and a {@link
 * DnValueMutator}.
 *
 * @author  Middleware Services
 */
public abstract class AbstractClassDescriptor implements ClassDescriptor
{

  /** Logger for this class. */
  protected final Logger logger = LoggerFactory.getLogger(getClass());

  /** Attribute value mutators for this class. */
  private final Map attributeMutators = new LinkedHashMap<>();

  /** Dn value mutator for this class. */
  private DnValueMutator dnMutator;


  /**
   * Sets the dn value mutator.
   *
   * @param  mutator  to set
   */
  protected void setDnValueMutator(final DnValueMutator mutator)
  {
    if (dnMutator != null) {
      throw new IllegalStateException("Found duplicate dn");
    }
    dnMutator = mutator;
  }


  /**
   * Adds an attribute value mutator to this class descriptor.
   *
   * @param  mutator  to add
   */
  protected void addAttributeValueMutator(final AttributeValueMutator mutator)
  {
    if (attributeMutators.containsKey(mutator.getName())) {
      throw new IllegalStateException("Found duplicate attribute name '" + mutator.getName() + "'");
    }
    attributeMutators.put(mutator.getName(), mutator);
  }


  /**
   * Adds attribute value mutators to this class descriptor.
   *
   * @param  mutators  to add
   */
  protected void addAttributeValueMutator(final Collection mutators)
  {
    mutators.forEach(this::addAttributeValueMutator);
  }


  @Override
  public DnValueMutator getDnValueMutator()
  {
    return dnMutator;
  }


  @Override
  public Collection getAttributeValueMutators()
  {
    return attributeMutators.values();
  }


  @Override
  public AttributeValueMutator getAttributeValueMutator(final String name)
  {
    return attributeMutators.get(name);
  }


  @Override
  public String toString()
  {
    return "[" +
      getClass().getName() + "@" + hashCode() + "::" +
      "dnMutator=" + dnMutator + ", " +
      "attributeMutators=" + attributeMutators + "]";
  }


  /** Stores the DN value from a {@link Entry} configuration. Setter method is a no-op. */
  protected static class SimpleDnValueMutator implements DnValueMutator
  {

    /** DN value to store. */
    private final String dn;


    /**
     * Creates a new simple dn value mutator.
     *
     * @param  value  that is the DN
     */
    public SimpleDnValueMutator(final String value)
    {
      dn = value;
    }


    @Override
    public String getValue(final Object object)
    {
      return dn;
    }


    @Override
    public void setValue(final Object object, final String value) {}
  }


  /** Stores an {@link Attribute} configuration in an {@link LdapAttribute} object. Setter methods are no-ops. */
  protected static class SimpleAttributeValueMutator implements AttributeValueMutator
  {

    /** Ldap attribute to operate on. */
    private final LdapAttribute la;


    /**
     * Creates a new simple attribute value mutator.
     *
     * @param  name  of the attribute
     * @param  values  of the attribute
     * @param  binary  whether values contains base64 encoded data
     */
    public SimpleAttributeValueMutator(final String name, final String[] values, final boolean binary)
    {
      la = new LdapAttribute();
      la.setBinary(binary);
      la.setName(name);
      if (binary) {
        for (String value : values) {
          la.addBinaryValues(LdapUtils.base64Decode(value));
        }
      } else {
        la.addStringValues(values);
      }
    }


    @Override
    public String getName()
    {
      return la.getName();
    }


    @Override
    public boolean isBinary()
    {
      return la.isBinary();
    }


    @Override
    public Collection getStringValues(final Object object)
    {
      return la.getStringValues();
    }


    @Override
    public Collection getBinaryValues(final Object object)
    {
      return la.getBinaryValues();
    }


    @Override
    public void setStringValues(final Object object, final Collection values) {}


    @Override
    public void setBinaryValues(final Object object, final Collection values) {}


    @Override
    public String toString()
    {
      return "[" + getClass().getName() + "@" + hashCode() + "::" + "ldapAttribute=" + la + "]";
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy