javax.faces.application.FacesMessage Maven / Gradle / Ivy
Show all versions of javax.faces Show documentation
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package javax.faces.application;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
/**
* FacesMessage represents a single validation (or
* other) message, which is typically associated with a particular
* component in the view. A {@link javax.faces.application.FacesMessage} instance may be created
* based on a specific messageId
. The specification defines
* the set of messageId
s for which there must be
* {@link javax.faces.application.FacesMessage} instances.
*
* The implementation must take the following steps when creating
* FacesMessage
instances given a messageId
:
*
*
*
* Call {@link Application#getMessageBundle}. If
* non-null
, locate the named ResourceBundle
,
* using the Locale
from the current {@link
* javax.faces.component.UIViewRoot} and see if it has a value for the
* argument messageId
. If it does, treat the value as the
* summary
of the FacesMessage
. If it does
* not, or if {@link Application#getMessageBundle} returned
* null
, look in the ResourceBundle
named by
* the value of the constant {@link #FACES_MESSAGES} and see if it has a
* value for the argument messageId
. If it does, treat the
* value as the summary
of the FacesMessage
.
* If it does not, there is no initialization information for the
* FacesMessage
instance.
*
* In all cases, if a ResourceBundle
hit is found for
* the {messageId}
, look for further hits under the key
* {messageId}_detail
. Use this value, if present, as
* the detail
for the returned FacesMessage
.
*
* Make sure to perform any parameter substitution required for the
* summary
and detail
of the
* FacesMessage
.
*
*
*
*/
public class FacesMessage implements Serializable {
// --------------------------------------------------------------- Constants
/**
* ResourceBundle
identifier for messages whose
* message identifiers are defined in the JavaServer Faces
* specification.
*/
public static final String FACES_MESSAGES = "javax.faces.Messages";
// ------------------------------------------------- Message Severity Levels
// Any new Severity values must go at the end of the list, or we will break
// backwards compatibility on serialized instances
private static final String SEVERITY_INFO_NAME = "INFO";
/**
* Message severity level indicating an informational message
* rather than an error.
*/
public static final Severity SEVERITY_INFO =
new Severity(SEVERITY_INFO_NAME);
private static final String SEVERITY_WARN_NAME = "WARN";
/**
* Message severity level indicating that an error might have
* occurred.
*/
public static final Severity SEVERITY_WARN =
new Severity(SEVERITY_WARN_NAME);
private static final String SEVERITY_ERROR_NAME = "ERROR";
/**
* Message severity level indicating that an error has
* occurred.
*/
public static final Severity SEVERITY_ERROR =
new Severity(SEVERITY_ERROR_NAME);
private static final String SEVERITY_FATAL_NAME = "FATAL";
/**
* Message severity level indicating that a serious error has
* occurred.
*/
public static final Severity SEVERITY_FATAL =
new Severity(SEVERITY_FATAL_NAME);
/**
* Array of all defined values, ascending order of ordinal value.
* Be sure you include any new instances created above, in the
* same order.
*/
private static final Severity[] values =
{ SEVERITY_INFO, SEVERITY_WARN, SEVERITY_ERROR, SEVERITY_FATAL };
/**
* Immutable List
of valid {@link javax.faces.application.FacesMessage.Severity}
* instances, in ascending order of their ordinal value.
*/
public static final List VALUES =
Collections.unmodifiableList(Arrays.asList(values));
private static Map _MODIFIABLE_MAP =
new HashMap(4, 1.0f);
static {
for (int i = 0, len = values.length; i < len; i++) {
_MODIFIABLE_MAP.put(values[i].severityName, values[i]);
}
}
/**
* Immutable Map
of valid {@link javax.faces.application.FacesMessage.Severity}
* instances, keyed by name.
*/
public final static Map VALUES_MAP =
Collections.unmodifiableMap(_MODIFIABLE_MAP);
private static final long serialVersionUID = -1180773928220076822L;
// ------------------------------------------------------------ Constructors
/**
* Construct a new {@link javax.faces.application.FacesMessage} with no initial
* values. The severity is set to Severity.INFO.
*/
public FacesMessage() {
super();
}
/**
* Construct a new {@link javax.faces.application.FacesMessage} with just a summary. The
* detail is null
, the severity is set to
* Severity.INFO
.
*/
public FacesMessage(String summary) {
super();
setSummary(summary);
}
/**
* Construct a new {@link javax.faces.application.FacesMessage} with the specified initial
* values. The severity is set to Severity.INFO.
*
* @param summary Localized summary message text
* @param detail Localized detail message text
*
* @throws IllegalArgumentException if the specified severity level
* is not one of the supported values
*/
public FacesMessage(String summary, String detail) {
super();
setSummary(summary);
setDetail(detail);
}
/**
* Construct a new FacesMessage
with the specified
* initial values.
*
* @param severity the severity
* @param summary Localized summary message text
* @param detail Localized detail message text
*
* @throws IllegalArgumentException if the specified severity level
* is not one of the supported values
*/
public FacesMessage(Severity severity, String summary,
String detail) {
super();
setSeverity(severity);
setSummary(summary);
setDetail(detail);
}
// ------------------------------------------------------ Instance Variables
private Severity severity = FacesMessage.SEVERITY_INFO;
private String summary = null;
private String detail = null;
private boolean rendered;
// ---------------------------------------------------------- Public Methods
/**
* Return the localized detail text. If no localized detail text has
* been defined for this message, return the localized summary text
* instead.
*/
public String getDetail() {
if (this.detail == null) {
return (this.summary);
} else {
return (this.detail);
}
}
/**
* Set the localized detail text.
*
* @param detail The new localized detail text
*/
public void setDetail(String detail) {
this.detail = detail;
}
/**
* Return the severity level.
*/
public Severity getSeverity() {
return (this.severity);
}
/**
* Set the severity level.
*
* @param severity The new severity level
*
* @throws IllegalArgumentException if the specified severity level
* is not one of the supported values
*/
public void setSeverity(Severity severity) {
if ((severity.getOrdinal() < SEVERITY_INFO.getOrdinal()) ||
(severity.getOrdinal() > SEVERITY_FATAL.getOrdinal())) {
throw new IllegalArgumentException(String.valueOf(severity));
}
this.severity = severity;
}
/**
* Return the localized summary text.
*/
public String getSummary() {
return (this.summary);
}
/**
* Set the localized summary text.
*
* @param summary The new localized summary text
*/
public void setSummary(String summary) {
this.summary = summary;
}
/**
* @return true
if {@link #rendered()} has been called, otherwise
* false
*
* @since 2.0
*/
public boolean isRendered() {
return rendered;
}
/**
* Marks this message as having been rendered to the client.
*
* @since 2.0
*/
public void rendered() {
this.rendered = true;
}
/**
* Persist {@link javax.faces.application.FacesMessage} artifacts,
* including the non serializable Severity
.
*/
private void writeObject(ObjectOutputStream out) throws IOException {
out.writeInt(getSeverity().getOrdinal());
out.writeObject(getSummary());
out.writeObject(getDetail());
out.writeObject(isRendered());
}
/**
* Reconstruct {@link javax.faces.application.FacesMessage} from
* serialized artifacts.
*/
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException {
severity = SEVERITY_INFO;
summary = null;
detail = null;
int ordinal = in.readInt();
if (ordinal == SEVERITY_INFO.getOrdinal()) {
setSeverity(FacesMessage.SEVERITY_INFO);
} else if (ordinal == SEVERITY_WARN.getOrdinal()) {
setSeverity(FacesMessage.SEVERITY_WARN);
} else if (ordinal == SEVERITY_ERROR.getOrdinal()) {
setSeverity(FacesMessage.SEVERITY_ERROR);
} else if (ordinal == SEVERITY_FATAL.getOrdinal()) {
setSeverity(FacesMessage.SEVERITY_FATAL);
}
setSummary((String)in.readObject());
setDetail((String)in.readObject());
this.rendered = (Boolean) in.readObject();
}
/**
* Class used to represent message severity levels in a typesafe
* enumeration.
*/
public static class Severity implements Comparable {
// ------------------------------------------------------- Constructors
/**
* Private constructor to disable the creation of new
* instances.
*/
private Severity(String newSeverityName) {
severityName = newSeverityName;
}
// -------------------------------------------------- Instance Variables
/**
* The ordinal value assigned to this instance.
*/
private final int ordinal = nextOrdinal++;
/**
* The (optional) name for this severity.
*/
String severityName = null;
// ----------------------------------------------------- Public Methods
/**
* Compare this {@link javax.faces.application.FacesMessage.Severity} instance to the
* specified one. Returns a negative integer, zero, or a
* positive integer if this object is less than, equal to, or
* greater than the specified object.
*
* @param other The other object to be compared to
*/
public int compareTo(Object other) {
return this.ordinal - ((Severity) other).ordinal;
}
/**
* Return the ordinal value of this {@link
* FacesMessage.Severity} instance.
*/
public int getOrdinal() {
return (this.ordinal);
}
/**
* Return a String representation of this {@link
* FacesMessage.Severity} instance.
*/
public String toString() {
if (null == severityName) {
return (String.valueOf(this.ordinal));
}
return (String.valueOf(this.severityName) + ' ' + this.ordinal);
}
// --------------------------------------------------- Static Variables
/**
* Static counter returning the ordinal value to be assigned to the
* next instance that is created.
*/
private static int nextOrdinal = 0;
}
}