Alachisoft.NCache.Common.Monitoring.MonitoringConfigManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nc-common Show documentation
Show all versions of nc-common Show documentation
Internal package of Alachisoft.
package Alachisoft.NCache.Common.Monitoring;
import Alachisoft.NCache.Common.AppUtil;
import Alachisoft.NCache.Common.Configuration.ConfigurationBuilder;
import Alachisoft.NCache.Common.Exceptions.ManagementException;
import com.alachisoft.ncache.common.protobuf.CounterCommandProtocol;
import com.alachisoft.ncache.common.protobuf.DataTypeManagementCommandProtocol;
import com.alachisoft.ncache.common.protobuf.ResponseProtocol;
import java.io.*;
import java.nio.file.Path;
public class MonitoringConfigManager {
static String DIRNAME = "config";
static String FILENAME = "monitoring.ncconf";
static String c_configDir = DIRNAME;
static String c_configFileName = FILENAME;
static String ENDSTRING = "\r\n";
static Object _lock = new Object();
static Counters _configuration;
private static boolean _isConfigFileFound=true;
public boolean getCongigFile(){ return _isConfigFileFound;}
public void setConfigFile(boolean value) { _isConfigFileFound=value;}
public MonitoringConfigManager() throws ManagementException, Exception
{
LoadConfiguration();
}
private void LoadConfiguration() throws ManagementException ,Exception{
try
{
CombinePath();
File file=new File(c_configFileName);
//If the file does not exist, create the file.
if (!file.exists())
{
setConfigFile(false);
if (_configuration == null)
{
_configuration = new Counters();
}
// SaveConfiguration();
}
else {
setConfigFile(true);
LoadXml();
}
}
catch (ManagementException ex)
{
c_configFileName = "";
throw ex;
}
catch (Exception ex)
{
c_configFileName = "";
throw ex;
}
}
public Category GetCategory(String categoryName)
{
if (_configuration != null)
{
for (Category category : _configuration.getCategory())
{
if (category.getName().equals(categoryName))
return category;
}
}
return null;
}
private void LoadXml() throws Exception {
if(c_configFileName==null)
CombinePath();
ConfigurationBuilder configBuilder =new ConfigurationBuilder(c_configFileName);
configBuilder.RegisterRootConfigurationObject(Counters.class);
configBuilder.ReadConfiguration();
Counters counterConfiguration = null;
Object[] configuration = configBuilder.getConfiguration();
if (configuration != null && configuration.length > 0)
{
for (int i = 0; i < configuration.length; i++)
{
counterConfiguration =(Counters) configuration[i];
break;
}
}
_configuration = counterConfiguration;
if (_configuration == null)
_configuration = new Counters();
}
private String ToXml() throws Exception {
StringBuilder sb = new StringBuilder();
Object[] configuration = new Object[1];
configuration[0] = _configuration;
ConfigurationBuilder cfgBuilder = new ConfigurationBuilder(configuration);
cfgBuilder.RegisterRootConfigurationObject(Counters.class);
sb.append(cfgBuilder.GetXmlString());
return sb.toString();
}
private void SaveConfiguration() throws ManagementException, IOException {
if (c_configFileName == null || c_configFileName == "")
CombinePath();
File fs=null;
FileWriter fw=null;
synchronized(_lock)
{
try
{
fs = new File(c_configFileName);
if(!fs.exists())
fs.createNewFile();
fw=new FileWriter(fs);
fw.write(ToXml());
fw.flush();
}
catch (Exception e)
{
throw new ManagementException(e.getMessage(), e);
}
finally
{
if (fw != null) fw.close();
}
}
}
public void CombinePath() throws ManagementException {
c_configDir = AppUtil.getInstallDir();
if (c_configDir == null || c_configDir.length() == 0)
{
throw new ManagementException("Missing installation folder information");
}
c_configDir = c_configDir + File.separator + DIRNAME;
/*if (!Directory.Exists(c_configDir))
Directory.CreateDirectory(c_configDir);
*/
c_configFileName = c_configDir + File.separator + FILENAME;
}
}