/*
 * Copyright 2013 - 2013 Cole Wen
 *
 * 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 org.maodian.flyingcat.xmpp.state;

import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Semaphore;

import org.maodian.flyingcat.xmpp.entity.JabberID;


/**
 * @author Cole Wen
 *
 */
public class DefaultXmppContextManager implements XmppContextManager {
  private final ConcurrentMap<String, ConcurrentMap<JabberID, XmppContext>> pool = new ConcurrentHashMap<>();
  private final Semaphore lock = new Semaphore(1);
  
  /* (non-Javadoc)
   * @see org.maodian.flyingcat.xmpp.state.XmppContextManager#getXmppContext(org.maodian.flyingcat.xmpp.entity.JabberID)
   */
  @Override
  public XmppContext getXmppContext(JabberID jid) {
    return pool.get(jid.getUid()).get(jid);
  }

  /* (non-Javadoc)
   * @see org.maodian.flyingcat.xmpp.state.XmppContextManager#getXmppContexts(java.lang.String)
   */
  @Override
  public Collection<XmppContext> getXmppContexts(String uid) {
    return pool.get(uid).values();
  }

  /* (non-Javadoc)
   * @see org.maodian.flyingcat.xmpp.state.XmppContextListener#onPreDestroy(org.maodian.flyingcat.xmpp.state.XmppContext)
   */
  @Override
  public void onPreDestroy(XmppContext ctx) {
    // TODO Auto-generated method stub
    
  }

  /* (non-Javadoc)
   * @see org.maodian.flyingcat.xmpp.state.XmppContextListener#onPostDestroy(org.maodian.flyingcat.xmpp.state.XmppContext)
   */
  @Override
  public void onPostDestroy(XmppContext ctx) {
    JabberID jid = ctx.getJabberID();
    try {
      lock.acquire();
      ConcurrentMap<JabberID, XmppContext> m = pool.get(jid.getUid());
      m.remove(jid);
      if (m.size() == 0) {
        pool.remove(jid.getUid());
      }
    } catch (InterruptedException e) {
      throw new RuntimeException();
    } finally {
      lock.release();
    }
  }

  /* (non-Javadoc)
   * @see org.maodian.flyingcat.xmpp.state.XmppContextListener#onPreLogin(org.maodian.flyingcat.xmpp.state.XmppContext)
   */
  @Override
  public void onPreLogin(XmppContext ctx) {
    
  }

  /* (non-Javadoc)
   * @see org.maodian.flyingcat.xmpp.state.XmppContextListener#onPostLogin(org.maodian.flyingcat.xmpp.state.XmppContext)
   */
  @Override
  public void onPostLogin(XmppContext ctx) {
    JabberID jid = ctx.getJabberID();
    try {
      lock.acquire();
      ConcurrentMap<JabberID, XmppContext> m = pool.get(jid.getUid());
      if (m == null) {
        m = new ConcurrentHashMap<>();
        pool.put(jid.getUid(), m);
      }
      m.put(jid, ctx);
    } catch (InterruptedException e) {
      throw new RuntimeException();
    } finally {
      lock.release();
    }
  }
  
  
}

@@ -631,24 +631,63 @@
 mpp.state;%0A%0A
+import java.lang.invoke.MethodHandles;%0A
 import java.
@@ -703,16 +703,16 @@
 ection;%0A
-
 import j
@@ -884,16 +884,73 @@
 bberID;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 %0A%0A/**%0A *
@@ -1045,16 +1045,115 @@
 nager %7B%0A
+  private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());%0A
   privat
@@ -2294,32 +2294,90 @@
 .getJabberID();%0A
+    log.debug(%22Post destroy of XmppContext for %7B%7D%22, jid);%0A
     try %7B%0A      
@@ -2507,24 +2507,101 @@
 e() == 0) %7B%0A
+        log.debug(%22No active XmppContext for uid %7B%7D anymore%22, jid.getUid());%0A
         pool
@@ -3203,24 +3203,80 @@
 JabberID();%0A
+    log.debug(%22Post login of XmppContext for %7B%7D%22, jid);%0A
     try %7B%0A  
@@ -3385,24 +3385,99 @@
  == null) %7B%0A
+        log.debug(%22No active XmppContext found for uid %7B%7D%22, jid.getUid());%0A
         m = 
@@ -3542,24 +3542,24 @@
 m);%0A      %7D%0A
-
       m.put(
@@ -3569,16 +3569,71 @@
 , ctx);%0A
+      log.debug(%22Complete post login of XmppContext%22);%0A
     %7D ca

add more log