summaryrefslogtreecommitdiff
path: root/java/src/IceGrid/TreeNode/Server.java
blob: 3041b66367bf2b88ebfe9e4d761c3647de229289 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
// **********************************************************************
//
// Copyright (c) 2003-2006 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
package IceGrid.TreeNode;

import java.awt.Component;
import java.awt.Cursor;

import javax.swing.Icon;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.SwingUtilities;

import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.DefaultTreeCellRenderer;

import IceGrid.AMI_Admin_startServer;
import IceGrid.AMI_Admin_stopServer;
import IceGrid.AMI_Admin_enableServer;
import IceGrid.AMI_Admin_patchServer;

import IceGrid.AdapterDescriptor;
import IceGrid.DistributionDescriptor;
import IceGrid.IceBoxDescriptor;
import IceGrid.Model;
import IceGrid.PropertyDescriptor;
import IceGrid.TemplateDescriptor;
import IceGrid.TreeModelI;
import IceGrid.ServerDescriptor;
import IceGrid.ServerDynamicInfo;
import IceGrid.ServerInstanceDescriptor;
import IceGrid.ServerState;
import IceGrid.SimpleInternalFrame;
import IceGrid.Utils;

//
// We can have 4 kinds of servers:
// - Plain server (no template)
// - Icebox server (no template)
// - Server instance
// - Icebox instance
//
class Server extends EditableParent
{ 
    static public ServerDescriptor
    copyDescriptor(ServerDescriptor sd)
    {
	ServerDescriptor copy = (ServerDescriptor)sd.clone();
	copy.adapters = Adapters.copyDescriptors(copy.adapters);
	copy.dbEnvs = DbEnvs.copyDescriptors(copy.dbEnvs);

	//
	// Update to properties is not atomic because of Adapter endpoints
	// (and possibly other properties set through a PropertiesHolder)
	//
	copy.properties = (java.util.LinkedList)copy.properties.clone();
	copy.distrib = (DistributionDescriptor)copy.distrib.clone();

	if(copy instanceof IceBoxDescriptor)
	{
	    IceBoxDescriptor ib = (IceBoxDescriptor)copy;
	    ib.services = Services.copyDescriptors(ib.services);
	}
	return copy;
    }

    static public ServerInstanceDescriptor
    copyDescriptor(ServerInstanceDescriptor sid)
    {
	return (ServerInstanceDescriptor)sid.clone();
    }
    
    static public void shallowRestore(ServerDescriptor from, ServerDescriptor into)
    {
	//
	// When editing a server or server template, if we update properties, 
	// we replace the entire field
	into.properties = from.properties;

	into.description = from.description;
	into.id = from.id;
	into.exe = from.exe;
	into.options = from.options;
	into.envs = from.envs;
	into.activation = from.activation;
	into.activationTimeout = from.activationTimeout;
	into.deactivationTimeout = from.deactivationTimeout;
	into.applicationDistrib = from.applicationDistrib;
	into.distrib.icepatch = from.distrib.icepatch;
	into.distrib.directories = from.distrib.directories;
    }

    static public ServerDescriptor newServerDescriptor()
    {
	return new ServerDescriptor(
	    new java.util.LinkedList(),
	    new java.util.LinkedList(),
	    new java.util.LinkedList(),
	    "",
	    "NewServer",
	    "",
	    "",
	    new java.util.LinkedList(),
	    new java.util.LinkedList(),
	    "manual",
	    "",
	    "",
	    true,
	    new IceGrid.DistributionDescriptor("", new java.util.LinkedList()));
    }

    static public IceBoxDescriptor newIceBoxDescriptor()
    {
	AdapterDescriptor serviceManager = new AdapterDescriptor(
	    "IceBox.ServiceManager",
	    "", 
	    "", // direct-adapter by default
	    "",
	    true,
	    true,
	    new java.util.LinkedList()
	    );

	java.util.LinkedList adapterList = new java.util.LinkedList();
	adapterList.add(serviceManager);
	
	PropertyDescriptor pd = 
	    new PropertyDescriptor("IceBox.ServiceManager.Endpoints",
				   "tcp -h 127.0.0.1");
	java.util.LinkedList properties = new java.util.LinkedList();
	properties.add(pd);

	return new IceBoxDescriptor(
	    adapterList,
	    properties,
	    new java.util.LinkedList(),
	    "",
	    "NewIceBox",
	    "",
	    "",
	    new java.util.LinkedList(),
	    new java.util.LinkedList(),
	    "manual",
	    "",
	    "",
	    true,
	    new IceGrid.DistributionDescriptor("", new java.util.LinkedList()),
	    new java.util.LinkedList()
	    );
    }
    
    //
    // Actions
    //
    public boolean[] getAvailableActions()
    {
	boolean[] actions = new boolean[ACTION_COUNT];
	actions[COPY] = true;
	
	Object clipboard = _model.getClipboard();
	if(clipboard != null && 
	   (clipboard instanceof ServerDescriptor
	    || clipboard instanceof ServerInstanceDescriptor))
	{
	    actions[PASTE] = true;
	}

	actions[DELETE] = true;
	if(!_ephemeral)
	{
	    actions[SHOW_VARS] = true;
	    actions[SUBSTITUTE_VARS] = true;
	}
	
	if(_instanceDescriptor == null)
	{
	    actions[NEW_ADAPTER] = (_adapters != null && _services == null);
	    actions[NEW_SERVICE] = (_services != null);
	    actions[NEW_DBENV] = (_dbEnvs != null);
	}
	
	ServerState state = getState();
	if(state != null && _model.getAdmin() != null)
	{
	    actions[START] = state == ServerState.Inactive && _enabled;
	    actions[STOP] = state != ServerState.Inactive;
	    actions[ENABLE] = !_enabled;
	    actions[DISABLE] = _enabled;

	    if(!_model.isUpdateInProgress())
	    {
		actions[SERVER_INSTALL_DISTRIBUTION] = 
		    !_serverDescriptor.distrib.icepatch.equals("");
	    }
	}

	return actions;
    }
    public JPopupMenu getPopupMenu()
    {
	if(_popup == null)
	{
	    _popup = new PopupMenu(_model);

	    JMenuItem item = new JMenuItem(_model.getActions()[NEW_ADAPTER]);
	    item.setText("New adapter");
	    _popup.add(item);

	    item = new JMenuItem(_model.getActions()[NEW_DBENV]);
	    item.setText("New DbEnv");
	    _popup.add(item);

	    item = new JMenuItem(_model.getActions()[NEW_SERVICE]);
	    item.setText("New service");
	    _popup.add(item);

	    _popup.addSeparator();
	    _popup.add(_model.getActions()[START]);
	    _popup.add(_model.getActions()[STOP]);
	    _popup.addSeparator();
	    _popup.add(_model.getActions()[ENABLE]);
	    _popup.add(_model.getActions()[DISABLE]);
	    _popup.addSeparator();
	    _popup.add(_model.getActions()[SERVER_INSTALL_DISTRIBUTION]);
	}
	return _popup;
    }
    public void copy()
    {
	if(_instanceDescriptor != null)
	{
	    _model.setClipboard(copyDescriptor(_instanceDescriptor));
	}
	else
	{
	    _model.setClipboard(copyDescriptor(_serverDescriptor));
	}
	_model.getActions()[PASTE].setEnabled(true);
    }
    public void paste()
    {
	_parent.paste();
    }
    public void newAdapter()
    {
	_adapters.newAdapter();
    }
    public void newDbEnv()
    {
	_dbEnvs.newDbEnv();
    }
    public void newService()
    {
	_services.newService();
    }

    public void start()
    {
	final String prefix = "Starting server '" + _id + "'...";
	_model.getStatusBar().setText(prefix);

	AMI_Admin_startServer cb = new AMI_Admin_startServer()
	    {
		//
		// Called by another thread!
		//
		public void ice_response()
		{
		    amiSuccess(prefix);
		}
		
		public void ice_exception(Ice.UserException e)
		{
		    amiFailure(prefix, "Failed to start " + _id, e);
		}

		public void ice_exception(Ice.LocalException e)
		{
		    amiFailure(prefix, "Failed to start " + _id, e.toString());
		}
	    };
	
	try
	{   
	    _model.getMainFrame().setCursor(
		Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
	   _model.getAdmin().startServer_async(cb, _id);
	}
	catch(Ice.LocalException e)
	{
	    failure(prefix, "Failed to start " + _id, e.toString());
	}
	finally
	{
	    _model.getMainFrame().setCursor(
		Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
	}
	
	//
	// Recompute actions in case this comes from popup menu
	// 
	_model.showActions(_model.getSelectedNode());
    }
    
    public void stop()
    {
	final String prefix = "Stopping server '" + _id + "'...";
	_model.getStatusBar().setText(prefix);

	AMI_Admin_stopServer cb = new AMI_Admin_stopServer()
	    {
		//
		// Called by another thread!
		//
		public void ice_response()
		{
		    amiSuccess(prefix);
		}
		
		public void ice_exception(Ice.UserException e)
		{
		    amiFailure(prefix, "Failed to stop " + _id, e);
		}

		public void ice_exception(Ice.LocalException e)
		{
		    amiFailure(prefix, "Failed to stop " + _id, e.toString());
		}
	    };
	
	try
	{   
	    _model.getMainFrame().setCursor(
		Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
	   _model.getAdmin().stopServer_async(cb, _id);
	}
	catch(Ice.LocalException e)
	{
	    failure(prefix, "Failed to stop " + _id, e.toString());
	}
	finally
	{
	    _model.getMainFrame().setCursor(
		Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
	}
	//
	// Recompute actions in case this comes from popup menu
	// 
	_model.showActions(_model.getSelectedNode());
    }

    public void enable()
    {
	enableServer(true);
    }

    public void disable()
    {
	enableServer(false);
    }

    public void serverInstallDistribution()
    {
	int shutdown = JOptionPane.showConfirmDialog(
	    _model.getMainFrame(),
	    "You are about to install or refresh your" 
	    + " server distribution and your application distribution onto this node.\n"
	    + " Do you want shut down all servers affected by this update?", 
	    "Patch Confirmation",
	    JOptionPane.YES_NO_CANCEL_OPTION);
       
	if(shutdown == JOptionPane.CANCEL_OPTION)
	{
	    //
	    // Recompute actions in case this comes from popup menu
	    // 
	    _model.showActions(_model.getSelectedNode());

	    return;
	}

	final String prefix = "Patching server '" + _id + "'...";
	_model.getStatusBar().setText(prefix);

	AMI_Admin_patchServer cb = new AMI_Admin_patchServer()
	    {
		//
		// Called by another thread!
		//
		public void ice_response()
		{
		    amiSuccess(prefix);
		}
		
		public void ice_exception(Ice.UserException e)
		{
		    amiFailure(prefix, "Failed to patch " + _id, e);
		}

		public void ice_exception(Ice.LocalException e)
		{
		    amiFailure(prefix, "Failed to patch " + _id, e.toString());
		}
	    };
	
	try
	{   
	    _model.getMainFrame().setCursor(
		Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
	   _model.getAdmin().patchServer_async(cb, _id, 
					       shutdown == JOptionPane.YES_OPTION);
	}
	catch(Ice.LocalException e)
	{
	    failure(prefix, "Failed to patch " + _id, e.toString());
	}
	finally
	{
	    _model.getMainFrame().setCursor(
		Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
	}

	//
	// Recompute actions in case this comes from popup menu
	// 
	_model.showActions(_model.getSelectedNode());
    }

    private void enableServer(boolean enable)
    {
	final String prefix = (enable ?
	    "Enabling" : "Disabling") + " server '" + _id + "'...";
	
	final String action = enable ? "enable" : "disable";
	
	_model.getStatusBar().setText(prefix);

	AMI_Admin_enableServer cb = new AMI_Admin_enableServer()
	    {
		//
		// Called by another thread!
		//
		public void ice_response()
		{
		    amiSuccess(prefix);
		}
		
		public void ice_exception(Ice.UserException e)
		{
		    amiFailure(prefix, "Failed to " + action + " " + _id, e);
		}

		public void ice_exception(Ice.LocalException e)
		{
		    amiFailure(prefix, "Failed to " + action + " " + _id, e.toString());
		}
	    };
	
	try
	{   
	    _model.getMainFrame().setCursor(
		Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
	   _model.getAdmin().enableServer_async(cb, _id, enable);
	}
	catch(Ice.LocalException e)
	{
	    failure(prefix, "Failed to " + action + " " + _id, e.toString());
	}
	finally
	{
	    _model.getMainFrame().setCursor(
		Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
	}
	//
	// Recompute actions in case this comes from popup menu
	// 
	_model.showActions(_model.getSelectedNode());

    }

    public Editor getEditor()
    {
	//
	// Pick the appropriate editor
	//
	Editor editor = null;
	if(_instanceDescriptor == null)
	{
	    if(_serverEditor == null)
	    {
		_serverEditor = new ServerEditor(_model, _model.getMainFrame());
	    }
	    _serverEditor.show(this);
	    return _serverEditor;
	}
	else
	{
	    if(_serverInstanceEditor == null)
	    {
		_serverInstanceEditor = new ServerInstanceEditor(
		    _model, _model.getMainFrame());
	    }
	    _serverInstanceEditor.show(this);
	    return _serverInstanceEditor;
	}
    }
	

    public Component getTreeCellRendererComponent(
	    JTree tree,
	    Object value,
	    boolean sel,
	    boolean expanded,
	    boolean leaf,
	    int row,
	    boolean hasFocus) 
    {
	if(_cellRenderer == null)
	{
	    //
	    // Initialization
	    //
	    _cellRenderer = new DefaultTreeCellRenderer();

	    //
	    // Regular servers
	    //
	    _icons = new Icon[7][2][2];
	    _icons[0][0][0] = Utils.getIcon("/icons/16x16/server_unknown.png");
	    _icons[ServerState.Inactive.value() + 1][0][0]
		= Utils.getIcon("/icons/16x16/server_inactive.png");
	    _icons[ServerState.Activating.value() + 1][0][0] = 
		Utils.getIcon("/icons/16x16/server_activating.png");
	    _icons[ServerState.Active.value() + 1][0][0] = 
		Utils.getIcon("/icons/16x16/server_active.png");
	    _icons[ServerState.Deactivating.value() + 1][0][0] = 
		Utils.getIcon("/icons/16x16/server_deactivating.png");
	    _icons[ServerState.Destroying.value() + 1][0][0] = 
		Utils.getIcon("/icons/16x16/server_destroying.png");
	    _icons[ServerState.Destroyed.value() + 1][0][0] = 
		Utils.getIcon("/icons/16x16/server_destroyed.png");

	    //
	    // IceBox servers
	    //
	    _icons[0][1][0] = Utils.getIcon("/icons/16x16/icebox_server_unknown.png");
	    _icons[ServerState.Inactive.value() + 1][1][0]
		= Utils.getIcon("/icons/16x16/icebox_server_inactive.png");
	    _icons[ServerState.Activating.value() + 1][1][0] = 
		Utils.getIcon("/icons/16x16/icebox_server_activating.png");
	    _icons[ServerState.Active.value() + 1][1][0] = 
		Utils.getIcon("/icons/16x16/icebox_server_active.png");
	    _icons[ServerState.Deactivating.value() + 1][1][0] = 
		Utils.getIcon("/icons/16x16/icebox_server_deactivating.png");
	    _icons[ServerState.Destroying.value() + 1][1][0] = 
		Utils.getIcon("/icons/16x16/icebox_server_destroying.png");
	    _icons[ServerState.Destroyed.value() + 1][1][0] = 
		Utils.getIcon("/icons/16x16/icebox_server_destroyed.png");

	    //
	    // Regular servers (disabled)
	    //
	    _icons[0][0][1] = Utils.getIcon("/icons/16x16/server_unknown.png");
	    _icons[ServerState.Inactive.value() + 1][0][1]
		= Utils.getIcon("/icons/16x16/server_disabled_inactive.png");
	    _icons[ServerState.Activating.value() + 1][0][1] = 
		Utils.getIcon("/icons/16x16/server_disabled_activating.png");
	    _icons[ServerState.Active.value() + 1][0][1] = 
		Utils.getIcon("/icons/16x16/server_disabled_active.png");
	    _icons[ServerState.Deactivating.value() + 1][0][1] = 
		Utils.getIcon("/icons/16x16/server_disabled_deactivating.png");
	    _icons[ServerState.Destroying.value() + 1][0][1] = 
		Utils.getIcon("/icons/16x16/server_disabled_destroying.png");
	    _icons[ServerState.Destroyed.value() + 1][0][1] = 
		Utils.getIcon("/icons/16x16/server_disabled_destroyed.png");

	    //
	    // IceBox servers (disabled)
	    //
	    _icons[0][1][1] = Utils.getIcon("/icons/16x16/icebox_server_unknown.png");
	    _icons[ServerState.Inactive.value() + 1][1][1]
		= Utils.getIcon("/icons/16x16/icebox_server_disabled_inactive.png");
	    _icons[ServerState.Activating.value() + 1][1][1] = 
		Utils.getIcon("/icons/16x16/icebox_server_disabled_activating.png");
	    _icons[ServerState.Active.value() + 1][1][1] = 
		Utils.getIcon("/icons/16x16/icebox_server_disabled_active.png");
	    _icons[ServerState.Deactivating.value() + 1][1][1] = 
		Utils.getIcon("/icons/16x16/icebox_server_disabled_deactivating.png");
	    _icons[ServerState.Destroying.value() + 1][1][1] = 
		Utils.getIcon("/icons/16x16/icebox_server_disabled_destroying.png");
	    _icons[ServerState.Destroyed.value() + 1][1][1] = 
		Utils.getIcon("/icons/16x16/icebox_server_disabled_destroyed.png");

	}

	int icebox = _serverDescriptor instanceof IceBoxDescriptor ? 1 : 0;
	int disabled = _enabled ? 0 : 1;

	if(expanded)
	{
	    _cellRenderer.setOpenIcon(_icons[_stateIconIndex][icebox][disabled]);
	}
	else
	{
	    _cellRenderer.setClosedIcon(_icons[_stateIconIndex][icebox][disabled]);
	}   

	_cellRenderer.setToolTipText(_toolTip);
	return _cellRenderer.getTreeCellRendererComponent(
	    tree, value, sel, expanded, leaf, row, hasFocus);
    }


    public PropertiesHolder getPropertiesHolder()
    {
	return _propertiesHolder;
    }

    public boolean destroy()
    {
	if(_parent == null)
	{
	    return false;
	}
	Node node = (Node)_parent;
	
	if(_ephemeral)
	{
	    node.removeChild(this, true);
	    return true;
	}
	else if(_model.canUpdate())
	{
	    if(_instanceDescriptor != null)
	    {
		node.removeDescriptor(_instanceDescriptor);
	    }
	    else
	    {
		node.removeDescriptor(_serverDescriptor);
	    }
	    node.removeElement(this, true);
	    return true;
	}
	return false;
    }


    public Object getDescriptor()
    {
	if(_instanceDescriptor != null)
	{
	    return _instanceDescriptor;
	}
	else
	{
	    return _serverDescriptor;
	}
    }

    public Object saveDescriptor()
    {
	if(_instanceDescriptor != null)
	{
	    return _instanceDescriptor.clone();
	}
	else
	{
	    ServerDescriptor clone = (ServerDescriptor)_serverDescriptor.clone();
	    clone.distrib = (IceGrid.DistributionDescriptor)clone.distrib.clone();
	    return clone;
	}
    }

    public void restoreDescriptor(Object savedDescriptor)
    {
	if(_instanceDescriptor != null)
	{   
	    ServerInstanceDescriptor copy = (ServerInstanceDescriptor)savedDescriptor;

	    _instanceDescriptor.template = copy.template;
	    _instanceDescriptor.parameterValues = copy.parameterValues;
	    
	    ServerTemplate t = getApplication().findServerTemplate(_instanceDescriptor.template);
	    _serverDescriptor = (ServerDescriptor)
		((TemplateDescriptor)t.getDescriptor()).descriptor;
	}
	else
	{
	    shallowRestore((ServerDescriptor)savedDescriptor, _serverDescriptor);
	}
    }

    //
    // Builds the server and all its sub-tree
    //
    Server(boolean brandNew, String serverId, 
	   Utils.Resolver resolver, ServerInstanceDescriptor instanceDescriptor,
	   ServerDescriptor serverDescriptor,
	   Application application) throws UpdateFailedException
    {
	super(brandNew, serverId, application.getModel());
	_ephemeral = false;
	rebuild(resolver, instanceDescriptor, serverDescriptor, application);
    }

    Server(String serverId, ServerInstanceDescriptor instanceDescriptor, 
	   ServerDescriptor serverDescriptor, Model model)
    {
	super(false, serverId, model);
	_ephemeral = true;
	try
	{
	    rebuild(null, instanceDescriptor, serverDescriptor, null);
	}
	catch(UpdateFailedException e)
	{
	    assert false;
	}
    }

    //
    // Update the server and all its subtree
    //
    void rebuild(Utils.Resolver resolver,  
		 ServerInstanceDescriptor instanceDescriptor,
		 ServerDescriptor serverDescriptor,
		 Application application) throws UpdateFailedException
    {
	assert serverDescriptor != null;
	_resolver = resolver;
	_instanceDescriptor = instanceDescriptor;
	_serverDescriptor = serverDescriptor;
	clearChildren();
	
	boolean isEditable = (instanceDescriptor == null);
	_propertiesHolder = new PropertiesHolder(serverDescriptor);	

	if(_ephemeral)
	{
	    _services = null;
	    _dbEnvs = null;
	    _adapters = null;
	}
	else
	{
	    try
	    {
		if(serverDescriptor instanceof IceBoxDescriptor)
		{
		    IceBoxDescriptor iceBoxDescriptor = (IceBoxDescriptor)serverDescriptor;
		    
		    _services = new Services(iceBoxDescriptor.services,
					     isEditable, _resolver, application);
		    addChild(_services);
		    //
		    // IceBox has not dbEnv
		    //
		    assert serverDescriptor.dbEnvs.size() == 0;
		    _dbEnvs = null;
		}
		else
		{
		    _services = null;   	
		    _dbEnvs = new DbEnvs(serverDescriptor.dbEnvs, 
					 isEditable, _resolver, _model);
		    addChild(_dbEnvs);
		}
		
		_adapters = new Adapters(serverDescriptor.adapters, 
					 isEditable, _services != null, 
					 _resolver, _model);
		addChild(_adapters);
	    }
	    catch(UpdateFailedException e)
	    {
		e.addParent(this);
		throw e;
	    }
	}
    }
    
    public void setParent(CommonBase parent)
    {
	if(!_ephemeral)
	{
	    Ice.IntHolder pid = new Ice.IntHolder();
	    Ice.BooleanHolder enabled = new Ice.BooleanHolder();
	    _state = _model.getRoot().registerServer(_resolver.find("node"),
						     this,
						     pid, enabled);
	    
	    _pid = pid.value;
	    _enabled = enabled.value;
	    _toolTip = toolTip(_state, _pid, _enabled);
	    if(_state != null)
	    {
		_stateIconIndex = _state.value() + 1;
	    }
	}
	super.setParent(parent);
    }

    public void clearParent()
    {
	if(_parent != null)
	{
	    if(!_ephemeral)
	    {
		_model.getRoot().unregisterServer(_resolver.find("node"),
						  this);
	    }
	    super.clearParent();
	}
    }
    
    java.util.List findServiceInstances(String template)
    {
	if(_services != null)
	{
	    return _services.findServiceInstances(template);
	}
	else
	{
	    return new java.util.LinkedList();
	}
    }

    void removeServiceInstances(String template)
    {
	if(_services != null)
	{
	    _services.removeServiceInstances(template);
	}
    }

    
    void updateDynamicInfo(ServerState state, int pid, boolean enabled)
    {
	if(state != _state || pid != _pid || enabled != _enabled)
	{
	    _state = state;
	    _pid = pid;
	    _enabled = enabled;
	    
	    _toolTip = toolTip(_state, _pid, _enabled);
	    if(_state == null)
	    {
		_stateIconIndex = 0;
	    }
	    else
	    {
		_stateIconIndex = _state.value() + 1;
	    }
	
	    //
	    // Change the node representation
	    //
	    fireNodeChangedEvent(this);
	}
    }

    ServerState getState()
    {
	return _state;
    }
    
    int getPid()
    {
	return _pid;
    }

    ServerInstanceDescriptor getInstanceDescriptor()
    {
	return _instanceDescriptor;
    }

    ServerDescriptor getServerDescriptor()
    {
	return _serverDescriptor;
    }

    void setServerDescriptor(ServerDescriptor sd)
    {
	_serverDescriptor = sd;
    }

    Services getServices()
    {
	return _services;
    }

    Utils.Resolver getResolver()
    {
	return _resolver;
    }

    Utils.Resolver getParentResolver()
    {
	if(_parent != null)
	{
	    return ((Node)_parent).getResolver();
	}
	else
	{
	    return null;
	}
    }

    public boolean isEphemeral()
    {
	return _ephemeral;
    }

    public String toString()
    {
	if(_instanceDescriptor == null || _ephemeral)
	{
	    return super.toString();
	}
	else
	{
	    return _id + ": " + _instanceDescriptor.template + "<>";
	}
    }

    static private String toolTip(ServerState state, int pid, boolean enabled)
    {
	String result = (state == null ? "Unknown" : state.toString());
	
	if(!enabled)
	{
	    result += ", disabled";
	}

	if(pid != 0)
	{
	    result += ", pid: " + pid;
	}
	return result;
    }

    private ServerState _state = null;
    private boolean _enabled = true;
    private int _stateIconIndex = 0;
    private int _pid = 0;
    private String _toolTip;

    private ServerInstanceDescriptor _instanceDescriptor;
    private ServerDescriptor _serverDescriptor;
    private final boolean _ephemeral;

    private Utils.Resolver _resolver;

    private PropertiesHolder _propertiesHolder;

    //
    // Children
    //
    private Services _services;
    private Adapters _adapters;
    private DbEnvs _dbEnvs;

    static private DefaultTreeCellRenderer _cellRenderer;
    static private Icon[][][] _icons;
 
    static private ServerEditor _serverEditor;
    static private ServerInstanceEditor _serverInstanceEditor;
    static private JPopupMenu _popup;
}