1
/** BEGIN COPYRIGHT BLOCK
2
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
3
* Copyright (C) 2005 Red Hat, Inc.
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.
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.
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 **/
20
package com.netscape.admin.dirserv.task;
22
import java.util.Enumeration;
23
import java.util.Hashtable;
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.*;
35
* Netscape Directory Server 5.0 LDAP Import task
40
* @see com.netscape.admin.client.dirserv.DSAdmin
41
* @see com.netscape.admin.client.dirserv
44
public class LDAPImport extends LDAPTask {
45
public LDAPImport(IDSModel model, Hashtable attributes) {
46
super(model, attributes);
48
_taskSection = "import";
50
LDAPAttribute instanceAttribute = (LDAPAttribute)(_attributes.get(INSTANCE));
51
String[] instanceValues = instanceAttribute.getStringValueArray();
53
_numberOfInstances = instanceValues.length;
55
createProgressDialog();
56
Thread th = new Thread(this);
58
((GenericProgressDialog)_progressDialog).packAndShow();
59
} catch ( Exception e ) {
60
Debug.println("LDAPTask.start(): " +
66
public LDAPImport(IDSModel model) {
67
this(model, (Hashtable)null);
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();
76
for (int i=0; i< instanceValues.length; i++) {
77
_currentInstance = instanceValues[i];
78
_currentInstanceNumber = i+1;
80
((GenericProgressDialog)_progressDialog).reset();
82
if (_numberOfInstances > 1) {
84
DSUtil.inverseAbreviateString(importFile, 40),
85
DSUtil.abreviateString(_currentInstance, 30),
86
String.valueOf(_currentInstanceNumber),
87
String.valueOf(_numberOfInstances)
89
labelText = DSUtil._resource.getString(_taskSection, "LDAPMode-progresslabel-title", args);
92
DSUtil.inverseAbreviateString(importFile, 40),
93
DSUtil.abreviateString(_currentInstance,30)
95
labelText = DSUtil._resource.getString(_taskSection, "LDAPMode-progresslabel-one-partition-title", args);
97
((GenericProgressDialog)_progressDialog).setTextInLabel(labelText);
99
instanceAttribute = new LDAPAttribute(INSTANCE, instanceValues[i]);
100
_attributes.put(INSTANCE, instanceAttribute);
103
((GenericProgressDialog)_progressDialog).waitForClose();
106
((GenericProgressDialog)_progressDialog).enableButtons(true);
107
updateProgressDialog();
109
_previousInstanceLogs = _statusProgressDialog.getLogFieldText();
111
if (_taskCancelled) {
117
protected boolean initTask() {
118
_taskCancelled = false;
121
LDAPAttributeSet attrSet = createTaskEntryAttributes();
123
_entry = new LDAPEntry(_taskDn, attrSet);
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"));
131
LDAPConnection ldc = getLDAPConnection();
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();
141
while ( enumVals.hasMoreElements() ) {
142
String aVal = ( String )enumVals.nextElement();
147
Debug.println("LDAPImport.initTask(): adding entry "+_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();
154
if ((ldapMessage != null) &&
155
(ldapMessage.length() > 0)) {
156
ldapError = ldapError + ". "+ldapMessage;
158
String[] args = {ldapError};
159
((GenericProgressDialog)_progressDialog).setTextInLabel(DSUtil._resource.getString(
161
"LDAPTaskError-starting-label",
169
protected LDAPAttributeSet createTaskEntryAttributes() {
170
LDAPAttributeSet attributeSet = super.createTaskEntryAttributes();
174
for( int i = 0; i < MANDATORY_ARGUMENTS.length; i++ ) {
175
if ((attr = (LDAPAttribute)_attributes.get(MANDATORY_ARGUMENTS[i])) == null) {
178
attributeSet.add(attr);
182
for( int i = 0; i < OPTIONAL_ARGUMENTS.length; i++) {
183
if ((attr = (LDAPAttribute)_attributes.get(OPTIONAL_ARGUMENTS[i])) != null) {
184
attributeSet.add(attr);
190
protected void createProgressDialog() {
191
String title = getTitle();
192
_progressDialog = new GenericProgressDialog(getModel().getFrame(),
194
GenericProgressDialog.CANCEL_AND_LOG_BUTTON_OPTION,
199
((GenericProgressDialog)_progressDialog).enableButtons(false);
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"));
205
_statusProgressDialog = new LDAPBasicProgressDialog(getModel().getFrame(),
206
DSUtil._resource.getString(_taskSection, "LDAPMode-Status-title"),
211
_statusProgressDialog.waitForClose();
214
public String getTitle() {
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);
221
title = DSUtil._resource.getString(_taskSection, "title");
226
protected void updateProgressDialog(Hashtable htUpdates) {
227
String text = (String)htUpdates.get(LDAPTask.TASK_LOG);
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);
233
if (text.lastIndexOf("Finished scanning file") >= 0) {
234
((GenericProgressDialog)_progressDialog).stepCompleted(1);
236
if (text.lastIndexOf("Import complete") >= 0) {
237
((GenericProgressDialog)_progressDialog).stepCompleted(2);
239
text = _previousInstanceLogs + "\n\n\n" + text;
240
htUpdates.put(LDAPTask.TASK_LOG, text);
242
_statusProgressDialog.update(htUpdates);
246
protected void finalizeTask(int result) {
247
Debug.println("LDAPImport.finalizeTask result "+result);
250
case LDAPTask.INSUFFICIENT_ACCESS_RIGHTS_ERROR:
251
_taskCancelled = true;
253
case LDAPTask.SERVER_UNAVAILABLE_ERROR:
254
_taskCancelled = true;
256
case LDAPTask.SERVER_UPDATING_ERROR:
257
_taskCancelled = true;
259
case LDAPTask.TASK_COMPLETED:
261
case LDAPTask.TASK_SUCCESSFULLY_COMPLETED:
262
((GenericProgressDialog)_progressDialog).stepCompleted(0);
263
((GenericProgressDialog)_progressDialog).stepCompleted(1);
264
((GenericProgressDialog)_progressDialog).stepCompleted(2);
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);
272
SwingUtilities.invokeAndWait(new Runnable() {
274
int wantToContinue = DSUtil.showConfirmationDialog( _progressDialog,
275
"LDAPMode-unsuccessful-wantToCancel",
278
if (wantToContinue != JOptionPane.YES_OPTION) {
279
_taskCancelled = true;
283
} catch (Exception e) {
285
if (!_taskCancelled) {
296
private final String IMPORT = "import";
298
protected int _currentInstanceNumber;
299
protected int _numberOfInstances;
300
protected String _currentInstance;
301
protected String _previousInstanceLogs = "";
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";
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";