summaryrefslogtreecommitdiff
path: root/java/src/IceGrid/TreeNode/AdapterEditor.java
blob: e8da0a4bc274235642285e16338ee805b497c2cd (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
// **********************************************************************
//
// Copyright (c) 2003-2005 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.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.CellConstraints;

import IceGrid.AdapterDescriptor;
import IceGrid.Model;
import IceGrid.ObjectDescriptor;
import IceGrid.TableDialog;
import IceGrid.Utils;

class AdapterEditor extends ListElementEditor
{
    public JComponent getCurrentStatus(Ice.StringHolder title)
    {
	JComponent panel = super.getCurrentStatus(title);

	if(panel != null)
	{
	    Adapter adapter = getAdapter();
	    if(adapter == null || adapter.getResolver() == null)
	    {
		panel = null;
	    }
	}
	return panel;
    }

    AdapterEditor(JFrame parentFrame)
    {
	super(true);

	_objects.setEditable(false);
	
	_currentStatus.setEditable(false);
	_currentEndpoints.setEditable(false);
	_currentEndpoints.setToolTipText(
	    "Endpoints registered with the IceGrid Registry during the activation of this adapter"); 

	//
	// Create buttons
	//
	
	//
	// _replicaGroupButton
	//
	Action gotoReplicaGroup = new AbstractAction(
	    "", Utils.getIcon("/icons/16x16/goto.png"))
	    {
		public void actionPerformed(ActionEvent e) 
		{
		    Object obj = _replicaGroupId.getSelectedItem();
		    Adapter adapter = getAdapter();

		    ReplicaGroup rg = null;
		    if(obj instanceof ReplicaGroup)
		    {
			rg = (ReplicaGroup)obj;
		    }
		    else
		    {
			String replicaGroupId = 
			    Utils.substitute(obj.toString(), adapter.getResolver());
		  
			rg = adapter.getApplication().
			    findReplicaGroup(replicaGroupId);
		    }

		    //
		    // The button is enabled therefore rg should be != null
		    //
		    if(rg != null)
		    {
			adapter.getModel().setSelectionPath(rg.getPath());
		    }
		}
	    };
	gotoReplicaGroup.putValue(Action.SHORT_DESCRIPTION, 
				  "Goto the definition of this replica group");
	_replicaGroupButton = new JButton(gotoReplicaGroup);
	
	//
	// _objectsButton
	//
	_objectsDialog = new TableDialog(parentFrame, 
					 "Registered Objects",
					 "Object Identity", 
					 "Type", true);
	
	Action openObjectsDialog = new AbstractAction("...")
	    {
		public void actionPerformed(ActionEvent e) 
		{
		    java.util.Map result = _objectsDialog.show(_objectsMap, 
							       getProperties());
		    if(result != null)
		    {
			updated();
			_objectsMap = result;
			setObjectsField();
		    }
		}
	    };
	openObjectsDialog.putValue(Action.SHORT_DESCRIPTION, 
				   "Edit registered objects");
	_objectsButton = new JButton(openObjectsDialog);
	
	
	Action checkRegisterProcess = new AbstractAction("Register Process")
	    {
		public void actionPerformed(ActionEvent e)
		{
		    updated();
		}
	    };
	_registerProcess = new JCheckBox(checkRegisterProcess);
	_registerProcess.setToolTipText(
	    "<html>During activation, create a Process object<br>"
	    + "in this adapter and register it with IceGrid<br>"
	    + "to enable clean shutdown; you should register<br>"
	    + "exactly one Process object per server.</html>");

	Action checkWaitForActivation = 
	    new AbstractAction("Wait for Activation")
	    {
		public void actionPerformed(ActionEvent e)
		{
		    updated();
		}
	    };
	_waitForActivation = new JCheckBox(checkWaitForActivation);
	_waitForActivation.setToolTipText(
	    "<html>When starting the enclosing server, "
	    + "does IceGrid<br>wait for this adapter to become active?</html>");
	//
	// Associate updateListener with various fields
	//
	_name.getDocument().addDocumentListener(
	    new DocumentListener() 
	    {
		public void changedUpdate(DocumentEvent e)
		{
		    update();
		}
		
		public void insertUpdate(DocumentEvent e)
		{
		    update();
		}
		
		public void removeUpdate(DocumentEvent e)
		{
		    update();
		}

		private void update()
		{
		    updated();
		    //
		    // Recompute default id
		    //
		    _defaultAdapterId = getAdapter().getDefaultAdapterId(_name.getText());
		    refreshId();
		}
	    });

	_name.setToolTipText(
	    "Identifies this object adapter within an Ice communicator");

	_endpoints.getDocument().addDocumentListener(_updateListener);
	_endpoints.setToolTipText(
	    "<html>The network interfaces on which this adapter receives requests;<br>"
	    + "for example:<br>" 
	    + " tcp (listen on all local interfaces using a random port)<br>"
	    + " tcp -h venus.foo.com (listen on just one interface)<br>"
	    + " tcp -t 10000 (sets a timeout of 10,000 milliseconds)<br>"
	    + " ssl -h venus.foo.com (accepts SSL connections instead of plain TCP)"
	    + "</html>");

	_description.getDocument().addDocumentListener(_updateListener);
	_description.setToolTipText(
	    "An optional description for this object adapter");

	JTextField idTextField = (JTextField)
	    _id.getEditor().getEditorComponent();
	idTextField.getDocument().addDocumentListener(_updateListener);
	_id.setToolTipText("If set, must be unique within this IceGrid deployment");

	JTextField replicaGroupIdTextField = (JTextField)
	    _replicaGroupId.getEditor().getEditorComponent();
	replicaGroupIdTextField.getDocument().addDocumentListener(_updateListener);
	_replicaGroupId.setToolTipText("Select a replica group");

	JTextField publishedEndpointsTextField = (JTextField)
	    _publishedEndpoints.getEditor().getEditorComponent();
	publishedEndpointsTextField.getDocument().addDocumentListener(_updateListener);
	_publishedEndpoints.setToolTipText(
	    "<html>Direct adapter: endpoints included in proxies created using this adapter.<br>"
	    + "Indirect adapter: endpoints registered with the IceGrid Registry during the activation of this adapter." 
	    + "</html>");
    }
    
   
    //
    // From Editor:
    //

    void writeDescriptor()
    {
	AdapterDescriptor descriptor = 
	    (AdapterDescriptor)getAdapter().getDescriptor();
	descriptor.name = _name.getText();
	descriptor.description = _description.getText();
	descriptor.id = getIdAsString();
	descriptor.replicaGroupId = getReplicaGroupIdAsString();
	descriptor.registerProcess = _registerProcess.isSelected();
	descriptor.waitForActivation = _waitForActivation.isSelected();
	descriptor.objects = mapToObjectDescriptorSeq(_objectsMap);
    }	    
    
    boolean isSimpleUpdate()
    {
	AdapterDescriptor descriptor = 
	    (AdapterDescriptor)getAdapter().getDescriptor();

	//
	// When id change, we need to re-register for updates
	//
	return descriptor.name.equals(_name.getText()) 
	    && descriptor.id.equals(getIdAsString());
    }

    void appendCurrentStatus(DefaultFormBuilder builder)
    {
	builder.append("Status" );
	builder.append(_currentStatus, 3);
	builder.nextLine();

	builder.append("Published Endpoints" );
	builder.append(_currentEndpoints, 3);
	builder.nextLine();
    }

    void appendProperties(DefaultFormBuilder builder)
    {
	builder.append("Adapter Name" );
	builder.append(_name, 3);
	builder.nextLine();
	
	builder.append("Description");
	builder.nextLine();
	builder.append("");
	builder.nextRow(-2);
	CellConstraints cc = new CellConstraints();
	JScrollPane scrollPane = new JScrollPane(_description);
	builder.add(scrollPane, 
		    cc.xywh(builder.getColumn(), builder.getRow(), 3, 3));
	builder.nextRow(2);
	builder.nextLine();

	builder.append("Adapter ID");
	builder.append(_id, 3);
	builder.nextLine();
	
	builder.append("Replica Group", _replicaGroupId);
	builder.append(_replicaGroupButton);
	builder.nextLine();

	builder.append("Registered Objects");
	builder.append(_objects, _objectsButton);
	builder.nextLine();
	
	builder.append("", _registerProcess);
	builder.nextLine();
	builder.append("", _waitForActivation);
	builder.nextLine();

	builder.append("Endpoints" );
	builder.append(_endpoints, 3);
	builder.nextLine();

	builder.append("Published Endpoints" );
	builder.append(_publishedEndpoints, 3);
	builder.nextLine();
    }

    void postUpdate()
    {
	//
	// Change enclosing properties after successful update
	//
	String name = _name.getText();
	Adapter adapter = getAdapter();
	if(!name.equals(_oldName))
	{
	    adapter.removeProperty(_oldName, "Endpoints");
	    adapter.removeProperty(_oldName, "PublishedEndpoints");
	}
	
	adapter.setProperty(name, "Endpoints", _endpoints.getText());
	
	Object published = _publishedEndpoints.getSelectedItem();
	if(published == PUBLISH_ACTUAL)
	{
	    adapter.removeProperty(name, "PublishedEndpoints");
	}
	else
	{
	    adapter.setProperty(name, "PublishedEndpoints",
				published.toString());

	}
    }
    
    private void setObjectsField()
    {
	Adapter adapter = getAdapter();

	final Utils.Resolver resolver = adapter.getModel().substitute() ? 
	    adapter.getResolver() : null;
	
	Ice.StringHolder toolTipHolder = new Ice.StringHolder();
	Utils.Stringifier stringifier = new Utils.Stringifier()
	    {
		public String toString(Object obj)
		{
		    java.util.Map.Entry entry = (java.util.Map.Entry)obj;
		    
		    return Utils.substitute((String)entry.getKey(), resolver) 
			+ " as '"
			+ Utils.substitute((String)entry.getValue(), resolver)
			+ "'";
		}
	    };
	
	_objects.setText(
	    Utils.stringify(_objectsMap.entrySet(), stringifier,
			    ", ", toolTipHolder));

	_objects.setToolTipText(toolTipHolder.value);
    }
    
    private void setId(String id)
    {
	if(id.equals(""))
	{
	    _id.setSelectedItem(DIRECT_ADAPTER);
	}
	else if(id.equals(_defaultAdapterId))
	{
	    _id.setSelectedItem(DEFAULT_ADAPTER_ID);
	}
	else
	{
	    _id.setSelectedItem(id);
	}
    }

    private void refreshId()
    {
	Object id = _id.getSelectedItem();
	_id.setModel(new DefaultComboBoxModel(new Object[]
	    {DIRECT_ADAPTER, DEFAULT_ADAPTER_ID}));
	_id.setSelectedItem(id);
    }

    private String getIdAsString()
    {
	Object obj = _id.getSelectedItem();
	if(obj == DIRECT_ADAPTER)
	{
	    return "";
	}
	else 
	{
	    return obj.toString();
	}
    }

    private void setReplicaGroupId(String replicaGroupId)
    {
	if(replicaGroupId.equals(""))
	{
	    _replicaGroupId.setSelectedItem(NOT_REPLICATED);
	}
	else
	{
	    ReplicaGroups replicaGroups =
		getAdapter().getApplication().getReplicaGroups();
	    
	    ReplicaGroup replicaGroup = 
		(ReplicaGroup)replicaGroups.findChild(replicaGroupId);
	    
	    if(replicaGroup != null)
	    {
		_replicaGroupId.setSelectedItem(replicaGroup);
	    }
	    else
	    {
		_replicaGroupId.setSelectedItem(replicaGroupId);
	    }
	}
    }
    
   
    private String getReplicaGroupIdAsString()
    {
	Object obj = _replicaGroupId.getSelectedItem();
	if(obj == NOT_REPLICATED)
	{
	    return "";
	}
	else
	{
	    return obj.toString();
	}
    }

    void show(Adapter adapter)
    {
	detectUpdates(false);
	setTarget(adapter);

	AdapterDescriptor descriptor = (AdapterDescriptor)adapter.getDescriptor();
	
	final Utils.Resolver resolver = adapter.getModel().substitute() ?  
	    adapter.getResolver() : null;

	boolean isEditable = adapter.isEditable() && resolver == null;
	boolean inIceBox = adapter.inIceBox();
	
	_oldName = descriptor.name;

	_name.setText(Utils.substitute(descriptor.name, resolver));
	_name.setEditable(isEditable && !inIceBox);

	_description.setText(
	    Utils.substitute(descriptor.description, resolver));
	_description.setEditable(isEditable);
	_description.setOpaque(isEditable);

	//
	// Need to make control editable & enabled before changing it
	//
	_id.setEnabled(true);
	_id.setEditable(true);	
	_defaultAdapterId = adapter.getDefaultAdapterId();
	refreshId();
	if(descriptor.id == null)
	{
	    descriptor.id = _defaultAdapterId;
	}

	setId(Utils.substitute(descriptor.id, resolver));
	_id.setEnabled(isEditable);
	_id.setEditable(isEditable);

	_replicaGroupId.setEnabled(true);
	_replicaGroupId.setEditable(true);	
	
	final ReplicaGroups replicaGroups =
	    adapter.getApplication().getReplicaGroups();
	_replicaGroupId.setModel(replicaGroups.createComboBoxModel(NOT_REPLICATED));

	_replicaGroupId.addItemListener(new ItemListener()
	    {
		public void itemStateChanged(ItemEvent e)
		{
		    if(e.getStateChange() == ItemEvent.SELECTED)
		    {
			Object item = e.getItem();
			boolean enabled = (item instanceof ReplicaGroup);
			if(!enabled && item != NOT_REPLICATED)
			{
			    if(resolver != null)
			    {
				String replicaGroupId = 
				    resolver.substitute(item.toString());
				enabled = (replicaGroups.findChild(replicaGroupId) != null);
			    }
			}
			_replicaGroupButton.setEnabled(enabled);
		    }
		}
	    });

	setReplicaGroupId(Utils.substitute(descriptor.replicaGroupId, resolver));
	_replicaGroupId.setEnabled(isEditable);
	_replicaGroupId.setEditable(isEditable);

	_endpoints.setText(
	    Utils.substitute(adapter.getProperty("Endpoints"), resolver));
	_endpoints.setEditable(isEditable);
	
	_publishedEndpoints.setEnabled(true);
	_publishedEndpoints.setEditable(true);
	String published = 
	    Utils.substitute(adapter.getProperty("PublishedEndpoints"), resolver);
	if(published == null || published.equals(""))
	{
	    _publishedEndpoints.setSelectedItem(PUBLISH_ACTUAL);
	}
	else
	{
	    _publishedEndpoints.setSelectedItem(published);
	}
	_publishedEndpoints.setEnabled(isEditable);
	_publishedEndpoints.setEditable(isEditable);

	//
	// Objects
	//
	_objectsMap = objectDescriptorSeqToMap(descriptor.objects);
	setObjectsField();
	_objectsButton.setEnabled(isEditable);
	
	_registerProcess.setSelected(descriptor.registerProcess);
	_registerProcess.setEnabled(isEditable);
	
	_waitForActivation.setSelected(descriptor.waitForActivation);
	_waitForActivation.setEnabled(isEditable);

	refreshCurrentStatus();

	_applyButton.setEnabled(adapter.isEphemeral());
	_discardButton.setEnabled(adapter.isEphemeral());	  
	detectUpdates(true);
    }

    public void refreshCurrentStatus()
    {
	Adapter adapter = getAdapter();

	//
	// Only when not in a template
	//
	if(adapter.getResolver() != null)
	{
	    String currentEndpoints = adapter.getCurrentEndpoints();
	    boolean active = (currentEndpoints != null);
	    
	    if(currentEndpoints == null)
	    {
		_currentStatus.setText("Inactive");
		_currentEndpoints.setText("");
	    }
	    else
	    {
		_currentStatus.setText("Active");
		_currentEndpoints.setText(currentEndpoints);
	    }
	}
    }

    Adapter getAdapter()
    {
	return (Adapter)_target;
    }

    static java.util.Map objectDescriptorSeqToMap(java.util.List objects)
    {
	java.util.Map result = new java.util.HashMap(objects.size());
	java.util.Iterator p = objects.iterator();
	while(p.hasNext())
	{
	    ObjectDescriptor od = (ObjectDescriptor)p.next();
	    result.put(Ice.Util.identityToString(od.id), od.type);
	}
	return result;
    }
    
    static java.util.LinkedList mapToObjectDescriptorSeq(java.util.Map map)
    {
	java.util.LinkedList result = new java.util.LinkedList();
	java.util.Iterator p = map.entrySet().iterator();
	while(p.hasNext())
	{
	    java.util.Map.Entry entry = (java.util.Map.Entry)p.next();
	    Ice.Identity id = 
		Ice.Util.stringToIdentity((String)entry.getKey());
	    String type = (String)entry.getValue();
	    result.add(new ObjectDescriptor(id, type));
	}
	return result;
    }

    private String _defaultAdapterId = "";

    private final Object DEFAULT_ADAPTER_ID = new Object()
	{
	    public String toString()
	    {
		return _defaultAdapterId;
	    }
	};
    
    private String _oldName;
    
    private JTextField _name = new JTextField(20);
    private JTextArea _description = new JTextArea(3, 20);

    private JComboBox _id = new JComboBox(new Object[]
	{DIRECT_ADAPTER, DEFAULT_ADAPTER_ID});
    private JComboBox _replicaGroupId = new JComboBox();
    private JButton _replicaGroupButton;

    private JTextField _endpoints = new JTextField(20);
    private JComboBox _publishedEndpoints = new JComboBox(
	new Object[]{PUBLISH_ACTUAL});

    private JTextField _currentStatus = new JTextField(20);
    private JTextField _currentEndpoints = new JTextField(20);

    private JCheckBox _registerProcess;
    private JCheckBox _waitForActivation;

    private JTextField _objects = new JTextField(20);
    private java.util.Map _objectsMap;
    private TableDialog _objectsDialog;
    private JButton _objectsButton;

    static private final Object PUBLISH_ACTUAL = new Object()
	{
	    public String toString()
	    {
		return "Actual endpoints";
	    }
	};

    static private final Object DIRECT_ADAPTER = new Object()
	{
	    public String toString()
	    {
		return "No ID (a direct adapter)";
	    }
	};
    
    static private final Object NOT_REPLICATED = new Object()
	{
	    public String toString()
	    {
		return "Does not belong to a replica group";
	    }
	};
}