Coverage Report - org.kuali.rice.ksb.messaging.bam.service.impl.BAMServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
BAMServiceImpl
0%
0/78
0%
0/16
2.118
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.ksb.messaging.bam.service.impl;
 17  
 
 18  
 import java.lang.reflect.Method;
 19  
 import java.sql.Timestamp;
 20  
 import java.util.List;
 21  
 
 22  
 import javax.xml.namespace.QName;
 23  
 
 24  
 import org.apache.log4j.Logger;
 25  
 import org.kuali.rice.core.api.config.property.Config;
 26  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 27  
 import org.kuali.rice.core.api.reflect.ObjectDefinition;
 28  
 import org.kuali.rice.ksb.api.bus.ServiceConfiguration;
 29  
 import org.kuali.rice.ksb.api.bus.ServiceDefinition;
 30  
 import org.kuali.rice.ksb.messaging.bam.BAMParam;
 31  
 import org.kuali.rice.ksb.messaging.bam.BAMTargetEntry;
 32  
 import org.kuali.rice.ksb.messaging.bam.dao.BAMDAO;
 33  
 import org.kuali.rice.ksb.messaging.bam.service.BAMService;
 34  
 
 35  
 
 36  0
 public class BAMServiceImpl implements BAMService {
 37  
 
 38  0
         private static final Logger LOG = Logger.getLogger(BAMServiceImpl.class);
 39  
 
 40  
         private BAMDAO dao;
 41  
 
 42  
         public BAMTargetEntry recordClientInvocation(ServiceConfiguration serviceConfiguration, Object target, Method method, Object[] params) {
 43  0
                 if (isEnabled()) {
 44  
                         try {
 45  0
                                 LOG.debug("A call was received... for service: " + serviceConfiguration.getServiceName().toString() + " method: " + method.getName());
 46  0
                                 BAMTargetEntry bamTargetEntry = getBAMTargetEntry(Boolean.FALSE, serviceConfiguration, target, method, params);
 47  0
                                 this.dao.save(bamTargetEntry);
 48  0
                                 return bamTargetEntry;
 49  0
                         } catch (Throwable t) {
 50  0
                                 LOG.error("BAM Failed to record client invocation", t);
 51  0
                                 return null;
 52  
                         }
 53  
                 }
 54  0
                 return null;
 55  
         }
 56  
 
 57  
         public BAMTargetEntry recordServerInvocation(Object target, ServiceDefinition serviceDefinition, Method method, Object[] params) {
 58  0
                 if (isEnabled()) {
 59  
                         try {
 60  0
                                 LOG.debug("A call was received... for service: " + target.getClass().getName() + " method: " + method.getName());
 61  0
                                 BAMTargetEntry bamTargetEntry = getBAMTargetEntry(Boolean.TRUE, serviceDefinition, target, method, params);
 62  0
                                 this.dao.save(bamTargetEntry);
 63  0
                                 return bamTargetEntry;
 64  0
                         } catch (Throwable t) {
 65  0
                                 LOG.error("BAM Failed to record server invocation", t);
 66  
                         }
 67  
                 }
 68  0
                 return null;
 69  
         }
 70  
 
 71  
         public BAMTargetEntry recordClientInvocationError(Throwable throwable, BAMTargetEntry bamTargetEntry) {
 72  0
                 if (bamTargetEntry != null) {
 73  
                         try {
 74  0
                                 setThrowableOnBAMTargetEntry(throwable, bamTargetEntry);
 75  0
                                 this.dao.save(bamTargetEntry);
 76  0
                                 return bamTargetEntry;
 77  0
                         } catch (Exception e) {
 78  0
                                 LOG.error("BAM Failed to record client invocation error", e);
 79  
                         }
 80  
                 }
 81  0
                 return null;
 82  
         }
 83  
 
 84  
         public BAMTargetEntry recordServerInvocationError(Throwable throwable, BAMTargetEntry bamTargetEntry) {
 85  0
                 if (bamTargetEntry != null) {
 86  
                         try {
 87  0
                                 setThrowableOnBAMTargetEntry(throwable, bamTargetEntry);
 88  0
                                 this.dao.save(bamTargetEntry);
 89  0
                                 return bamTargetEntry;
 90  0
                         } catch (Exception e) {
 91  0
                                 LOG.error("BAM Failed to record service invocation error", e);
 92  
                         }
 93  
                 }
 94  0
                 return null;
 95  
         }
 96  
 
 97  
         private void setThrowableOnBAMTargetEntry(Throwable throwable, BAMTargetEntry bamTargetEntry) {
 98  0
                 if (throwable != null) {
 99  0
                         bamTargetEntry.setExceptionMessage(throwable.getMessage());
 100  0
                         bamTargetEntry.setExceptionToString(makeStringfit(throwable.toString()));
 101  
                 }
 102  0
         }
 103  
 
 104  
         private BAMTargetEntry getBAMTargetEntry(Boolean serverInd, ServiceConfiguration serviceConfiguration, Object target, Method method, Object[] params) {
 105  0
                 BAMTargetEntry bamEntry = new BAMTargetEntry();
 106  0
                 bamEntry.setServerInvocation(serverInd);
 107  0
                 bamEntry.setServiceName(serviceConfiguration.getServiceName().toString());
 108  0
                 bamEntry.setServiceURL(serviceConfiguration.getEndpointUrl().toExternalForm());
 109  0
                 bamEntry.setTargetToString(makeStringfit(target.toString()));
 110  0
                 bamEntry.setMethodName(method.getName());
 111  0
                 bamEntry.setThreadName(Thread.currentThread().getName());
 112  0
                 bamEntry.setCallDate(new Timestamp(System.currentTimeMillis()));
 113  0
                 setBamParams(params, bamEntry);
 114  0
                 return bamEntry;
 115  
         }
 116  
         
 117  
         private BAMTargetEntry getBAMTargetEntry(Boolean serverInd, ServiceDefinition serviceDefinition, Object target, Method method, Object[] params) {
 118  0
                 BAMTargetEntry bamEntry = new BAMTargetEntry();
 119  0
                 bamEntry.setServerInvocation(serverInd);
 120  0
                 bamEntry.setServiceName(serviceDefinition.getServiceName().toString());
 121  0
                 bamEntry.setServiceURL(serviceDefinition.getEndpointUrl().toExternalForm());
 122  0
                 bamEntry.setTargetToString(makeStringfit(target.toString()));
 123  0
                 bamEntry.setMethodName(method.getName());
 124  0
                 bamEntry.setThreadName(Thread.currentThread().getName());
 125  0
                 bamEntry.setCallDate(new Timestamp(System.currentTimeMillis()));
 126  0
                 setBamParams(params, bamEntry);
 127  0
                 return bamEntry;
 128  
         }
 129  
 
 130  
         private void setBamParams(Object[] params, BAMTargetEntry bamEntry) {
 131  0
                 if (params == null) {
 132  0
                         return;
 133  
                 }
 134  0
                 for (int i = 0; i < params.length; i++) {
 135  0
                         BAMParam bamParam = new BAMParam();
 136  0
                         bamParam.setBamTargetEntry(bamEntry);
 137  0
                         bamParam.setParam(params[i].toString());
 138  0
                         bamEntry.addBamParam(bamParam);
 139  
                 }
 140  0
         }
 141  
 
 142  
         private String makeStringfit(String string) {
 143  0
                 if (string.length() > 1999) {
 144  0
                         return string.substring(0, 1999);
 145  
                 }
 146  0
                 return string;
 147  
         }
 148  
 
 149  
         public boolean isEnabled() {
 150  0
                 return Boolean.valueOf(ConfigContext.getCurrentContextConfig().getProperty(Config.BAM_ENABLED));
 151  
         }
 152  
 
 153  
         public BAMDAO getDao() {
 154  0
                 return this.dao;
 155  
         }
 156  
 
 157  
         public void setDao(BAMDAO dao) {
 158  0
                 this.dao = dao;
 159  0
         }
 160  
 
 161  
         public List<BAMTargetEntry> getCallsForService(QName serviceName) {
 162  0
                 return getDao().getCallsForService(serviceName);
 163  
         }
 164  
 
 165  
         public List<BAMTargetEntry> getCallsForRemotedClasses(ObjectDefinition objDef) {
 166  0
                 return getDao().getCallsForRemotedClasses(objDef);
 167  
         }
 168  
 
 169  
         public void clearBAMTables() {
 170  0
                 getDao().clearBAMTables();
 171  0
         }
 172  
 
 173  
         public List<BAMTargetEntry> getCallsForService(QName serviceName, String methodName) {
 174  0
                 return getDao().getCallsForService(serviceName, methodName);
 175  
         }
 176  
 
 177  
         public List<BAMTargetEntry> getCallsForRemotedClasses(ObjectDefinition objDef, String methodName) {
 178  0
                 return getDao().getCallsForRemotedClasses(objDef, methodName);
 179  
         }
 180  
 }