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