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

« back to all changes in this revision

Viewing changes to src/com/netscape/admin/dirserv/task/LDAPImport.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
 
 
20
package com.netscape.admin.dirserv.task;
 
21
 
 
22
import java.util.Enumeration;
 
23
import java.util.Hashtable;
 
24
import java.lang.*;
 
25
import javax.swing.*;
 
26
import java.awt.event.*;
 
27
import com.netscape.management.client.util.Debug;
 
28
import com.netscape.admin.dirserv.GenericProgressDialog;
 
29
import com.netscape.admin.dirserv.task.LDAPTask;
 
30
import com.netscape.admin.dirserv.DSUtil;
 
31
import com.netscape.admin.dirserv.IDSModel;
 
32
import netscape.ldap.*;
 
33
 
 
34
/**
 
35
 *      Netscape Directory Server 5.0 LDAP Import task
 
36
 *
 
37
 * @author  jvergara
 
38
 * @version %I%, %G%
 
39
 * @date                02/09/2000
 
40
* @see     com.netscape.admin.client.dirserv.DSAdmin
 
41
 * @see     com.netscape.admin.client.dirserv
 
42
 */
 
43
 
 
44
public class LDAPImport extends LDAPTask {
 
45
        public LDAPImport(IDSModel model, Hashtable attributes) {
 
46
                super(model, attributes);
 
47
                _taskType = IMPORT;
 
48
                _taskSection = "import";           
 
49
 
 
50
                LDAPAttribute instanceAttribute = (LDAPAttribute)(_attributes.get(INSTANCE));           
 
51
                String[] instanceValues = instanceAttribute.getStringValueArray();
 
52
                
 
53
                _numberOfInstances = instanceValues.length;
 
54
                try {
 
55
                        createProgressDialog();
 
56
                        Thread th = new Thread(this);
 
57
                        th.start();
 
58
                        ((GenericProgressDialog)_progressDialog).packAndShow();
 
59
                } catch ( Exception e ) {
 
60
                        Debug.println("LDAPTask.start(): " +
 
61
                                                  e );
 
62
                        e.printStackTrace();    
 
63
                }
 
64
        }
 
65
 
 
66
        public LDAPImport(IDSModel model) {
 
67
                this(model, (Hashtable)null);
 
68
        }
 
69
 
 
70
        public void run() {
 
71
                LDAPAttribute instanceAttribute = (LDAPAttribute)(_attributes.get(INSTANCE));
 
72
                LDAPAttribute importFileAttribute = (LDAPAttribute)(_attributes.get(FILENAME));
 
73
                String importFile = importFileAttribute.getStringValueArray()[0];
 
74
                String[] instanceValues = instanceAttribute.getStringValueArray();
 
75
 
 
76
                for (int i=0; i< instanceValues.length; i++) {                  
 
77
                        _currentInstance = instanceValues[i];
 
78
                        _currentInstanceNumber = i+1;
 
79
 
 
80
                        ((GenericProgressDialog)_progressDialog).reset();
 
81
                        String labelText;
 
82
                        if (_numberOfInstances > 1) {                           
 
83
                                String[] args = {
 
84
                                        DSUtil.inverseAbreviateString(importFile, 40),
 
85
                                        DSUtil.abreviateString(_currentInstance, 30),
 
86
                                        String.valueOf(_currentInstanceNumber),
 
87
                                        String.valueOf(_numberOfInstances)
 
88
                                };
 
89
                                labelText = DSUtil._resource.getString(_taskSection, "LDAPMode-progresslabel-title", args);                                                             
 
90
                        }  else {
 
91
                                String[] args = {
 
92
                                        DSUtil.inverseAbreviateString(importFile, 40),
 
93
                                        DSUtil.abreviateString(_currentInstance,30)
 
94
                                };
 
95
                                labelText = DSUtil._resource.getString(_taskSection, "LDAPMode-progresslabel-one-partition-title", args);
 
96
                        }
 
97
                        ((GenericProgressDialog)_progressDialog).setTextInLabel(labelText);
 
98
                
 
99
                        instanceAttribute = new LDAPAttribute(INSTANCE, instanceValues[i]);
 
100
                        _attributes.put(INSTANCE, instanceAttribute);                   
 
101
                        
 
102
                        if (!initTask()) {
 
103
                                ((GenericProgressDialog)_progressDialog).waitForClose();
 
104
                                break;
 
105
                        }
 
106
                        ((GenericProgressDialog)_progressDialog).enableButtons(true);
 
107
                        updateProgressDialog();
 
108
                        
 
109
                        _previousInstanceLogs = _statusProgressDialog.getLogFieldText();
 
110
 
 
111
                        if (_taskCancelled) {
 
112
                                break;
 
113
                        }
 
114
                }       
 
115
        }
 
116
 
 
117
        protected boolean initTask() {
 
118
                _taskCancelled = false;
 
119
 
 
120
                createTaskDn();
 
121
                LDAPAttributeSet attrSet = createTaskEntryAttributes();
 
122
                if (attrSet != null)
 
123
                        _entry = new LDAPEntry(_taskDn, attrSet);
 
124
                else {
 
125
                        Debug.println("LDAPImport.initTask():  Error, insufficient arguments to perform the task "+_taskType);
 
126
                        ((GenericProgressDialog)_progressDialog).setTextInLabel(DSUtil._resource.getString("general",
 
127
                                                                                                                                                                                           "LDAPTaskError-notenoughargs-label"));
 
128
                        return false;
 
129
                }                               
 
130
                
 
131
                LDAPConnection ldc = getLDAPConnection();               
 
132
                
 
133
                Debug.println("DN = " +_entry.getDN());
 
134
                Enumeration enumAttrs = attrSet.getAttributes();
 
135
                while ( enumAttrs.hasMoreElements() ) {
 
136
                        LDAPAttribute anAttr = (LDAPAttribute)enumAttrs.nextElement();
 
137
                        String attrName = anAttr.getName();
 
138
                        Debug.println( attrName );
 
139
                        Enumeration enumVals = anAttr.getStringValues();
 
140
                        
 
141
                        while ( enumVals.hasMoreElements() ) {
 
142
                                String aVal = ( String )enumVals.nextElement(); 
 
143
                        }
 
144
                }
 
145
                
 
146
                try {
 
147
                        Debug.println("LDAPImport.initTask(): adding entry "+_entry);
 
148
                        ldc.add(_entry);
 
149
                } catch (LDAPException lde) {
 
150
                        Debug.println ("LDAPImport.initTask(): LDAP error code = " + lde.getLDAPResultCode() + " error=" + lde);
 
151
                        String ldapError =  lde.errorCodeToString();
 
152
                        String ldapMessage = lde.getLDAPErrorMessage();
 
153
                        
 
154
                        if ((ldapMessage != null) &&
 
155
                                (ldapMessage.length() > 0)) {
 
156
                                ldapError = ldapError + ". "+ldapMessage;
 
157
                        } 
 
158
                        String[] args = {ldapError};
 
159
                        ((GenericProgressDialog)_progressDialog).setTextInLabel(DSUtil._resource.getString(
 
160
                                                                                                                                                                                           "general",
 
161
                                                                                                                                                                                           "LDAPTaskError-starting-label",
 
162
                                                                                                                                                                                           args));
 
163
                        return false;
 
164
                }
 
165
                return true;
 
166
        }
 
167
 
 
168
 
 
169
        protected LDAPAttributeSet createTaskEntryAttributes() {
 
170
                LDAPAttributeSet attributeSet = super.createTaskEntryAttributes();
 
171
                String value;
 
172
                LDAPAttribute attr;
 
173
 
 
174
                for( int i = 0; i < MANDATORY_ARGUMENTS.length; i++ ) {
 
175
                        if ((attr = (LDAPAttribute)_attributes.get(MANDATORY_ARGUMENTS[i])) == null) {
 
176
                                return null;
 
177
                        } else {
 
178
                                attributeSet.add(attr);
 
179
                        }
 
180
                }
 
181
                
 
182
                for( int i = 0; i < OPTIONAL_ARGUMENTS.length; i++) {
 
183
                        if ((attr = (LDAPAttribute)_attributes.get(OPTIONAL_ARGUMENTS[i])) != null) {
 
184
                                attributeSet.add(attr);
 
185
                        }       
 
186
                }
 
187
                return attributeSet;
 
188
        }
 
189
 
 
190
        protected void createProgressDialog() {         
 
191
                String title = getTitle();
 
192
                _progressDialog = new GenericProgressDialog(getModel().getFrame(), 
 
193
                                                                                                        true, 
 
194
                                                                                                        GenericProgressDialog.CANCEL_AND_LOG_BUTTON_OPTION, 
 
195
                                                                                                        title,
 
196
                                                                                                        null,
 
197
                                                                                                        this);
 
198
 
 
199
                ((GenericProgressDialog)_progressDialog).enableButtons(false);          
 
200
                
 
201
                ((GenericProgressDialog)_progressDialog).addStep(DSUtil._resource.getString(_taskSection, "LDAPMode-firstStep-title"));
 
202
                ((GenericProgressDialog)_progressDialog).addStep(DSUtil._resource.getString(_taskSection, "LDAPMode-secondStep-title"));
 
203
                ((GenericProgressDialog)_progressDialog).addStep(DSUtil._resource.getString(_taskSection, "LDAPMode-thirdStep-title"));
 
204
                
 
205
                _statusProgressDialog = new LDAPBasicProgressDialog(getModel().getFrame(), 
 
206
                                                                                                                         DSUtil._resource.getString(_taskSection, "LDAPMode-Status-title"), 
 
207
                                                                                                                         true, 
 
208
                                                                                                                         null,
 
209
                                                                                                                         this);
 
210
                                
 
211
                _statusProgressDialog.waitForClose();           
 
212
        }
 
213
 
 
214
        public String getTitle() {
 
215
                String title;   
 
216
                if (_numberOfInstances == 1) {
 
217
                        LDAPAttribute instanceAttribute = (LDAPAttribute)(_attributes.get(INSTANCE));           
 
218
                        String[] instanceValues = instanceAttribute.getStringValueArray();              
 
219
                        title = DSUtil._resource.getString(_taskSection, "initializebackend-title", instanceValues);
 
220
                } else {
 
221
                        title = DSUtil._resource.getString(_taskSection, "title");
 
222
                }
 
223
                return title;
 
224
        }
 
225
 
 
226
        protected void updateProgressDialog(Hashtable htUpdates) {
 
227
                String text = (String)htUpdates.get(LDAPTask.TASK_LOG);
 
228
                
 
229
                /* This is what appears in the server logs (this code has to be in sync with the server's output) */
 
230
                if (text.lastIndexOf("Processing file") >= 0) {
 
231
                        ((GenericProgressDialog)_progressDialog).stepCompleted(0);
 
232
                }
 
233
                if (text.lastIndexOf("Finished scanning file") >= 0) {
 
234
                        ((GenericProgressDialog)_progressDialog).stepCompleted(1);
 
235
                }
 
236
                if (text.lastIndexOf("Import complete") >= 0) {
 
237
                        ((GenericProgressDialog)_progressDialog).stepCompleted(2);
 
238
                }
 
239
                text = _previousInstanceLogs + "\n\n\n" + text;
 
240
                htUpdates.put(LDAPTask.TASK_LOG, text);
 
241
 
 
242
                _statusProgressDialog.update(htUpdates);
 
243
        }
 
244
 
 
245
 
 
246
        protected void finalizeTask(int result) {
 
247
                Debug.println("LDAPImport.finalizeTask result "+result);                
 
248
                
 
249
                switch (result) {
 
250
                case LDAPTask.INSUFFICIENT_ACCESS_RIGHTS_ERROR:
 
251
                        _taskCancelled = true;
 
252
                        break;
 
253
                case LDAPTask.SERVER_UNAVAILABLE_ERROR:
 
254
                        _taskCancelled = true;
 
255
                        break;
 
256
                case LDAPTask.SERVER_UPDATING_ERROR:
 
257
                        _taskCancelled = true;
 
258
                        break;
 
259
                case LDAPTask.TASK_COMPLETED:                           
 
260
                        break;
 
261
                case LDAPTask.TASK_SUCCESSFULLY_COMPLETED:
 
262
                        ((GenericProgressDialog)_progressDialog).stepCompleted(0);
 
263
                        ((GenericProgressDialog)_progressDialog).stepCompleted(1);
 
264
                        ((GenericProgressDialog)_progressDialog).stepCompleted(2);
 
265
                        break;
 
266
                case LDAPTask.TASK_UNSUCCESSFULLY_COMPLETED:
 
267
                        if (_currentInstanceNumber == _numberOfInstances) {
 
268
                                String labelText = DSUtil._resource.getString("general", "LDAPTaskCompleted-unsuccessful-label");
 
269
                                ((GenericProgressDialog)_progressDialog).setTextInLabel(labelText);
 
270
                        } else {
 
271
                                try {
 
272
                                        SwingUtilities.invokeAndWait(new Runnable() {
 
273
                                                public void run() {
 
274
                                                        int wantToContinue = DSUtil.showConfirmationDialog( _progressDialog,
 
275
                                                                                                                                                                "LDAPMode-unsuccessful-wantToCancel",
 
276
                                                                                                                                                                _currentInstance,
 
277
                                                                                                                                                                _taskSection);
 
278
                                                        if (wantToContinue != JOptionPane.YES_OPTION) {
 
279
                                                                _taskCancelled = true;
 
280
                                                        }
 
281
                                                }
 
282
                                        });
 
283
                                } catch (Exception e) {
 
284
                                }
 
285
                                if (!_taskCancelled) {
 
286
                                        waitForClose();
 
287
                                }                                       
 
288
                        }
 
289
                        break;
 
290
                }
 
291
                _result = result;
 
292
                waitForClose();
 
293
        }
 
294
 
 
295
 
 
296
        private final String IMPORT = "import";
 
297
 
 
298
        protected int _currentInstanceNumber;
 
299
        protected int _numberOfInstances;
 
300
        protected String _currentInstance;
 
301
        protected String _previousInstanceLogs = "";
 
302
 
 
303
        public String[] MANDATORY_ARGUMENTS = {FILENAME, INSTANCE};
 
304
        public String[] OPTIONAL_ARGUMENTS = {INCLUDE_SUFFIX, EXCLUDE_SUFFIX, INDEX_ATTRIBUTES};
 
305
        public final static String FILENAME = "nsFilename";
 
306
        public final static String INSTANCE = "nsInstance";
 
307
        public final static String INCLUDE_SUFFIX = "nsIncludeSuffix";
 
308
        public final static String EXCLUDE_SUFFIX = "nsExcludeSuffix";
 
309
        public final static String INDEX_ATTRIBUTES = "nsImportIndexAttrs";
 
310
 
 
311
        /* The following attributes are not used (for the moment) */
 
312
        public final static String CHUNK_SIZE = "nsImportChunkSize";
 
313
        public final static String UNIQUE_ID_GENERATOR = "nsUniqueIdGenerator";
 
314
        public final static String UNIQUE_ID_GENERATOR_NAMESPACE = "nsUniqueIdGeneratorNamespace";
 
315
}