001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2008-2009 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2016 ForgeRock AS.
016 */
017package org.forgerock.opendj.config.client;
018
019import java.util.Set;
020import java.util.SortedSet;
021
022import org.forgerock.opendj.config.AbstractManagedObjectDefinition;
023import org.forgerock.opendj.config.Configuration;
024import org.forgerock.opendj.config.ConfigurationClient;
025import org.forgerock.opendj.config.DefinitionDecodingException;
026import org.forgerock.opendj.config.InstantiableRelationDefinition;
027import org.forgerock.opendj.config.ManagedObjectNotFoundException;
028import org.forgerock.opendj.config.ManagedObjectPath;
029import org.forgerock.opendj.config.OptionalRelationDefinition;
030import org.forgerock.opendj.config.PropertyDefinition;
031import org.forgerock.opendj.config.SetRelationDefinition;
032import org.forgerock.opendj.config.client.spi.Driver;
033import org.forgerock.opendj.ldap.LdapException;
034import org.forgerock.opendj.server.config.client.RootCfgClient;
035
036/** Driver based client management connection context. */
037public abstract class DriverBasedManagementContext implements ManagementContext {
038
039    /** Creates a new management context. */
040    protected DriverBasedManagementContext() {
041        // No implementation required.
042    }
043
044    @Override
045    public final <C extends ConfigurationClient, S extends Configuration> boolean deleteManagedObject(
046            ManagedObjectPath<?, ?> parent, InstantiableRelationDefinition<C, S> rd, String name)
047            throws ManagedObjectNotFoundException, OperationRejectedException,
048            LdapException {
049        return getDriver().deleteManagedObject(parent, rd, name);
050    }
051
052    @Override
053    public final <C extends ConfigurationClient, S extends Configuration> boolean deleteManagedObject(
054            ManagedObjectPath<?, ?> parent, OptionalRelationDefinition<C, S> rd) throws
055            ManagedObjectNotFoundException, OperationRejectedException, LdapException {
056        return getDriver().deleteManagedObject(parent, rd);
057    }
058
059    @Override
060    public final <C extends ConfigurationClient, S extends Configuration> boolean deleteManagedObject(
061            ManagedObjectPath<?, ?> parent, SetRelationDefinition<C, S> rd, String name)
062            throws ManagedObjectNotFoundException, OperationRejectedException, LdapException {
063        return getDriver().deleteManagedObject(parent, rd, name);
064    }
065
066    @Override
067    @SuppressWarnings("unchecked")
068    public final <C extends ConfigurationClient, S extends Configuration> ManagedObject<? extends C> getManagedObject(
069            ManagedObjectPath<C, S> path) throws DefinitionDecodingException, ManagedObjectDecodingException,
070            ManagedObjectNotFoundException, LdapException {
071        // Be careful to handle the root configuration.
072        if (path.isEmpty()) {
073            return (ManagedObject<C>) getRootConfigurationManagedObject();
074        }
075
076        return getDriver().getManagedObject(path);
077    }
078
079    @Override
080    public final <P> P getPropertyValue(ManagedObjectPath<?, ?> path, PropertyDefinition<P> pd)
081            throws DefinitionDecodingException, LdapException, ManagedObjectNotFoundException {
082        Set<P> values = getPropertyValues(path, pd);
083        if (values.isEmpty()) {
084            return null;
085        } else {
086            return values.iterator().next();
087        }
088    }
089
090    @Override
091    public final <P> SortedSet<P> getPropertyValues(ManagedObjectPath<?, ?> path, PropertyDefinition<P> pd)
092            throws DefinitionDecodingException, LdapException, ManagedObjectNotFoundException {
093        return getDriver().getPropertyValues(path, pd);
094    }
095
096    @Override
097    public final RootCfgClient getRootConfiguration() {
098        return getRootConfigurationManagedObject().getConfiguration();
099    }
100
101    @Override
102    public final ManagedObject<RootCfgClient> getRootConfigurationManagedObject() {
103        return getDriver().getRootConfigurationManagedObject();
104    }
105
106    @Override
107    public final <C extends ConfigurationClient, S extends Configuration> String[] listManagedObjects(
108            ManagedObjectPath<?, ?> parent, InstantiableRelationDefinition<C, S> rd) throws
109            ManagedObjectNotFoundException, LdapException {
110        return listManagedObjects(parent, rd, rd.getChildDefinition());
111    }
112
113    @Override
114    public final <C extends ConfigurationClient, S extends Configuration> String[] listManagedObjects(
115            ManagedObjectPath<?, ?> parent, InstantiableRelationDefinition<C, S> rd,
116            AbstractManagedObjectDefinition<? extends C, ? extends S> d) throws
117            ManagedObjectNotFoundException, LdapException {
118        return getDriver().listManagedObjects(parent, rd, d);
119    }
120
121    @Override
122    public final <C extends ConfigurationClient, S extends Configuration> String[] listManagedObjects(
123            ManagedObjectPath<?, ?> parent, SetRelationDefinition<C, S> rd) throws
124            ManagedObjectNotFoundException, LdapException {
125        return getDriver().listManagedObjects(parent, rd, rd.getChildDefinition());
126    }
127
128    @Override
129    public final boolean managedObjectExists(ManagedObjectPath<?, ?> path) throws ManagedObjectNotFoundException,
130            LdapException {
131        return getDriver().managedObjectExists(path);
132    }
133
134    /**
135     * Gets the driver associated with this management context.
136     *
137     * @return Returns the driver associated with this management context.
138     */
139    protected abstract Driver getDriver();
140
141    @Override
142    public final void close() {
143        getDriver().close();
144    }
145
146}