com.ovea.tadjin.util.Program Maven / Gradle / Ivy
The newest version!
/**
* Copyright (C) 2011 Ovea
*
* 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 com.ovea.tadjin.util;
import com.ovea.tadjin.util.logging.JdkOverLog4j;
import com.ovea.tadjin.util.logging.Logging;
import com.ovea.tadjin.util.properties.PropertyPlaceholderResolver;
import com.ovea.tadjin.util.properties.PropertySettings;
import org.apache.commons.cli.*;
import java.io.*;
import java.lang.management.ManagementFactory;
import java.util.Properties;
public final class Program {
private static final Options OPTIONS = new Options()
.addOption(new Option("c", "conf", true, "Property configuration file (required). I.e. file:./program.properties"))
.addOption(new Option("h", "help", false, "This help"));
private final Class> mainClass;
private final String[] args;
private PropertySettings propertySettings;
private Program(Class> mainClass, String... args) {
this.mainClass = mainClass;
this.args = args;
}
public void validate() {
try {
CommandLine commandLine = new PosixParser().parse(OPTIONS, args);
if (commandLine.hasOption("help"))
helpAndExit();
else if (!commandLine.hasOption("conf"))
helpAndExit();
} catch (ParseException e) {
helpAndExit();
}
}
public void prepare() {
final PropertySettings settings = getOptions();
Properties properties = settings.getProperties();
for (String key : properties.stringPropertyNames())
if (System.getProperty(key) == null)
System.setProperty(key, properties.getProperty(key));
Logging.setupLog4j(settings.getResource("log4j.config"));
JdkOverLog4j.install();
final File pidFile = settings.getPath("pid.file");
if (pidFile.exists()) {
System.err.println("PID file already exists: " + pidFile);
System.exit(2);
}
pidFile.getParentFile().mkdirs();
String pidStr = System.getProperty("pid");
if (pidStr == null) {
pidStr = ManagementFactory.getRuntimeMXBean().getName();
pidStr = pidStr.substring(0, pidStr.indexOf('@'));
}
final int pid = Integer.parseInt(pidStr);
try {
PrintWriter pw = new PrintWriter(pidFile);
pw.print(pid);
pw.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e.getMessage(), e);
}
pidFile.deleteOnExit();
}
private void helpAndExit() {
new HelpFormatter().printHelp(mainClass.getName(), OPTIONS);
System.exit(1);
}
public PropertySettings getOptions() {
try {
if (propertySettings == null) {
CommandLine commandLine = new PosixParser().parse(OPTIONS, args);
Properties properties = load(Resource.from("file:" + commandLine.getOptionValue("conf")));
PropertyPlaceholderResolver resolver = new PropertyPlaceholderResolver();
propertySettings = new PropertySettings(resolver.resolveAll(System.getProperties(), SystemInfo.asProperties(), properties));
}
return propertySettings;
} catch (ParseException e) {
throw new IllegalArgumentException("bad program parameters");
}
}
private static Properties load(Resource propertyFile) {
Properties p = new Properties();
InputStream in = propertyFile.read();
try {
p.load(in);
return p;
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
} finally {
try {
in.close();
} catch (IOException ignored) {
}
}
}
public static Program from(Class> mainClass, String... args) {
return new Program(mainClass, args);
}
}