NumSubordinatesVirtualAttributeProvider.java revision 1213c76646ee52dee5230cb219a7af951e8433b9
760N/A/*
760N/A * CDDL HEADER START
760N/A *
760N/A * The contents of this file are subject to the terms of the
760N/A * Common Development and Distribution License, Version 1.0 only
760N/A * (the "License"). You may not use this file except in compliance
760N/A * with the License.
760N/A *
760N/A * You can obtain a copy of the license at
760N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE
760N/A * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
760N/A * See the License for the specific language governing permissions
760N/A * and limitations under the License.
760N/A *
760N/A * When distributing Covered Code, include this CDDL HEADER in each
760N/A * file and include the License file at
760N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
760N/A * add the following below this CDDL HEADER, with the fields enclosed
873N/A * by brackets "[]" replaced with your own identifying information:
760N/A * Portions Copyright [yyyy] [name of copyright owner]
760N/A *
760N/A * CDDL HEADER END
760N/A *
760N/A *
3232N/A * Copyright 2008-2009 Sun Microsystems, Inc.
760N/A * Portions Copyright 2012 ForgeRock AS
760N/A */
760N/Apackage org.opends.server.extensions;
760N/A
760N/A
760N/A
760N/Aimport java.util.Collections;
760N/Aimport java.util.List;
760N/Aimport java.util.Set;
4134N/A
4134N/Aimport org.opends.messages.Message;
4134N/Aimport org.opends.server.admin.std.server.NumSubordinatesVirtualAttributeCfg;
4134N/Aimport org.opends.server.api.VirtualAttributeProvider;
760N/Aimport org.opends.server.api.Backend;
760N/Aimport org.opends.server.config.ConfigException;
760N/Aimport org.opends.server.core.DirectoryServer;
760N/Aimport org.opends.server.core.SearchOperation;
760N/Aimport org.opends.server.loggers.debug.DebugTracer;
760N/Aimport org.opends.server.types.*;
760N/A
760N/Aimport static org.opends.messages.ExtensionMessages.*;
760N/Aimport static org.opends.server.loggers.debug.DebugLogger.getTracer;
760N/Aimport static org.opends.server.loggers.debug.DebugLogger.debugEnabled;
760N/A
760N/A
760N/A
760N/A/**
760N/A * This class implements a virtual attribute provider that is meant to serve the
760N/A * hasSubordinates operational attribute as described in
760N/A * draft-ietf-boreham-numsubordinates.
760N/A */
760N/Apublic class NumSubordinatesVirtualAttributeProvider
760N/A extends VirtualAttributeProvider<NumSubordinatesVirtualAttributeCfg>
760N/A{
760N/A /**
760N/A * The tracer object for the debug logger.
760N/A */
760N/A private static final DebugTracer TRACER = getTracer();
760N/A
760N/A /**
760N/A * Creates a new instance of this NumSubordinates virtual attribute provider.
760N/A */
760N/A public NumSubordinatesVirtualAttributeProvider()
760N/A {
760N/A super();
760N/A
760N/A // All initialization should be performed in the
760N/A // initializeVirtualAttributeProvider method.
760N/A }
760N/A
760N/A
760N/A
760N/A /**
760N/A * {@inheritDoc}
760N/A */
760N/A @Override()
4134N/A public void initializeVirtualAttributeProvider(
760N/A NumSubordinatesVirtualAttributeCfg configuration)
760N/A throws ConfigException, InitializationException
760N/A {
760N/A // No initialization is required.
760N/A }
760N/A
4134N/A
760N/A
760N/A /**
760N/A * {@inheritDoc}
760N/A */
760N/A @Override()
760N/A public boolean isMultiValued()
760N/A {
4134N/A return false;
760N/A }
760N/A
760N/A
760N/A
760N/A /**
760N/A * {@inheritDoc}
760N/A */
760N/A @Override()
4134N/A public Set<AttributeValue> getValues(Entry entry,
760N/A VirtualAttributeRule rule)
760N/A {
760N/A Backend backend = DirectoryServer.getBackend(entry.getDN());
760N/A
760N/A try
760N/A {
760N/A long count = backend.numSubordinates(entry.getDN(), false);
760N/A if(count >= 0)
760N/A {
760N/A AttributeValue value =
760N/A AttributeValues.create(ByteString.valueOf(String.valueOf(count)),
760N/A ByteString.valueOf(String.valueOf(count)));
760N/A return Collections.singleton(value);
760N/A }
760N/A }
760N/A catch(DirectoryException de)
760N/A {
760N/A if (debugEnabled())
760N/A {
760N/A TRACER.debugCaught(DebugLogLevel.ERROR, de);
760N/A }
760N/A }
760N/A
760N/A return Collections.emptySet();
760N/A }
760N/A
760N/A
760N/A
760N/A /**
760N/A * {@inheritDoc}
760N/A */
760N/A @Override()
760N/A public boolean hasValue(Entry entry, VirtualAttributeRule rule)
760N/A {
760N/A Backend backend = DirectoryServer.getBackend(entry.getDN());
760N/A
760N/A try
760N/A {
760N/A return backend.numSubordinates(entry.getDN(), false) >= 0;
760N/A }
760N/A catch(DirectoryException de)
760N/A {
760N/A if (debugEnabled())
760N/A {
760N/A TRACER.debugCaught(DebugLogLevel.ERROR, de);
760N/A }
760N/A
760N/A return false;
760N/A }
760N/A }
760N/A
760N/A
760N/A
760N/A /**
760N/A * {@inheritDoc}
760N/A */
760N/A @Override()
4134N/A public boolean hasValue(Entry entry, VirtualAttributeRule rule,
760N/A AttributeValue value)
760N/A {
760N/A Backend backend = DirectoryServer.getBackend(entry.getDN());
4134N/A
4134N/A try
4134N/A {
4134N/A long count = backend.numSubordinates(entry.getDN(), false);
4134N/A if(count >= 0)
4134N/A {
4134N/A return Long.parseLong(value.getNormalizedValue().toString())
760N/A == count;
4134N/A }
760N/A return false;
760N/A }
760N/A catch(DirectoryException de)
760N/A {
760N/A if (debugEnabled())
760N/A {
760N/A TRACER.debugCaught(DebugLogLevel.ERROR, de);
760N/A }
760N/A
760N/A return false;
4134N/A }
760N/A }
760N/A
760N/A
4134N/A
760N/A /**
4134N/A * {@inheritDoc}
760N/A */
760N/A @Override()
760N/A public ConditionResult matchesSubstring(Entry entry,
760N/A VirtualAttributeRule rule,
760N/A ByteString subInitial,
760N/A List<ByteString> subAny,
760N/A ByteString subFinal)
760N/A {
760N/A // This virtual attribute does not support substring matching.
760N/A return ConditionResult.UNDEFINED;
4134N/A }
760N/A
760N/A
760N/A
4134N/A /**
4134N/A * {@inheritDoc}
4134N/A */
760N/A @Override()
4134N/A public ConditionResult approximatelyEqualTo(Entry entry,
760N/A VirtualAttributeRule rule,
760N/A AttributeValue value)
760N/A {
760N/A // This virtual attribute does not support approximate matching.
760N/A return ConditionResult.UNDEFINED;
760N/A }
760N/A
760N/A
760N/A
760N/A /**
4134N/A * {@inheritDoc}
760N/A */
760N/A @Override()
760N/A public boolean isSearchable(VirtualAttributeRule rule,
4134N/A SearchOperation searchOperation,
4134N/A boolean isPreIndexed)
4134N/A {
4134N/A return false;
4134N/A }
4134N/A
760N/A
4134N/A
760N/A /**
760N/A * {@inheritDoc}
760N/A */
760N/A @Override()
760N/A public void processSearch(VirtualAttributeRule rule,
760N/A SearchOperation searchOperation)
760N/A {
760N/A searchOperation.setResultCode(ResultCode.UNWILLING_TO_PERFORM);
760N/A
760N/A Message message = ERR_NUMSUBORDINATES_VATTR_NOT_SEARCHABLE.get(
4134N/A rule.getAttributeType().getNameOrOID());
760N/A searchOperation.appendErrorMessage(message);
760N/A }
760N/A}
4134N/A
4134N/A