001: /*
002: * Copyright 2007 The Kuali Foundation
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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: */
016: package edu.iu.uis.eden.messaging.bam;
017:
018: import java.lang.reflect.Method;
019: import java.sql.Timestamp;
020: import java.util.List;
021:
022: import javax.xml.namespace.QName;
023:
024: import org.apache.log4j.Logger;
025: import org.kuali.rice.definition.ObjectDefinition;
026:
027: import edu.iu.uis.eden.messaging.ServiceInfo;
028: import edu.iu.uis.eden.messaging.bam.dao.BAMConfigDAO;
029: import edu.iu.uis.eden.messaging.bam.dao.BAMDAO;
030:
031: public class BAMServiceImpl implements BAMService {
032:
033: private static final Logger LOG = Logger
034: .getLogger(BAMServiceImpl.class);
035:
036: private BAMDAO dao;
037:
038: private BAMConfigDAO configDao;
039:
040: public BAMTargetEntry recordClientInvocation(
041: ServiceInfo serviceDefinition, Object target,
042: Method method, Object[] params) {
043: if (isEnabled()) {
044: try {
045: LOG.debug("A call was received... for service: "
046: + serviceDefinition.getQname().toString()
047: + " method: " + method.getName());
048: BAMTargetEntry bamTargetEntry = getBAMTargetEntry(
049: Boolean.FALSE, serviceDefinition, target,
050: method, params);
051: this .dao.save(bamTargetEntry);
052: return bamTargetEntry;
053: } catch (Throwable t) {
054: LOG.error("BAM Failed to record client invocation", t);
055: return null;
056: }
057: }
058: return null;
059: }
060:
061: public BAMTargetEntry recordServerInvocation(Object target,
062: ServiceInfo entry, Method method, Object[] params) {
063: if (isEnabled()) {
064: try {
065: LOG.debug("A call was received... for service: "
066: + target.getClass().getName() + " method: "
067: + method.getName());
068: BAMTargetEntry bamTargetEntry = getBAMTargetEntry(
069: Boolean.TRUE, entry, target, method, params);
070: this .dao.save(bamTargetEntry);
071: } catch (Throwable t) {
072: LOG.error("BAM Failed to record server invocation", t);
073: }
074: }
075: return null;
076: }
077:
078: public BAMTargetEntry recordClientInvocationError(
079: Throwable throwable, BAMTargetEntry bamTargetEntry) {
080: if (bamTargetEntry != null) {
081: try {
082: setThrowableOnBAMTargetEntry(throwable, bamTargetEntry);
083: this .dao.save(bamTargetEntry);
084: return bamTargetEntry;
085: } catch (Exception e) {
086: LOG.error(
087: "BAM Failed to record client invocation error",
088: e);
089: }
090: }
091: return null;
092: }
093:
094: public BAMTargetEntry recordServerInvocationError(
095: Throwable throwable, BAMTargetEntry bamTargetEntry) {
096: if (bamTargetEntry != null) {
097: try {
098: setThrowableOnBAMTargetEntry(throwable, bamTargetEntry);
099: this .dao.save(bamTargetEntry);
100: return bamTargetEntry;
101: } catch (Exception e) {
102: LOG
103: .error(
104: "BAM Failed to record service invocation error",
105: e);
106: }
107: }
108: return null;
109: }
110:
111: private void setThrowableOnBAMTargetEntry(Throwable throwable,
112: BAMTargetEntry bamTargetEntry) {
113: if (throwable != null) {
114: bamTargetEntry.setExceptionMessage(throwable.getMessage());
115: bamTargetEntry.setExceptionToString(makeStringfit(throwable
116: .toString()));
117: }
118: }
119:
120: private BAMTargetEntry getBAMTargetEntry(Boolean serverInd,
121: ServiceInfo entry, Object target, Method method,
122: Object[] params) {
123: BAMTargetEntry bamEntry = new BAMTargetEntry();
124: bamEntry.setServerInvocation(serverInd);
125: bamEntry.setServiceName(entry.getQname().toString());
126: bamEntry.setServiceURL(entry.getEndpointUrl());
127: bamEntry.setTargetToString(makeStringfit(target.toString()));
128: bamEntry.setMethodName(method.getName());
129: bamEntry.setThreadName(Thread.currentThread().getName());
130: bamEntry.setCallDate(new Timestamp(System.currentTimeMillis()));
131: setBamParams(params, bamEntry);
132: return bamEntry;
133: }
134:
135: private void setBamParams(Object[] params, BAMTargetEntry bamEntry) {
136: if (params == null) {
137: return;
138: }
139: for (int i = 0; i < params.length; i++) {
140: BAMParam bamParam = new BAMParam();
141: bamParam.setBamTargetEntry(bamEntry);
142: bamParam.setParam(params[i].toString());
143: bamEntry.addBamParam(bamParam);
144: }
145: }
146:
147: private String makeStringfit(String string) {
148: if (string.length() > 1999) {
149: return string.substring(0, 1999);
150: }
151: return string;
152: }
153:
154: public boolean isEnabled() {
155: return getConfigDao().getEnabledState();
156: }
157:
158: public BAMDAO getDao() {
159: return this .dao;
160: }
161:
162: public void setDao(BAMDAO dao) {
163: this .dao = dao;
164: }
165:
166: public BAMConfigDAO getConfigDao() {
167: return this .configDao;
168: }
169:
170: public void setConfigDao(BAMConfigDAO configDao) {
171: this .configDao = configDao;
172: }
173:
174: public List<BAMTargetEntry> getCallsForService(QName serviceName) {
175: return getDao().getCallsForService(serviceName);
176: }
177:
178: public List<BAMTargetEntry> getCallsForRemotedClasses(
179: ObjectDefinition objDef) {
180: return getDao().getCallsForRemotedClasses(objDef);
181: }
182:
183: public void clearBAMTables() {
184: getDao().clearBAMTables();
185: }
186:
187: public List<BAMTargetEntry> getCallsForService(QName serviceName,
188: String methodName) {
189: return getDao().getCallsForService(serviceName, methodName);
190: }
191:
192: public List<BAMTargetEntry> getCallsForRemotedClasses(
193: ObjectDefinition objDef, String methodName) {
194: return getDao().getCallsForRemotedClasses(objDef, methodName);
195: }
196: }
|