~ubuntu-branches/ubuntu/utopic/389-admin-console/utopic

« back to all changes in this revision

Viewing changes to src/com/netscape/management/admserv/AdminServer.java

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2011-10-12 11:05:09 UTC
  • Revision ID: package-import@ubuntu.com-20111012110509-ybd1jr5xeat2ug1i
Tags: upstream-1.1.8
ImportĀ upstreamĀ versionĀ 1.1.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** BEGIN COPYRIGHT BLOCK
 
2
 * Copyright (C) 2001 Sun Microsystems, Inc.  Used by permission.
 
3
 * Copyright (C) 2005 Red Hat, Inc.
 
4
 * All rights reserved.
 
5
 * 
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * as published by the Free Software Foundation; version 2
 
9
 * of the License.
 
10
 * 
 
11
 * This program 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
 
14
 * GNU General Public License for more details.
 
15
 * 
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
19
 * 
 
20
 * END COPYRIGHT BLOCK **/
 
21
package com.netscape.management.admserv;
 
22
 
 
23
import java.util.*;
 
24
import java.awt.*;
 
25
import java.awt.event.*;
 
26
import java.net.*;
 
27
import java.io.*;
 
28
import javax.swing.*;
 
29
import com.netscape.management.client.*;
 
30
import com.netscape.management.client.console.*;
 
31
import com.netscape.management.admserv.panel.IRestartControl;
 
32
import com.netscape.management.admserv.config.BaseConfigPanel;
 
33
import com.netscape.management.client.topology.*;
 
34
import com.netscape.management.client.util.*;
 
35
import com.netscape.management.nmclf.*;
 
36
import com.netscape.management.admserv.panel.*;
 
37
import netscape.ldap.*;
 
38
 
 
39
/**
 
40
 * Netscape admin server configuration entry point. The directory
 
41
 * server needs to contain the name of this class in order for the topology
 
42
 * view to load this class.
 
43
 */
 
44
public class AdminServer extends AbstractServerObject implements SuiConstants {
 
45
    public static ResourceSet _resource = new ResourceSet("com.netscape.management.admserv.admserv");
 
46
 
 
47
    ConsoleInfo _consoleInfo;
 
48
    int _serverStatus;
 
49
    private int adminUrlNodeDataIndex = -1;
 
50
 
 
51
    /**
 
52
    * Implements the IServerObject interface. Initializes the page with
 
53
    * the global information.
 
54
    *
 
55
    * @param info - global information.
 
56
    */
 
57
    public void initialize(ConsoleInfo info) {
 
58
        super.initialize(info);
 
59
        _consoleInfo = info;
 
60
        setIcon(new RemoteImage(_resource.getString("admin", "smallIcon")));
 
61
        _canRestartFromConsole = !info.getAdminURL().startsWith("https");
 
62
    }
 
63
 
 
64
    /**
 
65
       * Restart activator is used as a repository for various restart control parameters.
 
66
       * The reference to the activator is stored in the ConsoleInfo. panel/RestartOperation.java
 
67
       * is reading the restart parameters which are set by config panels and operations.
 
68
      */
 
69
    protected boolean _needsAutoRestart = false;
 
70
    protected boolean _canRestartFromConsole; // set in initialize()
 
71
    protected String _postRestartURL = null;
 
72
    protected boolean _isShutdown = false;
 
73
 
 
74
    private IRestartControl _restartControl = new IRestartControl() {
 
75
                public void setNeedsAutoRestart(boolean flag) {
 
76
                    _needsAutoRestart = flag;
 
77
                }
 
78
                public boolean needsAutoRestart() {
 
79
                    return _needsAutoRestart;
 
80
                }
 
81
 
 
82
                public void setCanRestartFromConsole(boolean flag) {
 
83
                    _canRestartFromConsole = flag;
 
84
                }
 
85
                public boolean canRestartFromConsole() {
 
86
                    return _canRestartFromConsole;
 
87
                }
 
88
 
 
89
                public void setPostRestartURL(String url) {
 
90
                    _postRestartURL = url;
 
91
                }
 
92
 
 
93
                public String getPostRestartURL() {
 
94
                    return _postRestartURL;
 
95
                }
 
96
 
 
97
                public void setServerShutdown(boolean flag) {
 
98
                    _isShutdown = flag;
 
99
                }
 
100
                public boolean isServerShutdown() {
 
101
                    return _isShutdown;
 
102
                }
 
103
 
 
104
 
 
105
            };
 
106
 
 
107
    /* TODO move to AdminTaskPage/ResourcePage/FrameworkInitializer
 
108
        IFrameworkListener _closeListener = new IFrameworkListener() {
 
109
            public void pageSelectionChanged(FrameworkEvent e) {}
 
110
 
 
111
            public void actionViewClosing(FrameworkEvent e) throws CloseVetoException {
 
112
                Debug.println("window closing, needs restart =" + _needsAutoRestart);
 
113
                if (_restartControl.needsAutoRestart()) {
 
114
                    SuiOptionPane.showMessageDialog(_frame,
 
115
                        _resource.getString("restart","mustRestart"),
 
116
                        _resource.getString("restart","title"),
 
117
                        SuiOptionPane.INFORMATION_MESSAGE);
 
118
 
 
119
                                AdminOperation operation = new RestartOperation(_consoleInfo);
 
120
                                operation.monitorOperation("");
 
121
                                String title = _resource.getString("taskName","restart");
 
122
                                DialogFrame dialog = new DialogFrame(_frame, title, operation.getPanel());
 
123
                                dialog.setVisible(true);
 
124
                                dialog.dispose();
 
125
                }
 
126
            }
 
127
        };
 
128
    */
 
129
 
 
130
    private boolean run(IPage viewInstance) {
 
131
        // do not run if the server is down
 
132
        if (getServerStatus() == IServerObject.STATUS_STOPPED) {
 
133
            Frame frame = viewInstance.getFramework().getJFrame();
 
134
            String title = _resource.getString("error","OpenServerTitle");
 
135
            String msg = java.text.MessageFormat.format(
 
136
                    _resource.getString("error","CanNotOpenServer"),
 
137
                    new Object[]{ getName()});
 
138
            BaseConfigPanel.clearProgressIndicator(
 
139
                    (Component) viewInstance);
 
140
            SuiOptionPane.showMessageDialog(frame, msg, title,
 
141
                    SuiOptionPane.ERROR_MESSAGE);
 
142
            return false;
 
143
        }
 
144
 
 
145
        // Augment the console info with the server name
 
146
        _consoleInfo.put("SIE", getSIE());
 
147
        _consoleInfo.put("SIE_VERSION", getProductVersion());        
 
148
        _consoleInfo.put("SERVER_NAME", getName());
 
149
        _consoleInfo.put("HOST_NAME", getHostName());
 
150
        _consoleInfo.put(IRestartControl.ID, _restartControl);
 
151
 
 
152
        createFramework();
 
153
 
 
154
        return true;
 
155
    }
 
156
 
 
157
    /**
 
158
      * This function is called when the admin server is double clicked on the topology view.
 
159
      */
 
160
    public boolean run(IPage viewInstance,
 
161
            IResourceObject selectionList[]) {
 
162
        // create new frame
 
163
        if (selectionList.length == 1) {
 
164
            return run(viewInstance);
 
165
        }
 
166
 
 
167
        // Do not run if the server is down
 
168
        if (getServerStatus() == IServerObject.STATUS_STOPPED) {
 
169
            Frame frame = viewInstance.getFramework().getJFrame();
 
170
            String title = _resource.getString("error","OpenServerTitle");
 
171
            String msg = java.text.MessageFormat.format(
 
172
                    _resource.getString("error","CanNotOpenServer"),
 
173
                    new Object[]{ getName()});
 
174
            BaseConfigPanel.clearProgressIndicator(
 
175
                    (Component) viewInstance);
 
176
            SuiOptionPane.showMessageDialog(frame, msg, title,
 
177
                    SuiOptionPane.ERROR_MESSAGE);
 
178
            return false;
 
179
        }
 
180
 
 
181
        for (int i = 0; i < selectionList.length; i++) {
 
182
            if (!(selectionList[i] instanceof ServerNode)) {
 
183
                Debug.println("can't run, selection is " +
 
184
                        selectionList[i].getClass().getName());
 
185
                return false;
 
186
            }
 
187
            ServerNode node = (ServerNode) selectionList[i];
 
188
            if (!(node.getServerObject() instanceof AdminServer)) {
 
189
                Debug.println("can't run, server is " +
 
190
                        node.getServerObject().getClass().getName());
 
191
                return false;
 
192
            }
 
193
 
 
194
        }
 
195
 
 
196
        createFramework();
 
197
 
 
198
        return true;
 
199
    }
 
200
 
 
201
 
 
202
    protected void createFramework() {
 
203
        Framework frame = new Framework(
 
204
                new AdminFrameworkInitializer(_consoleInfo));
 
205
        StatusItemSecureMode _statusSecureMode =
 
206
                new StatusItemSecureMode(Framework.STATUS_SECURE_MODE);
 
207
        _statusSecureMode.setSecureMode(
 
208
                _consoleInfo.getAdminURL().startsWith("https"));
 
209
        frame.addStatusItem(_statusSecureMode, IStatusItem.LEFTFIRST);
 
210
        _statusSecureMode.setToolTipText(_consoleInfo.getAdminURL());
 
211
    }
 
212
 
 
213
 
 
214
    // return the host in which this server is installed
 
215
    protected String getHostName() {
 
216
        return (String)_nodeDataTable.get("serverHostName");
 
217
    }
 
218
 
 
219
    // return SIE name "admin-serv-<host>"
 
220
    protected String getSIE() {
 
221
        return (String)_nodeDataTable.get("cn");
 
222
    }
 
223
 
 
224
    // return the Server version number e.g. "4.1"
 
225
    protected String getProductVersion() {
 
226
        return (String)_nodeDataTable.get("nsProductVersion");
 
227
    }
 
228
 
 
229
    public ConsoleInfo getConsoleInfo() {
 
230
        return _consoleInfo;
 
231
    }
 
232
 
 
233
 
 
234
    private URL _serverURL = null;
 
235
    private URLConnection _serverConnection = null;
 
236
 
 
237
    /**
 
238
     * Implements the IServerObject interface.
 
239
     */
 
240
    public synchronized int getServerStatus() {
 
241
        // for admin server, just check whether connections are accepted  on the URL;
 
242
        // if the URL does not respond, then it is down. Otherwise it is up.
 
243
 
 
244
        // In case the method is called before AdminServer in initialized
 
245
        if (_consoleInfo == null) {
 
246
            return STATUS_UNKNOWN;
 
247
        }
 
248
 
 
249
 
 
250
        try {
 
251
            String adminURL = _consoleInfo.getAdminURL() + "admin-serv/tasks/operation/StatusPing";
 
252
            AdmTask task = new AdmTask(new URL(adminURL),
 
253
                    _consoleInfo.getAuthenticationDN(),
 
254
                    _consoleInfo.getAuthenticationPassword());
 
255
            task.exec();
 
256
            int status = task.getStatus();
 
257
            if (status == 0) {
 
258
                _serverStatus = STATUS_STARTED;
 
259
            } else if (task.getException() != null && task.getException()
 
260
                    instanceof java.net.SocketException) {
 
261
                _serverStatus = STATUS_STOPPED;
 
262
            } else {
 
263
                _serverStatus = STATUS_UNKNOWN;
 
264
            }
 
265
            return _serverStatus;
 
266
        } catch (MalformedURLException ex) {
 
267
            Debug.println("ERROR AdminConfigData.isRunning: bad URL " +
 
268
                    _consoleInfo.getAdminURL());
 
269
            return _serverStatus = STATUS_UNKNOWN;
 
270
        }
 
271
 
 
272
 
 
273
    }
 
274
 
 
275
    /**
 
276
      * Implements the IServerObject interface. This will launch the
 
277
      * necessary CGI to clone the configuration of the reference server
 
278
      * to this server. Only the following attributes are cloned:
 
279
      * <ul>
 
280
      * <li>nsadminaccessaddresses
 
281
      * <li>nsadminaccesshosts
 
282
      * <li>nsadmincachelifetime
 
283
      * <li>nsadminenabledsgw
 
284
      * <li>nsadminenableenduser
 
285
      * <li>nsadminoneacldir
 
286
      * <li>nsdefaultacceptlanguage
 
287
      * </ul>
 
288
      *
 
289
      * @param  referenceDN - DN of server to clone from.
 
290
      */
 
291
    public void cloneFrom(String referenceDN) {
 
292
        String configDN = "cn=configuration," + referenceDN;
 
293
        String configURL = null;
 
294
        try {
 
295
            // First, get the configuration information to clone using the referenceDN.
 
296
            LDAPConnection ldc = _consoleInfo.getLDAPConnection();
 
297
            if (ldc == null) {
 
298
                ldc = new LDAPConnection();
 
299
            }
 
300
            if (ldc.isConnected() == false) {
 
301
                ldc.connect(_consoleInfo.getHost(), _consoleInfo.getPort(),
 
302
                        _consoleInfo.getAuthenticationDN(),
 
303
                        _consoleInfo.getAuthenticationPassword());
 
304
            }
 
305
            LDAPEntry entry = ldc.read(configDN);
 
306
            String addresses = LDAPUtil.flatting(
 
307
                    entry.getAttribute("nsadminaccessaddresses",
 
308
                    LDAPUtil.getLDAPAttributeLocale()));
 
309
            String hosts = LDAPUtil.flatting(
 
310
                    entry.getAttribute("nsadminaccesshosts",
 
311
                    LDAPUtil.getLDAPAttributeLocale()));
 
312
            String cache = LDAPUtil.flatting(
 
313
                    entry.getAttribute("nsadmincachelifetime",
 
314
                    LDAPUtil.getLDAPAttributeLocale()));
 
315
            String dsgw = LDAPUtil.flatting(
 
316
                    entry.getAttribute("nsadminenabledsgw",
 
317
                    LDAPUtil.getLDAPAttributeLocale()));
 
318
            String endUser = LDAPUtil.flatting(
 
319
                    entry.getAttribute("nsadminenableenduser",
 
320
                    LDAPUtil.getLDAPAttributeLocale()));
 
321
            String oneAclDir = LDAPUtil.flatting(
 
322
                    entry.getAttribute("nsadminoneacldir",
 
323
                    LDAPUtil.getLDAPAttributeLocale()));
 
324
            String defaultAccLang = LDAPUtil.flatting(
 
325
                    entry.getAttribute("nsdefaultacceptlanguage",
 
326
                    LDAPUtil.getLDAPAttributeLocale()));
 
327
            boolean enableDSGW;
 
328
            boolean enableEndUser;
 
329
            if (dsgw != null && dsgw.equalsIgnoreCase("off")) {
 
330
                enableDSGW = false;
 
331
            } else {
 
332
                enableDSGW = true;
 
333
            }
 
334
            if (endUser != null && endUser.equalsIgnoreCase("off")) {
 
335
                enableEndUser = false;
 
336
            } else {
 
337
                enableEndUser = true;
 
338
            }
 
339
 
 
340
            // Invoke CGI to configure the server with the cloned configuration information.
 
341
            // Some attributes, such as nsadminenableenduser, must be forced set.
 
342
            configURL =
 
343
                    new String(_consoleInfo.getAdminURL() + "admin-serv/tasks/Configuration/ServerSetup?op=force_set" +
 
344
                    "&configuration.nsadminaccessaddresses=" +
 
345
                    addresses + "&configuration.nsadminaccesshosts=" +
 
346
                    hosts + "&configuration.nsadmincachelifetime=" +
 
347
                    cache + "&configuration.nsadminoneacldir=" + oneAclDir +
 
348
                    "&configuration.nsdefaultacceptlanguage=" +
 
349
                    defaultAccLang);
 
350
            if (enableDSGW == true) {
 
351
                configURL = configURL + "&configuration.nsadminenabledsgw=";
 
352
            } else {
 
353
                configURL = configURL + "&configuration.nsadminenabledsgw=off";
 
354
            }
 
355
            if (enableEndUser == true) {
 
356
                configURL = configURL + "&configuration.nsadminenableenduser=";
 
357
            } else {
 
358
                configURL = configURL + "&configuration.nsadminenableenduser=off";
 
359
            }
 
360
            Debug.println("TRACE AdminServer.cloneFrom: configURL = " +
 
361
                    configURL);
 
362
 
 
363
            // Need to make this another thread. Display a dialog to show progress!
 
364
            URL configAdminURL = new URL(configURL);
 
365
            // Is authenticating with the ConsoleInfo correct???
 
366
            AdmTask task = new AdmTask(configAdminURL,
 
367
                    _consoleInfo.getAuthenticationDN(),
 
368
                    _consoleInfo.getAuthenticationPassword());
 
369
            int execStatus = task.exec();
 
370
            int taskStatus = task.getStatus();
 
371
            String title = _resource.getString("status","cloneTitle");
 
372
            if (execStatus == 0 && taskStatus == 0) {
 
373
                Debug.println("TRACE AdminServer.cloneFrom: config CGI succeeded!");
 
374
                String msg = java.text.MessageFormat.format(
 
375
                        _resource.getString("status",
 
376
                        "cloneSucceeded"), new Object[]{ getName()});
 
377
                SuiOptionPane.showMessageDialog(null, msg, title,
 
378
                        SuiOptionPane.INFORMATION_MESSAGE);
 
379
            } else {
 
380
                // Either the exec or the task failed.
 
381
                Debug.println(
 
382
                        "ERROR AdminServer.cloneFrom: config CGI status = " +
 
383
                        (execStatus == 0 ? taskStatus : execStatus));
 
384
                String msg = java.text.MessageFormat.format(
 
385
                        _resource.getString("status", "cloneFailed"),
 
386
                        new Object[]{ getName()});
 
387
                SuiOptionPane.showMessageDialog(null, msg, title,
 
388
                        SuiOptionPane.ERROR_MESSAGE);
 
389
            }
 
390
        }
 
391
        catch (LDAPException e) {
 
392
            // TODO: Use SuiOptionPane error message
 
393
            Debug.println(
 
394
                    "ERROR AdminServer.cloneFrom: LDAP read failed: " +
 
395
                    configDN);
 
396
        }
 
397
        catch (MalformedURLException e) {
 
398
            // TODO: Use SuiOptionPane error message
 
399
            Debug.println("ERROR AdminServer.cloneFrom: Bad URL: " +
 
400
                    configURL);
 
401
        }
 
402
    }
 
403
 
 
404
    /**
 
405
      * Number of entries for this node.
 
406
      * Implements INodeInfo
 
407
      *
 
408
      * @return number of entries
 
409
      */
 
410
    public int getNodeDataCount() 
 
411
    {
 
412
        adminUrlNodeDataIndex = super.getNodeDataCount();
 
413
        return adminUrlNodeDataIndex + 1;
 
414
    }
 
415
 
 
416
    public boolean isCloningEnabled()
 
417
    {
 
418
        return false;
 
419
    }
 
420
 
 
421
    /**
 
422
      * Return node entry at specified index.
 
423
      * Implements INodeInfo
 
424
      *
 
425
      * @return the specified node data
 
426
      */
 
427
    public NodeData getNodeData(int index) {
 
428
        if(index == adminUrlNodeDataIndex)
 
429
        {
 
430
            String port = null;  // legal value, if null, this NodeData will not appear in panel
 
431
            String url = _consoleInfo.getAdminURL();
 
432
            int portIndex = url.lastIndexOf(':');
 
433
            if(portIndex != -1)
 
434
                port = url.substring(portIndex+1).replace('/', ' ').trim();
 
435
            return new NodeData("", _resource.getString("infopanel", "Port"), port, false);
 
436
        }
 
437
        else
 
438
        {
 
439
            return super.getNodeData(index);
 
440
        }
 
441
    }
 
442
}