import java.util.*; import java.text.*; import java.io.*; /** * Reads info about environment variables from the .properties file * * @author aleman-meza, boanerges */ public class Environment { private static final String PROPERTIES_FILE = "dev_environment.properties"; private static final String PREFIX = "directory."; public static String HTML_DIRECTORY; public static String GNUPLOT; public static String PBM2GIF; public static String BASEURL; public static String SERVLET_UPLOAD; public static String SERVLET_GNUPLOT; public static String COLUMNS_DATA; public static String INDEX_HTML; private static String propertiesFileLocation; static { try { // read a system property of interest. it is set when this is ran like: // java -Dproperties.dir=/somedirectory/ Environment propertiesFileLocation = System.getProperty( "properties.dir", "" ); if( propertiesFileLocation != null && propertiesFileLocation != "" && ! propertiesFileLocation.endsWith( File.separator ) ) { propertiesFileLocation = propertiesFileLocation + File.separator; } Properties prop = new Properties(); prop.load( new FileInputStream( new File( propertiesFileLocation + PROPERTIES_FILE ) ) ); HTML_DIRECTORY = prop.getProperty( "html.directory", "/home/aleman/public_html/" ); if( ! HTML_DIRECTORY.endsWith( File.separator ) ) { HTML_DIRECTORY = HTML_DIRECTORY + File.separator; } GNUPLOT = prop.getProperty( "gnuplot", "/home/aleman/gnuplot" ); PBM2GIF = prop.getProperty( "pbm2gif", "/home/aleman/pbm2gif" ); BASEURL = prop.getProperty( "baseurl", "http://gene.genetics.uga.edu/~aleman/" ); if( ! BASEURL.endsWith( File.separator ) ) { BASEURL = BASEURL + File.separator; } SERVLET_UPLOAD = prop.getProperty( "servlet.upload", "http://gene.genetics.uga.edu:8080/servlet/FileUploadServlet" ); SERVLET_GNUPLOT = prop.getProperty( "servlet.gnuplot", "http://gene.genetics.uga.edu:8080/servlet/GnuplotServlet" ); COLUMNS_DATA = prop.getProperty( "columns_data.file", "columns.data" ); INDEX_HTML = prop.getProperty( "index_html.file", "index.html" ); // rewrite the properties file to set the default values when // the propertiesFileLocation has some path on it. (this class // was ran with something like -Dproperties.dir=/web/ab/ ) if( propertiesFileLocation != null & propertiesFileLocation != "" ) { prop.setProperty( "html.directory", HTML_DIRECTORY ); prop.setProperty( "gnuplot", GNUPLOT); prop.setProperty( "pbm2gif", PBM2GIF ); prop.setProperty( "baseurl", BASEURL ); prop.setProperty( "servlet.upload", SERVLET_UPLOAD ); prop.setProperty( "servlet.gnuplot", SERVLET_GNUPLOT ); prop.setProperty( "columns_data.file", COLUMNS_DATA ); prop.setProperty( "index_html.file", INDEX_HTML ); prop.store( new FileOutputStream( new File( propertiesFileLocation + PROPERTIES_FILE ) ), null ); } } catch( IOException ioe ) { String errmsg = "Environment: Error loading system properties"; System.err.println( errmsg ); errorReport( errmsg ); ioe.printStackTrace(); System.exit( -1 ); } } private Environment() { } /** * Creates/Updates the 'home' html file * uses the info from the .properties file to create a link * per directory number */ public static void createHtml() { try { Properties prop = new Properties(); prop.load( new FileInputStream( new File( propertiesFileLocation + PROPERTIES_FILE ) ) ); PrintStream htmlFile = new PrintStream( new FileOutputStream( new File( HTML_DIRECTORY + INDEX_HTML ) ) ); htmlFile.println(); htmlFile.println( "" ); htmlFile.println( " " ); htmlFile.println( " All Plots " ); htmlFile.println( " " ); htmlFile.println( " " ); htmlFile.println( " " ); htmlFile.println( "

Previously Uploaded Files :

" ); String desc = null; boolean none = true; DecimalFormat threeDigits = new DecimalFormat( "000" ); for( int i = 1; i < 2000; i++ ) { desc = prop.getProperty( PREFIX + threeDigits.format( i ) ); if( desc != null ) { htmlFile.println( " " + desc + "
" ); none = false; } } if( none ) { htmlFile.println( " - no previously uploaded files - !
" ); } htmlFile.println( "
" ); htmlFile.println( "
" ); htmlFile.println( " Upload datafile ( kin.o02 )
" ); htmlFile.println( " File :
" ); htmlFile.println( " Description :
" ); htmlFile.println( " " ); htmlFile.println( "
" ); htmlFile.println( "
" ); htmlFile.println( " C++ Executable of kin kin_c" ); htmlFile.println( " " ); htmlFile.println( "" ); htmlFile.close(); } catch( IOException ioe ) { String errmsg = "Environment: Error creating html(" + HTML_DIRECTORY + INDEX_HTML + ")"; System.err.println( errmsg ); errorReport( errmsg ); ioe.printStackTrace(); } } /** * Adds an entry to the .properties file * an entry is a pair of ( directory, description ) * @param dirno name of the directory * @param description description of the uploaded file */ public static void addEntry( String dirno, String description ) { try { if( description == null || description.trim().length() == 0 ) { description = "- no description -"; } Properties prop = new Properties(); prop.load( new FileInputStream( new File( PROPERTIES_FILE ) ) ); prop.setProperty( PREFIX + dirno, description ); prop.store( new FileOutputStream( new File( PROPERTIES_FILE ) ), null ); } catch( IOException ioe ) { String errmsg = "Environment: Error adding entries to the system properties"; System.err.println( errmsg ); errorReport( errmsg ); ioe.printStackTrace(); } } /** * Error reporting routine
* Sends a e-mail to the developer * not implemented */ public static void errorReport( String msg ) { /* try { Process p = null; Runtime r = Runtime.getRuntime(); String[] command = { "mail", "boanerg" }; p = r.exec( command ); //"echo \"" + msg + "\" | mail boanerg" ); int status = p.waitFor(); System.err.println( "status = " + status ); } catch( Exception e ) { e.printStackTrace(); } */ } /** * to be run only once to create some index.html file * sets the correct attribute for it */ public static void main( String args[] ) { createHtml(); try { Process p = null; Runtime r = Runtime.getRuntime(); p = r.exec( "chmod 755 " + HTML_DIRECTORY + INDEX_HTML ); p.waitFor(); } catch( Exception e ) { e.printStackTrace(); } } /* # a default .properties file could be something like this columns_data.file=columns.data gnuplot=/usr/local/bin/gnuplot pbm2gif=/home/grad/boanerg/genetics/test/pbm2gif servlet.upload=http\://commerce.cs.uga.edu\:8080/servlet/FileUploadServlet html.directory=/web/boanerg/public_html/stc/ servlet.gnuplot=http\://commerce.cs.uga.edu\:8080/servlet/GnuplotServlet directory.001=big\ test baseurl=http\://www.cs.uga.edu/~boanerg/stc/ index_html.file=index.html */ }