All Downloads are FREE. Search and download functionalities are using the official Maven repository.

os.rio-core.0.6.0.source-code.GentooChrootBuilder Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2014 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.rio;

import static com.google.common.collect.Lists.newArrayList;

import java.util.List;

import br.com.objectos.rio.core.os.Chroot;
import br.com.objectos.rio.core.os.ChrootMount;
import br.com.objectos.rio.core.os.ChrootUmount;

import br.com.objectos.way.base.io.Stdout;
import br.com.objectos.way.core.io.Directory;

/**
 * @author [email protected] (Marcio Endo)
 */
class GentooChrootBuilder {

  private final Directory mountDir;

  private final List commands = newArrayList();

  public GentooChrootBuilder(Directory mountDir) {
    this.mountDir = mountDir;
  }

  public GentooChrootBuilder add(String command) {
    commands.add(command);
    return this;
  }

  public GentooChrootBuilder add(String template, Object... args) {
    return add(String.format(template, args));
  }

  public GentooChrootBuilder addAll(List commands) {
    this.commands.addAll(commands);
    return this;
  }

  public AddAllIf addAllIf(List commands) {
    return new AddAllIf(commands);
  }

  public void exec() {
    ChrootMount mount = ChrootMount.at(mountDir).mount();

    try {

      Chroot chroot = Chroot.at(mountDir).addAll(commands).exec();
      Stdout.print(chroot);

    } finally {

      ChrootUmount umount = mount.umount();
      Stdout.print(umount);

    }
  }

  public class AddAllIf {

    private final List _cmds;

    public AddAllIf(List cmds) {
      this._cmds = cmds;
    }

    public GentooChrootBuilder isNotNull(Object val) {
      return isTrue(val != null);
    }

    public GentooChrootBuilder isTrue(boolean val) {
      if (val) {
        commands.addAll(_cmds);
      }
      return GentooChrootBuilder.this;
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy