KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > component > request > ComponentRequestQueueImpl


1 /*
2  * ################################################################
3  *
4  * ProActive: The Java(TM) library for Parallel, Distributed,
5  * Concurrent computing with Security and Mobility
6  *
7  * Copyright (C) 1997-2004 INRIA/University of Nice-Sophia Antipolis
8  * Contact: proactive-support@inria.fr
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  *
25  * Initial developer(s): The ProActive Team
26  * http://www.inria.fr/oasis/ProActive/contacts.html
27  * Contributor(s):
28  *
29  * ################################################################
30  */

31 package org.objectweb.proactive.core.component.request;
32
33 import org.apache.log4j.Logger;
34 import org.objectweb.fractal.api.control.LifeCycleController;
35 import org.objectweb.proactive.core.UniqueID;
36 import org.objectweb.proactive.core.body.request.BlockingRequestQueueImpl;
37 import org.objectweb.proactive.core.body.request.Request;
38 import org.objectweb.proactive.core.body.request.RequestFilter;
39
40
41 /**
42  * Extension of the standard ProActive request queue.
43  * It enables the control of the life cycle of components.
44  * The algorithm is the following :
45  *
46  * loop
47  * if componentLifeCycle.isStarted()
48  * get next request
49  * // all requests are served
50  * else if componentLifeCycle.isStopped()
51  * get next component controller request
52  * // only component request are served
53  * ;
54  *
55  * if gotten request is a component life cycle request
56  * if startFc --> set started = true
57  * if stopFc --> set started = false
58  *
59  *
60  * @author Matthieu Morel
61  *
62  */

63 public class ComponentRequestQueueImpl extends BlockingRequestQueueImpl implements ComponentRequestQueue {
64     protected static Logger logger = Logger.getLogger(ComponentRequestQueueImpl.class.getName());
65     private boolean started = false;
66     private RequestFilter requestFilterOnComponentControllerClasses;
67
68     /**
69      * Constructor for ComponentRequestQueueImpl.
70      * @param ownerID
71      */

72     public ComponentRequestQueueImpl(UniqueID ownerID) {
73         super(ownerID);
74         requestFilterOnComponentControllerClasses = new RequestFilterOnComponentControllerClasses();
75     }
76
77     public synchronized void start() {
78         started = true;
79     }
80
81     public synchronized void stop() {
82         started = false;
83     }
84
85     public synchronized boolean isStarted() {
86         return started;
87     }
88
89     public synchronized Request blockingRemoveOldest() {
90         Request req;
91
92         if (isStarted()) {
93             req = super.blockingRemoveOldest();
94         } else {
95 // if (logger.isDebugEnabled()) {
96
// logger.debug("ComponentBody is currently stopped");
97
// }
98
// // if the component is stopped, we serve only the component controller requests
99
req = super.blockingRemoveOldest(requestFilterOnComponentControllerClasses);
100         }
101
102         // if we get a life cycle controller action, we should directly process it here
103
if (req instanceof ComponentRequest) {
104             if (req.getMethodCall().getReifiedMethod().getDeclaringClass().equals(LifeCycleController.class)) {
105                 if (req.getMethodName().equals("startFc")) {
106                     start();
107                 } else {
108                     if (req.getMethodName().equals("stopFc")) {
109                         stop();
110                     }
111                 }
112             }
113         }
114         return req;
115     }
116
117     ///////////////////////////////////////////////////////////////////////////////////////////
118
// private filtering class
119
private class RequestFilterOnComponentControllerClasses implements RequestFilter, java.io.Serializable JavaDoc {
120         public RequestFilterOnComponentControllerClasses() {
121         }
122
123         public boolean acceptRequest(Request request) {
124             if (request instanceof ComponentRequest) {
125                 // request is accepted only if originating from a component controller class
126
return ((ComponentRequest) request).isControllerRequest();
127             } else {
128                 // standard requests cannot be component controller requests
129
return false;
130             }
131         }
132     }
133 }
Popular Tags