List of usage examples for java.util.prefs Preferences isUserNode
public abstract boolean isUserNode();
From source file:com.delcyon.capo.Configuration.java
@SuppressWarnings({ "unchecked", "static-access" })
public Configuration(String... programArgs) throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
documentBuilder = documentBuilderFactory.newDocumentBuilder();
options = new Options();
// the enum this is a little complicated, but it gives us a nice
// centralized place to put all of the system parameters
// and lets us iterate of the list of options and preferences
PREFERENCE[] preferences = PREFERENCE.values();
for (PREFERENCE preference : preferences) {
// not the most elegant, but there is no default constructor, but
// the has arguments value is always there
OptionBuilder optionBuilder = OptionBuilder.hasArg(preference.hasArgument);
if (preference.hasArgument == true) {
String[] argNames = preference.arguments;
for (String argName : argNames) {
optionBuilder = optionBuilder.withArgName(argName);
}/*from w w w .j a v a 2 s.c o m*/
}
optionBuilder = optionBuilder.withDescription(preference.getDescription());
optionBuilder = optionBuilder.withLongOpt(preference.getLongOption());
options.addOption(optionBuilder.create(preference.getOption()));
preferenceHashMap.put(preference.toString(), preference);
}
//add dynamic options
Set<String> preferenceProvidersSet = CapoApplication.getAnnotationMap()
.get(PreferenceProvider.class.getCanonicalName());
if (preferenceProvidersSet != null) {
for (String className : preferenceProvidersSet) {
Class preferenceClass = Class.forName(className).getAnnotation(PreferenceProvider.class)
.preferences();
if (preferenceClass.isEnum()) {
Object[] enumObjects = preferenceClass.getEnumConstants();
for (Object enumObject : enumObjects) {
Preference preference = (Preference) enumObject;
//filter out any preferences that don't belong on this server or client.
if (preference.getLocation() != Location.BOTH) {
if (CapoApplication.isServer() == true && preference.getLocation() == Location.CLIENT) {
continue;
} else if (CapoApplication.isServer() == false
&& preference.getLocation() == Location.SERVER) {
continue;
}
}
preferenceHashMap.put(preference.toString(), preference);
boolean hasArgument = false;
if (preference.getArguments() == null || preference.getArguments().length == 0) {
hasArgument = false;
} else {
hasArgument = true;
}
OptionBuilder optionBuilder = OptionBuilder.hasArg(hasArgument);
if (hasArgument == true) {
String[] argNames = preference.getArguments();
for (String argName : argNames) {
optionBuilder = optionBuilder.withArgName(argName);
}
}
optionBuilder = optionBuilder.withDescription(preference.getDescription());
optionBuilder = optionBuilder.withLongOpt(preference.getLongOption());
options.addOption(optionBuilder.create(preference.getOption()));
}
}
}
}
// create parser
CommandLineParser commandLineParser = new GnuParser();
this.commandLine = commandLineParser.parse(options, programArgs);
Preferences systemPreferences = Preferences
.systemNodeForPackage(CapoApplication.getApplication().getClass());
String capoDirString = null;
while (true) {
capoDirString = systemPreferences.get(PREFERENCE.CAPO_DIR.longOption, null);
if (capoDirString == null) {
systemPreferences.put(PREFERENCE.CAPO_DIR.longOption, PREFERENCE.CAPO_DIR.defaultValue);
capoDirString = PREFERENCE.CAPO_DIR.defaultValue;
try {
systemPreferences.sync();
} catch (BackingStoreException e) {
//e.printStackTrace();
if (systemPreferences.isUserNode() == false) {
System.err.println("Problem with System preferences, trying user's");
systemPreferences = Preferences
.userNodeForPackage(CapoApplication.getApplication().getClass());
continue;
} else //just bail out
{
throw e;
}
}
}
break;
}
disableAutoSync = hasOption(PREFERENCE.DISABLE_CONFIG_AUTOSYNC);
File capoDirFile = new File(capoDirString);
if (capoDirFile.exists() == false) {
if (disableAutoSync == false) {
capoDirFile.mkdirs();
}
}
File configDir = new File(capoDirFile, PREFERENCE.CONFIG_DIR.defaultValue);
if (configDir.exists() == false) {
if (disableAutoSync == false) {
configDir.mkdirs();
}
}
if (disableAutoSync == false) {
capoConfigFile = new File(configDir, CONFIG_FILENAME);
if (capoConfigFile.exists() == false) {
Document configDocument = CapoApplication.getDefaultDocument("config.xml");
FileOutputStream configFileOutputStream = new FileOutputStream(capoConfigFile);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(new DOMSource(configDocument), new StreamResult(configFileOutputStream));
configFileOutputStream.close();
}
configDocument = documentBuilder.parse(capoConfigFile);
} else //going memory only, because of disabled auto sync
{
configDocument = CapoApplication.getDefaultDocument("config.xml");
}
if (configDocument instanceof CDocument) {
((CDocument) configDocument).setSilenceEvents(true);
}
loadPreferences();
preferenceValueHashMap.put(PREFERENCE.CAPO_DIR.longOption, capoDirString);
//print out preferences
//this also has the effect of persisting all of the default values if a values doesn't already exist
for (PREFERENCE preference : preferences) {
if (getValue(preference) != null) {
CapoApplication.logger.log(Level.CONFIG, preference.longOption + "='" + getValue(preference) + "'");
}
}
CapoApplication.logger.setLevel(Level.parse(getValue(PREFERENCE.LOGGING_LEVEL)));
}