KernelUtils.java

  1. /*
  2.  * Copyright (C) 2003-2009 eXo Platform SAS.
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU Affero General Public License
  6.  * as published by the Free Software Foundation; either version 3
  7.  * of the License, or (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, see<http://www.gnu.org/licenses/>.
  16.  */
  17. package org.exoplatform.commons.testing;

  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.Iterator;
  21. import java.util.Map;

  22. import org.exoplatform.container.ExoContainer;
  23. import org.exoplatform.container.ExoContainerContext;
  24. import org.exoplatform.container.xml.InitParams;
  25. import org.exoplatform.container.xml.ObjectParameter;
  26. import org.exoplatform.container.xml.PropertiesParam;
  27. import org.exoplatform.container.xml.Property;
  28. import org.exoplatform.container.xml.ValueParam;
  29. import org.exoplatform.container.xml.ValuesParam;

  30. /**
  31.  * @author <a href="mailto:patrice.lamarque@exoplatform.com">Patrice Lamarque</a>
  32.  * @version $Revision$
  33.  */
  34. public class KernelUtils {
  35.  
  36.   private KernelUtils() {
  37.     // hidden
  38.   }

  39.   @SuppressWarnings("unchecked")
  40.   public static <T>T getService(Class<? extends T> clazz) {
  41.     ExoContainer container = ExoContainerContext.getCurrentContainer();
  42.     return (T) container.getComponentInstanceOfType(clazz);
  43.   }

  44.   public static void addValueParam(InitParams params, String name, String value) {
  45.     ValueParam param = new ValueParam();
  46.     param.setName(name);
  47.     param.setValue(value);
  48.     params.addParameter(param);
  49.    }
  50.  
  51.   public static void addObjectParam(InitParams params, String name, Object value) {
  52.     ObjectParameter param = new ObjectParameter();
  53.     param.setName(name);
  54.     param.setObject(value);
  55.     params.addParameter(param);
  56.    }

  57.   public static void addPropertiesParam(InitParams params, String name, Map<String, String> map) {
  58.     PropertiesParam param = new PropertiesParam();
  59.     param.setName(name);
  60.     Iterator<String> it = map.keySet().iterator();
  61.     while (it.hasNext()) {
  62.       String key = (String) it.next();
  63.       Property prop = new Property(key, map.get(key));
  64.       param.addProperty(prop);
  65.     }
  66.     params.addParameter(param);
  67.   }

  68.   public static void addValuesParam(InitParams params, String name, String... values) {
  69.     ValuesParam param = new ValuesParam();
  70.     param.setName(name);
  71.     param.setValues(new ArrayList<String>(Arrays.asList(values)));
  72.     params.addParameter(param);
  73.   }

  74. }