001/* 002# Licensed Materials - Property of IBM 003# Copyright IBM Corp. 2015 004 */ 005package simple; 006 007import java.util.HashMap; 008import java.util.Map; 009import java.util.logging.Level; 010import java.util.logging.Logger; 011 012import com.ibm.streams.operator.logging.TraceLevel; 013import com.ibm.streamsx.topology.context.ContextProperties; 014import com.ibm.streamsx.topology.context.JobProperties; 015import com.ibm.streamsx.topology.context.StreamsContext; 016 017public class Util { 018 private static Logger LOGGER = Logger.getLogger("simple.util"); 019 020 private static Map<String,Level> levelMap = new HashMap<String,Level>(); 021 static { 022 levelMap.put("off", TraceLevel.OFF); 023 levelMap.put("error", TraceLevel.ERROR); 024 levelMap.put("warn", TraceLevel.WARN); 025 levelMap.put("info", TraceLevel.INFO); 026 levelMap.put("trace", TraceLevel.TRACE); 027 levelMap.put("debug", TraceLevel.DEBUG); 028 } 029 030 /** 031 * Look for configuration properties present in <code>args</code> 032 * and sets the property in the <code>config</code> map. 033 * <p> 034 * An property item in <code>args</code> is required to be in the format: 035 * <pre> 036 * <property-name>=<value> 037 * </pre> 038 * @param args list to scan. Unrecognized items are silently ignored. 039 * @return configuration map 040 * @see StreamsContext#submit(com.ibm.streamsx.topology.Topology, Map) 041 */ 042 public static Map<String,Object> createConfig(String[] args) { 043 String[] cfgItems = { 044 ContextProperties.APP_DIR, // String 045 ContextProperties.KEEP_ARTIFACTS, // Boolean 046 ContextProperties.TOOLKIT_DIR, // String 047 ContextProperties.TRACING_LEVEL, // java.util.logging.Level 048 // TODO ContextProperties.VMARGS, // List<String> 049 JobProperties.GROUP, // String 050 JobProperties.NAME, // String 051 JobProperties.OVERRIDE_RESOURCE_LOAD_PROTECTION, // Boolean 052 JobProperties.DATA_DIRECTORY, // String 053 JobProperties.PRELOAD_APPLICATION_BUNDLES, // Boolean 054 }; 055 056 Map<String,Object> config = new HashMap<String,Object>(); 057 058 // handle param=<value> 059 for (String arg : args) { 060 String[] toks = arg.split("=", 2); 061 if (toks.length > 1) { 062 for (String item : cfgItems) { 063 if (toks[0].equals(item)) { 064 String valStr = toks[1]; 065 Object value = valStr; 066 if (item.equals(ContextProperties.TRACING_LEVEL)) 067 value = toTracingLevel(valStr); 068 else if (item.equals(ContextProperties.KEEP_ARTIFACTS)) 069 value = Boolean.valueOf(valStr); 070 else if (item.equals(JobProperties.OVERRIDE_RESOURCE_LOAD_PROTECTION)) 071 value = Boolean.valueOf(valStr); 072 else if (item.equals(JobProperties.PRELOAD_APPLICATION_BUNDLES)) 073 value = Boolean.valueOf(valStr); 074 075 config.put(item, value); 076 LOGGER.info("Setting config: item="+item+" value="+value+" [arg="+arg+"]"); 077 } 078 } 079 } 080 } 081 082 return config; 083 } 084 085 private static Level toTracingLevel(String str) { 086 Level l = levelMap.get(str.toLowerCase()); 087 if (l==null) 088 throw new IllegalArgumentException("Unrecognized tracing level '"+str+"'." 089 + " Must be one of: "+levelMap.keySet()); 090 return l; 091 } 092 093}