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