KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > plugins > coordinators > QueuesCoordinator


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Amir Shevat.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 /*
47  * Created on 15/06/2004
48  *
49  * Manta LTD
50  */

51 package org.mr.plugins.coordinators;
52
53 import java.util.ArrayList JavaDoc;
54 import java.util.HashMap JavaDoc;
55
56 import org.mr.MantaAgent;
57 import org.apache.commons.logging.Log;
58 import org.apache.commons.logging.LogFactory;
59 import org.mr.core.configuration.ConfigurationElement;
60 import org.mr.kernel.Plugin;
61 import org.mr.kernel.services.MantaService;
62 import org.mr.kernel.services.queues.QueueMaster;
63
64 /**
65  * QueuesCoordinator activates the coordinators of the queues this agent is responsible of
66  * @author Amir Shevat
67  *
68  *
69  */

70 public class QueuesCoordinator implements Plugin {
71     
72     public static final String JavaDoc COORDINATED_QUEUES_KEY = "plug-ins.queues-coordinator.queue";
73     public static final String JavaDoc DYNAMICLY_COORDINATED = "DYNAMICALLY_COORDINATED";
74     public static MantaAgent agent;
75     public static String JavaDoc[] myCoordinatedQueuesName = null;
76     public static HashMap JavaDoc queueToCoordinatorMap = new HashMap JavaDoc();
77     public static Log log;
78     DynamicQueuesCoordinator dqc;
79     
80     
81     public QueuesCoordinator(){
82         agent= MantaAgent.getInstance();
83         ArrayList JavaDoc queueList= MantaAgent.getInstance()
84             .getSingletonRepository().getConfigManager()
85                 .getConfigurationElements(COORDINATED_QUEUES_KEY);
86         
87         // parse the queues name
88
if(queueList!= null && queueList.size()>0 ){
89             myCoordinatedQueuesName = new String JavaDoc[queueList.size()];
90             for (int i = 0; i < queueList.size(); i++) {
91                 myCoordinatedQueuesName[i]= ((ConfigurationElement)queueList.get(i)).getValue();
92             }
93         }
94         log=LogFactory.getLog("QueuesCoordinator");
95     }
96     /* (non-Javadoc)
97      * @see org.mr.kernel.Plugin#getName()
98      */

99     public String JavaDoc getName() {
100         
101         return "QueuesCoordinator";
102     }
103
104     /* (non-Javadoc)
105      * @see org.mr.kernel.Plugin#getVersion()
106      */

107     public float getVersion() {
108         
109         return 1;
110     }
111
112     /**
113      * gets the managed queues from the configuration file and advertises himself as the queue coordinator
114      * after that this agent will ack as the coordinator for these queues
115      */

116     public void start() {
117         if(QueuesCoordinator.myCoordinatedQueuesName == null ||QueuesCoordinator.myCoordinatedQueuesName.length ==0 ){
118             if(log.isDebugEnabled()) {
119                 log.debug("coordinated_queues is null or empty -no Queues to coordinated");
120             }
121             return;
122         }
123         // start advertising queue coodinators
124
for (int i = 0; i < QueuesCoordinator.myCoordinatedQueuesName.length; i++) {
125             String JavaDoc queueName = QueuesCoordinator.myCoordinatedQueuesName[i];
126             if(queueName ==null ||queueName.length()==0 ){
127                 // making sure the configuration is not of an empty string or null
128
if (log.isInfoEnabled()) {
129                     log.info("Ignoring queue with empty name, possible configuration error in queues-coordinator.");
130                 }
131                 continue;
132             }
133             queueName = queueName.trim();
134             myCoordinatedQueuesName[i] = queueName;
135             
136             try{
137                 if(queueName.equals(DYNAMICLY_COORDINATED)){
138                     if (log.isInfoEnabled()) {
139                         log.info("Starting DynamicQueuesCoordinator");
140                     }
141                     dqc = new DynamicQueuesCoordinator();
142                     dqc.start();
143                     continue;
144                 }
145                 if (log.isDebugEnabled()) {
146                     log.debug("Starting to coordinate queue "+queueName);
147                 }
148                 MantaService queue = agent.getService(queueName, MantaService.SERVICE_TYPE_QUEUE);
149                 QueueMaster coordinator = new QueueMaster(agent.getAgentName(),queueName );
150                 agent.advertiseService(coordinator);
151                 queueToCoordinatorMap.put(queue ,coordinator );
152             }catch(Exception JavaDoc e){
153                 if (log.isErrorEnabled()) {
154                     log.error("Error in starting queue coordination "+queueName,e);
155                 }
156             }
157         }//for
158
}
159
160     /* (non-Javadoc)
161      * @see org.mr.kernel.Plugin#stop()
162      */

163     public void stop() {
164         if(myCoordinatedQueuesName == null ||myCoordinatedQueuesName.length ==0 ){
165             // no need to do anything
166
return;
167         }
168 // start recall queue coodinators
169
for (int i = 0; i < myCoordinatedQueuesName.length; i++) {
170             String JavaDoc queueName = myCoordinatedQueuesName[i];
171             try{
172                 if (log.isDebugEnabled()) {
173                     log.debug("Recalling queue coordinator "+queueName);
174                 }
175                 MantaService queue = agent.getService(queueName, MantaService.SERVICE_TYPE_QUEUE);
176                 QueueMaster coordinator =(QueueMaster)queueToCoordinatorMap.get(queue);
177                 agent.recallService(coordinator);
178                 queueToCoordinatorMap.remove(queue);
179             }catch(Exception JavaDoc e){
180                 if (log.isErrorEnabled()) {
181                     log.error("Error in recalling queue coordinator "+queueName,e);
182                 }
183             }
184         }//for
185
}
186 }
187
Popular Tags