001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.ksb.messaging.bam.service.impl; 017 018import java.lang.reflect.Method; 019import java.sql.Timestamp; 020import java.util.List; 021 022import javax.xml.namespace.QName; 023 024import org.apache.log4j.Logger; 025import org.kuali.rice.core.api.config.property.Config; 026import org.kuali.rice.core.api.config.property.ConfigContext; 027import org.kuali.rice.core.api.reflect.ObjectDefinition; 028import org.kuali.rice.ksb.api.bus.ServiceConfiguration; 029import org.kuali.rice.ksb.api.bus.ServiceDefinition; 030import org.kuali.rice.ksb.messaging.bam.BAMParam; 031import org.kuali.rice.ksb.messaging.bam.BAMTargetEntry; 032import org.kuali.rice.ksb.messaging.bam.dao.BAMDAO; 033import org.kuali.rice.ksb.messaging.bam.service.BAMService; 034 035 036public class BAMServiceImpl implements BAMService { 037 038 private static final Logger LOG = Logger.getLogger(BAMServiceImpl.class); 039 040 private BAMDAO dao; 041 042 public BAMTargetEntry recordClientInvocation(ServiceConfiguration serviceConfiguration, Object target, Method method, Object[] params) { 043 if (isEnabled()) { 044 try { 045 LOG.debug("A call was received... for service: " + serviceConfiguration.getServiceName().toString() + " method: " + method.getName()); 046 BAMTargetEntry bamTargetEntry = getBAMTargetEntry(Boolean.FALSE, serviceConfiguration, target, method, params); 047 this.dao.save(bamTargetEntry); 048 return bamTargetEntry; 049 } catch (Throwable t) { 050 LOG.error("BAM Failed to record client invocation", t); 051 return null; 052 } 053 } 054 return null; 055 } 056 057 public BAMTargetEntry recordServerInvocation(Object target, ServiceDefinition serviceDefinition, Method method, Object[] params) { 058 if (isEnabled()) { 059 try { 060 LOG.debug("A call was received... for service: " + target.getClass().getName() + " method: " + method.getName()); 061 BAMTargetEntry bamTargetEntry = getBAMTargetEntry(Boolean.TRUE, serviceDefinition, target, method, params); 062 this.dao.save(bamTargetEntry); 063 return bamTargetEntry; 064 } catch (Throwable t) { 065 LOG.error("BAM Failed to record server invocation", t); 066 } 067 } 068 return null; 069 } 070 071 public BAMTargetEntry recordClientInvocationError(Throwable throwable, BAMTargetEntry bamTargetEntry) { 072 if (bamTargetEntry != null) { 073 try { 074 setThrowableOnBAMTargetEntry(throwable, bamTargetEntry); 075 this.dao.save(bamTargetEntry); 076 return bamTargetEntry; 077 } catch (Exception e) { 078 LOG.error("BAM Failed to record client invocation error", e); 079 } 080 } 081 return null; 082 } 083 084 public BAMTargetEntry recordServerInvocationError(Throwable throwable, BAMTargetEntry bamTargetEntry) { 085 if (bamTargetEntry != null) { 086 try { 087 setThrowableOnBAMTargetEntry(throwable, bamTargetEntry); 088 this.dao.save(bamTargetEntry); 089 return bamTargetEntry; 090 } catch (Exception e) { 091 LOG.error("BAM Failed to record service invocation error", e); 092 } 093 } 094 return null; 095 } 096 097 private void setThrowableOnBAMTargetEntry(Throwable throwable, BAMTargetEntry bamTargetEntry) { 098 if (throwable != null) { 099 bamTargetEntry.setExceptionMessage(throwable.getMessage()); 100 bamTargetEntry.setExceptionToString(makeStringfit(throwable.toString())); 101 } 102 } 103 104 private BAMTargetEntry getBAMTargetEntry(Boolean serverInd, ServiceConfiguration serviceConfiguration, Object target, Method method, Object[] params) { 105 BAMTargetEntry bamEntry = new BAMTargetEntry(); 106 bamEntry.setServerInvocation(serverInd); 107 bamEntry.setServiceName(serviceConfiguration.getServiceName().toString()); 108 bamEntry.setServiceURL(serviceConfiguration.getEndpointUrl().toExternalForm()); 109 bamEntry.setTargetToString(makeStringfit(target.toString())); 110 bamEntry.setMethodName(method.getName()); 111 bamEntry.setThreadName(Thread.currentThread().getName()); 112 bamEntry.setCallDate(new Timestamp(System.currentTimeMillis())); 113 setBamParams(params, bamEntry); 114 return bamEntry; 115 } 116 117 private BAMTargetEntry getBAMTargetEntry(Boolean serverInd, ServiceDefinition serviceDefinition, Object target, Method method, Object[] params) { 118 BAMTargetEntry bamEntry = new BAMTargetEntry(); 119 bamEntry.setServerInvocation(serverInd); 120 bamEntry.setServiceName(serviceDefinition.getServiceName().toString()); 121 bamEntry.setServiceURL(serviceDefinition.getEndpointUrl().toExternalForm()); 122 bamEntry.setTargetToString(makeStringfit(target.toString())); 123 bamEntry.setMethodName(method.getName()); 124 bamEntry.setThreadName(Thread.currentThread().getName()); 125 bamEntry.setCallDate(new Timestamp(System.currentTimeMillis())); 126 setBamParams(params, bamEntry); 127 return bamEntry; 128 } 129 130 private void setBamParams(Object[] params, BAMTargetEntry bamEntry) { 131 if (params == null) { 132 return; 133 } 134 for (int i = 0; i < params.length; i++) { 135 BAMParam bamParam = new BAMParam(); 136 bamParam.setBamTargetEntry(bamEntry); 137 bamParam.setParam(params[i].toString()); 138 bamEntry.addBamParam(bamParam); 139 } 140 } 141 142 private String makeStringfit(String string) { 143 if (string.length() > 1999) { 144 return string.substring(0, 1999); 145 } 146 return string; 147 } 148 149 public boolean isEnabled() { 150 return Boolean.valueOf(ConfigContext.getCurrentContextConfig().getProperty(Config.BAM_ENABLED)); 151 } 152 153 public BAMDAO getDao() { 154 return this.dao; 155 } 156 157 public void setDao(BAMDAO dao) { 158 this.dao = dao; 159 } 160 161 public List<BAMTargetEntry> getCallsForService(QName serviceName) { 162 return getDao().getCallsForService(serviceName); 163 } 164 165 public List<BAMTargetEntry> getCallsForRemotedClasses(ObjectDefinition objDef) { 166 return getDao().getCallsForRemotedClasses(objDef); 167 } 168 169 public void clearBAMTables() { 170 getDao().clearBAMTables(); 171 } 172 173 public List<BAMTargetEntry> getCallsForService(QName serviceName, String methodName) { 174 return getDao().getCallsForService(serviceName, methodName); 175 } 176 177 public List<BAMTargetEntry> getCallsForRemotedClasses(ObjectDefinition objDef, String methodName) { 178 return getDao().getCallsForRemotedClasses(objDef, methodName); 179 } 180}