View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package javax.faces.application;
20  
21  import java.io.IOException;
22  import java.io.ObjectInputStream;
23  import java.io.ObjectOutputStream;
24  import java.io.Serializable;
25  import java.util.*;
26  
27  /**
28   * Represents a message to be displayed to the JSF application user.
29   * <p>
30   * Instances of this type are registered via FacesContext.addMessage, and are kept only until
31   * the end of the render phase. The standard h:message or h:messages components can render
32   * these message objects.
33   * <p>
34   * See the javadoc for this class in the
35   * <a href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/api/index.html">JSF Specification</a>
36   * for more details.
37   *
38   * @author Manfred Geiler (latest modification by $Author: jakobk $)
39   * @version $Revision: 1124395 $ $Date: 2011-05-18 14:37:27 -0500 (Wed, 18 May 2011) $
40   */
41  public class FacesMessage
42          implements Serializable
43  {
44      private static final long serialVersionUID = 4851488727794169661L;
45  
46      public static final String FACES_MESSAGES = "javax.faces.Messages";
47  
48      public static final FacesMessage.Severity SEVERITY_INFO = new Severity("Info", 1);
49      public static final FacesMessage.Severity SEVERITY_WARN = new Severity("Warn", 2);
50      public static final FacesMessage.Severity SEVERITY_ERROR = new Severity("Error", 3);
51      public static final FacesMessage.Severity SEVERITY_FATAL = new Severity("Fatal", 4);
52      public static final List VALUES;
53      public static final Map VALUES_MAP;
54      static
55      {
56          Map map = new HashMap(7);
57          map.put(SEVERITY_INFO.toString(), SEVERITY_INFO);
58          map.put(SEVERITY_WARN.toString(), SEVERITY_WARN);
59          map.put(SEVERITY_ERROR.toString(), SEVERITY_ERROR);
60          map.put(SEVERITY_FATAL.toString(), SEVERITY_FATAL);
61          VALUES_MAP = Collections.unmodifiableMap(map);
62  
63          List severityList = new ArrayList(map.values());
64          Collections.sort(severityList); // the JSF spec requires it to be sorted
65          VALUES = Collections.unmodifiableList(severityList);
66      }
67  
68      private transient FacesMessage.Severity _severity;  // transient, b/c FacesMessage.Severity is not Serializable
69      private String _summary;
70      private String _detail;
71  
72      public FacesMessage()
73      {
74          _severity = SEVERITY_INFO;
75      }
76  
77      public FacesMessage(String summary)
78      {
79          _summary = summary;
80          _severity = SEVERITY_INFO;
81      }
82  
83      public FacesMessage(String summary, String detail)
84      {
85          _summary = summary;
86          _detail = detail;
87          _severity = SEVERITY_INFO;
88      }
89  
90      public FacesMessage(FacesMessage.Severity severity,
91                          String summary,
92                          String detail)
93      {
94          if(severity == null) throw new NullPointerException("severity");
95          _severity = severity;
96          _summary = summary;
97          _detail = detail;
98      }
99  
100     public FacesMessage.Severity getSeverity()
101     {
102         return _severity;
103     }
104 
105     public void setSeverity(FacesMessage.Severity severity)
106     {
107         if(severity == null) throw new NullPointerException("severity");
108         _severity = severity;
109     }
110 
111     public String getSummary()
112     {
113         return _summary;
114     }
115 
116     public void setSummary(String summary)
117     {
118         _summary = summary;
119     }
120 
121     public String getDetail()
122     {
123         if (_detail == null)
124         {
125             // Javadoc:
126             // If no localized detail text has been defined for this message, return the localized summary text instead
127             return _summary;
128         }
129         return _detail;
130     }
131 
132     public void setDetail(String detail)
133     {
134         _detail = detail;
135     }
136 
137     private void writeObject(ObjectOutputStream out) throws IOException
138     {
139         out.defaultWriteObject();  // write summary, detail
140         out.writeInt(_severity._ordinal);  // FacesMessage.Severity is not Serializable, write ordinal only
141     }
142 
143     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
144     {
145         in.defaultReadObject();  // read summary, detail
146 
147         // FacesMessage.Severity is not Serializable, read ordinal and get related FacesMessage.Severity
148         int severityOrdinal = in.readInt();
149         _severity = (Severity) VALUES.get(severityOrdinal - 1);
150     }
151 
152 
153     public static class Severity
154             implements Comparable
155     {
156         private String _name;
157         private int _ordinal;
158 
159         private Severity(String name, int ordinal)
160         {
161             _name = name;
162             _ordinal = ordinal;
163         }
164 
165         public int getOrdinal()
166         {
167             return _ordinal;
168         }
169 
170         public String toString()
171         {
172             return _name;
173         }
174 
175         public int compareTo(Object o)
176         {
177             if (!(o instanceof Severity))
178             {
179                 throw new IllegalArgumentException(o.getClass().getName());
180             }
181             return getOrdinal() - ((Severity)o).getOrdinal();
182         }
183     }
184 
185 }