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