~ubuntu-branches/ubuntu/wily/389-ds-console/wily-proposed

« back to all changes in this revision

Viewing changes to src/com/netscape/admin/dirserv/DSTaskModel.java

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2012-03-15 19:58:37 UTC
  • Revision ID: package-import@ubuntu.com-20120315195837-296zyft51thld8q7
Tags: upstream-1.2.6
ImportĀ upstreamĀ versionĀ 1.2.6

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 modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation version 2 of the License.
 
9
 * 
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 * 
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 * END COPYRIGHT BLOCK **/
 
19
package com.netscape.admin.dirserv;
 
20
 
 
21
import java.util.*;
 
22
import java.awt.*;
 
23
import java.awt.event.*;
 
24
import javax.swing.*;
 
25
import javax.swing.tree.*;
 
26
import com.netscape.management.client.*;
 
27
import com.netscape.management.client.console.*;
 
28
import com.netscape.management.client.util.*;
 
29
import com.netscape.management.client.ace.ACIManager;
 
30
import com.netscape.management.client.security.CertificateDialog;
 
31
import com.netscape.management.client.security.PKCSConfigDialog;
 
32
import com.netscape.management.client.security.CertMigrateWizard;
 
33
import com.netscape.management.client.preferences.*;
 
34
import netscape.ldap.*;
 
35
 
 
36
 
 
37
public class DSTaskModel extends TaskModel
 
38
        implements IMenuInfo {
 
39
 
 
40
    public DSTaskModel( Vector v,
 
41
                                                ConsoleInfo ci, ConsoleInfo si ) {
 
42
                _serverInfo = si;
 
43
                _info = ci;
 
44
                if (v != null) {
 
45
                        _authListeners = v;
 
46
                }
 
47
                init();
 
48
        }
 
49
 
 
50
        public void reset( Vector v,
 
51
                           ConsoleInfo ci, ConsoleInfo si) {
 
52
                _serverInfo = si;
 
53
                _info = ci;
 
54
                if (v != null) {
 
55
                        _authListeners = v;
 
56
                }
 
57
                init();
 
58
        }
 
59
 
 
60
 
 
61
    private void init() {
 
62
                TaskObject root = new TaskObject( "root", _info );
 
63
                root.setAllowsChildren(true);
 
64
                String serverDN = _serverInfo.getCurrentDN();
 
65
                if ( serverDN != null ) {
 
66
                        /* Check if there is a list of tasks */
 
67
                        String order = findTaskOrder( serverDN );
 
68
 
 
69
                        /* Accumulate tasks in a hash table */
 
70
                        Hashtable list = new Hashtable();
 
71
                        Debug.println( "DSTaskModel.init: Searching for tasks under " +
 
72
                                                   serverDN );
 
73
                        findTasks( root, serverDN, list );
 
74
                        /* Need to go up one from the instance entry, to get non-instance-
 
75
                           specific task entries. */
 
76
                        serverDN = "cn=Tasks," +
 
77
                                new netscape.ldap.util.DN(
 
78
                                        _serverInfo.getCurrentDN() ).getParent().toString();
 
79
                        Debug.println( "DSTaskModel.init: Searching for tasks under " +
 
80
                                                   serverDN );
 
81
                        findTasks( root, serverDN, list );
 
82
                        Debug.println(9, "DSTaskModel.init: Finished searching for tasks");
 
83
 
 
84
                        /* Now sort them by preferred order, or just list them as found */
 
85
                        if ( order != null ) {
 
86
                                StringTokenizer st = new StringTokenizer( order, " " );
 
87
                                while( st.hasMoreTokens() ) {
 
88
                                        TaskObject task = (TaskObject)list.get( st.nextToken() );
 
89
                                        if ( task != null ) {
 
90
                                                root.add(task);
 
91
                                        }
 
92
                                }
 
93
                        } else {
 
94
                                Enumeration en = list.elements();
 
95
                                while( en.hasMoreElements() ) {
 
96
                                        root.add( (TaskObject)en.nextElement() );
 
97
                                }
 
98
                        }
 
99
                } else {
 
100
                        Debug.println( "DSTaskModel.init: no currentDN" );
 
101
                }
 
102
                setRoot(root);
 
103
        }
 
104
 
 
105
    private void findTasks( TaskObject root, String base, Hashtable list ) {
 
106
                // connect to the DS and search for task information
 
107
                LDAPConnection ldc = _info.getLDAPConnection();
 
108
                if ( ldc == null)
 
109
                        return;
 
110
                try {
 
111
                        String[] attrs = {"nsclassname", "nsexecref"};
 
112
                        Debug.println(9, "DSTaskModel.findTasks: searching for tasks " +
 
113
                                                  "under DN=" + base + " on directory server " +
 
114
                                                  DSUtil.format(ldc));
 
115
                        LDAPSearchResults result =
 
116
                                ldc.search( base, ldc.SCOPE_SUB,
 
117
                                                        "(objectclass=nstask)",
 
118
                                                        attrs, false );
 
119
 
 
120
                        while ( result.hasMoreElements() ) {
 
121
                                String sJavaClassName = null;
 
122
                                LDAPEntry findEntry = (LDAPEntry)result.nextElement();
 
123
                                LDAPAttribute anAttr =
 
124
                                        findEntry.getAttribute( attrs[0] );
 
125
                                if ( anAttr != null )
 
126
                                        sJavaClassName =
 
127
                                                LDAPUtil.flatting( anAttr.getStringValues() );
 
128
                                Debug.println(9, "DSTaskModel.findTasks: Found task " +
 
129
                                                          findEntry.getDN() +
 
130
                                                          " class name=" + sJavaClassName);
 
131
                                if ( sJavaClassName != null ) {
 
132
                                        // load the associated task class file
 
133
                                        try {
 
134
                                                Class c =
 
135
                                                        ClassLoaderUtil.getClass(_info,
 
136
                                                                                                         sJavaClassName);
 
137
                                                Debug.println(9, "DSTaskModel.findTasks: loaded " +
 
138
                                                                          " class =" + c);
 
139
                                                TaskObject task = (TaskObject)c.newInstance();
 
140
                                                Debug.println(9, "DSTaskModel.findTasks: new " +
 
141
                                                                          " task =" + task);
 
142
                                                ConsoleInfo taskConsoleInfo =
 
143
                                                        (ConsoleInfo)_serverInfo.clone();
 
144
                                                taskConsoleInfo.setCurrentDN(findEntry.getDN());
 
145
                                                /* Add a listener interface for
 
146
                                                   authentication changes */
 
147
                                                taskConsoleInfo.put( DSUtil.AUTH_CHANGE_LISTENERS,
 
148
                                                                                         _authListeners );
 
149
                                                anAttr = findEntry.getAttribute( attrs[1] );
 
150
                                                if ( anAttr != null ) {
 
151
                                                        String s = LDAPUtil.flatting(
 
152
                                                                anAttr.getStringValues() );
 
153
                                                        taskConsoleInfo.put( "execref", s );
 
154
                                                }
 
155
                                                taskConsoleInfo.put( "dstaskmodel", this );
 
156
                                                taskConsoleInfo.put( GlobalConstants.CONSOLE_INFO, _info );
 
157
                                                task.setConsoleInfo(taskConsoleInfo);
 
158
                                                String[] rdns =
 
159
                                                        LDAPDN.explodeDN( findEntry.getDN(), true );
 
160
                                                list.put( rdns[0], task );
 
161
                                                Debug.println(9, "DSTaskModel.findTasks: finish task " +
 
162
                                                                           task );
 
163
                                        } catch (Exception e) {
 
164
                                                Debug.println("DSTaskModel.findTasks: could not " +
 
165
                                                                          "load class: " + sJavaClassName + ", " +
 
166
                                                                          e);
 
167
                                                // This implicitly means that this task should
 
168
                                                // not show up in
 
169
                                                // in the Task list.
 
170
                                        }
 
171
                                }
 
172
                        }
 
173
                } catch ( LDAPException e ) {
 
174
                        Debug.println( "DSTaskModel.findTasks: " + e.toString() );
 
175
                }
 
176
        }
 
177
 
 
178
        private String findTaskOrder( String base ) {
 
179
                String order = null;
 
180
                /* See if there is a personal preference set */
 
181
                PreferenceManager pm =
 
182
                        PreferenceManager.getPreferenceManager(Framework.IDENTIFIER,
 
183
                                                                                                   Framework.VERSION);
 
184
                Preferences p = pm.getPreferences(PREFERENCES_TASK_TAB);
 
185
                if ( p != null ) {
 
186
                        order = p.getString( PREFERENCES_TASK_LIST );
 
187
                        if ( (order != null) && (order.trim().length() > 0) ) {
 
188
                                return order;
 
189
                        } else {
 
190
                                order = null;
 
191
                        }
 
192
                }
 
193
 
 
194
                LDAPConnection ldc = _info.getLDAPConnection();
 
195
                if ( ldc == null ) {
 
196
                        return null;
 
197
                }
 
198
                /* Check if there is a list */
 
199
                try {
 
200
                        String dn = "cn=task summary, cn=Operation, cn=Tasks," + base;
 
201
                        String[] attrs = {"description"};
 
202
                        LDAPEntry entry = ldc.read( dn, attrs );
 
203
                        if ( entry != null ) {
 
204
                                LDAPAttribute attr = entry.getAttribute( attrs[0] );
 
205
                                if ( attr != null ) {
 
206
                                        order = (String)attr.getStringValues().nextElement();
 
207
                                }
 
208
                        }
 
209
                } catch ( LDAPException ex ) {
 
210
                        Debug.println( "DSTaskModel.findTaskOrder: no list of tasks, " +
 
211
                                                   ex );
 
212
                }
 
213
                return order;
 
214
        }
 
215
 
 
216
    /**
 
217
      * Called internally when page is selected
 
218
      */
 
219
        public void pageSelected( IFramework framework, IPage viewInstance ) {
 
220
                if (getRefreshUponSelect()) {
 
221
                        init();
 
222
                        setRefreshUponSelect(false);
 
223
                }
 
224
        }
 
225
 
 
226
        public void setSelectedPage(IPage viewInstance) {
 
227
                if (viewInstance != null)
 
228
                        _viewInstance = viewInstance;
 
229
        }
 
230
 
 
231
        public IPage getSelectedPage() {
 
232
                return _viewInstance;
 
233
        }
 
234
 
 
235
    /**
 
236
         * Returns supported menu categories
 
237
         */
 
238
    public String[] getMenuCategoryIDs() {
 
239
                if (_categoryID == null) {
 
240
                        _categoryID = new String[]
 
241
                                {
 
242
                                        Framework.MENU_FILE,
 
243
                                                Framework.MENU_EDIT,
 
244
                                                ResourcePage.MENU_CONTEXT,
 
245
                                                MENU_SECURITY
 
246
                                                };
 
247
        }
 
248
                return _categoryID;
 
249
        }
 
250
 
 
251
        /**
 
252
         * add menu items for this page.
 
253
         */
 
254
    public IMenuItem[] getMenuItems(String categoryID) {
 
255
                if(categoryID.equals(Framework.MENU_FILE)) {
 
256
                        if (_menuFile == null) {
 
257
                                _menuFile = new IMenuItem[] {
 
258
                                        new MenuItemCategory( MENU_SECURITY,
 
259
                                                                                  _resource.getString("menu", "Security")),
 
260
                                                new MenuItemSeparator(),
 
261
                                                new MenuItemText( MENU_OPEN,
 
262
                                                                                  _resource.getString("menu", "open"),
 
263
                                                                                  _resource.getString("menu",
 
264
                                                                                                                          "open-description")),                                                         
 
265
                                                };                              
 
266
                        }
 
267
                        return _menuFile;
 
268
                } else if( categoryID.equals(MENU_SECURITY) ) {
 
269
                        if (_menuSecurity == null) {
 
270
                                _menuSecurity = new IMenuItem[] {
 
271
                                        new MenuItemText(
 
272
                                                                         MENU_KEYCERT_MANAGEMENT,
 
273
                                                                         _resource.getString( "menu",
 
274
                                                                                                                  "SecurityCertManagement"),
 
275
                                                                         _resource.getString( "menu",
 
276
                                                                                                                  "SecurityCertManagement-description")),
 
277
                                                new MenuItemText(
 
278
                                                                                 MENU_PKCS11,
 
279
                                                                                 _resource.getString("menu",
 
280
                                                                                                                         "SecurityPKCS11"),
 
281
                                                                                 _resource.getString("menu",
 
282
                                                                                                                         "SecurityPKCS11-description")),
 
283
                                                new MenuItemText(
 
284
                                                                                 MENU_KEYCERT_IMPORT,
 
285
                                                                                 _resource.getString( "menu",
 
286
                                                                                                                          "SecurityCertImport"),
 
287
                                                                                 _resource.getString( "menu",
 
288
                                                                                                                          "SecurityCertImport-description"))
 
289
                                                };
 
290
                        }
 
291
                        return _menuSecurity;
 
292
                } else if( categoryID.equals(Framework.MENU_EDIT) ) {
 
293
                        if (_menuEdit == null) {
 
294
                                _menuEdit = new IMenuItem[] {
 
295
                                        new MenuItemText( MENU_ACL,
 
296
                                                                          _resource.getString("menu", "editacls"),
 
297
                                                                          _resource.getString("menu",
 
298
                                                                                                                  "editacls-description"),
 
299
                                                                          false
 
300
                                                                          )
 
301
                                                };
 
302
                        }
 
303
                        return _menuEdit;
 
304
                }
 
305
                else if ( categoryID.equals(ResourcePage.MENU_CONTEXT) ) {
 
306
                        if (_menuContext == null) {
 
307
                                _menuContext = new IMenuItem[] {
 
308
                                        new MenuItemText( MENU_ACL,
 
309
                                                                          _resource.getString("menu", "editacls"),
 
310
                                                                          _resource.getString("menu",
 
311
                                                                                                                  "editacls-description")
 
312
                                                                          )
 
313
                                                };
 
314
                        }
 
315
                        return _menuContext;
 
316
                }
 
317
                return null;
 
318
        }
 
319
 
 
320
    public void actionObjectSelected( IPage viewInstance,
 
321
                                                                          ITaskObject selection,
 
322
                                                                          ITaskObject previousSelection ) {
 
323
                super.actionObjectSelected( viewInstance, selection,
 
324
                                                                        previousSelection);
 
325
                setSelectedPage(viewInstance);
 
326
                _selection = selection;
 
327
                if( selection == null ) {
 
328
                        fireDisableMenuItem( viewInstance, MENU_ACL );
 
329
                } else {
 
330
                        fireEnableMenuItem( viewInstance, MENU_ACL );
 
331
                }
 
332
        }
 
333
 
 
334
    /**
 
335
         * Notification that a menu item has been selected.
 
336
         */
 
337
    public void actionMenuSelected(IPage viewInstance, IMenuItem item) {
 
338
                setSelectedPage(viewInstance);
 
339
                if( item.getID().equals(MENU_OPEN) ) {
 
340
                        actionObjectRun(viewInstance, _selection);
 
341
                } else if( item.getID().equals(MENU_ACL) ) {
 
342
                        if ( _selection == null ) {
 
343
                                return;
 
344
                        }
 
345
                        String dn = _selection.getConsoleInfo().getCurrentDN();
 
346
                        ACIManager acm = new ACIManager(
 
347
                                getFrame(),
 
348
                                dn,
 
349
                                dn);
 
350
                        acm.show();
 
351
                } else if( item.getID().equals(MENU_KEYCERT_MANAGEMENT) ) {
 
352
                        Framework f = (Framework)viewInstance.getFramework();
 
353
                        try {
 
354
                                f.setBusyCursor(true);
 
355
 
 
356
                                CertificateDialog dlg = new CertificateDialog( 
 
357
                                        getFrame(),
 
358
                                        _info,
 
359
                                        (String)_serverInfo.get("SIE") );
 
360
                                dlg.showModal();
 
361
                        }
 
362
                        finally {
 
363
                                f.setBusyCursor(false);
 
364
                        }
 
365
                } else if( item.getID().equals(MENU_PKCS11) ) {
 
366
                        Framework f = (Framework)viewInstance.getFramework();
 
367
                        try {
 
368
                                f.setBusyCursor(true);
 
369
 
 
370
                                PKCSConfigDialog dlg = new PKCSConfigDialog( 
 
371
                                        getFrame(),
 
372
                                        _info,
 
373
                                        (String)_serverInfo.get("SIE") );
 
374
                                dlg.setVisible(true);
 
375
                        }
 
376
                        finally {
 
377
                                f.setBusyCursor(false);
 
378
                        }
 
379
                } else if( item.getID().equals(MENU_KEYCERT_IMPORT) ) {
 
380
                        actionMenuImport(viewInstance);
 
381
                }
 
382
        }
 
383
 
 
384
 
 
385
        /**
 
386
         * Import menu action.
 
387
         * Before opening the 'certificate import' dialog, 
 
388
         * we check if SSL is enabled or not. If yes, we
 
389
         * inform the user, it's not possible to import.
 
390
         */
 
391
        public void actionMenuImport(IPage viewInstance) {
 
392
                DSFramework fmk = (DSFramework)viewInstance.getFramework();
 
393
                DSAdmin serverObj = fmk.getServerObject();
 
394
 
 
395
                // Check if SSL is enabled
 
396
                boolean sslEnabled;
 
397
                try {
 
398
                        sslEnabled = serverObj.getSecurityState() == DSAdmin.SECURITY_ENABLE;
 
399
                }
 
400
                catch(LDAPException x) {
 
401
                        Debug.println(0, "DSTaskModel.actionMenuImport: failed to read directory");
 
402
                        Debug.println(0, "DSTaskModel.actionMenuImport: SSL is considered as off");
 
403
                        sslEnabled = false;
 
404
                }
 
405
 
 
406
                // Open the 'import certificate' dialog or warn the user.
 
407
                if (sslEnabled) {
 
408
                        DSUtil.showInformationDialog(getFrame(), "impossible-when-ssl-enabled", "", "dirtask");
 
409
                }
 
410
                else {
 
411
                        CertMigrateWizard w = new CertMigrateWizard(
 
412
                                getFrame(),
 
413
                                _info,
 
414
                                (String)_serverInfo.get("SIE") );
 
415
                        w.setVisible(true);
 
416
                        serverObj.fireDSAdminEvent(DSAdminEvent.IMPORT_CERTIFICATE);
 
417
                }
 
418
        }
 
419
 
 
420
 
 
421
    /**
 
422
      * Notification that the framework window is closing.
 
423
      * Called by ResourcePage
 
424
      */
 
425
        public void actionViewClosing(IPage viewInstance)
 
426
                        throws CloseVetoException {
 
427
                /* No unsaved changes in this model */
 
428
        }
 
429
 
 
430
    public void setWaitCursor( boolean on ) {
 
431
//              Debug.println( "DSTaskModel.setWaitCursor: " + on +
 
432
//                                         ", _viewInstance = " + _viewInstance );
 
433
                fireChangeFeedbackCursor(
 
434
                        null,
 
435
                        on ? FeedbackIndicator.FEEDBACK_WAIT :
 
436
                         FeedbackIndicator.FEEDBACK_DEFAULT );
 
437
 
 
438
                if (on) {
 
439
                        fireChangeStatusItemState( getSelectedPage(),
 
440
                                                                           ResourcePage.STATUS_PROGRESS,
 
441
                                                                           StatusItemProgress.STATE_BUSY );
 
442
                }
 
443
                else {
 
444
                        fireChangeStatusItemState( getSelectedPage(),
 
445
                                                                           ResourcePage.STATUS_PROGRESS,
 
446
                                                                           new Integer(0) );
 
447
        }
 
448
        }
 
449
 
 
450
        public void setRefreshUponSelect(boolean val) {
 
451
                _refreshUponSelect = val;
 
452
        }
 
453
 
 
454
        public boolean getRefreshUponSelect() {
 
455
                return _refreshUponSelect;
 
456
        }
 
457
        
 
458
        public void setFrame(JFrame f) {
 
459
                _frame = f;
 
460
        }
 
461
 
 
462
        public JFrame getFrame() {
 
463
                if (_frame == null) {
 
464
                        Debug.println(0, "DSTaskModel.getFrame: returning null frame !");
 
465
                }
 
466
                return _frame;  
 
467
        }
 
468
        
 
469
        // This flag will be true if the view for this model needs
 
470
        // to be refreshed the next time it's displayed e.g. because
 
471
        // the authentication changed and the new user may not have
 
472
        // permission to see the same view the old user saw
 
473
        private boolean _refreshUponSelect = false;
 
474
        static public String MENU_OPEN = "OPEN";
 
475
        static public String MENU_ACL = "ACL";
 
476
        static public String MENU_SECURITY = "SECURITY";
 
477
        static public String MENU_KEYCERT_MANAGEMENT = "CERT_MANAGEMENT";
 
478
        static public String MENU_PKCS11 = "PKCS_MANAGEMENT";
 
479
        static public String MENU_KEYCERT_IMPORT = "CERT_IMPORT";
 
480
    private static final String PREFERENCES_TASK_TAB = "TaskTab";
 
481
    private static final String PREFERENCES_TASK_LIST = "TaskList";
 
482
        static ResourceSet _resource = DSUtil._resource;
 
483
    protected ITaskObject _selection;
 
484
        private IPage _viewInstance = null;
 
485
        private ConsoleInfo _info;
 
486
        private ConsoleInfo _serverInfo;
 
487
        private Vector _authListeners = null;
 
488
        private JFrame _frame;
 
489
        private String[] _categoryID = null;
 
490
        private IMenuItem[] _menuFile = null;
 
491
        private IMenuItem[] _menuContext = null;
 
492
        private IMenuItem[] _menuSecurity = null;
 
493
        private IMenuItem[] _menuEdit = null;   
 
494
}