001package com.hammurapi.common; 002 003import java.lang.reflect.Array; 004import java.util.ArrayList; 005import java.util.Iterator; 006import java.util.List; 007import java.util.ServiceLoader; 008 009/** 010 * Helper class. 011 * @author Pavel Vlasov 012 * 013 */ 014public class Util { 015 016 /** 017 * Loads single instance of service. 018 * @param <T> 019 * @param type 020 * @return 021 */ 022 static Context loadContext() { 023 List<Context> parts = new ArrayList<Context>(); 024 Iterator<Context> it = ServiceLoader.load(Context.class, Util.class.getClassLoader()).iterator(); 025 while (it.hasNext()) { 026 parts.add(it.next()); 027 } 028 parts.add(new DefaultContext(Util.class.getClassLoader(), null)); 029 return new CompositeContext(parts.toArray(new Context[parts.size()])); 030 } 031 032 /** 033 * Wraps a single object into array to avoid problems with generic arrays. 034 * @param <V> 035 * @param obj 036 * @return 037 */ 038 @SuppressWarnings("unchecked") 039 public static <V> V[] wrap(V obj) { 040 if (obj == null) { 041 return (V[]) new Object[] {null}; 042 } 043 V[] ret = (V[]) Array.newInstance(obj.getClass(), 1); 044 ret[0] = obj; 045 return ret; 046 } 047 048}