KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > filetransfer > cif > SystemManagerImpl


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2002 USTL - LIFL - GOAL
5 Contact: openccm-team@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): Areski Flissi, Frederic Briclet.
23 Contributor(s): Christophe Contreras.
24
25 ====================================================================*/

26 package org.objectweb.ccm.filetransfer.cif;
27
28 import org.objectweb.ccm.filetransfer.*;
29
30 import org
31     .objectweb
32     .ccm
33     .filetransfer
34     .SystemManagerSessionComposition
35     .ComponentImpl;
36
37 /**
38  * @author <a HREF="mailto:Areski.Flissi@lifl.fr">Areski Flissi</A>
39  */

40 public class SystemManagerImpl
41     extends ComponentImpl
42     implements java.awt.event.ActionListener JavaDoc, java.awt.event.ItemListener JavaDoc {
43
44     protected TV tv;
45
46     protected org.omg.Components.Cookie ck[] =
47         new org.omg.Components.Cookie[50];
48     protected String JavaDoc named[] = new String JavaDoc[50];
49     protected int rank = 0;
50     protected String JavaDoc selected_comp;
51
52     /** To refer to the GUI frame. */
53     private java.awt.Frame JavaDoc frame_;
54     /** To refer to the GUI user input field. */
55     private java.awt.Label JavaDoc label_;
56     private java.awt.TextField JavaDoc text_;
57     /** To refer to the GUI user output area. */
58     private java.awt.TextArea JavaDoc textArea_;
59
60     private java.awt.Button JavaDoc button_tv;
61     private java.awt.Button JavaDoc button_tv_rem;
62     private java.awt.Choice JavaDoc choice_;
63     private java.awt.CheckboxGroup JavaDoc cbg;
64     private java.awt.Checkbox JavaDoc pc;
65     private java.awt.Checkbox JavaDoc pda;
66
67     int balancy_flag = 1;
68
69     public static final java.awt.Color JavaDoc white_ = java.awt.Color.white;
70     public static final java.awt.Color JavaDoc dark_ = java.awt.Color.darkGray;
71
72     // ==================================================================
73
//
74
// Constructor.
75
//
76
// ==================================================================
77

78     /** The default constructor. */
79     public SystemManagerImpl() {
80     }
81
82     // ==================================================================
83
//
84
// Methods for OMG IDL Components::EnterpriseComponent
85
//
86
// ==================================================================
87

88     /**
89      * Complete the component configuration.
90      *
91      * @exception org.omg.Components.InvalidConfiguration
92      * Thrown if the configuration is invalid.
93      */

94     public void configuration_complete()
95         throws org.omg.Components.InvalidConfiguration {
96
97         //Instantiating the GUI.
98

99         java.awt.GridBagLayout JavaDoc gridbag = new java.awt.GridBagLayout JavaDoc();
100         java.awt.GridBagConstraints JavaDoc c = new java.awt.GridBagConstraints JavaDoc();
101
102         // Creates a AWT Frame.
103
frame_ = new java.awt.Frame JavaDoc("The System Manager GUI");
104
105         frame_.setSize(400, 300);
106
107         frame_.setLayout(gridbag);
108
109         c.fill = java.awt.GridBagConstraints.BOTH;
110         c.weightx = 1.0;
111         c.gridwidth = java.awt.GridBagConstraints.REMAINDER;
112
113         // Creates a text area for displaying inputs.
114
textArea_ = new java.awt.TextArea JavaDoc(15, 60);
115         textArea_.setEditable(false);
116         textArea_.setBackground(dark_);
117         textArea_.setForeground(white_);
118         gridbag.setConstraints(textArea_, c);
119
120         // Component's name label
121
c.gridwidth = 1;
122         c.fill = java.awt.GridBagConstraints.BOTH;
123         c.weighty = 0.0;
124         c.weightx = 0.0;
125         label_ = new java.awt.Label JavaDoc("Name:");
126         gridbag.setConstraints(label_, c);
127
128         // Creates a text field for user's inputs.
129
c.weightx = 1.0;
130         c.gridwidth = java.awt.GridBagConstraints.REMAINDER;
131         text_ = new java.awt.TextField JavaDoc("", 1);
132         gridbag.setConstraints(text_, c);
133         
134         // To choose the destination node
135
c.gridwidth = java.awt.GridBagConstraints.RELATIVE;
136         c.weighty = 0.0;
137         c.weightx = 0.0;
138         cbg = new java.awt.CheckboxGroup JavaDoc();
139         pc = new java.awt.Checkbox JavaDoc("on PC",cbg,true);
140         gridbag.setConstraints(pc, c);
141         
142         c.gridwidth = java.awt.GridBagConstraints.REMAINDER;
143         c.weighty = 0.0;
144         c.weightx = 0.0;
145         pda = new java.awt.Checkbox JavaDoc("on PDA",cbg,false);
146         gridbag.setConstraints(pda, c);
147
148         c.gridwidth = java.awt.GridBagConstraints.REMAINDER;
149         c.weighty = 0.0;
150         c.weightx = 1.0;
151         // Creates buttons.
152
button_tv = new java.awt.Button JavaDoc("Create a TV component");
153         button_tv.addActionListener(this);
154         gridbag.setConstraints(button_tv, c);
155
156         c.gridwidth = java.awt.GridBagConstraints.RELATIVE;
157         c.weighty = 0.0;
158         c.weightx = 1.0;
159         choice_ = new java.awt.Choice JavaDoc();
160         choice_.addItemListener(this);
161         choice_.disable();
162         gridbag.setConstraints(choice_, c);
163
164         c.gridwidth = java.awt.GridBagConstraints.REMAINDER;
165         c.weighty = 0.0;
166         c.weightx = 1.0;
167         button_tv_rem = new java.awt.Button JavaDoc("Remove a TV component");
168         button_tv_rem.addActionListener(this);
169         button_tv_rem.disable();
170         gridbag.setConstraints(button_tv_rem, c);
171
172         frame_.add(textArea_);
173         frame_.add(label_);
174         frame_.add(text_);
175         frame_.add(pc);
176         frame_.add(pda);
177         frame_.add(button_tv);
178         frame_.add(choice_);
179         frame_.add(button_tv_rem);
180
181         
182         frame_.pack();
183         frame_.show();
184
185     }
186
187     // ==================================================================
188
//
189
// Methods for the OMG IDL org.omg.Components.SessionComponent
190
//
191
// ==================================================================
192

193     /**
194      * Container callback to signal that the component is removed.
195      *
196      * @throw org.omg.Components.CCMException For any problems.
197      */

198     public void ccm_remove() throws org.omg.Components.CCMException {
199         // Release the associated frame.
200

201     }
202
203     // ==================================================================
204
//
205
// Methods for OMG IDL testgroup::CCM_SystemManager
206
//
207
// ==================================================================
208

209     /* (non-Javadoc)
210      * @see org.objectweb.ccm.testgroup.ManageServiceOperations#create_component()
211      */

212     public void create_component(String JavaDoc component_name) {
213
214         // Getting the component's home ref
215
// TVHome tvhome_ =
216
// this.get_context().get_connections_tv_homes()[0].objref;
217

218         
219
220         try {
221
222             // Getting the HomeFinder
223
//Fred modification
224
/* org.omg.CosNaming.NameComponent[] nc =
225                new org.omg.CosNaming.NameComponent[1];
226
227                nc[0] = new org.omg.CosNaming.NameComponent("HomeFinder", "");
228                org.omg.CORBA.Object objc =
229                org
230                .objectweb
231                .openccm
232                .corba
233                .TheNameService
234                .getNamingContext()
235                .getNamingContext()
236                .resolve(nc);
237                
238                org.objectweb.openccm.Deployment.components.HFinder.HFinderManager hf =
239                org.objectweb.openccm.Deployment.components.HFinder.HFinderManagerHelper.narrow(objc);
240                org.objectweb.openccm.Deployment.components.HFinder.HomeFinder hfinder =
241                hf.provide_the_home_finder();
242             */

243             org.omg.CosNaming.NamingContextExt JavaDoc context=
244                 org.omg.CosNaming.NamingContextExtHelper
245                 .narrow(org.objectweb.openccm.corba.TheNameService
246                         .getNamingContext()
247                         .getNamingContext());
248             
249             org.omg.Components.HomeFinder hfinder=
250                 org.omg.Components
251                 .HomeFinderHelper
252                 .narrow(context.resolve(context.to_name("OpenCCM/Service/HomeFinder")));
253                
254             // Find the specific home by its name
255
org.omg.CORBA.Object JavaDoc ccmhome_obj =
256                 hfinder.find_home_by_name(this.compute_home());
257
258             TVHome tvh = TVHomeHelper.narrow(ccmhome_obj);
259
260             // Create a new component instance
261
tv = tvh.create();
262
263             // Configure the component
264
tv.the_name(component_name);
265
266             // Interconnect components
267

268             org.omg.CosNaming.NameComponent JavaDoc[] ncomp =
269                 new org.omg.CosNaming.NameComponent JavaDoc[1];
270
271             // ncomp[0] =
272
// new org.omg.CosNaming.NameComponent("Channel" + component_name, "");
273

274             // For demonstration use only.
275
// To do : replace with the previous commented lines
276
// (always connect new TV component to existent "ChannelF2" component
277
// to avoid unavailable names)
278

279             ncomp[0] = new org.omg.CosNaming.NameComponent JavaDoc("ChannelF2", "");
280
281             org.omg.CORBA.Object JavaDoc obj_ =
282                 org
283                 .objectweb
284                 .openccm
285                 .corba
286                 .TheNameService
287                 .getNamingContext()
288                 .getNamingContext()
289                 .resolve(ncomp);
290
291             org.objectweb.ccm.filetransfer.Channel chann_ =
292                 ChannelHelper.narrow(obj_);
293
294             this.ck[this.rank] =
295                 chann_.subscribe_to_tvs(tv.get_consumer_from_channels());
296
297             // Configuration completion
298
tv.configuration_complete();
299
300             this.named[this.rank] = component_name;
301             this.rank = this.rank + 1;
302
303             // Register the new component into the NameService
304

305             // Obtain the Name Service.
306
org.objectweb.openccm.corba.NamingContext ns =
307                 org.objectweb.openccm.corba.TheNameService.getNamingContext();
308
309             ns.rebind("TV" + component_name, tv);
310
311         } catch (Exception JavaDoc e) {
312             e.printStackTrace();
313         }
314
315     }
316
317     /* (non-Javadoc)
318      * @see org.objectweb.ccm.testgroup.ManageServiceOperations#create_home()
319      */

320     public void create_home(String JavaDoc home_name) {
321         // TODO Auto-generated method stub
322

323     }
324
325     public void destroy_component(String JavaDoc name) {
326
327         try {
328
329             // Disable the connexion
330

331             // Getting the Channel component connected to the TV component to destroy
332
org.omg.CosNaming.NameComponent JavaDoc[] ncomp =
333                 new org.omg.CosNaming.NameComponent JavaDoc[1];
334
335             //TO DO : replace <"F2"> with <name>
336
ncomp[0] =
337                 new org.omg.CosNaming.NameComponent JavaDoc("Channel" + "F2", "");
338
339             org.omg.CORBA.Object JavaDoc chann_obj =
340                 org
341                     .objectweb
342                     .openccm
343                     .corba
344                     .TheNameService
345                     .getNamingContext()
346                     .getNamingContext()
347                     .resolve(ncomp);
348
349             org.objectweb.ccm.filetransfer.Channel channel =
350                 ChannelHelper.narrow(chann_obj);
351
352             // Unsubscribe the event
353

354             // Getting the cookie
355
int index = 0;
356
357             for (int i = 0; i < rank; i++) {
358                 if (named[i].compareTo(name) == 0) {
359
360                     index = i;
361                 }
362
363             }
364
365             channel.unsubscribe_to_tvs(this.ck[index]);
366
367             // Getting the TV component to destroy
368

369             ncomp[0] = new org.omg.CosNaming.NameComponent JavaDoc("TV" + name, "");
370
371             org.omg.CORBA.Object JavaDoc tv_obj =
372                 org
373                     .objectweb
374                     .openccm
375                     .corba
376                     .TheNameService
377                     .getNamingContext()
378                     .getNamingContext()
379                     .resolve(ncomp);
380
381             org.objectweb.ccm.filetransfer.TV the_tv = TVHelper.narrow(tv_obj);
382
383             // Remove the TV named component
384
the_tv.remove();
385
386         } catch (Exception JavaDoc e) {
387             e.printStackTrace();
388         }
389
390     }
391
392     /* (non-Javadoc)
393      * @see org.objectweb.ccm.testgroup.ManageServiceOperations#destroy_home(java.lang.String)
394      */

395     public void destroy_home(String JavaDoc name) {
396         // TODO Auto-generated method stub
397

398     }
399
400     public String JavaDoc compute_home() {
401
402         // TO DO : put the load balancing code here
403
/*if (balancy_flag == 1)
404             balancy_flag = 2;
405         else
406             balancy_flag = 1;*/

407             
408         if (cbg.getSelectedCheckbox().getLabel()=="on PC") balancy_flag = 1;
409         else balancy_flag = 2;
410             
411             
412         String JavaDoc the_home_name = "TVHome" + balancy_flag;
413                
414
415         return the_home_name;
416     }
417
418     // ==================================================================
419
//
420
// Methods for interface java.awt.event.ActionListener
421
//
422
// ==================================================================
423

424     /**
425      * When the button is selectionned.
426      *
427      * @param e The associate ActionEvent.
428      */

429     public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
430
431         Object JavaDoc source = e.getSource();
432         int flag = 0;
433         if (source == button_tv) {
434                
435             String JavaDoc target = cbg.getSelectedCheckbox().getLabel();
436                 
437             if (rank == 0) {
438
439                 choice_.enable();
440                 button_tv_rem.enable();
441                 this.create_component(text_.getText());
442                 textArea_.append(
443                                         " <"
444                                             + text_.getText()
445                                             + ">"
446                                             + " TV created "
447                                             + target
448                                             + "\n");
449                 // Adding the new component to the choice list
450
choice_.add(text_.getText());
451                 choice_.select(text_.getText());
452                 this.selected_comp = text_.getText();
453             } else {
454                 // Check if component name already exists
455
for (int i = 0; i < rank; i++) {
456                     if (named[i].compareTo(text_.getText()) == 0) {
457                         textArea_.append(
458                             "This name <"
459                                 + text_.getText()
460                                 + "> already exists...operation cancelled"
461                                 + "\n");
462                         flag = 1;
463                         i = rank;
464                     }
465                 }
466                 if (flag == 0) {
467                     // Calls the manage system service : create component
468
choice_.enable();
469                     button_tv_rem.enable();
470                     this.create_component(text_.getText());
471                     
472                     
473                     textArea_.append(
474                         " <"
475                             + text_.getText()
476                             + ">"
477                             + " TV created "
478                             + target
479                             + "\n");
480                     
481                     // Adding the new component to the choice list
482
choice_.add(text_.getText());
483                     choice_.select(text_.getText());
484                     this.selected_comp = text_.getText();
485                 }
486
487             }
488
489         } else if (source == button_tv_rem) {
490
491             // Getting the component name to destroy
492
if (this.selected_comp != null) {
493
494                 this.destroy_component(this.selected_comp);
495
496                 //this.destroy_component(text_.getText());
497
textArea_.append(" <"+
498                     this.selected_comp + ">" + " TV destroyed" + "\n");
499
500                 // Remove the name in list
501
choice_.remove(this.selected_comp);
502                 for (int i = 0; i < rank; i++) {
503                     if (named[i].compareTo(this.selected_comp) == 0) {
504                         named[i] = "DESTROYED";
505                         i = rank;
506                     }
507                 }
508                 if (choice_.getItemCount() != 0) {
509                     choice_.select(0);
510                     this.selected_comp = choice_.getItem(0);
511                 } else {
512                     choice_.disable();
513                     button_tv_rem.disable();
514                 }
515
516             } else {
517                 textArea_.append(
518                     this.selected_comp
519                         + " cannot be removed or does not exist..."
520                         + "\n");
521             }
522
523         }
524         // Reset entered text
525
text_.setText("");
526
527     }
528
529     public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
530
531         this.selected_comp = evt.getItem().toString();
532
533     }
534
535 }
536
Popular Tags