summaryrefslogtreecommitdiffstats
path: root/base/console/src/com/netscape/admin/certsrv/CMSAdmin.java
blob: 94bb266d13ad76c3fb5790c81011abe6aaf008d4 (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
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.admin.certsrv;

import java.util.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.netscape.certsrv.common.*;
import com.netscape.management.client.*;
import com.netscape.management.client.topology.*;
import com.netscape.management.client.console.*;
import com.netscape.management.client.util.*;
import com.netscape.admin.certsrv.config.install.*;
import com.netscape.admin.certsrv.task.*;
import com.netscape.admin.certsrv.wizard.*;
import netscape.ldap.*;

/**
 * Netscape Certificate Server 4.0 configuration entry point. The
 * directory server needs to contain the name of this class in order
 * for the topology view to load this class.
 *
 * @author Jack Pan-Chen
 * @author Thomas Kwan
 * @version $Revision$, $Date$
 * @date        01/12/97
 */
public class CMSAdmin extends AbstractServerObject
    implements IWizardDone, IRemovableServerObject, IMenuInfo
{

    /*==========================================================
     * variables
     *==========================================================*/
    private static final String PREFIX = "CMSADMIN";
    private static final String START = "start";
    private static final String STOP = "stop";
    private static final String CONFIGURE = "configure";
//    private static final String START_DAEMON_CGI = "Tasks/Operation/StartDaemon";

    private ConsoleInfo mConsoleInfo;       // global information
    private CMSServerInfo mServerInfo;      // server-specific information
    private ConsoleInfo mServerInstanceInfo;
    private CMSServerInfo mStatusInfo;      // server-specific information
    private CMSUIFramework  mFramework;     // parent frame
    private CMSPageFeeder mPagefeeder;      // what generates tab views
    //private CMSInfoPanel mInfoPanel;        // information panel
    private RemoteImage mIconImage = null;  // server icon
    private String mServerID, mServerVersion, mInstallationDate, mServerRoot;
    private String mHost = null;            // server name
    private int mPort = 0;                  // server port
    private String mAdminURL = null;        // admin server url
    private int mServerStatus = STATUS_UNKNOWN;
    private StatusItemText mAuthid;
    private JFrame mActiveFrame;

    protected ResourceBundle mResource;   //resource boundle

    /*==========================================================
     * constructors
     *==========================================================*/
    public CMSAdmin()
    {
        //Debug.setTrace(true);
        if (mActiveFrame == null)
            mActiveFrame = UtilConsoleGlobals.getActivatedFrame();
        mResource = ResourceBundle.getBundle(CMSAdminResources.class.getName());
		// STATUS_UPDATE_INTERVAL = 1500000;
    }

    /*==========================================================
     * public methods
     *==========================================================*/

    public int getServerLastKnownStatus() {
        return mServerStatus;
    }

    /**
     *  Initialize the page with global information.
     *
     * @param info  global information.
     */
    public void initialize(ConsoleInfo info) {
        mConsoleInfo = info;
        mIconImage = CMSAdminUtil.getImage(CMSAdminResources.IMAGE_CERTICON_SMALL);
        getInfo( info.getCurrentDN());
        //getServerStatus();
        if (info.getCurrentDN() == null) {
          Debug.println( "initialized CMSAdmin (Standalone mode)");
        } else {
          super.initialize( info );
        }
        Debug.println( "initialized CMSAdmin for " + mConsoleInfo.getCurrentDN() );
    }


    /**
     * overwrite the base class to add admin port number
     * Not sure we want to do this.
     *
    protected Vector initializeNodeDataVector(String dataKeys[]) {
        Vector v = super.initializeNodeDataVector(dataKeys);
        if ( mServerInfo != null ) {
            int port = mServerInfo.getPort();
            if ( port > 0 ) {
                String label = mResource.getString(PREFIX+"_ADMINPORT_LABEL");
                v.addElement(new NodeData("nsServerPort",
                                          label,
                                          Integer.toString(port)));
            }
        }
        return v;
    }
     */

    /**
     *  return the server instance name instead for now
     *
     * @return return the server name
     *
    */
    public String getName() {
        return ("Certificate Server ("+ mServerID +")");
    }


    /**
     * Return the information panel. - Admin take over this already
     *
     * @return information panel
     *
    public Component getCustomPanel() {
        if(mInfoPanel == null)
            mInfoPanel = new CMSInfoPanel( (IServerObject)this,
                                        mHost,
                                        mPort,
                                        mServerVersion,
                                        mInstallationDate,
                                        mAdminURL);
        return mInfoPanel;
    }
    */

    /**
     * Return connection info for a server instance.
     * @return Connection info for a server instance.
     */
    public CMSServerInfo getServerInfo() {
        return mServerInfo;
    }

    public ConsoleInfo getServerInstanceInfo() {
        mServerInstanceInfo = (ConsoleInfo)mConsoleInfo.clone();
        return mServerInstanceInfo;
    }

    /**
     * Returns the global console info.
     *
     * @return Global console info reference.
     **/
    public ConsoleInfo getConsoleInfo() {
        return mConsoleInfo;
    }

    /**
     * This function is called when the certificate server is deselected
     * on the topology view.
     */
    public void unselect(IPage viewInstance) {
//      Debug.println( "DSAdmin unselect" );
        super.unselect(viewInstance);
        fireRemoveMenuItems( viewInstance, this );
    }

    /**
     * This function is called when the directory server is selected
     * on the topology view.
     */
    public void select(IPage viewInstance) {
/*
        if (_removed)
            return;
*/

        HourGlass hglass = new HourGlass(mActiveFrame);
        super.select(viewInstance); // sets _viewInstance used
                                    // by getViewInstance()
        Debug.println( "CMSAdmin.select(): viewInstance =" +
                       getViewInstance() );
        fireAddMenuItems( viewInstance, this );
        if (mPort == 0) {
            fireDisableMenuItem(viewInstance, START);
            fireDisableMenuItem(viewInstance, STOP);
            fireEnableMenuItem(viewInstance, CONFIGURE);
        } else if ( getServerStatus() == STATUS_STARTED ) {
            fireDisableMenuItem( viewInstance, START );
            fireEnableMenuItem(viewInstance, STOP);
            fireDisableMenuItem(viewInstance, CONFIGURE);
        } else {
            fireEnableMenuItem( viewInstance, START );
            fireDisableMenuItem( viewInstance, STOP );
            fireDisableMenuItem(viewInstance, CONFIGURE);
        }
        if (hglass != null) {
            hglass.setNonWaitCursor();
            hglass = null;
        }
    }

    /**
      * Returns supported menu categories
      */
    public String[] getMenuCategoryIDs() {
        return new String[]
        {
            ResourcePage.MENU_CONTEXT,
            ResourcePage.MENU_OBJECT
        };
    }

    /**
     * Add menu items for this page.
     *
     * @param category Which menu
     */
    public IMenuItem[] getMenuItems(String category) {
        /* Same for both CONTEXT and OBJECT menus */
        return new IMenuItem[] {
            new MenuItemText( CONFIGURE,
                              CMSAdminResources.MENU_CONFIGURE_SERVER,
                              CMSAdminResources.MENU_CONFIGURE_SERVER_DESC),
            new MenuItemText( START,
                              CMSAdminResources.MENU_START_SERVER,
                              CMSAdminResources.MENU_START_SERVER_DESC),

            new MenuItemText( STOP,
                              CMSAdminResources.MENU_STOP_SERVER,
                              CMSAdminResources.MENU_STOP_SERVER_DESC),
            new MenuItemSeparator() };
    }

    /**
      * Notification that a menu item has been selected.
      */
    public void actionMenuSelected(IPage viewInstance, IMenuItem item) {
        if (item.getID().equals(START)) {
            ConsoleInfo info = getServerInstanceInfo();
            /* Fire off the Start task */
            CMSStart task = new CMSStart();
            mConsoleInfo.put(CMSStart.START_TASK_CGI, mServerID);
            mConsoleInfo.put("serverRoot",mServerRoot);
            mConsoleInfo.put("servid", mServerID);
            task.initialize(mConsoleInfo);
            //task.setConsoleInfo( info );
            boolean status = task.run( null );
            if( status ) {
                getServerStatus();
                enableStartStop(viewInstance);
            }
        } else if(item.getID().equals(STOP)) {
            CMSStop task = new CMSStop();
            ConsoleInfo info = getServerInstanceInfo();
            mConsoleInfo.put(CMSStop.STOP_TASK_CGI, mServerID);
            mConsoleInfo.put("servid", mServerID);
            mConsoleInfo.put("serverRoot",mServerRoot);
            task.initialize(mConsoleInfo);
            boolean status = task.run( null );
            if ( status ) {
                getServerStatus();
                enableStartStop(viewInstance);
            }
        } else if (item.getID().equals(CONFIGURE)) {
            startupInstallationWizard(viewInstance);
        }
    }

    public void updateMenu(IPage viewInstance) {
        getInfo(getConsoleInfo().getCurrentDN());
        if (mPort != 0) {
            fireDisableMenuItem(viewInstance, CONFIGURE);
            getServerStatus();
            enableStartStop(viewInstance);
        }
    }

    /**
     * Enable/Disable start/stop action menu.
     */
    private void enableStartStop(IPage viewInstance) {
        if (mServerStatus == STATUS_STOPPED) {
            fireEnableMenuItem( viewInstance, START );
            fireDisableMenuItem( viewInstance, STOP );
        } else if (mServerStatus == STATUS_STARTED) {
            fireEnableMenuItem( viewInstance, STOP );
            fireDisableMenuItem( viewInstance, START );
        }
    }

    /**
     * Set the title bar in the following format:<p><pre>
     *      [server information] - [server type] - [nickname]</pre>
     * Administrator id is shown at lower status bar
     */
    public void updateTitle () {
        /*
        mAuthid.setState(mResource.getString("CMSADMIN_USER_LABEL")+
            " = "+mServerInfo.getUserId()+"  ");
        mFramework.getFramework().changeStatusItemState(mAuthid);
        */
        String id = mServerID;
        int i = id.indexOf( '-' );
        if ( (i > 0) && (i < (id.length()-1)) )
            id = id.substring( i + 1 );
        mFramework.getFramework().setTitle( mServerInfo.getHost()+" - "
            + mResource.getString(CMSAdminResources.CERT_SERVER_NAME)+
            " - "+ id );
    }

    private Hashtable createWizardInfo() {
        Hashtable data = new Hashtable();
		/* This does nothing
        data.put(ConfigConstants.TASKID,TaskId.TASK_LIST_PREVIOUS_STAGES);
        data.put(ConfigConstants.OPTYPE, OpDef.OP_READ);
        data.put(ConfigConstants.PR_CERT_INSTANCE_NAME, mServerID);
        data.put(ConfigConstants.PR_SERVER_ROOT, mServerRoot);
		*/

		// moved from WIIntroPage.java
        data.put(ConfigConstants.TASKID,TaskId.TASK_GET_DEFAULT_INFO);
        data.put(ConfigConstants.OPTYPE, OpDef.OP_READ);
        data.put(ConfigConstants.PR_CERT_INSTANCE_NAME,
          mConsoleInfo.get(ConfigConstants.PR_CERT_INSTANCE_NAME));
        // #344791 - help server to make up the hostname
        data.put(ConfigConstants.PR_HOST,
          mConsoleInfo.get(ConfigConstants.PR_HOST));
        data.put(ConfigConstants.PR_SERVER_ROOT,
          mConsoleInfo.get(ConfigConstants.PR_SERVER_ROOT));
        return data;
    }

    /**
     * This is called when the installwizard is done.
     */
    public void notify(WizardWidget w) {
        Debug.println("Configuration Completed");
	for (int i = 0; i < 10; i++) { // try to detect 10 times
        	Debug.println("Check Status #" + i);
        	if ( getServerStatus() == STATUS_STARTED ) {
			return;
		}
		try {
			Thread.currentThread().sleep(2000); // 2 seconds
		} catch (Exception e) {
		}
	}
    }

    /**
     * Start up the installation wizard
     */
    public void startupInstallationWizard(IPage viewInstance) {
        Hashtable data = new Hashtable();
        CMSStartDaemon daemon = new CMSStartDaemon();
        mConsoleInfo.put("servid", mServerID);
        mConsoleInfo.put(CMSStartDaemon.START_DAEMON_CGI, mServerID);
        mConsoleInfo.put(CMSConfigCert.CONFIG_CERT_CGI, mServerID);
        daemon.initialize(mConsoleInfo);
        data.put(ConfigConstants.PR_CERT_INSTANCE_NAME, mServerID);
       	Debug.println("about to run Daemon");
        boolean success = daemon.runDaemon(data);
       	Debug.println("run daemon success = "+success);
        data.clear();
        data = null;
        boolean isInfoReady = false;

        if (success) {
            InstallWizardInfo wizardInfo =
              new InstallWizardInfo(mConsoleInfo);
            wizardInfo.setAdminFrame(mActiveFrame);
       		Debug.println("CMSAdmin: creating new configCertCgi");
            CMSConfigCert configCertCgi = new CMSConfigCert();
            configCertCgi.initialize(wizardInfo);
       		Debug.println("CMSAdmin: back from creating new configCertCgi");
            data = createWizardInfo();
            isInfoReady = configCertCgi.configCert(data);
       		Debug.println("CMSAdmin: isInfoReady = "+isInfoReady);

            if (isInfoReady) {
                JFrame frame = new JFrame();
                Cursor cursor = new Cursor(Cursor.HAND_CURSOR);
                frame.setCursor(cursor);
                frame.invalidate();
                frame.validate();
                frame.repaint(1);
            // XXX - checking status is too slow, dont do it now.
                // InstallWizard wizard = new InstallWizard(
                //  mConsoleInfo.getFrame(), wizardInfo, this);
                InstallWizard wizard = new InstallWizard(mActiveFrame,
                  wizardInfo, null);
                wizardInfo.setAdminFrame(mActiveFrame);
                wizardInfo.put("viewInstance", viewInstance);
                wizardInfo.put("CMSAdmin", this);
                new Thread(wizard).start();
            }
        }
        data.clear();
        data = null;
        mServerStatus = STATUS_UNKNOWN;
    }

    /**
     * This function is called when the server is double clicked on
     * the topology view. Auth dialog is displayed to get user dn and pwd.
     */
    public boolean run(IPage viewInstance) {

        Debug.println("The user double click the icon "+getConsoleInfo().getCurrentDN());
        Debug.println("View instance in the run method -> "+viewInstance);

        if (getConsoleInfo().getCurrentDN() == null) {
          mServerID = (String)mConsoleInfo.get("cmsServerInstance");
        }

        mConsoleInfo.put(CMSRestart.RESTART_TASK_CGI, mServerID);
        mConsoleInfo.put(CMSStart.START_TASK_CGI, mServerID);
        mConsoleInfo.put(CMSStop.STOP_TASK_CGI, mServerID);
        mConsoleInfo.put("CMSAdmin", this);
		if (mPort == 0) {
        	getInfo(getConsoleInfo().getCurrentDN());
			if (mPort == 0) {
                startupInstallationWizard(viewInstance);
            	return false;
			}
		}

        if (getConsoleInfo().getCurrentDN() == null) {
          mHost = (String)mConsoleInfo.get("cmsHost");
          mPort = Integer.parseInt((String)mConsoleInfo.get("cmsPort"));
        } else {
          try {
            LDAPConnection ldc = mConsoleInfo.getLDAPConnection();
            if ( ldc == null ) {
                Debug.println( "No connection ready in ConsoelInfo" );
                ldc = new LDAPConnection();
                ldc.connect( mConsoleInfo.getHost(), mConsoleInfo.getPort(),
                             mConsoleInfo.getAuthenticationDN(),
                             mConsoleInfo.getAuthenticationPassword());
            }
            //Debug.println( "Fetching " + sBase + " from " +
            //               mConsoleInfo.getHost() + ":" + mConsoleInfo.getPort() );
            LDAPEntry entry = ldc.read( mConsoleInfo.getCurrentDN() );
            //Debug.println( "Got " + entry );

            try {
                String port = getAttrVal(entry, "nsserverport");

                if (port == null)
                    return false;
                else {
                    int portnum = Integer.parseInt(port);
                    if (portnum != mPort)
                        mPort = portnum;
                }
            } catch (Exception e) {
                return false;
            }
          } catch (LDAPException ex) {
            CMSAdminUtil.showErrorDialog(mConsoleInfo.getFrame(), mResource,
              ex.toString(), CMSAdminUtil.ERROR_MESSAGE);
          }
        }

        try {
            // server off
            if (getServerStatus() != STATUS_STARTED) {
                CMSAdminUtil.showMessageDialog(mConsoleInfo.getFrame(), mResource, PREFIX,
                    "SERVEROFF", CMSAdminUtil.ERROR_MESSAGE);
                return false;
            }

/*
            mServerInfo = new CMSServerInfo(mHost, mPort, d.getUsername(),
                d.getPassword(),
                mServerID, mInstallationDate, mServerVersion, mServerRoot);
*/
            String path = (String)mConsoleInfo.get("cmsPath");
            mServerInfo = new CMSServerInfo(mHost, mPort, "","",
                mServerID, mInstallationDate, mServerVersion, mServerRoot, path);


            String authType = mServerInfo.getAuthType();

            // server is alive, do authenticate if the server asks for
            // password-based authentication
            if (authType.equals("pwd")) {
                CMSPassword d = new CMSPassword(mActiveFrame);
                d.show();
                if (d.isCancel())
                    return false;
                mServerInfo = new CMSServerInfo(mHost, mPort, d.getUsername(),
                  d.getPassword(),
                  mServerID, mInstallationDate, mServerVersion, mServerRoot, path);
                mServerInfo.authenticate();
            }
            mConsoleInfo.put("serverInfo", mServerInfo);
        } catch (EAdminException ex) {
                System.exit(0); // exit if authentication fails
                CMSAdminUtil.showErrorDialog(mConsoleInfo.getFrame(), mResource,
                    ex.getMessage(), CMSAdminUtil.ERROR_MESSAGE);
                return false;
        }


        //LOAD UI FRAMEWORK
        try {
            mFramework = new CMSUIFramework(mConsoleInfo, mServerInfo);
        } catch (EAdminException e) {
            CMSAdminUtil.showErrorDialog(mConsoleInfo.getFrame(), mResource,
                e.getMessage(), CMSAdminUtil.ERROR_MESSAGE);
            return false;
        }

        //show secure status
        StatusItemSecureMode statusSecureMode = new StatusItemSecureMode(Framework.STATUS_SECURE_MODE);
        statusSecureMode.setSecureMode(true);
        mFramework.getFramework().addStatusItem(statusSecureMode, IStatusItem.LEFTFIRST);
        statusSecureMode.setToolTipText(mServerInfo.getHost()+":"+mServerInfo.getPort());

        //show login status
        updateTitle ();
        return true;
    }

    /**
     * Run the object
     * @param viewInstance CMSPageFeeder object
     * @param selectionList List of selected objects
     */
    public boolean run(IPage viewInstance, IResourceObject selectionList[]) {
        return run( viewInstance );
    }

    /**
     *  perform the specified action. The command string is specified either
     *  from the content menu or the menu bar.
     *
     * @param command   Command String
     */
    public void performAction(String command) {

    }

    /**
     *  Return the server icon.
     *
     * @return The Directory Server icon.
     */
    public Icon getIcon() {
        return mIconImage;
    }

    public int getStatus() {
        return mServerStatus;
    }

    /**
     *  Return the current status of the server (running or not).
     *
     * @return The Certificate Server status.
     */
    public int getServerStatus() {
            Debug.println("Check server status");
      if (getConsoleInfo().getCurrentDN() == null) {
		return STATUS_STARTED;
      } else {
	    if ( mPort == 0) {
	      mServerStatus = STATUS_UNKNOWN;
	      return STATUS_UNKNOWN;
	    }
	    try {
	      ConsoleInfo info = getServerInstanceInfo();
	      CMSStatus task = new CMSStatus();
	      mConsoleInfo.put(CMSStatus.STATUS_TASK_CGI, mServerID);
	      mConsoleInfo.put("serverRoot",mServerRoot);
	      mConsoleInfo.put("servid", mServerID);
	      task.initialize(mConsoleInfo);
	      //task.setConsoleInfo( info );
	      boolean status = task.run( null );
	      if( status) {
		mServerStatus = STATUS_STARTED;
		return STATUS_STARTED;
	      }
	      else{
                mServerStatus = STATUS_STOPPED;
                return STATUS_STOPPED;
	      }
	    }
	    catch (Exception e) {
	      String bob = e.toString();
	      Debug.println(bob);
              if (Debug.isEnabled())
	          e.printStackTrace();
	      return STATUS_UNKNOWN;
	    }
     }
    }


  /**
   *  Return the current status of the server (running or not)
   *  by pinging the agent HTTPS port.
   *
   * @return The Certificate Server status.
   */
  public boolean getStatusFromAgentPort() {

    if (mPort == 0) {
      getInfo(getConsoleInfo().getCurrentDN());
                    if (mPort == 0) {
		      //Debug.println("CMSAdmin: getServerStatus --> "+mServerStatus);
		      return false;
		    }
    }

    //check if ssl port is functional
    try {
      if ((mServerInfo == null) || (mServerInfo.getPort() == 0)) {
            String path = (String)mConsoleInfo.get("cmsPath");
	mServerInfo = new CMSServerInfo(mHost, mPort, "", "",
					mServerID, mInstallationDate, mServerVersion, mServerRoot, path);
      }

      mServerInfo.ping();

    } catch (EAdminException e) {
      Debug.println("CMSAdmin: getServerStatus() -"+e.toString());
      if (e.getMessageString().equals(CMSAdminResources.SERVER_NORESPONSE) ||
	  e.getMessageString().equals(CMSAdminResources.SERVER_UNREACHABLE) ||
	  e.getMessageString().equals(CMSAdminResources.IOEXCEPTION) ||
	  e.getMessageString().equals(CMSAdminResources.UNKNOWNEXCEPTION) ||
	  e.getMessageString().equals(CMSAdminResources.UNKNOWNHOST) ) {
	//                mServerStatus = STATUS_STOPPED;
	return false;
      }
      Debug.println("CMSAdmin: getServerStatus() -UNKNOWN");
      //            mServerStatus = STATUS_UNKNOWN;
      return false;
    }

    Debug.println("CMSAdmin: getServerStatus() -OK");
    //        mServerStatus = STATUS_STARTED;
    return true;
  }

    /**
     * The concrete class implementing this method will clone its
     * configuration from the reference server. This supports using the
     * GET method for cloning the server.
     *
     * @param  referenceDN - DN of server to clone from.
     */
    public void cloneFrom(String referenceDN) {
        //XXX TBD
    }

    /**
     * Implements the IRemovableServerObject interface.
     * @return  true if the server was successfully removed, false otherwise
     */

    public boolean removeServer() {
        Debug.println("--------------  removeServer() ==== --------------------");

	Debug.println("getting console obj");
	ConsoleInfo info = getServerInstanceInfo();
	Debug.println("constuctor for remove");
	/* Fire off the Remove task */
	CMSRemove task = new CMSRemove();
	mConsoleInfo.put(CMSRemove.REMOVE_TASK_CGI, mServerID);
	mConsoleInfo.put("serverRoot",mServerRoot);
	mConsoleInfo.put("servid", mServerID);
	Debug.println("initalizing remove");
	task.initialize(mConsoleInfo);
	//task.setConsoleInfo( info );
	Debug.println("about to run remove rask");
	boolean status = task.run( null );
	Debug.println("remove run");
	Debug.println("Remove server status: "+ status);
	String instance = (String) mConsoleInfo.get("ServerInstance");
	if (null == instance) {
	  instance = "";
	}

	if (true == status) {  /* successfully called remove cgi */
	  Debug.println("removing topology for the server");
	  LDAPConnection ldc = mConsoleInfo.getLDAPConnection();
	  Debug.println("got the ldap connection");
	  String[] attrs = { "*", "numsubordinates" };
	  String sieDN = mConsoleInfo.getCurrentDN();
	  Debug.println("removeServer:sieDN:" + sieDN);

	  LDAPEntry sieEntry = null;
	  try {
	    sieEntry = ldc.read( sieDN, attrs );
	    Debug.println("read a ldap entry");
	  } catch (Exception ex ) {
	    Debug.println( "removeServer <" + sieDN + "> " + ex);

	    /*
	      args[1] = ex.toString();
	      DSUtil.showErrorDialog(_info.getFrame(), "removeinstance",
	      args);
	      _removed = false; // remove failed
	      */
	      return false;
	  }
	  if (sieEntry != null )	{
	    try {
	      Debug.println("Calling delete_sieTree");
	      status = delete_sieTree(sieEntry );
	    } catch (Exception ex ) {
	      Debug.println( "removeServer:Unable to delete the " +
			     "tree");
	      /*
		args[1] = ex.toString();
		DSUtil.showErrorDialog(_info.getFrame(), "removesie",
		args);
		_removed = false; // remove failed
		*/
	      return false;
	    }
	    // Now we need to remove the reference of this server
	    Debug.println("calling remove_serverinstance");
	    status = remove_serverInstance(sieDN);
	  }
	  /*
	    if ( status	 == false) {
	    args[1] = "";
	    DSUtil.showErrorDialog(_info.getFrame(), "removesie",
	    args);
	    } else {
	    DSUtil.showInformationDialog(_info.getFrame(), "121",
	    (String)null) ;
	    }

	    */
	}
	//        CMSAdminUtil.showMessageDialog(mConsoleInfo.getFrame(), mResource, PREFIX,
	//                    "NOTIMPLEMENTED", CMSAdminUtil.ERROR_MESSAGE);
        return status;
    }



  private boolean delete_sieTree (LDAPEntry entry )
    throws LDAPException {

      LDAPConnection ldc = mConsoleInfo.getLDAPConnection();
      boolean ret = false;

      String dn = entry.getDN();
      if ( entryHasChildren( entry ) ) {
	LDAPSearchResults search_results = null;
	String[] attrs = { "numsubordinates" };
	search_results = ldc.search( dn,
				     LDAPConnection.SCOPE_ONE,
				     "(objectClass=*)", attrs, false );

	while ( search_results.hasMoreElements() ) {
				/* Get the next child */
	  LDAPEntry child_entry =
	    (LDAPEntry)search_results.nextElement();
	  ret = delete_sieTree( child_entry );

	}
      }
      ldc.delete(dn);
      return true;
  }

  static boolean entryHasChildren( LDAPEntry entry ) {
    boolean hasChildren = false;
    LDAPAttribute attr = entry.getAttribute(
					    "numsubordinates" );
    if ( attr != null ) {
      Enumeration e = attr.getStringValues();
      if ( e.hasMoreElements() ) {
	String s = (String)e.nextElement();
	int count = Integer.parseInt( s );
	if ( count > 0 ) {
	  hasChildren = true;
	}
      }
    }
    return hasChildren;
  }


  private boolean remove_serverInstance (String sieDN )  {

    LDAPSearchResults search_results = null;
    String baseDN =(String)	 mConsoleInfo.get("BaseDN");
    LDAPConnection ldc = mConsoleInfo.getLDAPConnection();
    String[] attrs = { "*", "uniquemember" };
    String filter = "(&(objectclass=groupOfUniquenames)(uniquemember=" +
      sieDN+"))";

    try {
      search_results = ldc.search( baseDN, ldc.SCOPE_SUB,
				   filter, attrs, false);
    } catch (LDAPException e) {
      Debug.println( "Failed to search - " + e.toString() );
      return false;
    }
    LDAPEntry entry = null;
    while ( search_results.hasMoreElements() ) {
      // need to remove the reference to the sieDN from
      // this entry.

      entry = (LDAPEntry)search_results.nextElement();
      String eDN = (String) entry.getDN();
      // Now we need to modify the entry to delete the
      // reference to the serevr.
      remove_intstanceFromEntry(ldc, eDN, sieDN);
    }
    return true;
  }

  private boolean remove_intstanceFromEntry ( LDAPConnection ldc,
					      String eDN, String sieDN )	{

    LDAPModificationSet mods = new LDAPModificationSet();
    LDAPAttribute attUmember = new LDAPAttribute("uniquemember", sieDN);
    Debug.println("DSAdmin:remove_intstanceFromEntry: Modifying entry:" +
		  eDN);
    mods.add( LDAPModification.DELETE, attUmember );
    try {
      ldc.modify(eDN, mods );
    } catch ( LDAPException e ) {
      Debug.println ( "Modifying " + eDN + ", " + e);
      return false;
    }
    return true;
  }

    /*==========================================================
     * private methods
     *==========================================================*/

    /**
     * Extract a single string
     *
     * @param entry A Directory entry
     * @param name Name of attribute to fetch
     * @return A concatenated string
     */
    private String getAttrVal( LDAPEntry entry, String name ) {
        LDAPAttribute findAttr =
            entry.getAttribute( name, LDAPUtil.getLDAPAttributeLocale() );
        if ( findAttr != null ) {
            return LDAPUtil.flatting(findAttr);
        }
/*
        Debug.println( "Attribute " + name + " not found in " +
            entry.getDN() );
*/
        return null;
    }

    /**
     *  get the attribute information to display in the information panel
     *
     * @param sDN   DN for the entry.
     */
    private void getInfo(String sDN) {
        //String sBase = "cn=configuration, " + sDN;
        if (sDN == null) {
        mHost = (String)mConsoleInfo.get("cmsHost");
        mPort = Integer.parseInt((String)mConsoleInfo.get("cmsPort"));
        } else {
        String sBase = sDN;
        try {
            LDAPConnection ldc = mConsoleInfo.getLDAPConnection();
            if ( ldc == null ) {
                Debug.println( "No connection ready in ConsoelInfo" );
                ldc = new LDAPConnection();
                ldc.connect( mConsoleInfo.getHost(), mConsoleInfo.getPort(),
                             mConsoleInfo.getAuthenticationDN(),
                             mConsoleInfo.getAuthenticationPassword());
            }
            //Debug.println( "Fetching " + sBase + " from " +
            //               mConsoleInfo.getHost() + ":" + mConsoleInfo.getPort() );
            LDAPEntry entry = ldc.read( sBase );
            //Debug.println( "Got " + entry );

            mHost = getAttrVal( entry, "serverHostName" );
            try {
                String port = getAttrVal(entry, "nsserverport");
                if (port == null)
                    mPort = 0;
                else
                    mPort = Integer.parseInt(port);
            } catch (Exception e) {
                mPort = 0;
            }

            // get the attribute information for display purposes
            mServerVersion = getAttrVal( entry, "serverVersionNumber" );
            mInstallationDate = getAttrVal( entry, "installationTimeStamp" );
            mServerID = getAttrVal( entry, "nsserverid" );
            mServerRoot = getAttrVal( entry, "serverroot" );
            Debug.println("CMSAdmin::PR_HOST = " + mHost);
            mConsoleInfo.put(ConfigConstants.PR_HOST, mHost);
            mConsoleInfo.put(ConfigConstants.PR_SERVER_ROOT, mServerRoot);
            mConsoleInfo.put(ConfigConstants.PR_CERT_INSTANCE_NAME, mServerID);
        } catch( Exception e) {
            Debug.println( "Fetching " + sBase + " from " +
                           mConsoleInfo.getHost() + ":" + mConsoleInfo.getPort() +
                           ", " + e );
        }

        mAdminURL = mConsoleInfo.getAdminURL();

        /* Extract the username part of the admin authentication DN */
        String[] rdns = LDAPDN.explodeDN( mConsoleInfo.getAuthenticationDN(),
                                          true );
        String s = rdns[0].trim();
        mConsoleInfo.put( "AdminUsername", s );
        //getServerStatus();
        }
    }

    /**
     * Note: it would be better if this method were declared in the superclass,
     * but having it here is better than nothing, since I need access to it
     * for getServerStatus() . . .
     */
    private IPage getViewInstance() {
        return _viewInstance;
    }

    public boolean isCloningEnabled() {
        return false;
    }

    public boolean isMigrationEnabled() {
        return false;
    }
}