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   * see Javadoc of <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
29   *
30   * @author Manfred Geiler (latest modification by $Author: jakobk $)
31   * @version $Revision: 1124393 $ $Date: 2011-05-18 14:31:56 -0500 (Wed, 18 May 2011) $
32   */
33  public class FacesMessage
34          implements Serializable
35  {
36      private static final long serialVersionUID = 4851488727794169661L;
37  
38      public static final String FACES_MESSAGES = "javax.faces.Messages";
39  
40      public static final FacesMessage.Severity SEVERITY_INFO = new Severity("Info", 1);
41      public static final FacesMessage.Severity SEVERITY_WARN = new Severity("Warn", 2);
42      public static final FacesMessage.Severity SEVERITY_ERROR = new Severity("Error", 3);
43      public static final FacesMessage.Severity SEVERITY_FATAL = new Severity("Fatal", 4);
44      public static final List VALUES;
45      public static final Map VALUES_MAP;
46      static
47      {
48          Map<String, FacesMessage.Severity> map = new HashMap<String, FacesMessage.Severity>(7);
49          map.put(SEVERITY_INFO.toString(), SEVERITY_INFO);
50          map.put(SEVERITY_WARN.toString(), SEVERITY_WARN);
51          map.put(SEVERITY_ERROR.toString(), SEVERITY_ERROR);
52          map.put(SEVERITY_FATAL.toString(), SEVERITY_FATAL);
53          VALUES_MAP = Collections.unmodifiableMap(map);
54  
55          List<FacesMessage.Severity> severityList = new ArrayList<FacesMessage.Severity>(map.values());
56          Collections.sort(severityList); // the JSF spec requires it to be sorted
57          VALUES = Collections.unmodifiableList(severityList);
58      }
59  
60      private transient FacesMessage.Severity _severity;  // transient, b/c FacesMessage.Severity is not Serializable
61      private String _summary;
62      private String _detail;
63  
64      public FacesMessage()
65      {
66          _severity = SEVERITY_INFO;
67      }
68  
69      public FacesMessage(String summary)
70      {
71          _summary = summary;
72          _severity = SEVERITY_INFO;
73      }
74  
75      public FacesMessage(String summary, String detail)
76      {
77          _summary = summary;
78          _detail = detail;
79          _severity = SEVERITY_INFO;
80      }
81  
82      public FacesMessage(FacesMessage.Severity severity,
83                          String summary,
84                          String detail)
85      {
86          if(severity == null) throw new NullPointerException("severity");
87          _severity = severity;
88          _summary = summary;
89          _detail = detail;
90      }
91  
92      public FacesMessage.Severity getSeverity()
93      {
94          return _severity;
95      }
96  
97      public void setSeverity(FacesMessage.Severity severity)
98      {
99          if(severity == null) throw new NullPointerException("severity");
100         _severity = severity;
101     }
102 
103     public String getSummary()
104     {
105         return _summary;
106     }
107 
108     public void setSummary(String summary)
109     {
110         _summary = summary;
111     }
112 
113     public String getDetail()
114     {
115         if (_detail == null)
116         {
117             // Javadoc:
118             // If no localized detail text has been defined for this message, return the localized summary text instead
119             return _summary;
120         }
121         return _detail;
122     }
123 
124     public void setDetail(String detail)
125     {
126         _detail = detail;
127     }
128 
129     private void writeObject(ObjectOutputStream out) throws IOException
130     {
131         out.defaultWriteObject();  // write summary, detail
132         out.writeInt(_severity._ordinal);  // FacesMessage.Severity is not Serializable, write ordinal only
133     }
134 
135     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
136     {
137         in.defaultReadObject();  // read summary, detail
138 
139         // FacesMessage.Severity is not Serializable, read ordinal and get related FacesMessage.Severity
140         int severityOrdinal = in.readInt();
141         _severity = (Severity) VALUES.get(severityOrdinal - 1);
142     }
143 
144 
145     public static class Severity
146             implements Comparable
147     {
148         private String _name;
149         private int _ordinal;
150 
151         private Severity(String name, int ordinal)
152         {
153             _name = name;
154             _ordinal = ordinal;
155         }
156 
157         public int getOrdinal()
158         {
159             return _ordinal;
160         }
161 
162         public String toString()
163         {
164             return _name;
165         }
166 
167         public int compareTo(Object o)
168         {
169             if (!(o instanceof Severity))
170             {
171                 throw new IllegalArgumentException(o.getClass().getName());
172             }
173             return getOrdinal() - ((Severity)o).getOrdinal();
174         }
175     }
176 
177 }