All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.seam.international.status.MessageFactory Maven / Gradle / Ivy

The newest version!
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2010, Red Hat, Inc., and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.seam.international.status;

import java.io.Serializable;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;

import org.jboss.seam.international.status.builder.BundleKey;
import org.jboss.seam.international.status.builder.BundleTemplateMessage;
import org.jboss.seam.international.status.builder.TemplateMessage;

/**
 * A utility for building {@link Message} objects via message templates, or
 * message bundles. See {@link TemplateMessage} or {@link BundleTemplateMessage}
 * .
 * 
 * @author Lincoln Baxter, III
 * 
 */
public class MessageFactory implements Serializable
{
   private static final long serialVersionUID = -7899463141244189001L;

   @Inject
   BeanManager manager;

   /*
    * Bundle Factory Methods
    */
   public BundleTemplateMessage info(final BundleKey message)
   {
      return getContextualInstance(BundleTemplateMessage.class).text(message).level(Level.INFO);
   }

   public BundleTemplateMessage info(final BundleKey message, final Object... params)
   {
      return getContextualInstance(BundleTemplateMessage.class).text(message).level(Level.INFO).textParams(params);
   }

   public BundleTemplateMessage warn(final BundleKey message)
   {
      return getContextualInstance(BundleTemplateMessage.class).text(message).level(Level.WARN);
   }

   public BundleTemplateMessage warn(final BundleKey message, final Object... params)
   {
      return getContextualInstance(BundleTemplateMessage.class).text(message).level(Level.WARN).textParams(params);
   }

   public BundleTemplateMessage error(final BundleKey message)
   {
      return getContextualInstance(BundleTemplateMessage.class).text(message).level(Level.ERROR);
   }

   public BundleTemplateMessage error(final BundleKey message, final Object... params)
   {
      return getContextualInstance(BundleTemplateMessage.class).text(message).level(Level.ERROR).textParams(params);
   }

   public BundleTemplateMessage fatal(final BundleKey message)
   {
      return getContextualInstance(BundleTemplateMessage.class).text(message).level(Level.FATAL);
   }

   public BundleTemplateMessage fatal(final BundleKey message, final Object... params)
   {
      return getContextualInstance(BundleTemplateMessage.class).text(message).level(Level.FATAL).textParams(params);
   }

   /*
    * Template Factory Methods
    */
   public TemplateMessage info(final String message)
   {
      return getContextualInstance(TemplateMessage.class).text(message).level(Level.INFO);
   }

   public TemplateMessage info(final String message, final Object... params)
   {
      return getContextualInstance(TemplateMessage.class).text(message).level(Level.INFO).textParams(params);
   }

   public TemplateMessage warn(final String message)
   {
      return getContextualInstance(TemplateMessage.class).text(message).level(Level.WARN);
   }

   public TemplateMessage warn(final String message, final Object... params)
   {
      return getContextualInstance(TemplateMessage.class).text(message).level(Level.WARN).textParams(params);
   }

   public TemplateMessage error(final String message)
   {
      return getContextualInstance(TemplateMessage.class).text(message).level(Level.ERROR);
   }

   public TemplateMessage error(final String message, final Object... params)
   {
      return getContextualInstance(TemplateMessage.class).text(message).level(Level.ERROR).textParams(params);
   }

   public TemplateMessage fatal(final String message)
   {
      return getContextualInstance(TemplateMessage.class).text(message).level(Level.FATAL);
   }

   public TemplateMessage fatal(final String message, final Object... params)
   {
      return getContextualInstance(TemplateMessage.class).text(message).level(Level.FATAL).textParams(params);
   }

   /**
    * Get a single CDI managed instance of a specific class. Return only the
    * first result if multiple beans are available.
    * 

* NOTE: Using this method should be avoided at all costs. * * @param manager The bean manager with which to perform the lookup. * @param type The class for which to return an instance. * @return The managed instance, or null if none could be provided. */ @SuppressWarnings("unchecked") private T getContextualInstance(final Class type) { T result = null; Bean bean = (Bean) manager.resolve(manager.getBeans(type)); if (bean != null) { CreationalContext context = manager.createCreationalContext(bean); if (context != null) { result = (T) manager.getReference(bean, type, context); } } return result; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy