cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From e..@apache.org
Subject [4/6] git commit: [CXF-5843]:Add operation to enable/disable ResponseTimeCounter
Date Mon, 07 Jul 2014 06:44:09 GMT
[CXF-5843]:Add operation to enable/disable ResponseTimeCounter


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/828d7e51
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/828d7e51
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/828d7e51

Branch: refs/heads/master
Commit: 828d7e5172162481ca361f8461a0e20ee30b53b0
Parents: 2d2a45a
Author: Jim Ma <ema@apache.org>
Authored: Mon Jul 7 11:30:19 2014 +0800
Committer: Jim Ma <ema@apache.org>
Committed: Mon Jul 7 14:43:39 2014 +0800

----------------------------------------------------------------------
 .../AbstractMessageResponseTimeInterceptor.java | 148 ++++++++++++-------
 1 file changed, 94 insertions(+), 54 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/828d7e51/rt/management/src/main/java/org/apache/cxf/management/interceptor/AbstractMessageResponseTimeInterceptor.java
----------------------------------------------------------------------
diff --git a/rt/management/src/main/java/org/apache/cxf/management/interceptor/AbstractMessageResponseTimeInterceptor.java
b/rt/management/src/main/java/org/apache/cxf/management/interceptor/AbstractMessageResponseTimeInterceptor.java
index 20d1925..71dffb0 100644
--- a/rt/management/src/main/java/org/apache/cxf/management/interceptor/AbstractMessageResponseTimeInterceptor.java
+++ b/rt/management/src/main/java/org/apache/cxf/management/interceptor/AbstractMessageResponseTimeInterceptor.java
@@ -18,10 +18,10 @@
  */
 package org.apache.cxf.management.interceptor;
 
-
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import javax.management.MalformedObjectNameException;
 import javax.management.ObjectName;
 
 import org.apache.cxf.Bus;
@@ -32,6 +32,7 @@ import org.apache.cxf.management.ManagementConstants;
 import org.apache.cxf.management.counters.Counter;
 import org.apache.cxf.management.counters.CounterRepository;
 import org.apache.cxf.management.counters.MessageHandlingTimeRecorder;
+import org.apache.cxf.management.counters.ResponseTimeCounter;
 import org.apache.cxf.message.Exchange;
 import org.apache.cxf.message.FaultMode;
 import org.apache.cxf.message.Message;
@@ -43,29 +44,29 @@ public abstract class AbstractMessageResponseTimeInterceptor extends AbstractPha
     private static final Logger LOG = LogUtils.getL7dLogger(AbstractMessageResponseTimeInterceptor.class);
     private static final String QUESTION_MARK = "?";
     private static final String ESCAPED_QUESTION_MARK = "\\?";
-    
+
     AbstractMessageResponseTimeInterceptor(String phase) {
         super(phase);
     }
-    
+
     protected boolean isClient(Message msg) {
-        return msg == null ? false : Boolean.TRUE.equals(msg.get(Message.REQUESTOR_ROLE));
       
+        return msg == null ? false : Boolean.TRUE.equals(msg.get(Message.REQUESTOR_ROLE));
     }
-   
+
     protected void beginHandlingMessage(Exchange ex) {
         if (null == ex) {
             return;
         }
-        MessageHandlingTimeRecorder mhtr = ex.get(MessageHandlingTimeRecorder.class);   
    
+        MessageHandlingTimeRecorder mhtr = ex.get(MessageHandlingTimeRecorder.class);
         if (null != mhtr) {
             mhtr.beginHandling();
         } else {
             mhtr = new MessageHandlingTimeRecorder(ex);
             mhtr.beginHandling();
-        }        
+        }
     }
-    
-    protected void endHandlingMessage(Exchange ex) {                
+
+    protected void endHandlingMessage(Exchange ex) {
         if (null == ex) {
             return;
         }
@@ -74,82 +75,121 @@ public abstract class AbstractMessageResponseTimeInterceptor extends
AbstractPha
             mhtr.endHandling();
             mhtr.setFaultMode(ex.get(FaultMode.class));
             increaseCounter(ex, mhtr);
-                        
-        } // else can't get the MessageHandling Infor  
+
+        } // else can't get the MessageHandling Infor
     }
-    
+
     protected void setOneWayMessage(Exchange ex) {
-        MessageHandlingTimeRecorder mhtr = ex.get(MessageHandlingTimeRecorder.class);   
    
+        MessageHandlingTimeRecorder mhtr = ex.get(MessageHandlingTimeRecorder.class);
         if (null == mhtr) {
-            mhtr = new MessageHandlingTimeRecorder(ex);            
+            mhtr = new MessageHandlingTimeRecorder(ex);
         } else {
             mhtr.endHandling();
         }
         mhtr.setOneWay(true);
         increaseCounter(ex, mhtr);
-    }    
-    
+    }
+
     private void increaseCounter(Exchange ex, MessageHandlingTimeRecorder mhtr) {
         Bus bus = ex.get(Bus.class);
         if (null == bus) {
             LOG.log(Level.INFO, "CAN_NOT_GET_BUS_FROM_EXCHANGE");
             bus = BusFactory.getThreadDefaultBus();
         }
-        
-        Message message = ex.getOutMessage();
-        
         CounterRepository cr = bus.getExtension(CounterRepository.class);
-        
+
         if (null == cr) {
             LOG.log(Level.WARNING, "NO_COUNTER_REPOSITORY");
             return;
         } else {
-            Service service = ex.get(Service.class);            
+            ObjectName serviceCountername = this.getServiceCounterName(ex);
+            cr.increaseCounter(serviceCountername, mhtr);
+
+            ObjectName operationCounter = this.getOperationCounterName(ex, serviceCountername);
+            cr.increaseCounter(operationCounter, mhtr);
+        }
+    }
+    
+    protected ObjectName getServiceCounterName(Exchange ex) {
+        Bus bus = ex.get(Bus.class);
+        StringBuilder buffer = new StringBuilder();
+        if (ex.get("org.apache.cxf.management.service.counter.name") != null) {
+            buffer.append((String)ex.get("org.apache.cxf.management.service.counter.name"));
+        } else {
+            Service service = ex.get(Service.class);
             Endpoint endpoint = ex.get(Endpoint.class);
-            
-            String serviceName = "\"" + escapePatternChars(service.getName().toString())
+ "\"";            
+
+            String serviceName = "\"" + escapePatternChars(service.getName().toString())
+ "\"";
             String portName = "\"" + endpoint.getEndpointInfo().getName().getLocalPart()
+ "\"";
-            
-            OperationInfo opInfo = ex.get(OperationInfo.class);
-            String operationName = opInfo == null ? null : "\"" + opInfo.getName().getLocalPart()
+ "\"";
-            
-            if (operationName == null) {
-                Object nameProperty = ex.get("org.apache.cxf.resource.operation.name");
-                if (nameProperty != null) {
-                    operationName = "\"" + escapePatternChars(nameProperty.toString()) +
"\"";
-                }
-            }
-            
-            StringBuilder buffer = new StringBuilder();
+
             buffer.append(ManagementConstants.DEFAULT_DOMAIN_NAME + ":");
             buffer.append(ManagementConstants.BUS_ID_PROP + "=" + bus.getId() + ",");
+            Message message = ex.getOutMessage();
             if (isClient(message)) {
-                buffer.append(ManagementConstants.TYPE_PROP + "=" + Counter.PERFORMANCE_COUNTER
+ ".Client,");
+                buffer.append(ManagementConstants.TYPE_PROP + "=" + Counter.PERFORMANCE_COUNTER
+                              + ".Client,");
             } else {
-                buffer.append(ManagementConstants.TYPE_PROP + "=" + Counter.PERFORMANCE_COUNTER
+ ".Server,");
+                buffer.append(ManagementConstants.TYPE_PROP + "=" + Counter.PERFORMANCE_COUNTER
+                              + ".Server,");
             }
             buffer.append(ManagementConstants.SERVICE_NAME_PROP + "=" + serviceName + ",");
-           
+
             buffer.append(ManagementConstants.PORT_NAME_PROP + "=" + portName);
-            String serviceCounterName = buffer.toString();
-            
-            try {           
-                ObjectName serviceCounter = 
-                    new ObjectName(serviceCounterName);                
-                cr.increaseCounter(serviceCounter, mhtr);
-                if (operationName != null) {
-                    buffer.append("," + ManagementConstants.OPERATION_NAME_PROP + "=" + operationName);
-                    String operationCounterName = buffer.toString();
-                    ObjectName operationCounter = new ObjectName(operationCounterName);
-                    cr.increaseCounter(operationCounter, mhtr);                
-                }
-            } catch (Exception exception) {
-                LOG.log(Level.WARNING, "CREATE_COUNTER_OBJECTNAME_FAILED", exception);
-            }
         }
+        ObjectName serviceCounterName = null;
+        try {
+            serviceCounterName = new ObjectName(buffer.toString());
+        } catch (MalformedObjectNameException e) {
+            LOG.log(Level.WARNING, "CREATE_COUNTER_OBJECTNAME_FAILED", e);
+        }
+        return serviceCounterName;
+        
     }
+  
+    protected boolean isServiceCounterEnabled(Exchange ex) {
+        Bus bus = ex.get(Bus.class);
+        CounterRepository counterRepo = bus.getExtension(CounterRepository.class);
+        if (counterRepo == null) {
+            return false;
+        }
+        ObjectName serviceCounterName = getServiceCounterName(ex);
+        ResponseTimeCounter serviceCounter = (ResponseTimeCounter)counterRepo.getCounter(serviceCounterName);
+        //If serviceCounter is null, we need to wait ResponseTimeOutInterceptor to create
it , hence set to true
+        if (serviceCounter == null || serviceCounter.isEnabled()) {
+            return true;
+        }
+        return false;   
+    }
+    
+    protected ObjectName getOperationCounterName(Exchange ex, ObjectName sericeCounterName)
{
+        OperationInfo opInfo = ex.get(OperationInfo.class);
+        String operationName = opInfo == null ? null : "\"" + opInfo.getName().getLocalPart()
+ "\"";
+
+        if (operationName == null) {
+            Object nameProperty = ex.get("org.apache.cxf.resource.operation.name");
+            if (nameProperty != null) {
+                operationName = "\"" + escapePatternChars(nameProperty.toString()) + "\"";
+            }
+        }
+        StringBuffer buffer = new StringBuffer(sericeCounterName.toString());
+        if (operationName != null) {
+            buffer.append("," + ManagementConstants.OPERATION_NAME_PROP + "=" + operationName);
+        }
+        String operationCounterName = buffer.toString();
+        ObjectName operationCounter = null;
+        try {
+            operationCounter = new ObjectName(operationCounterName);
+            
+        } catch (MalformedObjectNameException e) {
+            LOG.log(Level.WARNING, "CREATE_COUNTER_OBJECTNAME_FAILED", e);
+        }
+        return operationCounter;
         
-    protected String escapePatternChars(String value) {  
+    }
+    
+    
+
+    protected String escapePatternChars(String value) {
         // This can be replaced if really needed with pattern-based matching
         if (value.lastIndexOf(QUESTION_MARK) != -1) {
             value = value.replace(QUESTION_MARK, ESCAPED_QUESTION_MARK);


Mime
View raw message