001: import java.io.BufferedReader;
002: import java.io.BufferedWriter;
003: import java.io.FileReader;
004: import java.io.FileWriter;
005: import java.io.IOException;
006: 
007: /**
008:  * This class interacts with configuration file.   
009:  * It can write user name and password to configuration.XML 
010:  * or can read user name and password from this.  
011:  * @author Cagatay CEBI - [email protected]
012:  * @version 1.0
013:  */
014: public class Configuration implements BasicConstants{
015: 
016:         /**
017:          * Reads user info from configuration.XML and returns
018:          * user_name and password.  
019:          * @return UserInfo
020:          */
021:         public static UserInfo get_user_info( )
022:         {
023:                 String file_content = read_configuration_file( );
024:                 UserInfo user_info = new UserInfo( );
025:                 
026:                 try {
027:                 user_info.user_name = file_content.substring( 
028:                                 file_content.indexOf("<user_name>") + "<user_name>".length(),
029:                                 file_content.indexOf("</user_name>") );
030:                 user_info.password = file_content.substring(
031:                                 file_content.indexOf("<password>") + "<password>".length(),
032:                                 file_content.indexOf("</password>") );
033:                 }
034:                 catch(java.lang.StringIndexOutOfBoundsException exception ) {
035:                         System.err.println( "ERROR: INDEX OUT OF BOUNDS EXCEPTION IN 'Configuration' CLASS!" );
036:                         System.err.println( "EXCEPTION DETAIL:\n" + exception.toString( ) + "\n");                  
037:                 }
038:                 
039:                 file_content = null;
040:                 
041:                 return user_info;
042:         }
043:         
044:         /**
045:          * Write user_name and password to configuration.XML file.
046:          * @param user_name User Name
047:          * @param password Password
048:          */
049:         public static void write_configuration_file( 
050:                         String user_name, String password )
051:         {
052:                 try {
053:                          BufferedWriter out = new BufferedWriter(
054:                                          new FileWriter( CONFIG_FILE_ADDRESS ) );
055:                          out.write( set_configuration(user_name, password) );
056:                          out.close( );
057:                          } 
058:                 catch( IOException exception ) {
059:                                 System.err.println( "ERROR: INPUT/OUTPUT EXCEPTION IN 'Configuration' CLASS!" );
060:                                 System.err.println( "EXCEPTION DETAIL:\n" + exception.toString( ) + "\n");
061:                  } 
062:         }
063: 
064:         /**
065:          * Read content of configuration.XML file.
066:          * @return content of onfiguration.XML file
067:          */
068:         private static String read_configuration_file( )
069:         {
070:                 StringBuffer content = new StringBuffer();
071:             String str;
072:             try {
073:                 BufferedReader in = 
074:                         new BufferedReader( new FileReader( CONFIG_FILE_ADDRESS ));
075:                 while( (str = in.readLine( ) ) != null ) 
076:                     content.append( str+"\n" );
077:                 in.close( );
078:             }
079:              catch( IOException exception ) {
080:                                 System.err.println( "ERROR: INPUT/OUTPUT EXCEPTION IN 'Configuration' CLASS!" );
081:                                 System.err.println( "EXCEPTION DETAIL:\n" + exception.toString( ));
082:                                 System.err.println( "\n***************************************" );
083:                                 System.err.println( "HATA: KULLANICI ADI ve ŞİFRESİNİN TUTULDUĞU DOSYA BULUNAMAMIŞTIR.\n" + 
084:                                 "' java -jar LogMyIP.jar -s ' KOMUTUYLA BU DOSYAYI YARATMANIZ GEREKMEKTEDİR." );
085:                                 System.err.println( "***************************************\n" );
086: 
087:                  }
088:             str = content.toString( );
089:             content = null;
090:             return str; 
091:         }
092:         
093:         /**
094:          * set_configuration method prepares content of 
095:          * configuration.XML file. 
096:          * @param user_name User Name
097:          * @param password Password
098:          * @return Content to be created
099:          */
100:         private static String set_configuration( 
101:                         String user_name, String password )
102:         {
103:                 StringBuffer configuration_file = new StringBuffer( );
104:         
105:                 configuration_file.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
106:                 configuration_file.append("<!--WARNING: PLEASE DO NOT CHANGE OR MODIFY THIS CONFIGURATION FILE MANUALLY!-->");
107:                 configuration_file.append( "\n<LogMyIP>\n" );
108:                 configuration_file.append( "\t<user_name>" );
109:                 configuration_file.append( user_name );
110:                 configuration_file.append( "</user_name>\n" );
111:                 configuration_file.append( "\t<password>" );
112:                 configuration_file.append( password );
113:                 configuration_file.append( "</password>\n" );
114:                 configuration_file.append( "</LogMyIP>" );
115:                 String config = configuration_file.toString( );
116:                 configuration_file = null;
117:                 
118:                 return config;          
119:         }
120: }