diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/Messages.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/Messages.java index f0a2e145a..759f8af31 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/Messages.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/Messages.java @@ -22,14 +22,10 @@ import java.util.List; import java.util.Map; -import javax.annotation.PostConstruct; -import javax.enterprise.context.RequestScoped; -import javax.inject.Inject; +import javax.enterprise.inject.Vetoed; import org.slf4j.Logger; -import br.com.caelum.vraptor.Result; - /** * Managed class that stores all application messages like errors, warnings and info. This * class is useful to display messages categorized by severity in your view. To choose a severity @@ -43,37 +39,14 @@ * @since 4.1 * @author Otávio S Garcia */ -@RequestScoped +@Vetoed public class Messages { - public static final String ATTRIBUTE_KEY = "vmessages"; - private final static Logger log = getLogger(Messages.class); private Map> messages = new HashMap<>(); private boolean unhandledErrors = false; - private final Result result; - - /** - * @deprecated CDI eyes only - */ - protected Messages() { - this(null); - } - - @Inject - public Messages(Result result) { - this.result = result; - } - - @PostConstruct - protected void init() { - if(!result.included().containsKey(ATTRIBUTE_KEY)){ - result.include(ATTRIBUTE_KEY, this); - } - } - public Messages add(Message message) { get(message.getSeverity()).add(message); if(Severity.ERROR.equals(message.getSeverity())) { diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/MessagesProducer.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/MessagesProducer.java new file mode 100644 index 000000000..e1d4f3f5f --- /dev/null +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/MessagesProducer.java @@ -0,0 +1,38 @@ +package br.com.caelum.vraptor.validator; + +import static com.google.common.base.Objects.firstNonNull; + +import javax.enterprise.context.RequestScoped; +import javax.enterprise.inject.Produces; +import javax.inject.Inject; + +import br.com.caelum.vraptor.Result; + +@RequestScoped +public class MessagesProducer { + + private static final String MESSAGES_KEY = "vmessages"; + + private final Result result; + + /** + * @deprecated CDI eyes only + */ + protected MessagesProducer() { + this(null); + } + + @Inject + public MessagesProducer(Result result) { + this.result = result; + } + + @Produces @RequestScoped + public Messages create() { + Messages messages = (Messages) result.included().get(MESSAGES_KEY); + messages = firstNonNull(messages, new Messages()); + result.include(MESSAGES_KEY, messages); + return messages; + } + +} \ No newline at end of file