/* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * See LICENSE.txt included in this distribution for the specific * language governing permissions and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at LICENSE.txt. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. */ package org.opensolaris.opengrok.management.client; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.logging.Level; import org.opensolaris.opengrok.configuration.Configuration; import org.opensolaris.opengrok.management.Constants; import org.opensolaris.opengrok.management.OGAgent; import org.opensolaris.opengrok.util.IOUtils; /** * * @author Jan S Berg */ public class SettingsPersistence { /** property key used to obtain the timeout for indexing */ public static final String INDEXTIMEOUTKEY = Configuration.PROPERTY_KEY_PREFIX + "management.indextimeout"; /** property key used to obtain the JMX connection timeout value */ public static final String CONNECTIONTIMEOUTKEY = Configuration.PROPERTY_KEY_PREFIX + "management.connectiontimeout"; /** property key used to obtain the file log path */ public static final String LOGGINGPATHKEY = Constants.LOG_PATH; /** property key used to obtain file log level */ public static final String FILELOGLEVELKEY = Configuration.PROPERTY_KEY_PREFIX + "management.logging.filelevel"; /** property key used to obtain console log level */ public static final String CONSOLELOGLEVELKEY = Configuration.PROPERTY_KEY_PREFIX + "management.logging.consolelevel"; private final Properties ogcProperties = new Properties(); /** * Get settings for the OpenGrok Management Agent Client * Try if a config file has been given * if not set, try default settings (oga.properties in management directory) * @param cfgfile * @throws java.io.IOException */ @SuppressWarnings("resource") public SettingsPersistence(String cfgfile) throws IOException { InputStream in = null; try { in = OGAgent.class.getResourceAsStream("oga.properties"); if (in != null) { ogcProperties.load(in); } //do we need to propagate this up ? } finally { IOUtils.close(in); } if (cfgfile != null) { File propertyFile = new File(cfgfile); FileInputStream is = null; try { is = new FileInputStream(propertyFile); ogcProperties.load(is); //do we need to propagate this up ? } finally { //NOPMD IOUtils.close(is); } } } /** * Get the JMX URL to the agent. Generate a URL from host and port * properties if no URL has been specified. * * @return the URL to the agent */ public String getAgentUrl() { String url = ogcProperties.getProperty(Constants.JMX_URL); if (url == null) { String host = ogcProperties.getProperty(Constants.JMX_HOST, "localhost"); int jmxport = Integer.parseInt( ogcProperties.getProperty(Constants.JMX_PORT, "9292")); int rmiport = Integer.parseInt(ogcProperties.getProperty( Constants.RMI_PORT, String.valueOf(jmxport + 1))); url = "service:jmx:rmi://" + host + ":" + jmxport + "/jndi/rmi://" + host + ":" + rmiport + "/opengrok"; } return url; } /** * Get the property with the given key. * @param key key to use for lookup. * @return {@code null} if not found, the corresponding value otherwise. */ public String getProperty(String key) { return ogcProperties.getProperty(key); } /** * Set the property with the given key. * @param key key to use for mapping. * @param val value to set. */ public void setProperty(String key, String val) { ogcProperties.setProperty(key, val); } /** * Get log level assigned for logging to file. * @return {@code null} if not found, the corresponding value otherwise. */ public Level getFileLogLevel() { return Level.parse(ogcProperties.getProperty(FILELOGLEVELKEY)); } /** * Get log level assigned for logging to console. * @return {@code null} if not found, the corresponding value otherwise. */ public Level getConsoleLogLevel() { return Level.parse(ogcProperties.getProperty(CONSOLELOGLEVELKEY)); } }