/*
 * Copyright 2015 Adaptris Ltd.
 * 
 * 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.adaptris.core.services;

import javax.validation.constraints.NotBlank;

import com.thoughtworks.xstream.annotations.XStreamImplicit;
import org.slf4j.MDC;
import com.adaptris.annotation.AdapterComponent;
import com.adaptris.annotation.ComponentProfile;
import com.adaptris.annotation.DisplayOrder;
import com.adaptris.annotation.InputFieldHint;
import com.adaptris.core.AdaptrisMessage;
import com.adaptris.core.CoreException;
import com.adaptris.core.ServiceException;
import com.adaptris.core.ServiceImp;
import com.adaptris.core.util.ExceptionHelper;
import com.thoughtworks.xstream.annotations.XStreamAlias;

import java.util.ArrayList;
import java.util.List;

/**
 * Remove a mapped diagnostic context via {@link MDC#remove(String)}.
 * <p>
 * If you have a large number of workflows then it may be useful to use a mapped diagnostic context to provide additional
 * information into your logfile.
 * </p>
 * 
 * @config remove-logging-context-service
 * 
 */
@XStreamAlias("remove-logging-context-service")
@AdapterComponent
@ComponentProfile(summary = "Remove a mapped diagnostic context for logging; useful for filtering", tag = "service,logging,debug")
@DisplayOrder(order =
{
    "key"
})
public class RemoveLoggingContext extends ServiceImp {

  @InputFieldHint(expression = true)
  @XStreamImplicit(itemFieldName = "key")
  private List<String> keys = new ArrayList<>();

  public RemoveLoggingContext() {
    super();
  }

  public RemoveLoggingContext(String key) {
    this();
    getKeys().add(key);
  }

  @Override
  public void doService(AdaptrisMessage msg) throws ServiceException {
    try {
      for(String key : getKeys()) {
        MDC.remove(msg.resolve(key));
      }
    }
    catch (IllegalArgumentException | IllegalStateException e) {
      throw ExceptionHelper.wrapServiceException(e);
    }
  }

  @Override
  protected void initService() throws CoreException {
  }

  @Override
  protected void closeService() {
  }

  @Override
  public void prepare() throws CoreException {
  }

  public List<String> getKeys() {
    return keys;
  }

  /**
   * Set the keys for the mapped diagnostic context.
   * 
   * @param keys the key to set
   */
  public void setKey(List<String> keys) {
    this.keys = keys;
  }

}

@@ -645,102 +645,70 @@
 java
-x.validation.constraints.NotBlank;%0A%0Aimport com.thoughtworks.xstream.annotations.XStreamImplici
+.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.Lis
 t;%0Ai
@@ -1189,57 +1189,66 @@
 as;%0A
-%0A
 import 
-java.util.ArrayList;%0Aimport java.util.Lis
+com.thoughtworks.xstream.annotations.XStreamImplici
 t;%0A%0A
@@ -1775,16 +1775,17 @@
     %22key
+s
 %22%0A%7D)%0Apub
@@ -2082,26 +2082,52 @@
     
-g
+s
 etKeys(
-).add
+new ArrayList%3C%3E(Arrays.asList
 (key)
+))
 ;%0A  
@@ -2784,16 +2784,16 @@
 t%0A   */%0A
-
   public
@@ -2804,16 +2804,17 @@
 d setKey
+s
 (List%3CSt

Fix error getter/setter naming caused by INTERLOK-3080