b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg/*
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg * CDDL HEADER START
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg *
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg * The contents of this file are subject to the terms of the
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg * Common Development and Distribution License (the "License").
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg * You may not use this file except in compliance with the License.
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg *
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg * See LICENSE.txt included in this distribution for the specific
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg * language governing permissions and limitations under the License.
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg *
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg * When distributing Covered Code, include this CDDL HEADER in each
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg * file and include the License file at LICENSE.txt.
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg * If applicable, add the following below this CDDL HEADER, with the
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg * fields enclosed by brackets "[]" replaced with your own identifying
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg * information: Portions Copyright [yyyy] [name of copyright owner]
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg *
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg * CDDL HEADER END
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg */
4f7bde67c075330b1fbd5b9abe25e347eb8e5690Trond Norbye
01bfcfd168ac9fffe2d80a88093acccb88706ef6Knut Anders Hatlen/*
9f5e862909e44377e352d0fed9e5f582ee4e5773Knut Anders Hatlen * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
01bfcfd168ac9fffe2d80a88093acccb88706ef6Knut Anders Hatlen */
01bfcfd168ac9fffe2d80a88093acccb88706ef6Knut Anders Hatlen
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Bergpackage org.opensolaris.opengrok.management.client;
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Bergimport java.io.File;
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Bergimport java.io.FileInputStream;
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Bergimport java.io.IOException;
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Bergimport java.io.InputStream;
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Bergimport java.util.Properties;
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Bergimport java.util.logging.Level;
01bfcfd168ac9fffe2d80a88093acccb88706ef6Knut Anders Hatlenimport org.opensolaris.opengrok.management.Constants;
1ed29ca262fd65b3bd912e2c0c3d4e825c0ddd4dLubos Koscoimport org.opensolaris.opengrok.management.OGAgent;
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg/**
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg *
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg * @author Jan S Berg
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg */
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Bergpublic class SettingsPersistence {
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg
123edc876010aee39f9c9caf2520c418d6b96fcaTrond Norbye public static final String HOST = Constants.JMX_HOST;
123edc876010aee39f9c9caf2520c418d6b96fcaTrond Norbye public static final String JMXPORT = Constants.JMX_PORT;
123edc876010aee39f9c9caf2520c418d6b96fcaTrond Norbye public static final String RMIPORT = Constants.RMI_PORT;
123edc876010aee39f9c9caf2520c418d6b96fcaTrond Norbye public static final String JMXURL = Constants.JMX_URL;
123edc876010aee39f9c9caf2520c418d6b96fcaTrond Norbye public static final String INDEXTIMEOUTKEY = "org.opensolaris.opengrok.management.indextimeout";
123edc876010aee39f9c9caf2520c418d6b96fcaTrond Norbye public static final String CONNECTIONTIMEOUTKEY = "org.opensolaris.opengrok.management.connectiontimeout";
123edc876010aee39f9c9caf2520c418d6b96fcaTrond Norbye public static final String LOGGINGPATHKEY = Constants.LOG_PATH;
123edc876010aee39f9c9caf2520c418d6b96fcaTrond Norbye public static final String FILELOGLEVELKEY = "org.opensolaris.opengrok.management.logging.filelevel";
123edc876010aee39f9c9caf2520c418d6b96fcaTrond Norbye public static final String CONSOLELOGLEVELKEY = "org.opensolaris.opengrok.management.logging.consolelevel";
04eceded116c11ab5ed16e40196adbe969b94aabJorgen Austvik private final Properties ogcProperties = new Properties();
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg private File propertyFile;
f7d9cf264bdd4c0a04823aa684239d52870706c0Lubos Kosco private boolean existingSettings = false; //NOPMD we do change it, but in deep finally block
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg /**
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg * Get settings for the OpenGrok Management Agent Client
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg * Try if a config file has been given
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg * if not set, try default settings (oga.properties in management directory)
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg * @throws java.io.IOException
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg */
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg public SettingsPersistence(String cfgfile) throws IOException {
9f5e862909e44377e352d0fed9e5f582ee4e5773Knut Anders Hatlen try (InputStream in = OGAgent.class.getResourceAsStream("oga.properties")) {
bfb054a567cb4af250de4c052934c9541b1cd7faTrond Norbye if (in != null) {
bfb054a567cb4af250de4c052934c9541b1cd7faTrond Norbye ogcProperties.load(in);
3aa0947feb67d3e8292d84776638be98dd97fdc3Lubos Kosco }
5a4c3bef409b6a3c015b6e0a57e9ff79c6941e06Lubos Kosco } catch (IOException ioe) { //NOPMD
bfb054a567cb4af250de4c052934c9541b1cd7faTrond Norbye throw ioe; //do we need to propagate this up ?
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg }
3aa0947feb67d3e8292d84776638be98dd97fdc3Lubos Kosco
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg if (cfgfile != null) {
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg propertyFile = new File(cfgfile);
9f5e862909e44377e352d0fed9e5f582ee4e5773Knut Anders Hatlen try (FileInputStream is = new FileInputStream(propertyFile)) {
bfb054a567cb4af250de4c052934c9541b1cd7faTrond Norbye ogcProperties.load(is);
5a4c3bef409b6a3c015b6e0a57e9ff79c6941e06Lubos Kosco } catch (IOException ioe) { //NOPMD
bfb054a567cb4af250de4c052934c9541b1cd7faTrond Norbye throw ioe; //do we need to propagate this up ?
3aa0947feb67d3e8292d84776638be98dd97fdc3Lubos Kosco }
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg }
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg }
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg
01bfcfd168ac9fffe2d80a88093acccb88706ef6Knut Anders Hatlen /**
01bfcfd168ac9fffe2d80a88093acccb88706ef6Knut Anders Hatlen * Get the JMX URL to the agent. Generate a URL from host and port
01bfcfd168ac9fffe2d80a88093acccb88706ef6Knut Anders Hatlen * properties if no URL has been specified.
01bfcfd168ac9fffe2d80a88093acccb88706ef6Knut Anders Hatlen *
01bfcfd168ac9fffe2d80a88093acccb88706ef6Knut Anders Hatlen * @return the URL to the agent
01bfcfd168ac9fffe2d80a88093acccb88706ef6Knut Anders Hatlen */
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg public String getAgentUrl() {
01bfcfd168ac9fffe2d80a88093acccb88706ef6Knut Anders Hatlen String url = ogcProperties.getProperty(JMXURL);
01bfcfd168ac9fffe2d80a88093acccb88706ef6Knut Anders Hatlen if (url == null) {
01bfcfd168ac9fffe2d80a88093acccb88706ef6Knut Anders Hatlen String host = ogcProperties.getProperty(HOST, "localhost");
7bba4db4d753dfb9f7385c22d04350469988dc54Jens Elkner int jmxport = Integer.parseInt(
01bfcfd168ac9fffe2d80a88093acccb88706ef6Knut Anders Hatlen ogcProperties.getProperty(JMXPORT, "9292"));
7bba4db4d753dfb9f7385c22d04350469988dc54Jens Elkner int rmiport = Integer.parseInt(ogcProperties.getProperty(
01bfcfd168ac9fffe2d80a88093acccb88706ef6Knut Anders Hatlen RMIPORT, String.valueOf(jmxport + 1)));
01bfcfd168ac9fffe2d80a88093acccb88706ef6Knut Anders Hatlen url = "service:jmx:rmi://" + host + ":" + jmxport +
01bfcfd168ac9fffe2d80a88093acccb88706ef6Knut Anders Hatlen "/jndi/rmi://" + host + ":" + rmiport + "/opengrok";
01bfcfd168ac9fffe2d80a88093acccb88706ef6Knut Anders Hatlen }
01bfcfd168ac9fffe2d80a88093acccb88706ef6Knut Anders Hatlen return url;
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg }
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg public boolean hasExistingSettings() {
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg return existingSettings;
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg }
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg public String getProperty(String key) {
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg return ogcProperties.getProperty(key);
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg }
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg public void setProperty(String key, String val) {
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg ogcProperties.setProperty(key, val);
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg }
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg public Level getFileLogLevel() {
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg return Level.parse(ogcProperties.getProperty(FILELOGLEVELKEY));
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg }
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg public Level getConsoleLogLevel() {
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg return Level.parse(ogcProperties.getProperty(CONSOLELOGLEVELKEY));
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg }
b8f2cc83ded7038d6b2268f028350f59d4c939b1Jan S Berg}