340N/A/*
340N/A * CDDL HEADER START
340N/A *
340N/A * The contents of this file are subject to the terms of the
340N/A * Common Development and Distribution License (the "License").
340N/A * You may not use this file except in compliance with the License.
340N/A *
340N/A * See LICENSE.txt included in this distribution for the specific
340N/A * language governing permissions and limitations under the License.
340N/A *
340N/A * When distributing Covered Code, include this CDDL HEADER in each
340N/A * file and include the License file at LICENSE.txt.
340N/A * If applicable, add the following below this CDDL HEADER, with the
340N/A * fields enclosed by brackets "[]" replaced with your own identifying
340N/A * information: Portions Copyright [yyyy] [name of copyright owner]
340N/A *
340N/A * CDDL HEADER END
340N/A */
340N/A
340N/A/*
340N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
340N/A * Use is subject to license terms.
340N/A */
340N/Apackage org.opensolaris.opengrok.management;
340N/A
639N/Aimport java.io.IOException;
340N/Aimport java.util.Date;
340N/Aimport java.util.Properties;
639N/Aimport java.util.logging.Level;
340N/Aimport java.util.logging.Logger;
340N/Aimport javax.management.MBeanRegistration;
394N/Aimport javax.management.MBeanServer;
340N/Aimport javax.management.ObjectName;
340N/Aimport org.opensolaris.opengrok.Info;
639N/Aimport org.opensolaris.opengrok.OpenGrokLogger;
1462N/Aimport org.opensolaris.opengrok.configuration.Configuration;
340N/A
1462N/A/**
1462N/A * @author Jan S Berg
1462N/A * @version $Revision$
1462N/A */
340N/Apublic final class Management implements ManagementMBean, MBeanRegistration {
340N/A
1327N/A private static final Logger log = Logger.getLogger(Management.class.getName());
340N/A private static Management managementInstance = null;
456N/A private final Properties ogaProperties;
340N/A private final long startTime; // Stores the time this bean is created
340N/A private Boolean update = Boolean.FALSE;
340N/A private Integer noThreads = Integer.valueOf(1);
340N/A private String[] subFiles = new String[]{};
340N/A private String configurationFile = null;
340N/A private String publishHost = null;
340N/A
340N/A /**
340N/A * The only constructor is private, so other classes will only get an
340N/A * instance through the static factory method getInstance().
340N/A */
340N/A private Management(Properties ogaProperties) {
340N/A startTime = System.currentTimeMillis();
340N/A this.ogaProperties = ogaProperties;
340N/A updateProperties();
340N/A }
340N/A
340N/A private void updateProperties() {
1462N/A update = Boolean.valueOf(ogaProperties
1462N/A .getProperty(Configuration.PROPERTY_KEY_PREFIX + "indexer.updatedatabase"));
1462N/A noThreads = Integer.valueOf(ogaProperties
1462N/A .getProperty(Configuration.PROPERTY_KEY_PREFIX + "indexer.numberofthreads"));
1462N/A configurationFile = ogaProperties
1462N/A .getProperty(Configuration.PROPERTY_KEY_PREFIX + "configuration.file");
1462N/A String subfiles = ogaProperties
1462N/A .getProperty(Configuration.PROPERTY_KEY_PREFIX + "indexer.subfiles");
340N/A if (subfiles != null) {
340N/A subFiles = subfiles.split(",");
340N/A }
1462N/A publishHost = ogaProperties
1462N/A .getProperty(Configuration.PROPERTY_KEY_PREFIX + "indexer.publishserver.url");
340N/A
340N/A }
340N/A
340N/A /**
340N/A * Static factory method to get an instance of Management.
340N/A * @param ogaProperties The properties to use
373N/A * @return A management instance to use
340N/A */
591N/A @SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel")
340N/A public static synchronized Management getInstance(Properties ogaProperties) {
340N/A if (managementInstance == null) {
340N/A managementInstance = new Management(ogaProperties);
340N/A }
340N/A return managementInstance;
340N/A }
340N/A
340N/A /**
340N/A * Static factory method to get an instance of management.
340N/A * Returns null if Management has not been initialized yet.
1462N/A * @return a singleton
340N/A */
340N/A public static Management getInstance() {
340N/A return managementInstance;
340N/A }
340N/A
340N/A /**
340N/A * Get a selected property from configuration.
340N/A * @return String with property value
340N/A */
1462N/A @Override
340N/A public String getProperty(String key) {
340N/A return ogaProperties.getProperty(key);
340N/A }
340N/A
340N/A /**
340N/A * Set a selected property in the configuration.
340N/A * @param key the String key for the property to be set.
340N/A * $param value the String value for the property to be set.
340N/A */
1462N/A @Override
340N/A public void setProperty(String key, String value) {
340N/A if (key == null) {
340N/A log.severe("Trying to set property with key == null");
340N/A return;
340N/A }
340N/A ogaProperties.setProperty(key, value);
340N/A saveProperties();
340N/A }
340N/A
1462N/A private static void saveProperties() {
340N/A throw new UnsupportedOperationException("Not yet implemented");
340N/A }
340N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
340N/A public ObjectName preRegister(MBeanServer server, ObjectName name) {
340N/A return name;
340N/A }
340N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
340N/A public void postRegister(Boolean registrationDone) {
456N/A // not used
340N/A }
340N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
340N/A public void preDeregister() {
456N/A // not used
340N/A }
340N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
340N/A public void postDeregister() {
456N/A // not used
340N/A }
340N/A
340N/A /**
340N/A * Stops the agent, so it is not restarted.
340N/A */
1462N/A @Override
340N/A public void stop() {
340N/A log.warning("STOPPING AGENT!");
340N/A //WrapperManager.stop(0);
340N/A }
340N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
340N/A public String getSystemProperty(String key) {
340N/A return System.getProperty(key);
340N/A }
340N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
340N/A public void setSystemProperty(String key, String value) {
340N/A if (key == null) {
340N/A log.severe("Trying to set property with key == null");
340N/A return;
340N/A }
340N/A System.setProperty(key, value);
340N/A }
340N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
340N/A public String getSystemEnvProperty(String key) {
340N/A return System.getenv(key);
340N/A }
340N/A
340N/A /**
340N/A * Get the time (in milliseconds since 1970) when the agent was started
340N/A * @return long time when the agent was started, in milliseconds.
340N/A */
1462N/A @Override
340N/A public long getStartTime() {
340N/A return startTime;
340N/A }
340N/A
340N/A /**
340N/A * Get a Date object with the time the agent was started.
340N/A * @return Date with the starting date
340N/A */
1462N/A @Override
340N/A public Date getStartDate() {
340N/A return new Date(startTime);
340N/A }
340N/A
340N/A /**
340N/A * Get the version tag for the agent
340N/A * @return String the version tag for this agent
340N/A */
1462N/A @Override
340N/A public String getVersion() {
340N/A return Info.getFullVersion();
340N/A }
340N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
340N/A public void setUpdateIndexDatabase(Boolean val) {
340N/A this.update = val;
340N/A }
340N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
340N/A public Boolean getUpdateIndexDatabase() {
340N/A return update;
340N/A }
340N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
340N/A public void setNumberOfThreads(Integer val) {
340N/A this.noThreads = val;
340N/A }
340N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
340N/A public Integer getNumberOfThreads() {
340N/A return noThreads;
340N/A }
340N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
340N/A public void setSubFiles(String[] sublist) {
409N/A this.subFiles = (sublist == null) ? null : (String[]) sublist.clone();
340N/A }
340N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
436N/A @SuppressWarnings("PMD.MethodReturnsInternalArray")
340N/A public String[] getSubFiles() {
409N/A return (subFiles == null) ? null : (String[]) subFiles.clone();
340N/A }
340N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
340N/A public String getConfigurationFile() {
340N/A return configurationFile;
340N/A }
340N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
340N/A public String getPublishServerURL() {
340N/A return publishHost;
340N/A }
639N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
639N/A public void setFileLogLevel(Level level) {
639N/A OpenGrokLogger.setFileLogLevel(level);
639N/A }
639N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
639N/A public void setFileLogPath(String path) throws IOException {
639N/A OpenGrokLogger.setFileLogPath(path);
639N/A }
639N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
639N/A public Level getConsoleLogLevel() {
639N/A return OpenGrokLogger.getConsoleLogLevel();
639N/A }
639N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
639N/A public Level getFileLogLevel() {
639N/A return OpenGrokLogger.getFileLogLevel();
639N/A }
639N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
639N/A public String getFileLogPath() {
639N/A return OpenGrokLogger.getFileLogPath();
639N/A }
639N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
639N/A public void setPublishServerURL(String url) {
639N/A publishHost = url;
639N/A }
639N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
639N/A public void setConsoleLogLevel(Level level) {
639N/A OpenGrokLogger.setConsoleLogLevel(level);
639N/A }
658N/A
1462N/A /**
1462N/A * {@inheritDoc}
1462N/A */
1462N/A @Override
658N/A public void setConfigurationFile(String filename) {
658N/A configurationFile = filename;
658N/A }
340N/A}