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
|
// **********************************************************************
//
// 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.Component;
import javax.swing.Icon;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.DefaultTreeCellRenderer;
import IceGrid.*;
import IceGridGUI.*;
class PlainServer extends Communicator implements Server
{
static public ServerDescriptor
copyDescriptor(ServerDescriptor sd)
{
ServerDescriptor copy = (ServerDescriptor)sd.clone();
copy.adapters = Adapter.copyDescriptors(copy.adapters);
copy.dbEnvs = DbEnv.copyDescriptors(copy.dbEnvs);
copy.propertySet = PropertySet.copyDescriptor(copy.propertySet);
copy.distrib = (DistributionDescriptor)copy.distrib.clone();
if(copy instanceof IceBoxDescriptor)
{
IceBoxDescriptor ib = (IceBoxDescriptor)copy;
ib.services = ServiceInstance.copyDescriptors(ib.services);
}
return copy;
}
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.propertySet = from.propertySet;
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<AdapterDescriptor>(),
new PropertySetDescriptor(new String[0], new java.util.LinkedList<PropertyDescriptor>()),
new java.util.LinkedList<DbEnvDescriptor>(),
new String[0],
"",
"NewServer",
"",
"",
"",
new java.util.LinkedList<String>(),
new java.util.LinkedList<String>(),
"manual",
"",
"",
true,
new DistributionDescriptor("", new java.util.LinkedList<String>()),
false, // Allocatable
"");
}
static public IceBoxDescriptor newIceBoxDescriptor()
{
return new IceBoxDescriptor(
new java.util.LinkedList<AdapterDescriptor>(),
new PropertySetDescriptor(new String[0], new java.util.LinkedList<PropertyDescriptor>()),
new java.util.LinkedList<DbEnvDescriptor>(),
new String[0],
"",
"NewIceBox",
"",
"",
"",
new java.util.LinkedList<String>(),
new java.util.LinkedList<String>(),
"manual",
"",
"",
true,
new DistributionDescriptor("", new java.util.LinkedList<String>()),
false, // Allocatable
"",
new java.util.LinkedList<ServiceInstanceDescriptor>()
);
}
//
// Actions
//
public boolean[] getAvailableActions()
{
boolean[] actions = new boolean[ACTION_COUNT];
Object clipboard = getCoordinator().getClipboard();
if(clipboard != null &&
(clipboard instanceof ServerDescriptor
|| clipboard instanceof ServerInstanceDescriptor
|| (isIceBox() && (clipboard instanceof ServiceInstanceDescriptor))
|| (!isIceBox() && (clipboard instanceof Adapter.AdapterCopy
|| clipboard instanceof DbEnvDescriptor))))
{
actions[PASTE] = true;
}
actions[DELETE] = true;
if(!_ephemeral)
{
actions[COPY] = true;
actions[SHOW_VARS] = true;
actions[SUBSTITUTE_VARS] = true;
actions[NEW_ADAPTER] = !_services.initialized();
actions[NEW_DBENV] = !_services.initialized();
actions[NEW_SERVICE] = _services.initialized();
actions[NEW_SERVICE_FROM_TEMPLATE] = _services.initialized();
}
return actions;
}
public JPopupMenu getPopupMenu()
{
ApplicationActions actions = getCoordinator().getActionsForPopup();
if(_popup == null)
{
_popup = new JPopupMenu();
_popup.add(actions.get(NEW_ADAPTER));
_popup.add(actions.get(NEW_DBENV));
_popup.add(actions.get(NEW_SERVICE));
_popup.add(actions.get(NEW_SERVICE_FROM_TEMPLATE));
}
actions.setTarget(this);
return _popup;
}
public void copy()
{
getCoordinator().setClipboard(copyDescriptor(_descriptor));
getCoordinator().getActionsForMenu().get(PASTE).setEnabled(true);
}
public Editor getEditor()
{
if(_editor == null)
{
_editor = (PlainServerEditor)getRoot().getEditor(PlainServerEditor.class, this);
}
_editor.show(this);
return _editor;
}
protected Editor createEditor()
{
return new PlainServerEditor();
}
public Component getTreeCellRendererComponent(
JTree tree,
Object value,
boolean sel,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus)
{
if(_cellRenderer == null)
{
//
// Initialization
//
_cellRenderer = new DefaultTreeCellRenderer();
_serverIcon = Utils.getIcon("/icons/16x16/server_inactive.png");
_iceboxServerIcon = Utils.getIcon("/icons/16x16/icebox_server_inactive.png");
}
if(expanded)
{
_cellRenderer.setOpenIcon(isIceBox() ? _iceboxServerIcon : _serverIcon);
}
else
{
_cellRenderer.setClosedIcon(isIceBox() ? _iceboxServerIcon : _serverIcon);
}
return _cellRenderer.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
}
public void destroy()
{
Node node = (Node)_parent;
if(_ephemeral)
{
node.removeServer(this);
}
else
{
node.removeDescriptor(_descriptor);
node.removeServer(this);
node.getEditable().removeElement(_id, _editable, Server.class);
getRoot().updated();
}
}
public Object getDescriptor()
{
return _descriptor;
}
public Object saveDescriptor()
{
ServerDescriptor clone = (ServerDescriptor)_descriptor.clone();
clone.distrib = (DistributionDescriptor)clone.distrib.clone();
return clone;
}
public void restoreDescriptor(Object savedDescriptor)
{
shallowRestore((ServerDescriptor)savedDescriptor, _descriptor);
}
//
// Builds the server and all its sub-tree
//
PlainServer(boolean brandNew, TreeNode parent, String serverId, Utils.Resolver resolver,
ServerDescriptor serverDescriptor)
throws UpdateFailedException
{
super(parent, serverId);
_ephemeral = false;
_editable = new Editable(brandNew);
rebuild(resolver, serverDescriptor);
}
PlainServer(TreeNode parent, String serverId, ServerDescriptor serverDescriptor)
{
super(parent, serverId);
_ephemeral = true;
_editable = null;
try
{
rebuild(null, serverDescriptor);
}
catch(UpdateFailedException e)
{
assert false;
}
}
static java.util.List<String[]> createAttributes(ServerDescriptor descriptor)
{
java.util.List<String[]> attributes = new java.util.LinkedList<String[]>();
attributes.add(createAttribute("id", descriptor.id));
if(descriptor.activation.length() > 0)
{
attributes.add(createAttribute("activation", descriptor.activation));
}
if(descriptor.activationTimeout.length() > 0)
{
attributes.add(createAttribute("activation-timeout", descriptor.activationTimeout));
}
if(!descriptor.applicationDistrib)
{
attributes.add(createAttribute("application-distrib", "false"));
}
if(descriptor.deactivationTimeout.length() > 0)
{
attributes.add(createAttribute("deactivation-timeout", descriptor.deactivationTimeout));
}
if(descriptor.exe.length() > 0)
{
attributes.add(createAttribute("exe", descriptor.exe));
}
if(descriptor.iceVersion.length() > 0)
{
attributes.add(createAttribute("ice-version", descriptor.iceVersion));
}
if(descriptor.pwd.length() > 0)
{
attributes.add(createAttribute("pwd", descriptor.pwd));
}
return attributes;
}
static void writeOptions(XMLWriter writer, java.util.List<String> options)
throws java.io.IOException
{
for(String p : options)
{
writer.writeElement("option", p);
}
}
static void writeEnvs(XMLWriter writer, java.util.List<String> envs)
throws java.io.IOException
{
for(String p : envs)
{
writer.writeElement("env", p);
}
}
void write(XMLWriter writer)
throws java.io.IOException
{
if(!_ephemeral)
{
if(isIceBox())
{
writer.writeStartTag("icebox", createAttributes(_descriptor));
if(_descriptor.description.length() > 0)
{
writer.writeElement("description", _descriptor.description);
}
writeOptions(writer, _descriptor.options);
writeEnvs(writer, _descriptor.envs);
writePropertySet(writer, "", "", _descriptor.propertySet, _descriptor.adapters, _descriptor.logs);
writeLogs(writer, _descriptor.logs, _descriptor.propertySet.properties);
writeDistribution(writer, _descriptor.distrib);
_adapters.write(writer, _descriptor.propertySet.properties);
_services.write(writer);
writer.writeEndTag("icebox");
}
else
{
writer.writeStartTag("server", createAttributes(_descriptor));
if(_descriptor.description.length() > 0)
{
writer.writeElement("description", _descriptor.description);
}
writeOptions(writer, _descriptor.options);
writeEnvs(writer, _descriptor.envs);
writePropertySet(writer, _descriptor.propertySet, _descriptor.adapters, _descriptor.logs);
writeLogs(writer, _descriptor.logs, _descriptor.propertySet.properties);
writeDistribution(writer, _descriptor.distrib);
_adapters.write(writer, _descriptor.propertySet.properties);
_dbEnvs.write(writer);
writer.writeEndTag("server");
}
}
}
boolean isIceBox()
{
return _descriptor instanceof IceBoxDescriptor;
}
public Object rebuild(java.util.List<Editable> editables)
throws UpdateFailedException
{
Node node = (Node)_parent;
PlainServer newServer = node.createServer(false, _descriptor);
Object backup = null;
if(_id.equals(newServer.getId()))
{
//
// A simple update. We can't simply rebuild server because
// we need to keep a backup
//
if(_editable.isModified())
{
newServer.getEditable().markModified();
}
node.removeServer(this);
try
{
node.insertServer(newServer, true);
}
catch(UpdateFailedException e)
{
assert false; // impossible, we just removed a child with
// this id
}
}
else
{
//
// Typically, the id uses a variable whose definition changed
//
newServer.getEditable().markNew();
node.removeServer(this);
backup = node.getEditable().save();
node.getEditable().removeElement(_id, _editable, Server.class);
try
{
node.insertServer(newServer, true);
}
catch(UpdateFailedException e)
{
restore(backup);
throw e;
}
}
return backup;
}
public void restore(Object backupObj)
{
Editable backup = (Editable)backupObj;
Node node = (Node)_parent;
if(backup != null)
{
node.getEditable().restore(backup);
}
TreeNode badServer = node.findChildWithDescriptor(_descriptor);
if(badServer != null)
{
node.removeServer(badServer);
}
try
{
node.insertServer(this, true);
}
catch(UpdateFailedException e)
{
assert false; // impossible
}
}
void setServerDescriptor(ServerDescriptor descriptor)
{
_descriptor = descriptor;
}
//
// Update the server and all its subtree
//
void rebuild(Utils.Resolver resolver, ServerDescriptor serverDescriptor)
throws UpdateFailedException
{
assert serverDescriptor != null;
_resolver = resolver;
_descriptor = serverDescriptor;
_adapters.clear();
_dbEnvs.clear();
_services.clear();
if(!_ephemeral)
{
_adapters.init(_descriptor.adapters);
if(isIceBox())
{
IceBoxDescriptor iceBoxDescriptor = (IceBoxDescriptor)_descriptor;
_services.init(iceBoxDescriptor.services);
//
// IceBox has not dbEnv
//
assert _descriptor.dbEnvs.size() == 0;
}
else
{
_dbEnvs.init(_descriptor.dbEnvs);
}
}
}
CommunicatorDescriptor getCommunicatorDescriptor()
{
return _descriptor;
}
Utils.Resolver getResolver()
{
return _resolver;
}
public Editable getEditable()
{
return _editable;
}
Editable getEnclosingEditable()
{
return _editable;
}
public boolean isEphemeral()
{
return _ephemeral;
}
private ServerDescriptor _descriptor;
private final boolean _ephemeral;
private PlainServerEditor _editor;
private Utils.Resolver _resolver;
private Editable _editable;
static private DefaultTreeCellRenderer _cellRenderer;
static private Icon _serverIcon;
static private Icon _iceboxServerIcon;
static private JPopupMenu _popup;
}
|