root/trunk/components/server/src/ome/services/util/ServiceHandler.java
| Revision 2677, 8.6 kB (checked in by jmoore, 5 months ago) | |
|---|---|
|
|
| Line | |
|---|---|
| 1 | /* |
| 2 | * ome.services.util.ServiceHandler |
| 3 | * |
| 4 | * Copyright 2006 University of Dundee. All rights reserved. |
| 5 | * Use is subject to license terms supplied in LICENSE.txt |
| 6 | */ |
| 7 | |
| 8 | package ome.services.util; |
| 9 | |
| 10 | import java.lang.annotation.Annotation; |
| 11 | import java.lang.reflect.Method; |
| 12 | import java.util.Arrays; |
| 13 | import java.util.Set; |
| 14 | |
| 15 | import ome.annotations.AnnotationUtils; |
| 16 | import ome.annotations.ApiConstraintChecker; |
| 17 | import ome.annotations.Hidden; |
| 18 | import ome.conditions.ApiUsageException; |
| 19 | import ome.conditions.InternalException; |
| 20 | import ome.conditions.OptimisticLockException; |
| 21 | import ome.conditions.RootException; |
| 22 | import ome.conditions.ValidationException; |
| 23 | import ome.security.basic.CurrentDetails; |
| 24 | import ome.services.messages.RegisterServiceCleanupMessage; |
| 25 | |
| 26 | import org.aopalliance.intercept.MethodInterceptor; |
| 27 | import org.aopalliance.intercept.MethodInvocation; |
| 28 | import org.apache.commons.logging.Log; |
| 29 | import org.apache.commons.logging.LogFactory; |
| 30 | import org.hibernate.PropertyValueException; |
| 31 | import org.springframework.context.ApplicationEvent; |
| 32 | import org.springframework.context.ApplicationListener; |
| 33 | import org.springframework.dao.DataIntegrityViolationException; |
| 34 | import org.springframework.dao.InvalidDataAccessResourceUsageException; |
| 35 | import org.springframework.dao.OptimisticLockingFailureException; |
| 36 | import org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException; |
| 37 | import org.springframework.orm.hibernate3.HibernateSystemException; |
| 38 | |
| 39 | /** |
| 40 | * |
| 41 | */ |
| 42 | public class ServiceHandler implements MethodInterceptor, ApplicationListener { |
| 43 | |
| 44 | private static Log log = LogFactory.getLog(ServiceHandler.class); |
| 45 | |
| 46 | private final CurrentDetails cd; |
| 47 | |
| 48 | public void onApplicationEvent(ApplicationEvent arg0) { |
| 49 | if (arg0 instanceof RegisterServiceCleanupMessage) { |
| 50 | RegisterServiceCleanupMessage cleanup = (RegisterServiceCleanupMessage) arg0; |
| 51 | cd.addCleanup(cleanup); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | public ServiceHandler(CurrentDetails cd) { |
| 56 | this.cd = cd; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation) |
| 61 | */ |
| 62 | public Object invoke(MethodInvocation arg0) throws Throwable { |
| 63 | if (arg0 == null) { |
| 64 | throw new InternalException( |
| 65 | "Cannot act on null MethodInvocation. Stopping."); |
| 66 | } |
| 67 | |
| 68 | Class implClass = arg0.getThis().getClass(); |
| 69 | Method mthd = arg0.getMethod(); |
| 70 | Object[] args = arg0.getArguments(); |
| 71 | |
| 72 | ApiConstraintChecker.errorOnViolation(implClass, mthd, args); |
| 73 | |
| 74 | if (log.isInfoEnabled()) { |
| 75 | // Method and arguments |
| 76 | log.info(" Meth:\t" + arg0.getMethod().getDeclaringClass() + "." |
| 77 | + arg0.getMethod().getName()); |
| 78 | log.info(" Args:\t" + getArgumentsString(arg0)); |
| 79 | } |
| 80 | |
| 81 | // Results and/or Exceptions |
| 82 | Object o; |
| 83 | String finalOutput = ""; |
| 84 | |
| 85 | try { |
| 86 | o = arg0.proceed(); |
| 87 | finalOutput = " Rslt:\t" + o; |
| 88 | return o; |
| 89 | } catch (Throwable t) { |
| 90 | finalOutput = " Excp:\t" + t; |
| 91 | throw getAndLogException(t); |
| 92 | } finally { |
| 93 | if (log.isInfoEnabled()) { |
| 94 | log.info(finalOutput); |
| 95 | } |
| 96 | cleanup(); |
| 97 | } |
| 98 | |
| 99 | } |
| 100 | |
| 101 | protected void cleanup() { |
| 102 | Set<RegisterServiceCleanupMessage> cleanups = cd.emptyCleanups(); |
| 103 | for (RegisterServiceCleanupMessage registerServiceCleanupMessage : cleanups) { |
| 104 | try { |
| 105 | log.info("Cleanup: " + registerServiceCleanupMessage.resource); |
| 106 | registerServiceCleanupMessage.close(); |
| 107 | } catch (Exception e) { |
| 108 | log.warn("Error while cleaning up", e); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | protected Throwable getAndLogException(Throwable t) { |
| 114 | if (null == t) { |
| 115 | log.error("Exception thrown. (null)"); |
| 116 | return new InternalException("Exception thrown with null message"); |
| 117 | } else { |
| 118 | String msg = " Wrapped Exception: (" + t.getClass().getName() |
| 119 | + "):\n" + t.getMessage(); |
| 120 | |
| 121 | if (RootException.class.isAssignableFrom(t.getClass())) { |
| 122 | return t; |
| 123 | } else if (OptimisticLockingFailureException.class |
| 124 | .isAssignableFrom(t.getClass())) { |
| 125 | OptimisticLockException ole = new OptimisticLockException(t |
| 126 | .getMessage()); |
| 127 | ole.setStackTrace(t.getStackTrace()); |
| 128 | printException("OptimisticLockingFailureException thrown.", t); |
| 129 | return ole; |
| 130 | } |
| 131 | |
| 132 | else if (IllegalArgumentException.class.isAssignableFrom(t |
| 133 | .getClass())) { |
| 134 | ApiUsageException aue = new ApiUsageException(t.getMessage()); |
| 135 | aue.setStackTrace(t.getStackTrace()); |
| 136 | printException("IllegalArgumentException thrown.", t); |
| 137 | return aue; |
| 138 | } |
| 139 | |
| 140 | else if (InvalidDataAccessResourceUsageException.class |
| 141 | .isAssignableFrom(t.getClass())) { |
| 142 | ApiUsageException aue = new ApiUsageException(t.getMessage()); |
| 143 | aue.setStackTrace(t.getStackTrace()); |
| 144 | printException( |
| 145 | "InvalidDataAccessResourceUsageException thrown.", t); |
| 146 | return aue; |
| 147 | } |
| 148 | |
| 149 | else if (DataIntegrityViolationException.class.isAssignableFrom(t |
| 150 | .getClass())) { |
| 151 | ValidationException ve = new ValidationException(t.getMessage()); |
| 152 | ve.setStackTrace(t.getStackTrace()); |
| 153 | printException("DataIntegrityViolationException thrown.", t); |
| 154 | return ve; |
| 155 | } |
| 156 | |
| 157 | else if (HibernateObjectRetrievalFailureException.class |
| 158 | .isAssignableFrom(t.getClass())) { |
| 159 | ValidationException ve = new ValidationException(t.getMessage()); |
| 160 | ve.setStackTrace(t.getStackTrace()); |
| 161 | printException( |
| 162 | "HibernateObjectRetrievealFailureException thrown.", t); |
| 163 | return ve; |
| 164 | } |
| 165 | |
| 166 | else if (HibernateSystemException.class.isAssignableFrom(t |
| 167 | .getClass())) { |
| 168 | Throwable cause = t.getCause(); |
| 169 | if (cause == null || cause == t) { |
| 170 | return wrapUnknown(t, msg); |
| 171 | } else if (PropertyValueException.class.isAssignableFrom(cause |
| 172 | .getClass())) { |
| 173 | ValidationException ve = new ValidationException(cause |
| 174 | .getMessage()); |
| 175 | ve.setStackTrace(cause.getStackTrace()); |
| 176 | printException("PropertyValueException thrown.", cause); |
| 177 | return ve; |
| 178 | } else { |
| 179 | return wrapUnknown(t, msg); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | else { |
| 184 | return wrapUnknown(t, msg); |
| 185 | } |
| 186 | |
| 187 | } |
| 188 | |
| 189 | } |
| 190 | |
| 191 | private Throwable wrapUnknown(Throwable t, String msg) { |
| 192 | // Wrap all other exceptions in InternalException |
| 193 | InternalException re = new InternalException(msg); |
| 194 | re.setStackTrace(t.getStackTrace()); |
| 195 | printException("Unknown exception thrown.", t); |
| 196 | return re; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * produces a String from the arguments array. Argument parameters marked as |
| 201 | * {@link Hidden} will be replaced by "*******". |
| 202 | */ |
| 203 | private String getArgumentsString(MethodInvocation mi) { |
| 204 | String arguments; |
| 205 | Object[] args = mi.getArguments(); |
| 206 | |
| 207 | if (args == null || args.length < 1) { |
| 208 | return "()"; |
| 209 | } |
| 210 | |
| 211 | String[] prnt = new String[args.length]; |
| 212 | for (int i = 0; i < prnt.length; i++) { |
| 213 | prnt[i] = args[i] == null ? "null" : args[i].toString(); |
| 214 | } |
| 215 | |
| 216 | Object[] allAnnotations = AnnotationUtils.findParameterAnnotations(mi |
| 217 | .getThis().getClass(), mi.getMethod()); |
| 218 | |
| 219 | for (int j = 0; j < allAnnotations.length; j++) { |
| 220 | Annotation[][] anns = (Annotation[][]) allAnnotations[j]; |
| 221 | if (anns == null) { |
| 222 | continue; |
| 223 | } |
| 224 | |
| 225 | for (int i = 0; i < args.length; i++) { |
| 226 | Annotation[] annotations = anns[i]; |
| 227 | |
| 228 | for (Annotation annotation : annotations) { |
| 229 | if (Hidden.class.equals(annotation.annotationType())) { |
| 230 | prnt[i] = "********"; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | arguments = Arrays.asList(prnt).toString(); |
| 237 | return arguments; |
| 238 | } |
| 239 | |
| 240 | private void printException(String msg, Throwable ex) { |
| 241 | if (log.isWarnEnabled()) { |
| 242 | log.warn(msg + "\n", ex); |
| 243 | } |
| 244 | } |
| 245 | } |
Note: See TracBrowser
for help on using the browser.
