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