KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > mom > dest > Destination


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2004 - 2007 ScalAgent Distributed Technologies
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): ScalAgent Distributed Technologies
21  * Contributor(s):
22  */

23 package org.objectweb.joram.mom.dest;
24
25 import java.util.Properties JavaDoc;
26
27 import org.objectweb.joram.mom.notifications.AbstractRequest;
28 import org.objectweb.joram.mom.notifications.ClientMessages;
29 import org.objectweb.joram.mom.notifications.ExceptionReply;
30 import org.objectweb.joram.mom.notifications.Monit_FreeAccess;
31 import org.objectweb.joram.mom.notifications.Monit_GetDMQSettings;
32 import org.objectweb.joram.mom.notifications.Monit_GetReaders;
33 import org.objectweb.joram.mom.notifications.Monit_GetStat;
34 import org.objectweb.joram.mom.notifications.Monit_GetWriters;
35 import org.objectweb.joram.mom.notifications.RequestGroupNot;
36 import org.objectweb.joram.mom.notifications.SetDMQRequest;
37 import org.objectweb.joram.mom.notifications.SetRightRequest;
38 import org.objectweb.joram.mom.notifications.SpecialAdminRequest;
39 import org.objectweb.joram.shared.JoramTracing;
40 import org.objectweb.joram.shared.excepts.MomException;
41 import org.objectweb.util.monolog.api.BasicLevel;
42 import org.objectweb.util.monolog.api.Logger;
43
44 import fr.dyade.aaa.agent.Agent;
45 import fr.dyade.aaa.agent.AgentId;
46 import fr.dyade.aaa.agent.Channel;
47 import fr.dyade.aaa.agent.DeleteNot;
48 import fr.dyade.aaa.agent.Notification;
49 import fr.dyade.aaa.agent.UnknownAgent;
50 import fr.dyade.aaa.agent.UnknownNotificationException;
51 import fr.dyade.aaa.util.Debug;
52 import fr.dyade.aaa.util.management.MXWrapper;
53
54 /**
55  * A <code>Destination</code> agent is an agent hosting a MOM destination,
56  * for example a <tt>Queue</tt> or a <tt>Topic</tt>.
57  * Its behaviour is provided by a <code>DestinationImpl</code> instance.
58  *
59  * @see DestinationItf
60  */

61 public abstract class Destination extends Agent implements AdminDestinationItf {
62
63   public static Logger logger = Debug.getLogger(Destination.class.getName());
64   
65   /**
66    * The reference of the <code>DestinationItf</code> instance providing this
67    * this agent with its <tt>Destination</tt> behaviour.
68    */

69   protected DestinationImpl destImpl;
70
71   /**
72    * Empty constructor for newInstance().
73    */

74   public Destination() {}
75
76   /**
77    * Constructor with parameters for fixing the destination and specifying its
78    * identifier.
79    * It is uniquely used by the AdminTopic agent.
80    */

81   protected Destination(String JavaDoc name, boolean fixed, int stamp) {
82     super(name, fixed, stamp);
83   }
84
85   /**
86    * Initializes the destination by creating the <tt>DestinationItf</tt>
87    * object.
88    *
89    * @param adminId Identifier of the destination administrator.
90    * @param prop The initial set of properties.
91    */

92   public final void init(AgentId adminId, Properties JavaDoc properties) {
93     destImpl = createsImpl(adminId, properties);
94     destImpl.setAgent(this);
95   }
96
97   /**
98    * Creates the specific implementation.
99    *
100    * @param adminId Identifier of the topic administrator.
101    * @param prop The initial set of properties.
102    */

103   public abstract DestinationImpl createsImpl(AgentId adminId, Properties JavaDoc prop);
104
105   /**
106    * Gives this agent an opportunity to initialize after having been deployed,
107    * and each time it is loaded into memory.
108    *
109    * @param firstTime true when first called by the factory
110    *
111    * @exception Exception
112    * unspecialized exception
113    */

114   protected void agentInitialize(boolean firstTime) throws Exception JavaDoc {
115     super.agentInitialize(firstTime);
116     try {
117       MXWrapper.registerMBean(destImpl, "Joram", getMBeanName());
118     } catch (Exception JavaDoc exc) {
119       JoramTracing.dbgDestination.log(BasicLevel.ERROR,
120                                       this + " jmx failed", exc);
121     }
122   }
123
124   /** Finalizes the agent before it is garbaged. */
125   public void agentFinalize(boolean lastTime) {
126     try {
127       MXWrapper.unregisterMBean("Joram", getMBeanName());
128     } catch (Exception JavaDoc exc) {
129       if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
130         JoramTracing.dbgProxy.log(BasicLevel.DEBUG, "", exc);
131     }
132     super.agentFinalize(lastTime);
133   }
134
135   private String JavaDoc getMBeanName() {
136     return new StringBuffer JavaDoc()
137       .append("type=Destination")
138       .append(",name=").append((name==nullName)?getId().toString():name)
139       .toString();
140   }
141
142   /**
143    * Distributes the received notifications to the appropriate reactions.
144    * @throws Exception
145    */

146   public void react(AgentId from, Notification not) throws Exception JavaDoc {
147     if (logger.isLoggable(BasicLevel.DEBUG))
148       logger.log(BasicLevel.DEBUG,
149                  "Destination.react(" + from + ',' + not + ')');
150
151     // set agent no save (this is the default).
152
setNoSave();
153     
154     try {
155       if (not instanceof SetRightRequest)
156         destImpl.setRightRequest(from, (SetRightRequest) not);
157       else if (not instanceof SetDMQRequest)
158         destImpl.setDMQRequest(from, (SetDMQRequest) not);
159       else if (not instanceof Monit_GetReaders)
160         destImpl.monitGetReaders(from, (Monit_GetReaders) not);
161       else if (not instanceof Monit_GetWriters)
162         destImpl.monitGetWriters(from, (Monit_GetWriters) not);
163       else if (not instanceof Monit_FreeAccess)
164         destImpl.monitFreeAccess(from, (Monit_FreeAccess) not);
165       else if (not instanceof Monit_GetDMQSettings)
166         destImpl.monitGetDMQSettings(from, (Monit_GetDMQSettings) not);
167       else if (not instanceof Monit_GetStat)
168         destImpl.monitGetStat(from, (Monit_GetStat) not);
169       else if (not instanceof SpecialAdminRequest)
170         destImpl.specialAdminRequest(from, (SpecialAdminRequest) not);
171       else if (not instanceof ClientMessages)
172         destImpl.clientMessages(from, (ClientMessages) not);
173       else if (not instanceof UnknownAgent)
174         destImpl.unknownAgent(from, (UnknownAgent) not);
175       else if (not instanceof DeleteNot)
176         destImpl.deleteNot(from, (DeleteNot) not);
177       else if (not instanceof RequestGroupNot)
178         destImpl.requestGroupNot(from, (RequestGroupNot)not);
179       else if (not instanceof DeleteNot && destImpl.canBeDeleted())
180         // A DeleteNot notification is finally processed at the
181
// Agent level when its processing went successful in
182
// the DestinationItf instance.
183
super.react(from, not);
184       else
185         throw new UnknownNotificationException(not.getClass().getName());
186     } catch (MomException exc) {
187       // MOM Exceptions are sent to the requester.
188
if (logger.isLoggable(BasicLevel.WARN))
189         logger.log(BasicLevel.WARN, this + ".react()", exc);
190
191       AbstractRequest req = (AbstractRequest) not;
192       Channel.sendTo(from, new ExceptionReply(req, exc));
193     } catch (UnknownNotificationException exc) {
194       super.react(from, not);
195     }
196   }
197   
198   
199   protected void setNoSave() {
200     if (JoramTracing.dbgDestination.isLoggable(BasicLevel.DEBUG))
201       JoramTracing.dbgDestination.log(BasicLevel.DEBUG,
202                                       this + ": setNoSave().");
203     super.setNoSave();
204   }
205
206   protected void setSave() {
207     if (JoramTracing.dbgDestination.isLoggable(BasicLevel.DEBUG))
208       JoramTracing.dbgDestination.log(BasicLevel.DEBUG,
209                                     this + ": setSave().");
210     super.setSave();
211   }
212 }
213
Popular Tags