Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014-2015 Philip Helger (www.helger.com) philip[at]helger[dot]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.helger.webbasics.smtp;
 
 import java.util.Map;
 import java.util.Set;
 
 
This class manages NamedSMTPSettings objects.

Author(s):
Philip Helger
 
 public class NamedSMTPSettingsManager extends AbstractSimpleDAO implements IHasSize
 {
   private static final String ELEMENT_NAMEDSMTPSETTINGSLIST = "namedsmtpsettingslist";
   private static final String ELEMENT_NAMEDSMTPSETTINGS = "namedsmtpsettings";
 
   private static final ReadWriteLock s_aRWLock = new ReentrantReadWriteLock ();
 
   private static boolean s_bCreateDefaults = true;
 
   private final Map <StringNamedSMTPSettingsm_aMap = new HashMap <StringNamedSMTPSettings> ();
 
   public static boolean isCreateDefaults ()
   {
     .readLock ().lock ();
     try
     {
       return ;
     }
     finally
     {
       .readLock ().unlock ();
     }
   }
 
   public static void setCreateDefaults (final boolean bCreateDefaults)
   {
     .writeLock ().lock ();
     try
     {
        = bCreateDefaults;
     }
     finally
     {
       .writeLock ().unlock ();
     }
   }
 
   public NamedSMTPSettingsManager (@Nonnull @Nonempty final String sFilenamethrows DAOException
   {
     super (sFilename);
     initialRead ();
   }
 
   public void reload () throws DAOException
   {
     .writeLock ().lock ();
     try
    {
      .clear ();
      initialRead ();
    }
    finally
    {
      .writeLock ().unlock ();
    }
  }
  protected EChange onInit ()
  {
    if (!isCreateDefaults ())
      return .;
    // Create default item with as little data as possible
                                     .,
                                     new SMTPSettings (.)));
    return .;
  }
  protected EChange onRead (@Nonnull final IMicroDocument aDoc)
  {
    for (final IMicroElement eNamedSMTPSettings : aDoc.getDocumentElement ()
                                                      .getAllChildElements ())
      _addItem (MicroTypeConverter.convertToNative (eNamedSMTPSettingsNamedSMTPSettings.class));
    return .;
  }
  {
    final IMicroDocument aDoc = new MicroDocument ();
    for (final NamedSMTPSettings aNamedSMTPSettings : ContainerHelper.getSortedByKey ().values ())
      eRoot.appendChild (MicroTypeConverter.convertToMicroElement (aNamedSMTPSettings));
    return aDoc;
  }
  private void _addItem (@Nonnull final NamedSMTPSettings aNamedSMTPSettings)
  {
    final String sUserID = aNamedSMTPSettings.getID ();
    if (.containsKey (sUserID))
      throw new IllegalArgumentException ("NamedSMTPSettings ID " + sUserID + " is already in use!");
    .put (sUserIDaNamedSMTPSettings);
  }
  public int size ()
  {
    .readLock ().lock ();
    try
    {
      return .size ();
    }
    finally
    {
      .readLock ().unlock ();
    }
  }
  public boolean isEmpty ()
  {
    .readLock ().lock ();
    try
    {
      return .isEmpty ();
    }
    finally
    {
      .readLock ().unlock ();
    }
  }

  

Returns:
A copy of all contained SMTP settings as map from ID to object. Never null but maybe empty.
  {
    .readLock ().lock ();
    try
    {
      return ContainerHelper.newMap ();
    }
    finally
    {
      .readLock ().unlock ();
    }
  }

  
Check if the specified settings ID is contained or not.

Parameters:
sID The ID to check. May be null.
Returns:
true if the passed ID is contained
  public boolean containsSettings (@Nullable final String sID)
  {
    if (StringHelper.hasNoText (sID))
      return false;
    .readLock ().lock ();
    try
    {
      return .containsKey (sID);
    }
    finally
    {
      .readLock ().unlock ();
    }
  }

  
Get the settings of the specified ID

Parameters:
sID The ID to search. May be null.
Returns:
null if no such settings are contained.
  public NamedSMTPSettings getSettings (@Nullable final String sID)
  {
    if (StringHelper.hasNoText (sID))
      return null;
    .readLock ().lock ();
    try
    {
      return .get (sID);
    }
    finally
    {
      .readLock ().unlock ();
    }
  }

  
Get the settings with the specified name

Parameters:
sName The name to search. May be null.
Returns:
null if no such settings are contained.
  public NamedSMTPSettings getSettingsOfName (@Nullable final String sName)
  {
    if (StringHelper.hasNoText (sName))
      return null;
    .readLock ().lock ();
    try
    {
      for (final NamedSMTPSettings aSettings : .values ())
        if (aSettings.getName ().equals (sName))
          return aSettings;
      return null;
    }
    finally
    {
      .readLock ().unlock ();
    }
  }

  
Create a new settings object.

Parameters:
sName The name of the settings. May neither be null nor empty.
aSettings The main SMTP settings. May not be null.
Returns:
The created NamedSMTPSettings object and never null.
  public NamedSMTPSettings addSettings (@Nonnull @Nonempty final String sName, @Nonnull final ISMTPSettings aSettings)
  {
    final NamedSMTPSettings aNamedSettings = new NamedSMTPSettings (sNameaSettings);
    .writeLock ().lock ();
    try
    {
      _addItem (aNamedSettings);
      markAsChanged ();
    }
    finally
    {
      .writeLock ().unlock ();
    }
                                     aNamedSettings.getID (),
                                     aNamedSettings.getName (),
                                     aSettings.getHostName (),
                                     Integer.toString (aSettings.getPort ()),
                                     aSettings.getCharset (),
                                     Boolean.toString (aSettings.isSSLEnabled ()),
                                     Boolean.toString (aSettings.isSTARTTLSEnabled ()),
                                     Long.toString (aSettings.getConnectionTimeoutMilliSecs ()),
                                     Long.toString (aSettings.getTimeoutMilliSecs ()));
    return aNamedSettings;
  }

  
Update an existing settings object.

Parameters:
sID The ID of the settings object to be updated. May be null.
sName The new name of the settings. May neither be null nor empty.
aSettings The new SMTP settings. May not be null.
Returns:
com.helger.commons.state.EChange.CHANGED if something was changed.
  public EChange updateSettings (@Nullable final String sID,
                                 @Nonnull @Nonempty final String sName,
                                 @Nonnull final ISMTPSettings aSettings)
  {
    final NamedSMTPSettings aNamedSettings = getSettings (sID);
    if (aNamedSettings == null)
    {
      AuditUtils.onAuditModifyFailure (.sID"no-such-id");
      return .;
    }
    .writeLock ().lock ();
    try
    {
      EChange eChange = .;
      eChange = eChange.or (aNamedSettings.setName (sName));
      eChange = eChange.or (aNamedSettings.setSMTPSettings (aSettings));
      if (eChange.isUnchanged ())
        return .;
      markAsChanged ();
    }
    finally
    {
      .writeLock ().unlock ();
    }
                                     aNamedSettings.getID (),
                                     aNamedSettings.getName (),
                                     aSettings.getHostName (),
                                     Integer.toString (aSettings.getPort ()),
                                     aSettings.getCharset (),
                                     Boolean.toString (aSettings.isSSLEnabled ()),
                                     Boolean.toString (aSettings.isSTARTTLSEnabled ()),
                                     Long.toString (aSettings.getConnectionTimeoutMilliSecs ()),
                                     Long.toString (aSettings.getTimeoutMilliSecs ()));
    return .;
  }

  
Remove the SMTP settings with the specified ID.

Parameters:
sID The ID to be removed. May be null.
Returns:
com.helger.commons.state.EChange.CHANGED if a removal was performed.
  public EChange removeSettings (@Nullable final String sID)
  {
    EChange eChange;
    .writeLock ().lock ();
    try
    {
      eChange = EChange.valueOf (.remove (sID) != null);
      if (eChange.isChanged ())
        markAsChanged ();
    }
    finally
    {
      .writeLock ().unlock ();
    }
    if (eChange.isChanged ())
    else
      AuditUtils.onAuditDeleteFailure (.sID"no-such-id");
    return eChange;
  }

  
Remove all contained SMTP settings

Returns:
com.helger.commons.state.EChange.CHANGED if a removal was performed.
  public EChange removeAllSettings ()
  {
    // Get all available settings IDs
    Set <StringaAllIDs;
    .readLock ().lock ();
    try
    {
      aAllIDs = ContainerHelper.newSet (.keySet ());
    }
    finally
    {
      .readLock ().unlock ();
    }
    // Batch remove all settings
    EChange eChange = .;
    try
    {
      for (final String sID : aAllIDs)
        eChange = eChange.or (removeSettings (sID));
    }
    finally
    {
      endWithoutAutoSave ();
    }
    return eChange;
  }
  public String toString ()
  {
    return new ToStringGenerator (this).append ("map").toString ();
  }
New to GrepCode? Check out our FAQ X