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 ForgeRock AS
026 */
027package org.opends.server.admin.client;
028
029import java.io.Closeable;
030import java.util.Set;
031import java.util.SortedSet;
032
033import org.opends.server.admin.AbstractManagedObjectDefinition;
034import org.opends.server.admin.Configuration;
035import org.opends.server.admin.ConfigurationClient;
036import org.opends.server.admin.DefinitionDecodingException;
037import org.opends.server.admin.InstantiableRelationDefinition;
038import org.opends.server.admin.ManagedObjectNotFoundException;
039import org.opends.server.admin.ManagedObjectPath;
040import org.opends.server.admin.OptionalRelationDefinition;
041import org.opends.server.admin.PropertyDefinition;
042import org.opends.server.admin.PropertyException;
043import org.opends.server.admin.SetRelationDefinition;
044import org.opends.server.admin.client.spi.Driver;
045import org.opends.server.admin.std.client.RootCfgClient;
046
047/**
048 * Client management connection context.
049 */
050public abstract class ManagementContext implements Closeable {
051
052  /**
053   * Creates a new management context.
054   */
055  protected ManagementContext() {
056    // No implementation required.
057  }
058
059
060
061  /**
062   * Deletes the named instantiable child managed object from the
063   * named parent managed object.
064   *
065   * @param <C>
066   *          The type of client managed object configuration that the
067   *          relation definition refers to.
068   * @param <S>
069   *          The type of server managed object configuration that the
070   *          relation definition refers to.
071   * @param parent
072   *          The path of the parent managed object.
073   * @param rd
074   *          The instantiable relation definition.
075   * @param name
076   *          The name of the child managed object to be removed.
077   * @return Returns <code>true</code> if the named instantiable
078   *         child managed object was found, or <code>false</code>
079   *         if it was not found.
080   * @throws IllegalArgumentException
081   *           If the relation definition is not associated with the
082   *           parent managed object's definition.
083   * @throws ManagedObjectNotFoundException
084   *           If the parent managed object could not be found.
085   * @throws OperationRejectedException
086   *           If the managed object cannot be removed due to some
087   *           client-side or server-side constraint which cannot be
088   *           satisfied (for example, if it is referenced by another
089   *           managed object).
090   * @throws AuthorizationException
091   *           If the server refuses to remove the managed objects
092   *           because the client does not have the correct
093   *           privileges.
094   * @throws CommunicationException
095   *           If the client cannot contact the server due to an
096   *           underlying communication problem.
097   */
098  public final <C extends ConfigurationClient, S extends Configuration>
099  boolean deleteManagedObject(
100      ManagedObjectPath<?, ?> parent, InstantiableRelationDefinition<C, S> rd,
101      String name) throws IllegalArgumentException,
102      ManagedObjectNotFoundException, OperationRejectedException,
103      AuthorizationException, CommunicationException {
104    return getDriver().deleteManagedObject(parent, rd, name);
105  }
106
107
108
109  /**
110   * Deletes the optional child managed object from the named parent
111   * managed object.
112   *
113   * @param <C>
114   *          The type of client managed object configuration that the
115   *          relation definition refers to.
116   * @param <S>
117   *          The type of server managed object configuration that the
118   *          relation definition refers to.
119   * @param parent
120   *          The path of the parent managed object.
121   * @param rd
122   *          The optional relation definition.
123   * @return Returns <code>true</code> if the optional child managed
124   *         object was found, or <code>false</code> if it was not
125   *         found.
126   * @throws IllegalArgumentException
127   *           If the relation definition is not associated with the
128   *           parent managed object's definition.
129   * @throws ManagedObjectNotFoundException
130   *           If the parent managed object could not be found.
131   * @throws OperationRejectedException
132   *           If the managed object cannot be removed due to some
133   *           client-side or server-side constraint which cannot be
134   *           satisfied (for example, if it is referenced by another
135   *           managed object).
136   * @throws AuthorizationException
137   *           If the server refuses to remove the managed objects
138   *           because the client does not have the correct
139   *           privileges.
140   * @throws CommunicationException
141   *           If the client cannot contact the server due to an
142   *           underlying communication problem.
143   */
144  public final <C extends ConfigurationClient, S extends Configuration>
145  boolean deleteManagedObject(
146      ManagedObjectPath<?, ?> parent, OptionalRelationDefinition<C, S> rd)
147      throws IllegalArgumentException, ManagedObjectNotFoundException,
148      OperationRejectedException, AuthorizationException,
149      CommunicationException {
150    return getDriver().deleteManagedObject(parent, rd);
151  }
152
153
154
155  /**
156   * Deletes s set child managed object from the
157   * named parent managed object.
158   *
159   * @param <C>
160   *          The type of client managed object configuration that the
161   *          relation definition refers to.
162   * @param <S>
163   *          The type of server managed object configuration that the
164   *          relation definition refers to.
165   * @param parent
166   *          The path of the parent managed object.
167   * @param rd
168   *          The set relation definition.
169   * @param name
170   *          The name of the child managed object to be removed.
171   * @return Returns <code>true</code> if the set
172   *         child managed object was found, or <code>false</code>
173   *         if it was not found.
174   * @throws IllegalArgumentException
175   *           If the relation definition is not associated with the
176   *           parent managed object's definition.
177   * @throws ManagedObjectNotFoundException
178   *           If the parent managed object could not be found.
179   * @throws OperationRejectedException
180   *           If the managed object cannot be removed due to some
181   *           client-side or server-side constraint which cannot be
182   *           satisfied (for example, if it is referenced by another
183   *           managed object).
184   * @throws AuthorizationException
185   *           If the server refuses to remove the managed objects
186   *           because the client does not have the correct
187   *           privileges.
188   * @throws CommunicationException
189   *           If the client cannot contact the server due to an
190   *           underlying communication problem.
191   */
192  public final <C extends ConfigurationClient, S extends Configuration>
193  boolean deleteManagedObject(
194      ManagedObjectPath<?, ?> parent, SetRelationDefinition<C, S> rd,
195      String name) throws IllegalArgumentException,
196      ManagedObjectNotFoundException, OperationRejectedException,
197      AuthorizationException, CommunicationException {
198    return getDriver().deleteManagedObject(parent, rd, name);
199  }
200
201
202
203  /**
204   * Gets the named managed object.
205   *
206   * @param <C>
207   *          The type of client managed object configuration that the
208   *          path definition refers to.
209   * @param <S>
210   *          The type of server managed object configuration that the
211   *          path definition refers to.
212   * @param path
213   *          The path of the managed object.
214   * @return Returns the named managed object.
215   * @throws DefinitionDecodingException
216   *           If the managed object was found but its type could not
217   *           be determined.
218   * @throws ManagedObjectDecodingException
219   *           If the managed object was found but one or more of its
220   *           properties could not be decoded.
221   * @throws ManagedObjectNotFoundException
222   *           If the requested managed object could not be found on
223   *           the server.
224   * @throws AuthorizationException
225   *           If the server refuses to retrieve the managed object
226   *           because the client does not have the correct
227   *           privileges.
228   * @throws CommunicationException
229   *           If the client cannot contact the server due to an
230   *           underlying communication problem.
231   */
232  @SuppressWarnings("unchecked")
233  public final <C extends ConfigurationClient, S extends Configuration>
234  ManagedObject<? extends C> getManagedObject(
235      ManagedObjectPath<C, S> path) throws DefinitionDecodingException,
236      ManagedObjectDecodingException, ManagedObjectNotFoundException,
237      AuthorizationException, CommunicationException {
238    // Be careful to handle the root configuration.
239    if (path.isEmpty()) {
240      return (ManagedObject<C>) getRootConfigurationManagedObject();
241    }
242
243    return getDriver().getManagedObject(path);
244  }
245
246
247
248  /**
249   * Gets the effective value of a property in the named managed
250   * object.
251   *
252   * @param <PD>
253   *          The type of the property to be retrieved.
254   * @param path
255   *          The path of the managed object containing the property.
256   * @param pd
257   *          The property to be retrieved.
258   * @return Returns the property's effective value, or
259   *         <code>null</code> if there are no values defined.
260   * @throws IllegalArgumentException
261   *           If the property definition is not associated with the
262   *           referenced managed object's definition.
263   * @throws DefinitionDecodingException
264   *           If the managed object was found but its type could not
265   *           be determined.
266   * @throws PropertyException
267   *           If the managed object was found but the requested
268   *           property could not be decoded.
269   * @throws ManagedObjectNotFoundException
270   *           If the requested managed object could not be found on
271   *           the server.
272   * @throws AuthorizationException
273   *           If the server refuses to retrieve the managed object
274   *           because the client does not have the correct
275   *           privileges.
276   * @throws CommunicationException
277   *           If the client cannot contact the server due to an
278   *           underlying communication problem.
279   */
280  public final <PD> PD getPropertyValue(ManagedObjectPath<?, ?> path,
281      PropertyDefinition<PD> pd) throws IllegalArgumentException,
282      DefinitionDecodingException, AuthorizationException,
283      ManagedObjectNotFoundException, CommunicationException,
284      PropertyException {
285    Set<PD> values = getPropertyValues(path, pd);
286    if (values.isEmpty()) {
287      return null;
288    } else {
289      return values.iterator().next();
290    }
291  }
292
293
294
295  /**
296   * Gets the effective values of a property in the named managed
297   * object.
298   *
299   * @param <PD>
300   *          The type of the property to be retrieved.
301   * @param path
302   *          The path of the managed object containing the property.
303   * @param pd
304   *          The property to be retrieved.
305   * @return Returns the property's effective values, or an empty set
306   *         if there are no values defined.
307   * @throws IllegalArgumentException
308   *           If the property definition is not associated with the
309   *           referenced managed object's definition.
310   * @throws DefinitionDecodingException
311   *           If the managed object was found but its type could not
312   *           be determined.
313   * @throws PropertyException
314   *           If the managed object was found but the requested
315   *           property could not be decoded.
316   * @throws ManagedObjectNotFoundException
317   *           If the requested managed object could not be found on
318   *           the server.
319   * @throws AuthorizationException
320   *           If the server refuses to retrieve the managed object
321   *           because the client does not have the correct
322   *           privileges.
323   * @throws CommunicationException
324   *           If the client cannot contact the server due to an
325   *           underlying communication problem.
326   */
327  public final <PD> SortedSet<PD> getPropertyValues(
328      ManagedObjectPath<?, ?> path, PropertyDefinition<PD> pd)
329      throws IllegalArgumentException, DefinitionDecodingException,
330      AuthorizationException, ManagedObjectNotFoundException,
331      CommunicationException, PropertyException {
332    return getDriver().getPropertyValues(path, pd);
333  }
334
335
336
337  /**
338   * Gets the root configuration client associated with this
339   * management context.
340   *
341   * @return Returns the root configuration client associated with
342   *         this management context.
343   */
344  public final RootCfgClient getRootConfiguration() {
345    return getRootConfigurationManagedObject().getConfiguration();
346  }
347
348
349
350  /**
351   * Gets the root configuration managed object associated with this
352   * management context.
353   *
354   * @return Returns the root configuration managed object associated
355   *         with this management context.
356   */
357  public final
358  ManagedObject<RootCfgClient> getRootConfigurationManagedObject() {
359    return getDriver().getRootConfigurationManagedObject();
360  }
361
362
363
364  /**
365   * Lists the child managed objects of the named parent managed
366   * object.
367   *
368   * @param <C>
369   *          The type of client managed object configuration that the
370   *          relation definition refers to.
371   * @param <S>
372   *          The type of server managed object configuration that the
373   *          relation definition refers to.
374   * @param parent
375   *          The path of the parent managed object.
376   * @param rd
377   *          The instantiable relation definition.
378   * @return Returns the names of the child managed objects.
379   * @throws IllegalArgumentException
380   *           If the relation definition is not associated with the
381   *           parent managed object's definition.
382   * @throws ManagedObjectNotFoundException
383   *           If the parent managed object could not be found.
384   * @throws AuthorizationException
385   *           If the server refuses to list the managed objects
386   *           because the client does not have the correct
387   *           privileges.
388   * @throws CommunicationException
389   *           If the client cannot contact the server due to an
390   *           underlying communication problem.
391   */
392  public final <C extends ConfigurationClient, S extends Configuration>
393  String[] listManagedObjects(
394      ManagedObjectPath<?, ?> parent, InstantiableRelationDefinition<C, S> rd)
395      throws IllegalArgumentException, ManagedObjectNotFoundException,
396      AuthorizationException, CommunicationException {
397    return listManagedObjects(parent, rd, rd.getChildDefinition());
398  }
399
400
401
402  /**
403   * Lists the child managed objects of the named parent managed
404   * object which are a sub-type of the specified managed object
405   * definition.
406   *
407   * @param <C>
408   *          The type of client managed object configuration that the
409   *          relation definition refers to.
410   * @param <S>
411   *          The type of server managed object configuration that the
412   *          relation definition refers to.
413   * @param parent
414   *          The path of the parent managed object.
415   * @param rd
416   *          The instantiable relation definition.
417   * @param d
418   *          The managed object definition.
419   * @return Returns the names of the child managed objects which are
420   *         a sub-type of the specified managed object definition.
421   * @throws IllegalArgumentException
422   *           If the relation definition is not associated with the
423   *           parent managed object's definition.
424   * @throws ManagedObjectNotFoundException
425   *           If the parent managed object could not be found.
426   * @throws AuthorizationException
427   *           If the server refuses to list the managed objects
428   *           because the client does not have the correct
429   *           privileges.
430   * @throws CommunicationException
431   *           If the client cannot contact the server due to an
432   *           underlying communication problem.
433   */
434  public final <C extends ConfigurationClient, S extends Configuration>
435  String[] listManagedObjects(
436      ManagedObjectPath<?, ?> parent, InstantiableRelationDefinition<C, S> rd,
437      AbstractManagedObjectDefinition<? extends C, ? extends S> d)
438      throws IllegalArgumentException, ManagedObjectNotFoundException,
439      AuthorizationException, CommunicationException {
440    return getDriver().listManagedObjects(parent, rd, d);
441  }
442
443
444
445  /**
446   * Lists the child managed objects of the named parent managed
447   * object.
448   *
449   * @param <C>
450   *          The type of client managed object configuration that the
451   *          relation definition refers to.
452   * @param <S>
453   *          The type of server managed object configuration that the
454   *          relation definition refers to.
455   * @param parent
456   *          The path of the parent managed object.
457   * @param rd
458   *          The set relation definition.
459   * @return Returns the names of the child managed objects.
460   * @throws IllegalArgumentException
461   *           If the relation definition is not associated with the
462   *           parent managed object's definition.
463   * @throws ManagedObjectNotFoundException
464   *           If the parent managed object could not be found.
465   * @throws AuthorizationException
466   *           If the server refuses to list the managed objects
467   *           because the client does not have the correct
468   *           privileges.
469   * @throws CommunicationException
470   *           If the client cannot contact the server due to an
471   *           underlying communication problem.
472   */
473  public final <C extends ConfigurationClient, S extends Configuration>
474  String[] listManagedObjects(
475      ManagedObjectPath<?, ?> parent, SetRelationDefinition<C, S> rd)
476      throws IllegalArgumentException, ManagedObjectNotFoundException,
477      AuthorizationException, CommunicationException {
478    return getDriver().listManagedObjects(parent, rd, rd.getChildDefinition());
479  }
480
481
482
483  /**
484   * Determines whether or not the named managed object exists.
485   *
486   * @param path
487   *          The path of the named managed object.
488   * @return Returns <code>true</code> if the named managed object
489   *         exists, <code>false</code> otherwise.
490   * @throws ManagedObjectNotFoundException
491   *           If the parent managed object could not be found.
492   * @throws AuthorizationException
493   *           If the server refuses to make the determination because
494   *           the client does not have the correct privileges.
495   * @throws CommunicationException
496   *           If the client cannot contact the server due to an
497   *           underlying communication problem.
498   */
499  public final boolean managedObjectExists(ManagedObjectPath<?, ?> path)
500      throws ManagedObjectNotFoundException, AuthorizationException,
501      CommunicationException {
502    return getDriver().managedObjectExists(path);
503  }
504
505
506
507  /**
508   * Gets the driver associated with this management context.
509   *
510   * @return Returns the driver associated with this management
511   *         context.
512   */
513  protected abstract Driver getDriver();
514
515
516
517  /**
518   * Closes this management context.
519   */
520  @Override
521  public final void close() {
522    getDriver().close();
523  }
524
525}