View Javadoc

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