KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > impl > DefaultMessageFactory


1 package org.jacorb.notification.impl;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2004 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.jacorb.notification.AnyMessage;
26 import org.jacorb.notification.MessageFactory;
27 import org.jacorb.notification.StructuredEventMessage;
28 import org.jacorb.notification.TypedEventMessage;
29 import org.jacorb.notification.interfaces.Disposable;
30 import org.jacorb.notification.interfaces.Message;
31 import org.jacorb.notification.servant.AbstractProxyConsumerI;
32 import org.jacorb.notification.util.AbstractObjectPool;
33 import org.jacorb.notification.util.AbstractPoolablePool;
34 import org.omg.CORBA.Any JavaDoc;
35 import org.omg.CORBA.Bounds JavaDoc;
36 import org.omg.CORBA.NVList JavaDoc;
37 import org.omg.CORBA.NamedValue JavaDoc;
38 import org.omg.CosNotification.Property;
39 import org.omg.CosNotification.StructuredEvent;
40 import org.omg.CosNotification.StructuredEventHelper;
41
42 /**
43  * @author Alphonse Bendt
44  * @version $Id: DefaultMessageFactory.java,v 1.4 2005/05/01 21:00:40 alphonse.bendt Exp $
45  */

46
47 public class DefaultMessageFactory implements Disposable, MessageFactory
48 {
49     private final AbstractObjectPool typedEventMessagePool_ = new AbstractPoolablePool(
50             "TypedEventMessagePool")
51     {
52         public Object JavaDoc newInstance()
53         {
54             return new TypedEventMessage();
55         }
56     };
57
58     private final AbstractObjectPool anyMessagePool_ = new AbstractPoolablePool("AnyMessagePool")
59     {
60         public Object JavaDoc newInstance()
61         {
62             return new AnyMessage();
63         }
64     };
65
66     private final AbstractObjectPool structuredEventMessagePool_ = new AbstractPoolablePool(
67             "StructuredEventMessagePool")
68     {
69         public Object JavaDoc newInstance()
70         {
71             return new StructuredEventMessage();
72         }
73     };
74
75     public DefaultMessageFactory(Configuration conf)
76     {
77         anyMessagePool_.configure(conf);
78
79         structuredEventMessagePool_.configure(conf);
80
81         typedEventMessagePool_.configure(conf);
82     }
83
84     public void dispose()
85     {
86         structuredEventMessagePool_.dispose();
87
88         anyMessagePool_.dispose();
89
90         typedEventMessagePool_.dispose();
91     }
92
93     public String JavaDoc getControllerName()
94     {
95         return "org.jacorb.notification.jmx.MessageFactoryControl";
96     }
97
98     ////////////////////////////////////////
99

100     // Used by the Proxies
101

102     /**
103      * create a Message wrapping an unstructured event.
104      */

105     public Message newMessage(Any JavaDoc any, AbstractProxyConsumerI consumer)
106     {
107         if (StructuredEventHelper.type().equals(any.type()))
108         {
109             // received a StructuredEvent wrapped inside an Any
110
// see Spec. 2-11
111
return newMessage(StructuredEventHelper.extract(any), consumer);
112         }
113
114         AnyMessage _mesg = newAnyMessage(any);
115
116         _mesg.setFilterStage(consumer.getFirstStage());
117
118         return _mesg.getHandle();
119     }
120
121     /**
122      * create a Message wrapping a structured event.
123      */

124     public Message newMessage(StructuredEvent structuredEvent, AbstractProxyConsumerI consumer)
125     {
126         final String JavaDoc _typeName = structuredEvent.header.fixed_header.event_type.type_name;
127
128         if (AnyMessage.TYPE_NAME.equals(_typeName))
129         {
130             // received an Any wrapped inside a StructuredEvent
131
// see Spec. 2-11
132
return newMessage(structuredEvent.remainder_of_body, consumer);
133         }
134
135         StructuredEventMessage _mesg = (StructuredEventMessage) structuredEventMessagePool_
136                 .lendObject();
137
138         _mesg.setFilterStage(consumer.getFirstStage());
139
140         _mesg.setStructuredEvent(structuredEvent, consumer.isStartTimeSupported(), consumer
141                 .isTimeOutSupported());
142
143         return _mesg.getHandle();
144     }
145
146     /**
147      * create a Message wrapping a typed event.
148      */

149     public Message newMessage(String JavaDoc interfaceName, String JavaDoc operationName, NVList JavaDoc args,
150             AbstractProxyConsumerI consumer)
151     {
152         try
153         {
154             TypedEventMessage _mesg = (TypedEventMessage) typedEventMessagePool_.lendObject();
155
156             Property[] _props = new Property[args.count()];
157
158             for (int x = 0; x < _props.length; ++x)
159             {
160                 NamedValue JavaDoc _nv = args.item(x);
161
162                 _props[x] = new Property(_nv.name(), _nv.value());
163             }
164
165             _mesg.setTypedEvent(interfaceName, operationName, _props);
166
167             _mesg.setFilterStage(consumer.getFirstStage());
168             
169            return _mesg.getHandle();
170         } catch (Bounds JavaDoc e)
171         {
172             // this should never happen as NamedValue bounds are checked always.
173
throw new RuntimeException JavaDoc(e.getMessage());
174         }
175     }
176
177     ////////////////////////////////////////
178

179     // used by the Filters
180

181     /**
182      * create a message wrapping a typed event.
183      */

184     public Message newMessage(Property[] props)
185     {
186         TypedEventMessage _mesg = (TypedEventMessage) typedEventMessagePool_.lendObject();
187
188         _mesg.setTypedEvent(props);
189
190         return _mesg.getHandle();
191     }
192
193     /**
194      * create a Message wrapping a unstructured event.
195      */

196     public Message newMessage(Any JavaDoc any)
197     {
198         if (StructuredEventHelper.type().equals(any.type()))
199         {
200             return newMessage(StructuredEventHelper.extract(any));
201         }
202         
203         AnyMessage _mesg = newAnyMessage(any);
204
205         return _mesg.getHandle();
206     }
207
208     private AnyMessage newAnyMessage(Any JavaDoc any)
209     {
210         AnyMessage _mesg = (AnyMessage) anyMessagePool_.lendObject();
211
212         _mesg.setAny(any);
213         
214         return _mesg;
215     }
216
217     /**
218      * create a message wrapping a structured event.
219      */

220     public Message newMessage(StructuredEvent structuredEvent)
221     {
222         String JavaDoc _typeName = structuredEvent.header.fixed_header.event_type.type_name;
223
224         if (AnyMessage.TYPE_NAME.equals(_typeName))
225         {
226             return newMessage(structuredEvent.remainder_of_body);
227         }
228
229         StructuredEventMessage _mesg = (StructuredEventMessage) structuredEventMessagePool_
230                 .lendObject();
231
232         _mesg.setStructuredEvent(structuredEvent, false, false);
233
234         return _mesg.getHandle();
235     }
236 }
Popular Tags