KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > imr > util > ImRModel


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

21 package org.jacorb.imr.util;
22
23 import org.apache.avalon.framework.logger.Logger;
24 import org.apache.avalon.framework.configuration.*;
25
26 import org.jacorb.imr.*;
27 import org.jacorb.imr.AdminPackage.*;
28
29 import java.util.*;
30
31 import javax.swing.tree.*;
32 import javax.swing.*;
33 import javax.swing.table.*;
34
35 /**
36  * This class provides the GUIs functionality
37  * communicating with the repository.
38  *
39  * @author Nicolas Noffke
40  *
41  * $Id: ImRModel.java,v 1.10 2004/05/06 12:39:59 nicolas Exp $
42  */

43
44 public class ImRModel
45 {
46     private Admin m_admin;
47     private org.jacorb.orb.ORB m_orb;
48
49     private org.jacorb.config.Configuration configuration = null;
50
51     /** the specific logger for this component */
52     private Logger logger = null;
53
54     private ServerInfo[] m_servers;
55     private ImRInfo m_imr_info;
56
57     private DefaultMutableTreeNode m_top_node;
58     private JTree m_tree;
59     private DefaultTreeModel m_tree_model;
60
61     private ImRPOATableModel m_poa_model;
62     private ImRServerTableModel m_server_model;
63
64     private Hashtable m_server_names;
65
66     private JComboBox m_host_selector;
67     private DefaultComboBoxModel m_host_selector_model;
68     private Hashtable m_host_names;
69
70     private RefreshThread m_refresh_thread;
71     protected int m_current_refresh_interval = 20000;
72     protected boolean m_refresh_disabled = false;
73
74     private Vector m_server_nodes;
75     private Vector m_poa_nodes; //contains other Vectors
76

77     /**
78      * The constructor. Connects to default repository and fetches the servers.
79      */

80
81     public ImRModel()
82     {
83     m_orb = (org.jacorb.orb.ORB)org.omg.CORBA.ORB.init(new String JavaDoc[0], null);
84         configuration = m_orb.getConfiguration();
85         logger = configuration.getNamedLogger("jacorb.imr.model");
86
87         try
88         {
89             m_admin = AdminHelper.narrow( m_orb.resolve_initial_references("ImplementationRepository"));
90         }
91         catch( org.omg.CORBA.ORBPackage.InvalidName JavaDoc in )
92         {
93             if (logger.isWarnEnabled())
94                 logger.warn("Could not contact Impl. Repository!");
95             return;
96         }
97     
98     fetchImRInfo();
99
100     m_top_node = new DefaultMutableTreeNode(m_imr_info);
101     m_tree_model = new DefaultTreeModel(m_top_node, false);
102     m_tree = new JTree(m_tree_model);
103     
104     m_server_model = new ImRServerTableModel(this);
105     m_poa_model = new ImRPOATableModel();
106     
107     m_server_names = new Hashtable();
108     
109     m_server_nodes = new Vector();
110     m_poa_nodes = new Vector();
111
112     m_host_names = new Hashtable();
113     m_host_selector_model = new DefaultComboBoxModel();
114     m_host_selector = new JComboBox(m_host_selector_model);
115     m_host_selector.setEditable(true);
116
117     fetchServers();
118     m_tree.expandRow(0);
119
120     m_refresh_thread = new RefreshThread(m_current_refresh_interval);
121     }
122
123     /**
124      * Connect the manager to a remote repository.
125      *
126      * @param ior_url an url pointing to the IOR file of a remote repository.
127      */

128     public void connectTo(String JavaDoc ior_url)
129     {
130         try
131         {
132             m_admin = AdminHelper.narrow( m_orb.resolve_initial_references("ImplementationRepository"));
133         }
134         catch( org.omg.CORBA.ORBPackage.InvalidName JavaDoc in )
135         {
136             if (logger.isWarnEnabled())
137                 logger.warn("Could not contact Impl. Repository!");
138             return;
139         }
140     
141     fetchImRInfo();
142
143     m_top_node.setUserObject(m_imr_info);
144
145     fetchServers();
146     
147     setRefreshInterval(m_current_refresh_interval);
148     }
149     
150     /**
151      * Get a JComboBox containing all known hostnames.
152      *
153      * @return a JComboBox.
154      */

155     public JComboBox getHostSelector(){
156     return m_host_selector;
157     }
158
159     /**
160      * Get the table model for the POA table.
161      *
162      * @param the model for the POA table.
163      */

164     public TableModel getPOATableModel(){
165     return m_poa_model;
166     }
167
168     /**
169      * Get the table model for the server table.
170      *
171      * @param the model for the server table.
172      */

173     public TableModel getServerTableModel(){
174     return m_server_model;
175     }
176
177     /**
178      * Set the POA table model to the specific server, i.e.
179      * the POA table displays this servers poas.
180      *
181      * @param name the servers name to build the table for.
182      */

183     public void poaTableForServer(ServerInfo server){
184     m_poa_model.setPOAs(server.poas);
185     }
186
187     /**
188      * Fetch all servers from the repository. Rebuild Tree and HostSelector.
189      */

190     public void fetchServers(){
191     m_servers = m_admin.list_servers();
192
193     m_server_model.setServers(m_servers);
194
195     m_server_names.clear();
196     for (int _i = 0; _i < m_servers.length; _i++)
197         m_server_names.put(m_servers[_i].name, new Integer JavaDoc(_i));
198
199     String JavaDoc _server = m_poa_model.getServerName();
200     if (_server != null)
201         m_poa_model.setPOAs(m_servers[indexForServerName(_server)].poas);
202
203     buildTree();
204     buildHostSelectorModel();
205     }
206
207     /**
208      * Remove a server from the repository.
209      *
210      * @param name the servers name.
211      */

212     public void removeServer(String JavaDoc name){
213     removeServer(indexForServerName(name));
214     }
215
216     /**
217      * Remove a server from the repository.
218      *
219      * @param server_row the servers row in the table.
220      */

221     public void removeServer(int server_row){
222     try{
223         m_admin.unregister_server(m_servers[server_row].name);
224     }catch(Exception JavaDoc _e){
225         handleException (_e);
226     }
227
228     fetchServers();
229     }
230
231     /**
232      * Hold a server.
233      *
234      * @param name the servers name.
235      */

236     public void holdServer(String JavaDoc name){
237     holdServer(indexForServerName(name));
238     }
239
240     /**
241      * Hold a server.
242      *
243      * @param server_row the servers row in the table.
244      */

245     public void holdServer(int server_row){
246     try{
247         m_admin.hold_server(m_servers[server_row].name);
248     }catch(Exception JavaDoc _e){
249        handleException (_e);
250     }
251
252     refreshServer(server_row);
253     }
254
255     /**
256      * Release a server.
257      *
258      * @param name the servers name.
259      */

260     public void releaseServer(String JavaDoc name){
261     releaseServer(indexForServerName(name));
262     }
263
264     /**
265      * Release a server.
266      *
267      * @param server_row the servers row in the table.
268      */

269     public void releaseServer(int server_row){
270     try{
271         m_admin.release_server(m_servers[server_row].name);
272     }catch(Exception JavaDoc _e){
273        handleException (_e);
274     }
275
276     refreshServer(server_row);
277     }
278
279     /**
280      * Refresh a server.
281      *
282      * @param name the servers name.
283      */

284     public void refreshServer(String JavaDoc name){
285     refreshServer(indexForServerName(name));
286     }
287
288     /**
289      * Refresh a server.
290      *
291      * @param server_row the servers row in the table.
292      */

293     public void refreshServer(int index){
294     try{
295         ServerInfo _server = m_admin.get_server_info(m_servers[index].name);
296
297         m_servers[index] = _server;
298
299         buildServerNode(index);
300
301         m_server_model.serverRefreshed(index);
302
303         if (m_host_names.put(m_servers[index].host, m_servers[index].host) == null)
304         m_host_selector_model.addElement(m_servers[index].host);
305
306         if ( _server.name.equals(m_poa_model.getServerName()))
307           m_poa_model.setPOAs(_server.poas);
308
309     }catch(Exception JavaDoc _e){
310        handleException (_e);
311     }
312     }
313
314     /**
315      * Set a server down.
316      *
317      * @param name the servers name.
318      */

319     public void setServerDown(String JavaDoc name){
320     setServerDown(indexForServerName(name));
321     }
322
323     /**
324      * Set a server down.
325      *
326      * @param server_row the servers row in the table.
327      */

328     public void setServerDown(int server_row){
329     Registration _reg = RegistrationHelper.narrow(m_admin);
330
331     try{
332         _reg.set_server_down(m_servers[server_row].name);
333     }catch (Exception JavaDoc _e){
334        handleException (_e);
335     }
336     
337     refreshServer(m_servers[server_row].name);
338     }
339
340     /**
341      * Add a server to the repository.
342      *
343      * @param name the servers name.
344      * @param command the servers startup command. Leave empty (not null)
345      * if automatic startup is not desired.
346      * @param host the host the server is running on.
347      */

348     public void addServer(String JavaDoc name, String JavaDoc command, String JavaDoc host){
349     try{
350         m_admin.register_server(name, command, host);
351     }catch (Exception JavaDoc _e){
352        handleException (_e);
353     }
354
355     fetchServers();
356     }
357
358     /**
359      * Get the tree representation of the server structure.
360      *
361      * @return a JTree.
362      */

363     public JTree getTree(){
364     return m_tree;
365     }
366     
367     /**
368      * Shut the repository down.
369      *
370      * @param wait true, if ORB should wait for still open connections to be
371      * closed by clients.
372      */

373     public void imrShutdown(boolean wait){
374     disableRefresh();
375
376     try{
377         m_admin.shutdown(wait);
378         
379         m_top_node.removeAllChildren();
380         m_servers = null;
381     }catch (Exception JavaDoc _e){
382        handleException (_e);
383     }
384     }
385        
386     /**
387      * Make a backup of the server table.
388      */

389     public void saveTable(){
390     try{
391         m_admin.save_server_table();
392     }catch (Exception JavaDoc _e){
393        handleException (_e);
394     }
395     }
396
397     /**
398      * Get the row number of a POA in the POA table.
399      *
400      * @param server the server node the POA belongs to.
401      * @param poa the poas poa node.
402      */

403     public int getRow(ServerInfo server, POAInfo poa){
404     for(int _i = 0; _i < server.poas.length; _i++){
405         if (server.poas[_i] == poa)
406         return _i;
407     }
408
409     return -1;
410     }
411
412     /**
413      * Set the interval by which the internal data is refreshed.
414      *
415      * @param intervel refresh interval in ms.
416      */

417     public void setRefreshInterval(int interval){
418     m_current_refresh_interval = interval;
419     m_refresh_disabled = false;
420     m_refresh_thread.setInterval(interval);
421     }
422     
423     /**
424      * Disable the automatic refresh.
425      */

426     public void disableRefresh(){
427     m_refresh_disabled = true;
428     m_refresh_thread.setInterval(0);
429     }
430
431     /**
432      * Update a server in the repository by changes the user made in the server
433      * table of the GUI.
434      *
435      * @param server_row the row of the server in the table.
436      * @param field_name the columns name.
437      * @param new_value the cells new value.
438      */

439     protected void updateServer(int server_row, String JavaDoc field_name, Object JavaDoc new_value){
440     String JavaDoc _host = m_servers[server_row].host;
441     String JavaDoc _cmd = m_servers[server_row].command;
442
443     if (new_value instanceof String JavaDoc){
444         if (field_name.equals("Host")){
445         _host = (String JavaDoc) new_value;
446         if (m_host_names.put(new_value, new_value) == null)
447             m_host_selector_model.addElement(new_value);
448         }
449         else if (field_name.equals("Command"))
450         _cmd = (String JavaDoc) new_value;
451         
452         try{
453         m_admin.edit_server(m_servers[server_row].name, _cmd, _host);
454         }catch (Exception JavaDoc _e){
455         handleException (_e);
456         }
457     }
458     else if(new_value instanceof Boolean JavaDoc){
459         if (field_name.equals("active")){
460         if (! ((Boolean JavaDoc) new_value).booleanValue())
461             setServerDown(m_servers[server_row].name);
462         }
463         else if (field_name.equals("holding")){
464         try{
465             if (((Boolean JavaDoc) new_value).booleanValue())
466             m_admin.hold_server(m_servers[server_row].name);
467             else
468             m_admin.release_server(m_servers[server_row].name);
469             
470         }catch (Exception JavaDoc _e){
471             handleException (_e);
472         }
473         }
474     }
475     
476     refreshServer(m_servers[server_row].name);
477     }
478
479     /**
480      * Bring up error message Dialog.
481      *
482      * @param e the exception that has been thrown.
483      */

484
485     private void handleException (Exception JavaDoc e)
486     {
487         if (e instanceof org.omg.CORBA.UserException JavaDoc)
488         {
489             String JavaDoc _msg = e.toString();
490             if (e instanceof IllegalServerName)
491                 _msg = "The specified server name is not allowed";
492             else if (e instanceof DuplicateServerName)
493                 _msg = "A server with name " +
494                     ((DuplicateServerName) e).name +
495                     " has already been registered with the repository";
496             else if (e instanceof FileOpFailed)
497                 _msg = "The backup operation failed";
498             
499             JOptionPane.showMessageDialog(new JFrame(), _msg,
500                                           "An error occurred",
501                                           JOptionPane.ERROR_MESSAGE);
502         }
503         else
504         {
505             if (logger.isWarnEnabled())
506                 logger.warn("Exception: " + e.getMessage());
507         }
508     }
509
510
511     /**
512      * Fill the model of the combo box with host names.
513      * After fetching hosts from the repository, they are "pinged" in order to
514      * see if they are still up.
515      */

516
517     private void buildHostSelectorModel()
518     {
519     HostInfo[] _hosts = m_admin.list_hosts();
520
521     for (int _i = 0; _i < _hosts.length; _i++){
522         try{
523         ServerStartupDaemon _ssd = ServerStartupDaemonHelper.narrow(m_orb.string_to_object(_hosts[_i].ior_string));
524         _ssd.get_system_load();
525         _ssd._release();
526         
527         // ssd is up and seems to work
528
if (m_host_names.put(_hosts[_i].name, _hosts[_i].name) == null)
529            m_host_selector_model.addElement(_hosts[_i].name);
530         } catch (Exception JavaDoc _e){
531         //ignore
532
}
533     }
534
535     for (int _i = 0; _i < m_servers.length; _i++)
536         if (m_host_names.put(m_servers[_i].host, m_servers[_i].host) == null)
537         m_host_selector_model.addElement(m_servers[_i].host);
538     }
539
540     /**
541      * Get a servers row by its name.
542      *
543      * @param name the servers name.
544      * @return the servers row
545      */

546     private int indexForServerName(String JavaDoc name){
547     return ((Integer JavaDoc) m_server_names.get(name)).intValue();
548     }
549
550     /**
551      * Get the ImRInfo struct from the repository.
552      */

553     private void fetchImRInfo(){
554     Registration _reg = RegistrationHelper.narrow(m_admin);
555
556     m_imr_info = _reg.get_imr_info();
557     }
558
559     /**
560      * Build a tree node for a server, with all its
561      * dependend POAs.
562      *
563      * @param server the servers ServerInfo struct.
564      */

565     private void buildServerNode(int index){
566     DefaultMutableTreeNode _server_node;
567     POAInfo[] _poa_array = m_servers[index].poas;
568     Vector _poas;
569     if (index < m_server_nodes.size()){
570         // a server node for that index exists
571
_server_node = (DefaultMutableTreeNode) m_server_nodes.elementAt(index);
572         _poas = (Vector) m_poa_nodes.elementAt(index);
573
574     }
575     else{
576         // a new server node has to be created
577
_server_node = new DefaultMutableTreeNode(m_servers[index]);
578         m_server_nodes.addElement(_server_node);
579         m_tree_model.insertNodeInto(_server_node, m_top_node, index);
580         m_tree.scrollPathToVisible(new TreePath(_server_node.getPath()));
581
582         _poas = new Vector();
583         m_poa_nodes.addElement(_poas);
584     }
585
586     int _i;
587     //update existing nodes
588
for(_i = 0; _i < _poas.size(); _i++){
589         if (_i < _poa_array.length){
590         DefaultMutableTreeNode _poa = (DefaultMutableTreeNode) _poas.elementAt(_i);
591         _poa.setUserObject(_poa_array[_i]);
592         }
593         else
594         break;
595     }
596     if (_i >= _poa_array.length){
597         //remove surplus nodes
598
for (int _j = _poas.size() - 1; _j >= _i; _j--){
599         DefaultMutableTreeNode _poa = (DefaultMutableTreeNode) _poas.elementAt(_j);
600         _poas.removeElementAt(_j);
601         m_tree_model.removeNodeFromParent(_poa);
602         }
603     }
604     else{
605         // build new nodes
606
for (int _j = _i; _j < _poa_array.length; _j++){
607         DefaultMutableTreeNode _poa = new DefaultMutableTreeNode(_poa_array[_j]);
608         _poas.addElement(_poa);
609         m_tree_model.insertNodeInto(_poa, _server_node, _j);
610         }
611     }
612     }
613
614     /**
615      * Remove a server node from the tree.
616      *
617      * @param index the servers index in the table.
618      */

619     private void removeServerNode(int index){
620     DefaultMutableTreeNode _server_node = (DefaultMutableTreeNode) m_server_nodes.elementAt(index);
621     Vector _poas = (Vector) m_poa_nodes.elementAt(index);
622     
623     for (int _j = _poas.size() - 1; _j >= 0; _j--){
624         DefaultMutableTreeNode _poa = (DefaultMutableTreeNode) _poas.elementAt(_j);
625         _poas.removeElementAt(_j);
626         m_tree_model.removeNodeFromParent(_poa);
627     }
628  
629     m_server_nodes.removeElementAt(index);
630     m_tree_model.removeNodeFromParent(_server_node);
631     }
632     
633
634
635     /**
636      * Build the tree by building all its server nodes. The
637      * root node stays always the same.
638      */

639     private void buildTree(){
640     int _i;
641     // update exisiting nodes
642
for(_i = 0; _i < m_server_nodes.size(); _i++){
643         if (_i < m_servers.length){
644         DefaultMutableTreeNode _server = (DefaultMutableTreeNode) m_server_nodes.elementAt(_i);
645         _server.setUserObject(m_servers[_i]);
646         buildServerNode(_i);
647         }
648         else
649         break;
650     }
651     
652     if (_i >= m_servers.length){
653         //remove surplus nodes
654
for (int _j = m_server_nodes.size() - 1; _j >= _i; _j--)
655         removeServerNode(_j);
656     }
657     else{
658         //add new nodes
659
for (int _j = _i; _j < m_servers.length; _j++)
660         buildServerNode(_j);
661     }
662     }
663
664     private class RefreshThread extends Thread JavaDoc{
665     private long m_interval;
666     private boolean m_run = true;
667
668     public RefreshThread(long interval){
669         m_interval = interval;
670
671         start();
672     }
673
674     public synchronized void run(){
675         while (true){
676         while (m_interval <= 0){
677             try{
678             this.wait();
679             }catch (Exception JavaDoc _e){
680             handleException(_e);
681             }
682         }
683         try{
684             fetchServers();
685            }catch (Exception JavaDoc _e){
686                handleException(_e);
687            }
688
689         try{
690             this.wait(m_interval);
691         }catch (Exception JavaDoc _e){
692             handleException(_e);
693         }
694         }
695     }
696
697     public synchronized void setInterval(long interval)
698         {
699         m_interval = interval;
700
701         this.notifyAll();
702     }
703     }
704 } // ImRModel
705

706
707
708
709
710
711
712
713
Popular Tags