KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > logdemo > AsyncServerWithLogImpl


1 // ====================================================================
2
//
3
// ECM: The Extensible Container Model
4
// Copyright (C) 2004 THALES
5
// Contact: openccm-ecm@objectweb.org
6
//
7
// This library is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU Lesser General Public
9
// License as published by the Free Software Foundation; either
10
// version 2.1 of the License, or any later version.
11
//
12
// This library 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 library; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
// USA
21
//
22
// Initial developer(s): Mathieu Vadet.
23
// Initial Funding: IST COACH European project (IST-2001-34445)
24
// http://www.ist-coach.org
25
//
26
// ====================================================================
27

28
29
30 package org.objectweb.ccm.logdemo;
31
32 /**
33  ** This is the ECM-aware implementation of the OMG IDL3 <tt>AsyncServer</tt> component type
34  ** which uses the simple log service.
35  **/

36 public class AsyncServerWithLogImpl
37 extends AsyncServer_MainSegBase
38 implements java.io.Serializable JavaDoc,
39            org.coach.ECM.ECMExecutor,
40            org.objectweb.ccm.logservice.LogCallback
41 {
42     // server
43
private String JavaDoc _name;
44     private javax.swing.JFrame JavaDoc _frame;
45     private javax.swing.JTextArea JavaDoc _text;
46
47     public
48     AsyncServerWithLogImpl()
49     {
50         // server
51
_name = null;
52         _frame = null;
53         _text = null;
54     }
55
56     //
57
// overriden operations
58
//
59

60     final public void
61     configuration_complete()
62     throws org.omg.Components.InvalidConfiguration
63     {
64         // create a GUI
65
// CENTER: text field to display client inputs
66
_text = new javax.swing.JTextArea JavaDoc(40, 20);
67         _text.setEditable(false);
68
69         // MAIN panel
70
javax.swing.JPanel JavaDoc panel = new javax.swing.JPanel JavaDoc(new java.awt.BorderLayout JavaDoc());
71         panel.add(new javax.swing.JScrollPane JavaDoc(_text), java.awt.BorderLayout.CENTER);
72         _frame = new javax.swing.JFrame JavaDoc(_name + " GUI");
73         _frame.setSize(400, 300);
74         _frame.getContentPane().add(panel);
75         _frame.pack();
76         _frame.show();
77     }
78
79     //
80
// IDL:omg.org/Components/SessionComponent:1.0
81
//
82

83     final public void
84     ccm_activate()
85     throws org.omg.Components.CCMException
86     {
87         // NOTE: TODO: should be done externally using the CSD info
88
// register value factory
89
getCCM2Context().register_valuefactory(AsyncMessageHelper.id(), new AsyncMessageFactoryImpl());
90
91         // show the frame if hidden
92
if ((_frame!=null) && (!_frame.isShowing())){
93             _frame.show();
94         }
95     }
96
97     final public void
98     ccm_passivate()
99     throws org.omg.Components.CCMException
100     {
101         // hide the frame and clear the text field
102
_frame.hide();
103         _text.setText(null);
104     }
105
106     final public void
107     ccm_remove()
108     throws org.omg.Components.CCMException
109     {
110         // Release the associated frame.
111
_frame.dispose();
112         _frame = null;
113     }
114
115     //
116
// IDL:coach.org/ECM/ECMExecutor:1.0
117
//
118

119     final public org.omg.CORBA.Object JavaDoc
120     get_service_callback(String JavaDoc sid, String JavaDoc cid)
121     throws org.coach.ECM.UnknownService,
122            org.coach.ECM.UnknownServiceCallback
123     {
124         if (!sid.equals(org.objectweb.ccm.logservice.LOG_SERVICE_ID.value)) {
125             throw new org.coach.ECM.UnknownService();
126         }
127
128         if (!cid.equals(org.objectweb.ccm.logservice.LOG_SERVICE_CALLBACK_ID.value)) {
129             throw new org.coach.ECM.UnknownServiceCallback();
130         }
131
132         return this;
133     }
134
135     //
136
// IDL:objectweb.org/ccm/logservice/LogCallback:1.0
137
//
138

139     final public String JavaDoc
140     get_identity()
141     {
142         return "Server:"+name();
143     }
144
145     //
146
// IDL:org.omg/Components/EventConsumerBase:1.0
147
//
148

149     final public void
150     push_event(org.omg.Components.EventBase evt)
151     throws org.omg.Components.BadEventType
152     {
153         if (evt instanceof AsyncMessage) {
154             push_AsyncMessage((AsyncMessage)evt);
155             return ;
156         }
157
158         throw new org.omg.Components.BadEventType();
159     }
160
161     //
162
// IDL:objectweb.org/ccm/logdemo/MessageConsumer:1.0
163
//
164

165     final public void
166     push_AsyncMessage(AsyncMessage amsg)
167     {
168         // add content to the text field
169
_text.append("["+amsg.msg.header+"] "+amsg.msg.content + "\n");
170
171         // use the log service internal interface to send a log event.
172
try {
173             // cast the context to ExtCCMContext
174
org.coach.ECM.ECMContext ctx = getECMContext();
175             org.objectweb.ccm.logservice.LogInternal internal = null;
176
177             // may raise UnknownService or UnknownServiceInternal exceptions if the service is not installed
178
// or if the particular internal was not registered by the service
179
Object JavaDoc obj = ctx.get_service_internal(org.objectweb.ccm.logservice.LOG_SERVICE_ID.value,
180                                                   org.objectweb.ccm.logservice.LOG_SERVICE_INTERNAL_ID.value);
181
182             // should not fail
183
internal = (org.objectweb.ccm.logservice.LogInternal)obj;
184
185             // build a log event with INFO_LEVEL
186
org.objectweb.corba.logservice.LogEvent evt = new org.objectweb.corba.logservice.LogEvent();
187             evt.level = org.objectweb.corba.logservice.DEBUG_LEVEL.value;
188             org.objectweb.corba.logservice.MessageContent content = new org.objectweb.corba.logservice.MessageContent();
189             java.util.Calendar JavaDoc cal = java.util.Calendar.getInstance();
190             content.timestamp = java.lang.Long.toString(cal.getTimeInMillis());
191             content.interception_point = "";
192             content.operation_name = "push_AsyncMessage";
193             content.sender_id = "";
194             content.receiver_id = get_identity();
195             content.data = "["+amsg.msg.header+"] "+amsg.msg.content;
196             evt.message = content;
197             // send
198
internal.log_event(evt);
199         } catch (Exception JavaDoc ex) {
200             ex.printStackTrace();
201         }
202     }
203
204     //
205
// IDL:objectweb.org/ccm/logdemo/AsyncServer:1.0
206
//
207

208     final public void
209     name(String JavaDoc n)
210     {
211         _name = n;
212
213         if (_frame!=null) {
214             _frame.setTitle(_name+" GUI");
215         }
216     }
217
218     final public String JavaDoc
219     name()
220     {
221         return _name;
222     }
223 }
224
Popular Tags