This project has retired. For details please refer to its Attic page.
Source code
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.tang.util;
020
021import org.apache.reef.tang.*;
022import org.apache.reef.tang.annotations.Name;
023import org.apache.reef.tang.annotations.NamedParameter;
024import org.apache.reef.tang.annotations.Parameter;
025import org.apache.reef.tang.exceptions.BindException;
026import org.apache.reef.tang.exceptions.InjectionException;
027import org.apache.reef.tang.formats.CommandLine;
028import org.apache.reef.tang.formats.ConfigurationFile;
029import org.apache.reef.tang.implementation.InjectionPlan;
030import org.apache.reef.tang.implementation.protobuf.ProtocolBufferClassHierarchy;
031import org.apache.reef.tang.proto.ClassHierarchyProto;
032
033import javax.inject.Inject;
034import java.io.File;
035import java.io.FileInputStream;
036import java.io.IOException;
037import java.io.InputStream;
038
039public class ValidateConfiguration {
040  private final String target;
041  private final File ch;
042  private final File inConfig;
043  private final File outConfig;
044//  @NamedParameter(short_name="ip")
045//  public class InjectionPlanOut implements Name<File> { }
046
047  @Inject
048  public ValidateConfiguration(
049      @Parameter(ClassHierarchyIn.class) File ch,
050      @Parameter(ConfigurationIn.class) File inConfig,
051      @Parameter(ConfigurationOut.class) File outConfig) {
052    this.target = null;
053    this.ch = ch;
054    this.inConfig = inConfig;
055    this.outConfig = outConfig;
056//    this.injectionPlan = injectionPlan;
057  }
058
059  @Inject
060  public ValidateConfiguration(
061      @Parameter(Target.class) String injectedClass,
062      @Parameter(ClassHierarchyIn.class) File ch,
063      @Parameter(ConfigurationIn.class) File inConfig,
064      @Parameter(ConfigurationOut.class) File outConfig) {
065    this.target = injectedClass;
066    this.ch = ch;
067    this.inConfig = inConfig;
068    this.outConfig = outConfig;
069  }
070
071  public static void main(String[] argv) throws IOException, BindException, InjectionException {
072    @SuppressWarnings("unchecked")
073    JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder((Class<? extends ExternalConstructor<?>>[]) new Class[]{FileParser.class});
074    CommandLine cl = new CommandLine(cb);
075    cl.processCommandLine(argv,
076        Target.class,
077        ClassHierarchyIn.class,
078        ConfigurationIn.class,
079        ConfigurationOut.class);
080    ValidateConfiguration bip = Tang.Factory.getTang().newInjector(cb.build()).getInstance(ValidateConfiguration.class);
081    bip.validatePlan();
082  }
083
084  public void validatePlan() throws IOException, BindException, InjectionException {
085
086    final Tang t = Tang.Factory.getTang();
087
088    final ClassHierarchyProto.Node root;
089    try (final InputStream chin = new FileInputStream(this.ch)) {
090      root = ClassHierarchyProto.Node.parseFrom(chin);
091    }
092
093    final ClassHierarchy ch = new ProtocolBufferClassHierarchy(root);
094    final ConfigurationBuilder cb = t.newConfigurationBuilder(ch);
095
096    if (!inConfig.canRead()) {
097      throw new IOException("Cannot read input config file: " + inConfig);
098    }
099
100    ConfigurationFile.addConfiguration(cb, inConfig);
101
102    if (target != null) {
103      final Injector i = t.newInjector(cb.build());
104      final InjectionPlan<?> ip = i.getInjectionPlan(target);
105      if (!ip.isInjectable()) {
106        throw new InjectionException(target + " is not injectable: " + ip.toCantInjectString());
107      }
108    }
109
110    ConfigurationFile.writeConfigurationFile(cb.build(), outConfig);
111
112//    Injector i = t.newInjector(cb.build());
113//    InjectionPlan<?> ip = i.getInjectionPlan(target);
114//    try (final OutputStream ipout = new FileOutputStream(injectionPlan)) {
115//      new ProtocolBufferInjectionPlan().serialize(ip).writeTo(ipout);
116//    }
117  }
118
119  public static class FileParser implements ExternalConstructor<File> {
120    private final File f;
121
122    @Inject
123    FileParser(String name) {
124      f = new File(name);
125    }
126
127    @Override
128    public File newInstance() {
129      return f;
130    }
131
132  }
133//  private final File injectionPlan;
134
135  @NamedParameter(short_name = "class")
136  public class Target implements Name<String> {
137  }
138
139  @NamedParameter(short_name = "ch")
140  public class ClassHierarchyIn implements Name<File> {
141  }
142
143  @NamedParameter(short_name = "in")
144  public class ConfigurationIn implements Name<File> {
145  }
146
147  @NamedParameter(short_name = "out")
148  public class ConfigurationOut implements Name<File> {
149  }
150}