01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.aspects.asynchronous.aspects.jboss;
23:
24: import org.jboss.aop.annotation.AnnotationElement;
25: import org.jboss.aop.joinpoint.MethodInvocation;
26: import org.jboss.aspects.asynchronous.aspects.AsynchronousFacade;
27: import org.jboss.aspects.asynchronous.aspects.AsynchronousFacadeImpl;
28: import org.jboss.aspects.asynchronous.aspects.Utils;
29: import org.jboss.aspects.asynchronous.concurrent.ThreadManagerFactory;
30:
31: /**
32: * @author <a href="mailto:chussenet@yahoo.com">{Claude Hussenet Independent Consultant}</a>.
33: * @version <tt>$Revision: 57186 $</tt>
34: */
35: public class AsynchronousAspect {
36: private AsynchronousInvokeTask asynchronousInvokeTask = new AsynchronousInvokeTask();
37:
38: public AsynchronousAspect() {
39: }
40:
41: public Object execute(MethodInvocation invocation) throws Throwable {
42: AsynchronousFacade asynchronousResult = null;
43: if (invocation.getTargetObject() != null)
44: asynchronousResult = (AsynchronousFacade) invocation
45: .getTargetObject();
46: else {
47: Object[] tObject = invocation.getArguments();
48: for (int i = 0; i < tObject.length; i++) {
49: Object object = tObject[i];
50: if (object instanceof AsynchronousFacade) {
51: asynchronousResult = (AsynchronousFacade) object;
52: break;
53: }
54: }
55: if (asynchronousResult == null)
56: asynchronousResult = new AsynchronousFacadeImpl();
57: }
58: Asynchronous asynchronous = (Asynchronous) AnnotationElement
59: .getAnyAnnotation(invocation.getMethod(),
60: Asynchronous.class);
61: if (asynchronous != null) {
62: if (asynchronousResult.getTimeout() == 0)
63: asynchronousResult.setTimeout(asynchronous.timeout());
64:
65: if ((asynchronousResult.getId() != null)
66: && (asynchronousResult.getId().equals("None"))
67: && (asynchronous.id() != null)
68: && (asynchronous.id().length() != 0))
69: asynchronousResult.setId(asynchronous.id());
70: }
71: asynchronousResult
72: .setAsynchronousTask(ThreadManagerFactory
73: .getThreadManager()
74: .process(
75: ThreadManagerFactory
76: .createNewThreadManagerRequest(
77: asynchronousResult
78: .getId(),
79: new InvokeTaskInputParameters(
80: (MethodInvocation) invocation
81: .copy()),
82: asynchronousInvokeTask,
83: asynchronousResult
84: .getTimeout())));
85: Class returnType = invocation.getActualMethod().getReturnType();
86: return Utils.returnInitObject(returnType);
87: }
88: }
|