3853N/A/*
3853N/A * CDDL HEADER START
3853N/A *
3853N/A * The contents of this file are subject to the terms of the
3853N/A * Common Development and Distribution License, Version 1.0 only
3853N/A * (the "License"). You may not use this file except in compliance
3853N/A * with the License.
3853N/A *
3853N/A * You can obtain a copy of the license at
3853N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE
3853N/A * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
3853N/A * See the License for the specific language governing permissions
3853N/A * and limitations under the License.
3853N/A *
3853N/A * When distributing Covered Code, include this CDDL HEADER in each
3853N/A * file and include the License file at
3853N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
3853N/A * add the following below this CDDL HEADER, with the fields enclosed
3853N/A * by brackets "[]" replaced with your own identifying information:
3853N/A * Portions Copyright [yyyy] [name of copyright owner]
3853N/A *
3853N/A * CDDL HEADER END
3853N/A *
3853N/A *
4136N/A * Copyright 2008-2009 Sun Microsystems, Inc.
3853N/A */
3853N/A
3853N/Apackage org.opends.guitools.controlpanel.ui;
3853N/A
3853N/Aimport static org.opends.messages.AdminToolMessages.*;
3853N/A
3853N/Aimport java.awt.Component;
3853N/Aimport java.awt.GridBagConstraints;
3853N/Aimport java.io.IOException;
3853N/Aimport java.io.StringReader;
3853N/Aimport java.util.ArrayList;
3853N/Aimport java.util.Collection;
3853N/Aimport java.util.HashSet;
3853N/Aimport java.util.List;
3853N/Aimport java.util.Set;
3853N/Aimport java.util.TreeSet;
3853N/Aimport javax.naming.ldap.InitialLdapContext;
3853N/Aimport javax.swing.SwingUtilities;
3853N/A
3853N/Aimport org.opends.guitools.controlpanel.datamodel.BackendDescriptor;
3853N/Aimport org.opends.guitools.controlpanel.datamodel.ControlPanelInfo;
4500N/Aimport org.opends.guitools.controlpanel.datamodel.ServerDescriptor;
3853N/Aimport org.opends.guitools.controlpanel.datamodel.VLVIndexDescriptor;
3853N/Aimport org.opends.guitools.controlpanel.datamodel.VLVSortOrder;
3853N/Aimport org.opends.guitools.controlpanel.event.ConfigurationChangeEvent;
3853N/Aimport org.opends.guitools.controlpanel.task.OfflineUpdateException;
3853N/Aimport org.opends.guitools.controlpanel.task.Task;
3853N/Aimport org.opends.guitools.controlpanel.util.ConfigReader;
3853N/Aimport org.opends.guitools.controlpanel.util.Utilities;
3853N/Aimport org.opends.messages.Message;
3920N/Aimport org.opends.server.admin.client.ManagementContext;
3920N/Aimport org.opends.server.admin.client.ldap.JNDIDirContextAdaptor;
3920N/Aimport org.opends.server.admin.client.ldap.LDAPManagementContext;
3920N/Aimport org.opends.server.admin.std.client.LocalDBBackendCfgClient;
3920N/Aimport org.opends.server.admin.std.client.LocalDBVLVIndexCfgClient;
3920N/Aimport org.opends.server.admin.std.client.RootCfgClient;
3920N/Aimport org.opends.server.admin.std.meta.LocalDBVLVIndexCfgDefn;
3853N/Aimport org.opends.server.admin.std.meta.LocalDBVLVIndexCfgDefn.Scope;
3853N/Aimport org.opends.server.core.DirectoryServer;
3853N/Aimport org.opends.server.types.DN;
3853N/Aimport org.opends.server.types.Entry;
3853N/Aimport org.opends.server.types.LDIFImportConfig;
3853N/Aimport org.opends.server.types.OpenDsException;
3853N/Aimport org.opends.server.util.LDIFReader;
3853N/A
3853N/A/**
3853N/A * Panel that appears when the user defines a new VLV index.
3853N/A *
3853N/A */
3853N/Apublic class NewVLVIndexPanel extends AbstractVLVIndexPanel
3853N/A{
3853N/A private static final long serialVersionUID = 1554866540747530939L;
3853N/A
3853N/A /**
3853N/A * Constructor of the panel.
3853N/A * @param backendName the backend where the index will be created.
3853N/A * @param relativeComponent the component relative to which the dialog
3853N/A * containing this panel will be centered.
3853N/A */
3853N/A public NewVLVIndexPanel(String backendName, Component relativeComponent)
3853N/A {
3853N/A super(backendName, relativeComponent);
3853N/A createBasicLayout(this, new GridBagConstraints(), false);
3853N/A }
3853N/A
3853N/A /**
3853N/A * {@inheritDoc}
3853N/A */
3853N/A public Message getTitle()
3853N/A {
3853N/A return INFO_CTRL_PANEL_NEW_VLV_INDEX_TITLE.get();
3853N/A }
3853N/A
3853N/A /**
3853N/A * {@inheritDoc}
3853N/A */
3853N/A public Component getPreferredFocusComponent()
3853N/A {
3853N/A return name;
3853N/A }
3853N/A
3853N/A /**
3853N/A * {@inheritDoc}
3853N/A */
3853N/A public void configurationChanged(ConfigurationChangeEvent ev)
3853N/A {
4500N/A ServerDescriptor desc = ev.getNewDescriptor();
4500N/A if (updateLayout(desc))
3853N/A {
4500N/A updateErrorPaneAndOKButtonIfAuthRequired(desc,
4500N/A isLocal() ? INFO_CTRL_PANEL_AUTHENTICATION_REQUIRED_FOR_NEW_VLV.get() :
4500N/A INFO_CTRL_PANEL_CANNOT_CONNECT_TO_REMOTE_DETAILS.get(desc.getHostname()));
3853N/A }
3853N/A }
3853N/A
3853N/A /**
3919N/A * Updates the contents of the panel with the provided backend.
3919N/A * @param backend the backend where the index will be created.
3919N/A */
3919N/A public void update(BackendDescriptor backend)
3919N/A {
3919N/A updateBaseDNCombo(backend);
3919N/A backendName.setText(backend.getBackendID());
3919N/A }
3919N/A
3919N/A /**
3853N/A * {@inheritDoc}
3853N/A */
3853N/A public void okClicked()
3853N/A {
3853N/A List<Message> errors = checkErrors(true);
3853N/A if (errors.isEmpty())
3853N/A {
3853N/A ProgressDialog dlg = new ProgressDialog(
4832N/A Utilities.createFrame(),
3853N/A Utilities.getParentDialog(this),
3853N/A INFO_CTRL_PANEL_NEW_VLV_INDEX_TITLE.get(), getInfo());
3853N/A NewVLVIndexTask newTask = new NewVLVIndexTask(getInfo(), dlg);
3853N/A for (Task task : getInfo().getTasks())
3853N/A {
3853N/A task.canLaunch(newTask, errors);
3853N/A }
3853N/A if (errors.isEmpty())
3853N/A {
3853N/A // Check filters
3853N/A if (checkIndexRequired())
3853N/A {
3853N/A String indexName = name.getText().trim();
3853N/A launchOperation(newTask,
3853N/A INFO_CTRL_PANEL_CREATING_NEW_VLV_INDEX_SUMMARY.get(indexName),
3853N/A INFO_CTRL_PANEL_CREATING_NEW_VLV_INDEX_SUCCESSFUL_SUMMARY.get(),
3853N/A INFO_CTRL_PANEL_CREATING_NEW_VLV_INDEX_SUCCESSFUL_DETAILS.get(
3853N/A indexName),
3853N/A ERR_CTRL_PANEL_CREATING_NEW_VLV_INDEX_ERROR_SUMMARY.get(),
3853N/A ERR_CTRL_PANEL_CREATING_NEW_VLV_INDEX_ERROR_DETAILS.get(),
3853N/A null,
3853N/A dlg);
3853N/A dlg.setVisible(true);
3853N/A Utilities.getParentDialog(this).setVisible(false);
3853N/A }
3853N/A }
3853N/A }
3853N/A
3853N/A if (!errors.isEmpty())
3853N/A {
3853N/A displayErrorDialog(errors);
3853N/A }
3853N/A }
3853N/A
3853N/A /**
3853N/A * The task in charge of creating the VLV index.
3853N/A *
3853N/A */
3853N/A protected class NewVLVIndexTask extends Task
3853N/A {
3853N/A private Set<String> backendSet;
3853N/A private String indexName;
3853N/A private Scope scope;
3853N/A private List<VLVSortOrder> sortOrder;
3853N/A private String baseDN;
3853N/A private String filterValue;
3853N/A private String backendID;
3853N/A private String ldif;
3853N/A private String sortOrderStringValue;
3853N/A private int maxBlock;
3853N/A private VLVIndexDescriptor newIndex;
3853N/A
3853N/A /**
3853N/A * The constructor of the task.
3853N/A * @param info the control panel info.
3853N/A * @param dlg the progress dialog that shows the progress of the task.
3853N/A */
3853N/A public NewVLVIndexTask(ControlPanelInfo info, ProgressDialog dlg)
3853N/A {
3853N/A super(info, dlg);
3853N/A backendSet = new HashSet<String>();
3853N/A backendSet.add(backendName.getText());
3853N/A indexName = name.getText().trim();
3853N/A sortOrder = getSortOrder();
3853N/A baseDN = getBaseDN();
3853N/A filterValue = filter.getText().trim();
3853N/A scope = getScope();
3853N/A backendID = backendName.getText();
3853N/A ldif = getIndexLDIF(indexName);
3853N/A sortOrderStringValue = getSortOrderStringValue(sortOrder);
3853N/A maxBlock = Integer.parseInt(maxBlockSize.getText());
3853N/A }
3853N/A
3853N/A /**
3853N/A * {@inheritDoc}
3853N/A */
3853N/A public Type getType()
3853N/A {
3853N/A return Type.NEW_INDEX;
3853N/A }
3853N/A
3853N/A /**
3853N/A * {@inheritDoc}
3853N/A */
3853N/A public Set<String> getBackends()
3853N/A {
3853N/A return backendSet;
3853N/A }
3853N/A
3853N/A /**
3853N/A * {@inheritDoc}
3853N/A */
3853N/A public Message getTaskDescription()
3853N/A {
3853N/A return INFO_CTRL_PANEL_NEW_VLV_INDEX_TASK_DESCRIPTION.get(
3853N/A indexName, backendID);
3853N/A }
3853N/A
3853N/A /**
3853N/A * {@inheritDoc}
3853N/A */
3853N/A public boolean canLaunch(Task taskToBeLaunched,
3853N/A Collection<Message> incompatibilityReasons)
3853N/A {
3853N/A boolean canLaunch = true;
4500N/A if (state == State.RUNNING && runningOnSameServer(taskToBeLaunched))
3853N/A {
3853N/A // All the operations are incompatible if they apply to this
3853N/A // backend for safety. This is a short operation so the limitation
3853N/A // has not a lot of impact.
3853N/A Set<String> backends =
3853N/A new TreeSet<String>(taskToBeLaunched.getBackends());
3853N/A backends.retainAll(getBackends());
3853N/A if (backends.size() > 0)
3853N/A {
3853N/A incompatibilityReasons.add(getIncompatibilityMessage(this,
3853N/A taskToBeLaunched));
3853N/A canLaunch = false;
3853N/A }
3853N/A }
3853N/A return canLaunch;
3853N/A }
3853N/A
3853N/A private void updateConfiguration() throws OpenDsException
3853N/A {
3853N/A boolean configHandlerUpdated = false;
3853N/A try
3853N/A {
3853N/A if (!isServerRunning())
3853N/A {
3853N/A configHandlerUpdated = true;
3853N/A getInfo().stopPooling();
3853N/A if (getInfo().mustDeregisterConfig())
3853N/A {
3853N/A DirectoryServer.deregisterBaseDN(DN.decode("cn=config"));
3853N/A }
3853N/A DirectoryServer.getInstance().initializeConfiguration(
3853N/A org.opends.server.extensions.ConfigFileHandler.class.getName(),
3853N/A ConfigReader.configFile);
3853N/A getInfo().setMustDeregisterConfig(true);
3853N/A }
3853N/A else
3853N/A {
3853N/A SwingUtilities.invokeLater(new Runnable()
3853N/A {
3853N/A public void run()
3853N/A {
4935N/A List<String> args = getObfuscatedCommandLineArguments(
4935N/A getDSConfigCommandLineArguments());
3853N/A args.removeAll(getConfigCommandLineArguments());
4935N/A printEquivalentCommandLine(getConfigCommandLineName(),
4935N/A args,
4935N/A INFO_CTRL_PANEL_EQUIVALENT_CMD_TO_CREATE_VLV_INDEX.get());
3853N/A }
3853N/A });
3853N/A }
3853N/A SwingUtilities.invokeLater(new Runnable()
3853N/A {
3853N/A /**
3853N/A * {@inheritDoc}
3853N/A */
3853N/A public void run()
3853N/A {
3853N/A getProgressDialog().appendProgressHtml(
3853N/A Utilities.getProgressWithPoints(
3853N/A INFO_CTRL_PANEL_CREATING_NEW_VLV_INDEX_PROGRESS.get(
3853N/A indexName),
3853N/A ColorAndFontConstants.progressFont));
3853N/A }
3853N/A });
3853N/A if (isServerRunning())
3853N/A {
3853N/A // Create additional indexes and display the equivalent command.
3853N/A // Everything is done in the method createAdditionalIndexes
3853N/A createIndex(getInfo().getDirContext());
3853N/A }
3853N/A else
3853N/A {
3853N/A createIndex();
3853N/A }
3853N/A SwingUtilities.invokeLater(new Runnable()
3853N/A {
3853N/A /**
3853N/A * {@inheritDoc}
3853N/A */
3853N/A public void run()
3853N/A {
3853N/A getProgressDialog().appendProgressHtml(
3853N/A Utilities.getProgressDone(ColorAndFontConstants.progressFont));
3853N/A }
3853N/A });
3853N/A }
3853N/A finally
3853N/A {
3853N/A if (configHandlerUpdated)
3853N/A {
3853N/A DirectoryServer.getInstance().initializeConfiguration(
3853N/A ConfigReader.configClassName, ConfigReader.configFile);
4136N/A getInfo().startPooling();
3853N/A }
3853N/A }
3853N/A }
3853N/A
3853N/A private void createIndex() throws OpenDsException
3853N/A {
3853N/A LDIFImportConfig ldifImportConfig = null;
3853N/A try
3853N/A {
3920N/A String topEntryDN =
3920N/A "cn=VLV Index,"+Utilities.getRDNString("ds-cfg-backend-id",
3920N/A backendName.getText())+",cn=Backends,cn=config";
3920N/A boolean topEntryExists =
3920N/A DirectoryServer.getConfigHandler().entryExists(
3920N/A DN.decode(topEntryDN));
3920N/A
3920N/A if (!topEntryExists)
3920N/A {
3920N/A String completeLDIF =
3920N/A Utilities.makeLdif(
3920N/A "dn: "+topEntryDN,
3920N/A "objectClass: top",
3920N/A "objectClass: ds-cfg-branch",
3920N/A "cn: VLV Index", "") + ldif;
3920N/A ldifImportConfig =
3920N/A new LDIFImportConfig(new StringReader(completeLDIF));
3920N/A }
3920N/A else
3920N/A {
3920N/A ldifImportConfig = new LDIFImportConfig(new StringReader(ldif));
3920N/A }
3920N/A
3920N/A
3853N/A LDIFReader reader = new LDIFReader(ldifImportConfig);
3853N/A Entry backendConfigEntry;
3853N/A while ((backendConfigEntry = reader.readEntry()) != null)
3853N/A {
3853N/A DirectoryServer.getConfigHandler().addEntry(backendConfigEntry, null);
3853N/A }
3853N/A DirectoryServer.getConfigHandler().writeUpdatedConfig();
3853N/A }
3853N/A catch (IOException ioe)
3853N/A {
3853N/A throw new OfflineUpdateException(
3853N/A ERR_CTRL_PANEL_ERROR_UPDATING_CONFIGURATION.get(ioe.toString()),
3853N/A ioe);
3853N/A }
3853N/A finally
3853N/A {
3853N/A if (ldifImportConfig != null)
3853N/A {
3853N/A ldifImportConfig.close();
3853N/A }
3853N/A }
3853N/A }
3853N/A
3853N/A private void createIndex(InitialLdapContext ctx) throws OpenDsException
3853N/A {
3920N/A ManagementContext mCtx = LDAPManagementContext.createFromContext(
3920N/A JNDIDirContextAdaptor.adapt(ctx));
3920N/A RootCfgClient root = mCtx.getRootConfiguration();
3920N/A LocalDBBackendCfgClient backend =
3920N/A (LocalDBBackendCfgClient)root.getBackend(backendName.getText());
3920N/A LocalDBVLVIndexCfgDefn provider = LocalDBVLVIndexCfgDefn.getInstance();
3920N/A LocalDBVLVIndexCfgClient index =
3920N/A backend.createLocalDBVLVIndex(provider, name.getText(), null);
3853N/A
3920N/A index.setFilter(filter.getText().trim());
3920N/A index.setSortOrder(getSortOrderStringValue(getSortOrder()));
3920N/A index.setBaseDN(DN.decode(getBaseDN()));
3920N/A index.setScope(getScope());
3920N/A index.setMaxBlockSize(Integer.parseInt(maxBlockSize.getText().trim()));
3920N/A index.commit();
3853N/A }
3853N/A
3853N/A /**
3853N/A * {@inheritDoc}
3853N/A */
4134N/A @Override
3853N/A protected String getCommandLinePath()
3853N/A {
3853N/A return null;
3853N/A }
3853N/A
3853N/A /**
3853N/A * {@inheritDoc}
3853N/A */
4134N/A @Override
3853N/A protected ArrayList<String> getCommandLineArguments()
3853N/A {
3853N/A return new ArrayList<String>();
3853N/A }
3853N/A
3853N/A private String getConfigCommandLineName()
3853N/A {
3853N/A if (isServerRunning())
3853N/A {
3853N/A return getCommandLinePath("dsconfig");
3853N/A }
3853N/A else
3853N/A {
3853N/A return null;
3853N/A }
3853N/A }
3853N/A
3853N/A /**
3853N/A * {@inheritDoc}
3853N/A */
4134N/A @Override
3853N/A public void runTask()
3853N/A {
3853N/A state = State.RUNNING;
3853N/A lastException = null;
3853N/A
3853N/A try
3853N/A {
3853N/A updateConfiguration();
3853N/A for (BackendDescriptor backend :
3853N/A getInfo().getServerDescriptor().getBackends())
3853N/A {
3853N/A if (backend.getBackendID().equalsIgnoreCase(backendID))
3853N/A {
3853N/A newIndex = new VLVIndexDescriptor(
3853N/A indexName, backend, DN.decode(baseDN),
3853N/A scope, filterValue, sortOrder, maxBlock);
3853N/A getInfo().registerModifiedIndex(newIndex);
3853N/A notifyConfigurationElementCreated(newIndex);
3853N/A break;
3853N/A }
3853N/A }
3853N/A state = State.FINISHED_SUCCESSFULLY;
3853N/A }
3853N/A catch (Throwable t)
3853N/A {
3853N/A lastException = t;
3853N/A state = State.FINISHED_WITH_ERROR;
3853N/A }
3853N/A }
3853N/A
3853N/A /**
3853N/A * {@inheritDoc}
3853N/A */
4134N/A @Override
3853N/A public void postOperation()
3853N/A {
3853N/A if ((lastException == null) && (state == State.FINISHED_SUCCESSFULLY) &&
3853N/A (newIndex != null))
3853N/A {
3853N/A rebuildIndexIfNecessary(newIndex, getProgressDialog());
3853N/A }
3853N/A }
3853N/A
3853N/A private ArrayList<String> getDSConfigCommandLineArguments()
3853N/A {
3853N/A ArrayList<String> args = new ArrayList<String>();
3853N/A args.add("create-local-db-vlv-index");
3853N/A args.add("--backend-name");
3853N/A args.add(backendID);
3853N/A args.add("--type");
3853N/A args.add("generic");
3853N/A
3853N/A args.add("--index-name");
3853N/A args.add(indexName);
3853N/A
3853N/A args.add("--set");
3853N/A args.add("base-dn:"+baseDN);
3853N/A
3853N/A args.add("--set");
3853N/A args.add("filter:"+filterValue);
3853N/A
3853N/A args.add("--set");
3853N/A args.add("scope:"+scope.toString());
3853N/A
3853N/A args.add("--set");
3853N/A args.add("sort-order:"+sortOrderStringValue);
3853N/A
3853N/A args.addAll(getConnectionCommandLineArguments());
3936N/A args.add(getNoPropertiesFileArgument());
3853N/A args.add("--no-prompt");
3853N/A return args;
3853N/A }
3853N/A }
3853N/A}