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.admin;
028    import org.opends.messages.Message;
029    
030    
031    
032    import java.util.Collection;
033    import java.util.Collections;
034    import java.util.HashMap;
035    import java.util.Locale;
036    import java.util.Map;
037    import java.util.MissingResourceException;
038    
039    import org.opends.server.admin.std.meta.RootCfgDefn;
040    import org.opends.server.util.Validator;
041    
042    
043    
044    /**
045     * An interface for querying the properties of a tag.
046     * <p>
047     * Tags are used to group related managed objects together into
048     * categories.
049     */
050    public final class Tag implements Comparable<Tag> {
051    
052      // All the tags.
053      private static final Map<String, Tag> tags = new HashMap<String, Tag>();
054    
055    
056    
057      /**
058       * Defines a new tag with the specified name.
059       *
060       * @param name
061       *          The name of the new tag.
062       */
063      public static void define(String name) {
064        Tag tag = new Tag(name);
065    
066        // Register the tag.
067        tags.put(name, tag);
068      }
069    
070    
071    
072      /**
073       * Returns the tag associated with the specified name.
074       *
075       * @param name
076       *          The name of the tag.
077       * @return Returns the tag associated with the specified name.
078       * @throws IllegalArgumentException
079       *           If the tag name was not recognized.
080       */
081      public static Tag valueOf(String name) throws IllegalArgumentException {
082        Validator.ensureNotNull(name);
083    
084        // Hack to force initialization of the tag definitions.
085        RootCfgDefn.getInstance();
086    
087        Tag tag = tags.get(name.toLowerCase());
088    
089        if (tag == null) {
090          throw new IllegalArgumentException("Unknown tag \"" + name + "\"");
091        }
092    
093        return tag;
094      }
095    
096    
097    
098      /**
099       * Returns an unmodifiable collection view of the set of registered
100       * tags.
101       *
102       * @return Returns an unmodifiable collection view of the set of
103       *         registered tags.
104       */
105      public static Collection<Tag> values() {
106        // Hack to force initialization of the tag definitions.
107        RootCfgDefn.getInstance();
108    
109        return Collections.unmodifiableCollection(tags.values());
110      }
111    
112      // The name of the tag.
113      private final String name;
114    
115    
116    
117      // Private constructor.
118      private Tag(String name) {
119        this.name = name;
120      }
121    
122    
123    
124      /**
125       * {@inheritDoc}
126       */
127      public final int compareTo(Tag o) {
128        return name.compareTo(o.name);
129      }
130    
131    
132    
133      /**
134       * {@inheritDoc}
135       */
136      @Override
137      public final boolean equals(Object obj) {
138        if (this == obj) {
139          return true;
140        }
141    
142        if (obj instanceof Tag) {
143          Tag other = (Tag) obj;
144          return other.name.equals(this.name);
145        }
146    
147        return false;
148      }
149    
150    
151    
152      /**
153       * Gets the name of this tag.
154       *
155       * @return Returns the name of this tag.
156       */
157      public final String getName() {
158        return name;
159      }
160    
161    
162    
163      /**
164       * Gets the synopsis of this tag in the default locale.
165       *
166       * @return Returns the synopsis of this tag in the default locale.
167       */
168      public final Message getSynopsis() {
169        return getSynopsis(Locale.getDefault());
170      }
171    
172    
173    
174      /**
175       * Gets the synopsis of this tag in the specified locale.
176       *
177       * @param locale
178       *          The locale.
179       * @return Returns the synopsis of this tag in the specified locale.
180       */
181      public final Message getSynopsis(Locale locale) {
182        ManagedObjectDefinitionI18NResource resource =
183          ManagedObjectDefinitionI18NResource.getInstance();
184        String property = "tag." + name + ".synopsis";
185        try {
186          return resource.getMessage(RootCfgDefn.getInstance(), property, locale);
187        } catch (MissingResourceException e) {
188          return null;
189        }
190      }
191    
192    
193    
194      /**
195       * {@inheritDoc}
196       */
197      @Override
198      public final int hashCode() {
199        return name.hashCode();
200      }
201    
202    
203    
204      /**
205       * {@inheritDoc}
206       */
207      @Override
208      public final String toString() {
209        return name;
210      }
211    
212    }