summaryrefslogtreecommitdiff
path: root/java/src/IceGridGUI/Application/ReplicaGroupEditor.java
blob: f3b2f301fffd50c972ff32130ced510bd8f0d063 (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
// **********************************************************************
//
// Copyright (c) 2003-2009 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 IceGridGUI.Application;

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.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

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

import IceGrid.*;
import IceGridGUI.*;

class ReplicaGroupEditor extends Editor
{
    protected boolean applyUpdate(boolean refresh)
    {
        ReplicaGroup replicaGroup = (ReplicaGroup)_target;
        Root root = replicaGroup.getRoot();

        root.disableSelectionListener();
        try
        {
            if(replicaGroup.isEphemeral())
            {
                ReplicaGroups replicaGroups = (ReplicaGroups)replicaGroup.getParent();
                writeDescriptor();
                ReplicaGroupDescriptor descriptor = (ReplicaGroupDescriptor)replicaGroup.getDescriptor();
                replicaGroup.destroy(); // just removes the child

                try
                {
                    replicaGroups.tryAdd(descriptor, true);
                }
                catch(UpdateFailedException e)
                {
                    //
                    // Add back ephemeral child
                    //
                    try
                    {
                        replicaGroups.insertChild(replicaGroup, true);
                    }
                    catch(UpdateFailedException die)
                    {
                        assert false;
                    }
                    root.setSelectedNode(replicaGroup);

                    JOptionPane.showMessageDialog(
                        root.getCoordinator().getMainFrame(),
                        e.toString(),
                        "Apply failed",
                        JOptionPane.ERROR_MESSAGE);
                    return false;
                }

                //
                // Success
                //
                _target = replicaGroups.findChildWithDescriptor(descriptor);
                root.updated();
            }
            else if(isSimpleUpdate())
            {
                writeDescriptor();
                root.updated();
                replicaGroup.getEditable().markModified();
            }
            else
            {
                //
                // Save to be able to rollback
                //
                Object savedDescriptor = replicaGroup.saveDescriptor();
                ReplicaGroups replicaGroups = (ReplicaGroups)replicaGroup.getParent();
                writeDescriptor();
                ReplicaGroupDescriptor descriptor = (ReplicaGroupDescriptor)replicaGroup.getDescriptor();

                replicaGroups.removeChild(replicaGroup);
                try
                {
                    replicaGroups.tryAdd(descriptor, false);
                }
                catch(UpdateFailedException e)
                {
                    //
                    // Restore all
                    //
                    try
                    {
                        replicaGroups.insertChild(replicaGroup, true);
                    }
                    catch(UpdateFailedException die)
                    {
                        assert false;
                    }
                    replicaGroup.restoreDescriptor(savedDescriptor);
                    root.setSelectedNode(_target);

                    JOptionPane.showMessageDialog(
                        root.getCoordinator().getMainFrame(),
                        e.toString(),
                        "Apply failed",
                        JOptionPane.ERROR_MESSAGE);
                    return false;
                }

                //
                // Success
                //

                // replaced by brand new ReplicaGroup
                replicaGroups.getEditable().
                    removeElement(replicaGroup.getId(), replicaGroup.getEditable(), ReplicaGroup.class);

                _target = replicaGroups.findChildWithDescriptor(descriptor);
                root.updated();

                if(refresh)
                {
                    root.setSelectedNode(_target);
                }
            }

            if(refresh)
            {
                root.getCoordinator().getCurrentTab().showNode(_target);
            }
            _applyButton.setEnabled(false);
            _discardButton.setEnabled(false);
            return true;
        }
        finally
        {
            root.enableSelectionListener();
        }
    }

    Utils.Resolver getDetailResolver()
    {
        Root root = _target.getRoot();

        if(root.getCoordinator().substitute())
        {
            return root.getResolver();
        }
        else
        {
            return null;
        }
    }

    ReplicaGroupEditor()
    {
        _objects = new SimpleMapField(this, true, "Identity", "Type");

        //
        // load balancing
        //
        _loadBalancing.addItemListener(new ItemListener()
            {
                public void itemStateChanged(ItemEvent e)
                {
                    if(e.getStateChange() == ItemEvent.SELECTED)
                    {
                        updated();

                        Object item = e.getItem();
                        _loadSampleLabel.setVisible(item == ADAPTIVE);
                        _loadSample.setVisible(item == ADAPTIVE);
                    }
                }
            });
        _loadBalancing.setToolTipText(
            "<html>Specifies how IceGrid selects adapters and return<br>"
            + "their endpoints when resolving a replica group ID</html>");

        //
        // Associate updateListener with various fields
        //
        _id.getDocument().addDocumentListener(_updateListener);
        _id.setToolTipText("Must be unique within this IceGrid deployment");

        _description.getDocument().addDocumentListener(_updateListener);
        _description.setToolTipText("An optional description for this replica group");

        _nReplicas.getDocument().addDocumentListener(_updateListener);
        _nReplicas.setToolTipText("<html>IceGrid returns the endpoints of "
                                  + "up to <i>number</i> adapters<br>"
                                  + "when resolving a replica group ID.<br>"
                                  + "Enter 0 to returns the endpoints of all adapters.</html>");

        _loadSample.setEditable(true);
        JTextField loadSampleTextField = (JTextField)_loadSample.getEditor().getEditorComponent();
        loadSampleTextField.getDocument().addDocumentListener(_updateListener);
        _loadSample.setToolTipText("Use the load average or CPU usage over the last 1, 5 or 15 minutes?");
    }

    void writeDescriptor()
    {
        ReplicaGroupDescriptor descriptor = (ReplicaGroupDescriptor)getReplicaGroup().getDescriptor();

        descriptor.id = _id.getText().trim();
        descriptor.description = _description.getText();
        descriptor.objects = _objectList;

        Object loadBalancing = _loadBalancing.getSelectedItem();
        if(loadBalancing == ORDERED)
        {
            descriptor.loadBalancing =  new OrderedLoadBalancingPolicy(_nReplicas.getText().trim());
        }
        else if(loadBalancing == RANDOM)
        {
            descriptor.loadBalancing = new RandomLoadBalancingPolicy(_nReplicas.getText().trim());
        }
        else if(loadBalancing == ROUND_ROBIN)
        {
            descriptor.loadBalancing = new RoundRobinLoadBalancingPolicy(_nReplicas.getText().trim());
        }
        else if(loadBalancing == ADAPTIVE)
        {
            descriptor.loadBalancing = new AdaptiveLoadBalancingPolicy(
                _nReplicas.getText().trim(), _loadSample.getSelectedItem().toString().trim());
        }
        else
        {
            assert false;
        }
    }

    boolean isSimpleUpdate()
    {
        ReplicaGroupDescriptor descriptor = (ReplicaGroupDescriptor)getReplicaGroup().getDescriptor();
        return descriptor.id.equals(_id.getText().trim());
    }

    protected void appendProperties(DefaultFormBuilder builder)
    {
        builder.append("Replica Group ID");
        builder.append(_id, 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("Well-known Objects");
        builder.nextLine();
        builder.append("");
        builder.nextLine();
        builder.append("");
        builder.nextLine();
        builder.append("");
        builder.nextRow(-6);
        scrollPane = new JScrollPane(_objects);
        builder.add(scrollPane, cc.xywh(builder.getColumn(), builder.getRow(), 3, 7));
        builder.nextRow(6);
        builder.nextLine();

        builder.append("Load Balancing Policy");
        builder.append(_loadBalancing, 3);
        builder.nextLine();
        builder.append("How many Adapters? (0 = all)");
        builder.append(_nReplicas, 3);
        builder.nextLine();
        _loadSampleLabel = builder.append("Load Sample");
        builder.append(_loadSample, 3);
        builder.nextLine();
    }

    protected void buildPropertiesPanel()
    {
        super.buildPropertiesPanel();
        _propertiesPanel.setName("Replica Group Properties");
    }

    protected boolean validate()
    {
        //
        // First validate stringified identities
        //
        _objectList = mapToObjectDescriptorSeq(_objects.get());

        if(_objectList == null)
        {
            return false;
        }

        return check(new String[]{"Replica Group ID", _id.getText().trim()});
    }

    void show(ReplicaGroup replicaGroup)
    {
        //
        // Make sure everything is built
        //
        getProperties();

        detectUpdates(false);
        _target = replicaGroup;

        Utils.Resolver resolver = getDetailResolver();
        boolean isEditable = (resolver == null);

        ReplicaGroupDescriptor descriptor = (ReplicaGroupDescriptor)replicaGroup.getDescriptor();

        _id.setText(descriptor.id);
        _id.setEditable(isEditable);

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

        _objects.set(objectDescriptorSeqToMap(descriptor.objects), resolver, isEditable);

        _loadBalancing.setEnabled(true);

        if(descriptor.loadBalancing == null)
        {
            _loadBalancing.setSelectedItem(RANDOM);
            _nReplicas.setText("0");
            _loadSample.setSelectedItem("1");
        }
        else if(descriptor.loadBalancing instanceof RandomLoadBalancingPolicy)
        {
            _loadBalancing.setSelectedItem(RANDOM);
            _nReplicas.setText(Utils.substitute(descriptor.loadBalancing.nReplicas, resolver));
            _loadSample.setSelectedItem("1");
        }
        else if(descriptor.loadBalancing instanceof OrderedLoadBalancingPolicy)
        {
            _loadBalancing.setSelectedItem(ORDERED);
            _nReplicas.setText(Utils.substitute(descriptor.loadBalancing.nReplicas, resolver));
            _loadSample.setSelectedItem("1");
        }
        else if(descriptor.loadBalancing instanceof RoundRobinLoadBalancingPolicy)
        {
            _loadBalancing.setSelectedItem(ROUND_ROBIN);
            _nReplicas.setText(Utils.substitute(descriptor.loadBalancing.nReplicas, resolver));
            _loadSample.setSelectedItem("1");
        }
        else if(descriptor.loadBalancing instanceof AdaptiveLoadBalancingPolicy)
        {
            _loadBalancing.setSelectedItem(ADAPTIVE);
            _nReplicas.setText(Utils.substitute(descriptor.loadBalancing.nReplicas, resolver));

            _loadSample.setSelectedItem(
                Utils.substitute(((AdaptiveLoadBalancingPolicy)descriptor.loadBalancing).loadSample, resolver));
        }
        else
        {
            assert false;
        }
        _nReplicas.setEditable(isEditable);
        _loadSample.setEditable(isEditable);
        _loadBalancing.setEnabled(isEditable);

        _applyButton.setEnabled(replicaGroup.isEphemeral());
        _discardButton.setEnabled(replicaGroup.isEphemeral());
        detectUpdates(true);
        if(replicaGroup.isEphemeral())
        {
            updated();
        }
    }

    private ReplicaGroup getReplicaGroup()
    {
        return (ReplicaGroup)_target;
    }

    private java.util.Map<String, String> objectDescriptorSeqToMap(java.util.List<ObjectDescriptor> objects)
    {
        java.util.Map<String, String> result = new java.util.TreeMap<String, String>();
        for(ObjectDescriptor p : objects)
        {
            result.put(Ice.Util.identityToString(p.id), p.type);
        }
        return result;
    }

    private java.util.LinkedList<ObjectDescriptor> mapToObjectDescriptorSeq(java.util.Map<String, String> map)
    {
        String badIdentities = "";
        java.util.LinkedList<ObjectDescriptor> result = new java.util.LinkedList<ObjectDescriptor>();

        for(java.util.Map.Entry<String, String> p : map.entrySet())
        {
            try
            {
                Ice.Identity id = Ice.Util.stringToIdentity(p.getKey());
                String type = p.getValue();
                result.add(new ObjectDescriptor(id, type));
            }
            catch(Ice.IdentityParseException ex)
            {
                badIdentities += "- " + p.getKey() + "\n";
            }
        }
        if(!badIdentities.equals(""))
        {
            JOptionPane.showMessageDialog(
                _target.getCoordinator().getMainFrame(),
                "The following identities could not be parsed properly:\n" + badIdentities,
                "Validation failed",
                JOptionPane.ERROR_MESSAGE);

            return null;
        }
        else
        {
            return result;
        }
    }

    static private String ORDERED = "Ordered";
    static private String RANDOM = "Random";
    static private String ROUND_ROBIN = "Round-robin";
    static private String ADAPTIVE = "Adaptive";

    private JTextField _id = new JTextField(20);
    private JTextArea _description = new JTextArea(3, 20);

    private JComboBox _loadBalancing = new JComboBox(new Object[] {ADAPTIVE, ORDERED, RANDOM, ROUND_ROBIN});

    private JTextField _nReplicas = new JTextField(20);

    private JLabel _loadSampleLabel;
    private JComboBox _loadSample = new JComboBox(new Object[] {"1", "5", "15"});

    private SimpleMapField _objects;
    private java.util.LinkedList<ObjectDescriptor> _objectList;
}