001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.examples.group.bgd.utils; 020 021import org.apache.reef.driver.task.TaskConfiguration; 022import org.apache.reef.driver.task.TaskConfigurationOptions; 023import org.apache.reef.examples.group.bgd.MasterTask; 024import org.apache.reef.tang.Configuration; 025import org.apache.reef.tang.Injector; 026import org.apache.reef.tang.JavaConfigurationBuilder; 027import org.apache.reef.tang.Tang; 028import org.apache.reef.tang.annotations.Name; 029import org.apache.reef.tang.exceptions.InjectionException; 030import org.apache.reef.tang.formats.AvroConfigurationSerializer; 031import org.apache.reef.tang.formats.ConfigurationSerializer; 032 033import java.util.logging.Level; 034import java.util.logging.Logger; 035 036/** 037 * Subconfiguration: limit given configuration to a given list of classes. 038 */ 039public final class SubConfiguration { 040 041 private static final Logger LOG = Logger.getLogger(SubConfiguration.class.getName()); 042 043 @SafeVarargs 044 public static Configuration from( 045 final Configuration baseConf, final Class<? extends Name<?>>... classes) { 046 047 final Injector injector = Tang.Factory.getTang().newInjector(baseConf); 048 final JavaConfigurationBuilder confBuilder = Tang.Factory.getTang().newConfigurationBuilder(); 049 050 for (final Class<? extends Name<?>> clazz : classes) { 051 try { 052 confBuilder.bindNamedParameter(clazz, 053 injector.getNamedInstance((Class<? extends Name<Object>>) clazz).toString()); 054 } catch (final InjectionException ex) { 055 final String msg = "Exception while creating subconfiguration"; 056 LOG.log(Level.WARNING, msg, ex); 057 throw new RuntimeException(msg, ex); 058 } 059 } 060 061 return confBuilder.build(); 062 } 063 064 public static void main(final String[] args) throws InjectionException { 065 066 final Configuration conf = TaskConfiguration.CONF 067 .set(TaskConfiguration.IDENTIFIER, "TASK") 068 .set(TaskConfiguration.TASK, MasterTask.class) 069 .build(); 070 071 final ConfigurationSerializer confSerizalizer = new AvroConfigurationSerializer(); 072 final Configuration subConf = SubConfiguration.from(conf, TaskConfigurationOptions.Identifier.class); 073 LOG.log(Level.INFO, "OUT: Base conf:\n{0}", confSerizalizer.toString(conf)); 074 LOG.log(Level.INFO, "OUT: Sub conf:\n{0}", confSerizalizer.toString(subConf)); 075 } 076 077 /** 078 * Empty private constructor to prohibit instantiation of utility class. 079 */ 080 private SubConfiguration() { 081 } 082}