You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2010/11/10 17:26:53 UTC

svn commit: r1033567 - in /james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver: HookResultJMXMonitor.java HookStats.java HookStatsMBean.java

Author: norman
Date: Wed Nov 10 16:26:53 2010
New Revision: 1033567

URL: http://svn.apache.org/viewvc?rev=1033567&view=rev
Log:
Add a new Hook which can be enabled via smtpserver.xml to get statistics about the configured hooks via JMX (JAMES-1057)

Added:
    james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/HookResultJMXMonitor.java
    james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/HookStats.java
    james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/HookStatsMBean.java

Added: james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/HookResultJMXMonitor.java
URL: http://svn.apache.org/viewvc/james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/HookResultJMXMonitor.java?rev=1033567&view=auto
==============================================================================
--- james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/HookResultJMXMonitor.java (added)
+++ james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/HookResultJMXMonitor.java Wed Nov 10 16:26:53 2010
@@ -0,0 +1,76 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.apache.james.smtpserver;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.annotation.PreDestroy;
+
+import org.apache.james.protocols.smtp.SMTPSession;
+import org.apache.james.protocols.smtp.hook.HookResult;
+import org.apache.james.protocols.smtp.hook.HookResultHook;
+
+/**
+ * {@link HookResultHook} implementation which will register a {@link HookStatsMBean} under JMX for every Hook it processed 
+ *
+ */
+public class HookResultJMXMonitor implements HookResultHook {
+
+    private Map<String, HookStats> hookStats = new HashMap<String, HookStats>();
+    
+    /*
+     * (non-Javadoc)
+     * @see org.apache.james.protocols.smtp.hook.HookResultHook#onHookResult(org.apache.james.protocols.smtp.SMTPSession, org.apache.james.protocols.smtp.hook.HookResult, java.lang.Object)
+     */
+    public HookResult onHookResult(SMTPSession session, HookResult result,
+            Object hook) {
+        String hookName = hook.getClass().getName();
+        try {
+            HookStats stats;
+            synchronized (hookStats) {
+                stats = hookStats.get(hookName);
+                if (stats == null) {
+                    stats = new HookStats(hookName);
+                    hookStats.put(hookName, stats);
+                }
+            }
+           
+            stats.increment(result.getResult());
+        } catch (Exception e) {
+            session.getLogger().error(
+                    "Unable to register HookStats for hook " + hookName, e);
+        }
+
+        return result;
+    }
+
+
+    @PreDestroy
+    public void dispose() {
+        synchronized (hookStats) {
+            Iterator<HookStats> stats = hookStats.values().iterator();
+            while(stats.hasNext()) {
+                stats.next().dispose();
+            }
+            hookStats.clear();
+        }
+    }
+}

Added: james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/HookStats.java
URL: http://svn.apache.org/viewvc/james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/HookStats.java?rev=1033567&view=auto
==============================================================================
--- james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/HookStats.java (added)
+++ james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/HookStats.java Wed Nov 10 16:26:53 2010
@@ -0,0 +1,134 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.apache.james.smtpserver;
+
+import java.lang.management.ManagementFactory;
+import java.util.concurrent.atomic.AtomicLong;
+
+import javax.management.InstanceAlreadyExistsException;
+import javax.management.MBeanRegistrationException;
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.NotCompliantMBeanException;
+import javax.management.ObjectName;
+import javax.management.StandardMBean;
+
+import org.apache.james.lifecycle.Disposable;
+import org.apache.james.protocols.smtp.hook.HookReturnCode;
+
+/**
+ * JMX Bean which keep track of statistics for a given Hook
+ * 
+ *
+ */
+public class HookStats extends StandardMBean implements HookStatsMBean, Disposable {
+    
+    private AtomicLong ok = new AtomicLong(0);
+    private AtomicLong declined = new AtomicLong(0);
+    private AtomicLong deny = new AtomicLong(0);
+    private AtomicLong denysoft = new AtomicLong(0);
+    private AtomicLong all = new AtomicLong(0);
+
+    private String name;
+    private MBeanServer mbeanserver;
+    private String hookname;
+
+    public HookStats(String hookname) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, MalformedObjectNameException, NullPointerException {
+        super(HookStatsMBean.class);
+        this.hookname = hookname;
+        name = "org.apache.james:type=server,name=smtpserver,handler=hook,hook=" + hookname;
+        mbeanserver = ManagementFactory.getPlatformMBeanServer();
+        ObjectName baseObjectName = new ObjectName(name);
+        mbeanserver.registerMBean(this, baseObjectName);
+    }
+    
+    public void increment(int code) {
+        switch (code) {
+        case HookReturnCode.OK:
+            ok.incrementAndGet();
+        case HookReturnCode.DECLINED:
+            declined.incrementAndGet();
+        case HookReturnCode.DENYSOFT:
+            denysoft.incrementAndGet();
+        case HookReturnCode.DENY:
+            deny.incrementAndGet();
+        }
+        all.incrementAndGet();
+    }
+
+    /*
+     * (non-Javadoc)
+     * @see org.apache.james.smtpserver.HookStatsMBean#getOk()
+     */
+    public long getOk() {
+        return ok.get();
+    }
+
+    /*
+     * (non-Javadoc)
+     * @see org.apache.james.smtpserver.HookStatsMBean#getDeclined()
+     */
+    public long getDeclined() {
+        return declined.get();
+    }
+
+    /*
+     * (non-Javadoc)
+     * @see org.apache.james.smtpserver.HookStatsMBean#getDeny()
+     */
+    public long getDeny() {
+        return deny.get();
+    }
+
+    /*
+     * (non-Javadoc)
+     * @see org.apache.james.smtpserver.HookStatsMBean#getDenysoft()
+     */
+    public long getDenysoft() {
+        return denysoft.get();
+    }
+
+    /*
+     * (non-Javadoc)
+     * @see org.apache.james.lifecycle.Disposable#dispose()
+     */
+    public void dispose() {
+        try {
+            mbeanserver.unregisterMBean(new ObjectName(name));
+        } catch (Exception e) {
+            // ignore here;
+        }
+    }
+
+    /*
+     * (non-Javadoc)
+     * @see org.apache.james.smtpserver.HookStatsMBean#getName()
+     */
+    public String getName() {
+        return hookname;
+    }
+
+    /*
+     * (non-Javadoc)
+     * @see org.apache.james.smtpserver.HookStatsMBean#getAll()
+     */
+    public long getAll() {
+        return all.get();
+    }
+}

Added: james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/HookStatsMBean.java
URL: http://svn.apache.org/viewvc/james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/HookStatsMBean.java?rev=1033567&view=auto
==============================================================================
--- james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/HookStatsMBean.java (added)
+++ james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/HookStatsMBean.java Wed Nov 10 16:26:53 2010
@@ -0,0 +1,70 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.apache.james.smtpserver;
+
+
+/**
+ * JMX Bean which shows Hook statistics
+ * 
+ *
+ */
+public interface HookStatsMBean {
+
+    /**
+     * Return the name of the Hook
+     * 
+     * @return name
+     */
+    public String getName();
+    
+    /**
+     * Return the count of how many OK the hook returned
+     * 
+     * @return ok
+     */
+    public long getOk();
+    
+    /**
+     * Return the count of how many DECLINED the hook returned
+     * 
+     * @return declined
+     */
+    public long getDeclined();
+    
+    /**
+     * Return the count of how many DENY the hook returned
+     * 
+     * @return deny
+     */
+    public long getDeny();
+    
+    /**
+     * Return the count of how many DENYSOFT the hook returned
+     * 
+     * @return denysoft
+     */
+    public long getDenysoft();
+    
+    /**
+     * Return the count of how many transaction this hook as processed
+     *  
+     * @return all
+     */
+    public long getAll();
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org