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    
028    package org.opends.messages;
029    
030    import java.util.HashMap;
031    import java.util.Map;
032    import java.util.EnumSet;
033    import java.util.Set;
034    import java.util.HashSet;
035    import java.util.Collections;
036    
037    /**
038     * Defines values for message severity.  Severities contain an
039     * integer value that can be used for bitwise operations as well
040     * as a short abbreviated string form of each value.
041     */
042    @org.opends.server.types.PublicAPI(
043        stability=org.opends.server.types.StabilityLevel.UNCOMMITTED,
044        mayInstantiate=false,
045        mayExtend=false,
046        mayInvoke=true)
047    public enum Severity {
048    
049      /**
050       * The severity that will be used for informational messages.
051       */
052      INFORMATION(0x00000000, "INFO", "INFO"),
053    
054      /**
055       * The severity that will be used for mild warning messages.
056       */
057      MILD_WARNING(0x00010000, "MILD_WARN", "WARN"),
058    
059      /**
060       * The severity that will be used for severe warning messages.
061       */
062      SEVERE_WARNING(0x00020000, "SEVERE_WARN", "WARN"),
063    
064      /**
065       * The severity that will be used for mild error messages.
066       */
067      MILD_ERROR(0x00030000, "MILD_ERR", "ERR"),
068    
069      /**
070       * The severity that will be used for severe error messages.
071       */
072      SEVERE_ERROR(0x00040000, "SEVERE_ERR", "ERR"),
073    
074      /**
075       * The severity that will be used for fatal error messages.
076       */
077      FATAL_ERROR(0x00050000, "FATAL_ERR", "ERR"),
078    
079      /**
080       * The severity that will be used for debug messages.
081       */
082      DEBUG(0x00060000, "DEBUG", "DEBUG"),
083    
084      /**
085       * The severity that will be used for important informational
086       * messages.
087       */
088      NOTICE(0x00070000, "NOTICE", "NOTE");
089    
090      static private Set<String> PROPERTY_KEY_FORM_VALUES_SET;
091    
092      static private Map<String,Severity> PROPERTY_KEY_FORM_MAP;
093    
094      static private Map<Integer,Severity> MASK_VALUE_MAP;
095    
096      static {
097        MASK_VALUE_MAP = new HashMap<Integer,Severity>();
098        for (Severity c : EnumSet.allOf(Severity.class)) {
099          MASK_VALUE_MAP.put(c.mask, c);
100        }
101      }
102    
103      static {
104        PROPERTY_KEY_FORM_MAP = new HashMap<String,Severity>();
105        PROPERTY_KEY_FORM_VALUES_SET = new HashSet<String>();
106        for (Severity s : EnumSet.allOf(Severity.class)) {
107          PROPERTY_KEY_FORM_MAP.put(s.propertyKeyFormName(), s);
108          PROPERTY_KEY_FORM_VALUES_SET.add(s.propertyKeyFormName());
109        }
110      }
111    
112      /**
113       * Returns a set of string representing all <code>Severitys'</code>
114       * abbreviated representations.
115       * @return set of messageDescriptorForm strings
116       */
117      static public Set<String> getPropertyKeyFormSet() {
118        return Collections.unmodifiableSet(PROPERTY_KEY_FORM_VALUES_SET);
119      }
120    
121      /**
122       * Obtains the <code>Severity</code> associated with a given mask
123       * value.
124       * @param mask for which a <code>Severity</code> is obtained.
125       * @return Severity associated with <code>mask</code>
126       */
127      static public Severity parseMask(int mask) {
128        Severity sev = MASK_VALUE_MAP.get(mask);
129        if (sev == null) {
130          throw new IllegalArgumentException(
131                  "No Severity defined with int value " + mask);
132        }
133        return sev;
134      }
135    
136      /**
137       * Returns the <code>Severity</code> associated with the input
138       * string <code>s</code> which can either be a severity's name
139       * or messageDescriptorForm.
140       * @param s Severity name or messageDescriptorForm
141       * @return Severity assocated with <code>s</code>
142       */
143      static public Severity parseString(String s) {
144        Severity sev = PROPERTY_KEY_FORM_MAP.get(s);
145        if (sev == null) {
146          sev = valueOf(s);
147        }
148        return sev;
149      }
150    
151      /**
152       * Obtains the <code>Severity</code> associated with the the input
153       * message ID <code>msgId</code>.
154       * @param msgId int message ID
155       * @return Severity assocated with the ID
156       */
157      static public Severity parseMessageId(int msgId) {
158        return parseMask(msgId & 0x000F0000);
159      }
160    
161      private final int mask;
162      private final String propertyKeyForm;
163      private final String messageDescriptorForm;
164    
165      /**
166       * Returns the mask associated with this <code>Severity</code>.
167       * @return mask for this severity
168       */
169      public int getMask() {
170        return mask;
171      }
172    
173      /**
174       * Gets the abbreviated form of this <code>Severity</code>.
175       * @return String abbreviated form
176       */
177      public String messageDesciptorName() {
178        return messageDescriptorForm;
179      }
180    
181      /**
182       * Gets the name of this severity as it must appear in the
183       * property key name in a messages file.
184       *
185       * @return name of this severity
186       */
187      public String propertyKeyFormName() {
188        return propertyKeyForm;
189      }
190    
191      private Severity(int mask, String propertyKeyForm,
192                       String messageDescriptorName) {
193        this.mask = mask;
194        this.propertyKeyForm = propertyKeyForm;
195        this.messageDescriptorForm = messageDescriptorName;
196      }
197    
198    }