ENOEHT's Java Source Series (3) - Handling Profile

zhaozj2021-02-16  50

We often use this configuration file in the program:

Listener = org.kyle.net.svr.sample.sampleListenerImpl

ServerAddress = 127.0.0.1

ListeningPort = 80

Listenertimeout = 120

STATELESSSERVICE = TRUE

Loglevel = all

LogPath = Server.log

A source code for processing such a configuration file here is provided here.

Package org.kyle.util;

Import java.io. *;

Import java.util. *;

/ / Load the configuration file and provide a method of reading various types from the configuration file

Public Class Profile

{

Protected Properties ApplicationProps;

Protected string m_configurationFileName = NULL;

PRIVATE BOOLEAN M_DEBUG = FALSE;

Public profile (Boolean Debug)

{

THIS ();

m_debug = debug;

}

Public profile ()

{

"System.getProperty (" Mainconfigfile "," Server.cfg ");

}

Public profile (String ConfigurationFileName)

{

THIS.M_CONFIGURATIONFILENAME = ConfigurationFileName;

LoadCFG (ConfigurationFileName);

}

Public void loadingFileName), STRING VOILENAME

{

IF (ConfigurationFileName == NULL)

{

System.exit (-1);

}

Try {

ApplicationProps = new property ();

FileInputStream in = New fileinputstream (configurationFilename);

ApplicationProps.Load (in);

In.Close ();

}

Catch (IOEXCEPTION IE)

{

System.exit (-1);

}

}

Public void loadingconfig ()

{

LoadConfig (m_configurationFilename);

}

Public void saveconfig ()

{

Try

{

FileOutputStream out = new fileoutputstream (m_configurationFileName);

BufferedWriter Writer = New OutputStreamWriter (OUT, "8859_1");

Synchronized (ApplicationProps)

{

Iterator ity = new treset ()). Iterator ();

While (item.hasnext ())

{

String Key = (string) iterator.next ();

Writer.write (Key "=" ApplicationProps.getProperty (key));

Writer.newline ();

}

}

Writer.close ();

Out.close ();

} Catch (IOException IE)

{

System.out.println (IE.toString ());

}

}

Public void showconfig ()

{

ApplicationProps.List (System.out);

}

Public property getProperty ()

{

Return ApplicationProps;

}

String getString (String Section, String Key, String Default)

{

Return GetString (key, default);

}

Public String getString (String Key, String Default)

{

String rval = ApplicationProps.getProperty (key, default);

Return rval == NULL? RVAL: RVAL.TRIM ();

}

Public String getString (String Key)

{

String rval = applicationprops.getproperty (key);

Return rval == NULL? RVAL: RVAL.TRIM ();

}

Public Boolean getBoolean (String Key, Boolean Default)

{

String rval = getString (key);

// if (rval == null) return default;

IF ("True" .Equalsignorecase (RVAL)) Return True;

IF ("false". Equalsignorecase (RVAL)) Return False;

Return Default;

}

Public int GetInt (String Key, int default)

{

Try {

Return getInt (key);

} catch (exception e) {

ApplicationProps.SetProperty (Key, String.Valueof (Default));

Return Default;

}

}

Protected int Getint (String Key) THROWS NUMBERFORMATEXCEPTION

{

String rval = getString (key);

Return Integer.Parseint (RVAL);

}

Public String getConfigurationFileName ()

{

Return M_ConfigurationFileName;

}

Private Void LoadCFG (String ConfigurationFileName)

{

IF (ConfigurationFileName == NULL)

{

System.out.println ("Assigned A Null Configuration File. Default Setting Used.");

}

Try

{

ApplicationProps = new property ();

FileInputStream in = New fileinputstream (configurationFilename);

ApplicationProps.Load (in);

In.Close ();

}

Catch (IOEXCEPTION IOE)

{

System.out.println ("Loading Configuration from File" ConfigurationFileName "Failed."); System.out.Println ("Default Setting Will BE Used.");

}

}

}

Package org.kyle.util;

Import java.net. *;

// Call the parent class load the configuration file and read the data, and read its value according to the key value in the configuration file.

Public Class Genprofile Extends Profile

{

Public genprofile ()

{

Super ();

BuildCachedCrypt ();

}

Public genprofile (String cfgfilename)

{

Super (cfgfilename);

BuildCachedCrypt ();

}

Public String getListenerImpl ()

{

Return GetString ("Listener", "Org.kyle.net.svr.sample.sampleListenerImpl");

}

Public inetaddress GetServeraddress ()

{

Try

{

String Svraddr = GetString ("ServerAddress", NULL);

IF (svraddr == null) Return NULL;

Return inetaddress.getbyname (svraddr);

}

UnknownHOSTEXCEPTION UHE

{

Debug.info (UHE);

}

Return NULL;

}

Public int getListenat ()

{

Return GetInt ("ListeningPort", 80);

}

Public int gettimeout ()

{

Return GetInt ("Listenertimeout", 120);

}

Public Boolean StatelessService ()

{

Return getBoolean ("statelessservice", true);

}

Public String getLoglevel ()

{

Return GetString ("Loglevel", "config");

}

Public string getLogpath ()

{

Return GetString ("LogPath", "Server.log");

}

}

Instructions:

String cfgfile = "server.cfg";

GenProfile M_ENV = New Genprofile (cfgfile);

This allows for, for example, M_env. GetServeradDress (), etc., can be used in the program to obtain the corresponding content of the configuration file.

转载请注明原文地址:https://www.9cbs.com/read-21320.html

New Post(0)