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
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.extensions;
028    import org.opends.messages.Message;
029    
030    
031    
032    import java.util.LinkedHashSet;
033    import java.util.List;
034    import java.util.Set;
035    
036    import org.opends.server.admin.server.ConfigurationChangeListener;
037    import org.opends.server.admin.std.server.UserDefinedVirtualAttributeCfg;
038    import org.opends.server.api.VirtualAttributeProvider;
039    import org.opends.server.config.ConfigException;
040    import org.opends.server.core.SearchOperation;
041    import org.opends.server.types.AttributeType;
042    import org.opends.server.types.AttributeValue;
043    import org.opends.server.types.ConfigChangeResult;
044    import org.opends.server.types.Entry;
045    import org.opends.server.types.InitializationException;
046    import org.opends.server.types.ResultCode;
047    import org.opends.server.types.VirtualAttributeRule;
048    
049    
050    
051    /**
052     * This class implements a virtual attribute provider that allows administrators
053     * to define their own values that will be inserted into any entry that matches
054     * the criteria defined in the virtual attribute rule.  This can be used to
055     * provide functionality like Class of Service (CoS) in the Sun Java System
056     * Directory Server.
057     */
058    public class UserDefinedVirtualAttributeProvider
059           extends VirtualAttributeProvider<UserDefinedVirtualAttributeCfg>
060           implements ConfigurationChangeListener<UserDefinedVirtualAttributeCfg>
061    {
062      // The current configuration for this virtual attribute provider.
063      private UserDefinedVirtualAttributeCfg currentConfig;
064    
065    
066    
067      /**
068       * Creates a new instance of this member virtual attribute provider.
069       */
070      public UserDefinedVirtualAttributeProvider()
071      {
072        super();
073    
074        // All initialization should be performed in the
075        // initializeVirtualAttributeProvider method.
076      }
077    
078    
079    
080      /**
081       * {@inheritDoc}
082       */
083      @Override()
084      public void initializeVirtualAttributeProvider(
085                                UserDefinedVirtualAttributeCfg configuration)
086             throws ConfigException, InitializationException
087      {
088        this.currentConfig = configuration;
089        configuration.addUserDefinedChangeListener(this);
090      }
091    
092    
093    
094      /**
095       * {@inheritDoc}
096       */
097      @Override()
098      public void finalizeVirtualAttributeProvider()
099      {
100        currentConfig.removeUserDefinedChangeListener(this);
101      }
102    
103    
104    
105      /**
106       * {@inheritDoc}
107       */
108      @Override()
109      public boolean isMultiValued()
110      {
111        if (currentConfig == null)
112        {
113          return true;
114        }
115        else
116        {
117          return (currentConfig.getValue().size() > 1);
118        }
119      }
120    
121    
122    
123      /**
124       * {@inheritDoc}
125       */
126      @Override()
127      public LinkedHashSet<AttributeValue> getValues(Entry entry,
128                                                     VirtualAttributeRule rule)
129      {
130        AttributeType attributeType = rule.getAttributeType();
131        Set<String> userDefinedValues = currentConfig.getValue();
132    
133        LinkedHashSet<AttributeValue> values =
134             new LinkedHashSet<AttributeValue>(userDefinedValues.size());
135        for (String valueString : userDefinedValues)
136        {
137          values.add(new AttributeValue(attributeType, valueString));
138        }
139    
140        return values;
141      }
142    
143    
144    
145      /**
146       * {@inheritDoc}
147       */
148      @Override()
149      public boolean isSearchable(VirtualAttributeRule rule,
150                                  SearchOperation searchOperation)
151      {
152        // We will not allow searches based only on user-defined virtual attributes.
153        return false;
154      }
155    
156    
157    
158      /**
159       * {@inheritDoc}
160       */
161      @Override()
162      public void processSearch(VirtualAttributeRule rule,
163                                SearchOperation searchOperation)
164      {
165        searchOperation.setResultCode(ResultCode.UNWILLING_TO_PERFORM);
166        return;
167      }
168    
169    
170    
171      /**
172       * {@inheritDoc}
173       */
174      public boolean isConfigurationChangeAcceptable(
175                          UserDefinedVirtualAttributeCfg configuration,
176                          List<Message> unacceptableReasons)
177      {
178        // The new configuration should always be acceptable.
179        return true;
180      }
181    
182    
183    
184      /**
185       * {@inheritDoc}
186       */
187      public ConfigChangeResult applyConfigurationChange(
188                                     UserDefinedVirtualAttributeCfg configuration)
189      {
190        // Just accept the new configuration as-is.
191        currentConfig = configuration;
192    
193        return new ConfigChangeResult(ResultCode.SUCCESS, false);
194      }
195    }
196