1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 public class BAMServiceImpl implements BAMService {
37
38 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 if (isEnabled()) {
44 try {
45 LOG.debug("A call was received... for service: " + serviceConfiguration.getServiceName().toString() + " method: " + method.getName());
46 BAMTargetEntry bamTargetEntry = getBAMTargetEntry(Boolean.FALSE, serviceConfiguration, target, method, params);
47 this.dao.save(bamTargetEntry);
48 return bamTargetEntry;
49 } catch (Throwable t) {
50 LOG.error("BAM Failed to record client invocation", t);
51 return null;
52 }
53 }
54 return null;
55 }
56
57 public BAMTargetEntry recordServerInvocation(Object target, ServiceDefinition serviceDefinition, Method method, Object[] params) {
58 if (isEnabled()) {
59 try {
60 LOG.debug("A call was received... for service: " + target.getClass().getName() + " method: " + method.getName());
61 BAMTargetEntry bamTargetEntry = getBAMTargetEntry(Boolean.TRUE, serviceDefinition, target, method, params);
62 this.dao.save(bamTargetEntry);
63 return bamTargetEntry;
64 } catch (Throwable t) {
65 LOG.error("BAM Failed to record server invocation", t);
66 }
67 }
68 return null;
69 }
70
71 public BAMTargetEntry recordClientInvocationError(Throwable throwable, BAMTargetEntry bamTargetEntry) {
72 if (bamTargetEntry != null) {
73 try {
74 setThrowableOnBAMTargetEntry(throwable, bamTargetEntry);
75 this.dao.save(bamTargetEntry);
76 return bamTargetEntry;
77 } catch (Exception e) {
78 LOG.error("BAM Failed to record client invocation error", e);
79 }
80 }
81 return null;
82 }
83
84 public BAMTargetEntry recordServerInvocationError(Throwable throwable, BAMTargetEntry bamTargetEntry) {
85 if (bamTargetEntry != null) {
86 try {
87 setThrowableOnBAMTargetEntry(throwable, bamTargetEntry);
88 this.dao.save(bamTargetEntry);
89 return bamTargetEntry;
90 } catch (Exception e) {
91 LOG.error("BAM Failed to record service invocation error", e);
92 }
93 }
94 return null;
95 }
96
97 private void setThrowableOnBAMTargetEntry(Throwable throwable, BAMTargetEntry bamTargetEntry) {
98 if (throwable != null) {
99 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 }