Package com.itstherules.mediacentre.model

Source Code of com.itstherules.mediacentre.model.HostScalar

package com.itstherules.mediacentre.model;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import com.itstherules.stream.model.AnyItemsModel;
import com.itstherules.stream.model.DirectoriesModel;

public class HostScalar {
   
    private static final String CONFIGURATION = "configuration";

  public String value() {
      try {
        String name = new AnyItemsModel(CONFIGURATION).asList().get(0).getName();
        if("".equals(name) || name == null){ throw new NullPointerException();}
        return name;
      } catch (Exception e) {
        try {
          return InetAddress.getLocalHost().getCanonicalHostName();
        } catch (UnknownHostException e2) {
          return "localhost";
        }
      }
    }
   
  public void persist(String hostName) throws IOException {
    String itemPath = itemPath();
    File directory = new File(itemPath);
    for (File file : directory.listFiles()) {
      file.delete();
    }
    File fileToWrite = new File(itemPath + hostName);
    fileToWrite.createNewFile();
  }
 
  private String itemPath() {
    DirectoriesModel directory = new DirectoriesModel(CONFIGURATION);
    return directory.getDirectoryPath();
  }

}
TOP

Related Classes of com.itstherules.mediacentre.model.HostScalar

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.