Management.java revision 1462
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
340N/Aimport java.io.IOException;
340N/Aimport java.util.Date;
340N/Aimport java.util.Properties;
340N/Aimport java.util.logging.Level;
340N/Aimport java.util.logging.Logger;
394N/Aimport javax.management.MBeanRegistration;
394N/Aimport javax.management.MBeanServer;
394N/Aimport javax.management.ObjectName;
394N/Aimport org.opensolaris.opengrok.Info;
394N/Aimport org.opensolaris.opengrok.OpenGrokLogger;
394N/Aimport org.opensolaris.opengrok.configuration.Configuration;
340N/A
394N/A/**
394N/A * @author Jan S Berg
394N/A * @version $Revision$
394N/A */
394N/Apublic final class Management implements ManagementMBean, MBeanRegistration {
340N/A
340N/A private static final Logger log = Logger.getLogger(Management.class.getName());
340N/A private static Management managementInstance = null;
340N/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() {
340N/A update = Boolean.valueOf(ogaProperties
340N/A .getProperty(Configuration.PROPERTY_KEY_PREFIX + "indexer.updatedatabase"));
340N/A noThreads = Integer.valueOf(ogaProperties
340N/A .getProperty(Configuration.PROPERTY_KEY_PREFIX + "indexer.numberofthreads"));
340N/A configurationFile = ogaProperties
340N/A .getProperty(Configuration.PROPERTY_KEY_PREFIX + "configuration.file");
340N/A String subfiles = ogaProperties
340N/A .getProperty(Configuration.PROPERTY_KEY_PREFIX + "indexer.subfiles");
340N/A if (subfiles != null) {
340N/A subFiles = subfiles.split(",");
340N/A }
340N/A publishHost = ogaProperties
340N/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
340N/A * @return A management instance to use
340N/A */
340N/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 }
368N/A return managementInstance;
375N/A }
368N/A
368N/A /**
368N/A * Static factory method to get an instance of management.
340N/A * Returns null if Management has not been initialized yet.
340N/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 */
340N/A @Override
340N/A public String getProperty(String key) {
340N/A return ogaProperties.getProperty(key);
373N/A }
373N/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 */
340N/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
340N/A private static void saveProperties() {
340N/A throw new UnsupportedOperationException("Not yet implemented");
340N/A }
340N/A
340N/A /**
340N/A * {@inheritDoc}
340N/A */
340N/A @Override
340N/A public ObjectName preRegister(MBeanServer server, ObjectName name) {
340N/A return name;
340N/A }
340N/A
340N/A /**
340N/A * {@inheritDoc}
340N/A */
340N/A @Override
340N/A public void postRegister(Boolean registrationDone) {
340N/A // not used
340N/A }
340N/A
340N/A /**
340N/A * {@inheritDoc}
340N/A */
340N/A @Override
340N/A public void preDeregister() {
368N/A // not used
340N/A }
340N/A
368N/A /**
340N/A * {@inheritDoc}
340N/A */
340N/A @Override
340N/A public void postDeregister() {
340N/A // not used
340N/A }
340N/A
340N/A /**
340N/A * Stops the agent, so it is not restarted.
340N/A */
340N/A @Override
340N/A public void stop() {
340N/A log.warning("STOPPING AGENT!");
340N/A //WrapperManager.stop(0);
373N/A }
373N/A
340N/A /**
340N/A * {@inheritDoc}
340N/A */
340N/A @Override
340N/A public String getSystemProperty(String key) {
373N/A return System.getProperty(key);
340N/A }
340N/A
340N/A /**
340N/A * {@inheritDoc}
340N/A */
340N/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
340N/A /**
340N/A * {@inheritDoc}
340N/A */
340N/A @Override
340N/A public String getSystemEnvProperty(String key) {
340N/A return System.getenv(key);
340N/A }
340N/A
373N/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 */
373N/A @Override
340N/A public long getStartTime() {
340N/A return startTime;
340N/A }
340N/A
340N/A /**
373N/A * Get a Date object with the time the agent was started.
340N/A * @return Date with the starting date
340N/A */
340N/A @Override
340N/A public Date getStartDate() {
340N/A return new Date(startTime);
340N/A }
340N/A
/**
* Get the version tag for the agent
* @return String the version tag for this agent
*/
@Override
public String getVersion() {
return Info.getFullVersion();
}
/**
* {@inheritDoc}
*/
@Override
public void setUpdateIndexDatabase(Boolean val) {
this.update = val;
}
/**
* {@inheritDoc}
*/
@Override
public Boolean getUpdateIndexDatabase() {
return update;
}
/**
* {@inheritDoc}
*/
@Override
public void setNumberOfThreads(Integer val) {
this.noThreads = val;
}
/**
* {@inheritDoc}
*/
@Override
public Integer getNumberOfThreads() {
return noThreads;
}
/**
* {@inheritDoc}
*/
@Override
public void setSubFiles(String[] sublist) {
this.subFiles = (sublist == null) ? null : (String[]) sublist.clone();
}
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("PMD.MethodReturnsInternalArray")
public String[] getSubFiles() {
return (subFiles == null) ? null : (String[]) subFiles.clone();
}
/**
* {@inheritDoc}
*/
@Override
public String getConfigurationFile() {
return configurationFile;
}
/**
* {@inheritDoc}
*/
@Override
public String getPublishServerURL() {
return publishHost;
}
/**
* {@inheritDoc}
*/
@Override
public void setFileLogLevel(Level level) {
OpenGrokLogger.setFileLogLevel(level);
}
/**
* {@inheritDoc}
*/
@Override
public void setFileLogPath(String path) throws IOException {
OpenGrokLogger.setFileLogPath(path);
}
/**
* {@inheritDoc}
*/
@Override
public Level getConsoleLogLevel() {
return OpenGrokLogger.getConsoleLogLevel();
}
/**
* {@inheritDoc}
*/
@Override
public Level getFileLogLevel() {
return OpenGrokLogger.getFileLogLevel();
}
/**
* {@inheritDoc}
*/
@Override
public String getFileLogPath() {
return OpenGrokLogger.getFileLogPath();
}
/**
* {@inheritDoc}
*/
@Override
public void setPublishServerURL(String url) {
publishHost = url;
}
/**
* {@inheritDoc}
*/
@Override
public void setConsoleLogLevel(Level level) {
OpenGrokLogger.setConsoleLogLevel(level);
}
/**
* {@inheritDoc}
*/
@Override
public void setConfigurationFile(String filename) {
configurationFile = filename;
}
}