package org.cipres.kepler.registry; import java.io.File; import java.net.URL; import java.net.URLDecoder; import java.util.Calendar; import java.util.Date; import org.apache.log4j.Logger; import org.cipres.properties.PropertyManager; public class Globals { private static Logger logger = Logger.getLogger(Globals.class.getName()); //Application properties set when app is initialized private String sampleFilesDir; private String guiXmlDir; private String tempDir; private String binDir; private String dirSep; private String osName; private String registryXmlFilepath; private CipresKeplerRegistry registry; /* baseDir is the base of the cipres-kepler installation, $KEPLER * configDir is the dir that stores the configuration file and guiXML files. It is in $KEPLER/configs/ptolemy/configs/kepler/cipres/ * CipresKeplerRegistry.xml is stored in configDir, guiXML files are stored in its subdir configDir/gui/ * homeDir is the dir that stores all the temporary/intermediate files. It is in $HOME/.kepler/cipres/temp/ */ private String baseDir; private String configDir; private String homeDir; //String constants public static final String OSWINDOWS = "win"; public static final String OSMAC = "mac"; //Singleton vars private static boolean m_initialized = false; private static Globals m_instance = new Globals(); private Globals() { try { this.setDirs(); this.initializeRegistry(); m_initialized = true; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Globals getInstance() { if (!m_initialized) { logger.fatal("Globals was not properly initialized"); return null; } return m_instance; } /** * @return Returns the binDir. */ public String getBinDir() { return binDir; } /** * @return Returns the dirSep. */ public String getDirSep() { return dirSep; } /** * @return Returns the guiXmlDir. */ public String getGuiXmlDir() { return guiXmlDir; } /** * @return Returns the osName. */ public String getOsName() { return osName; } /** * @return Returns the registry. */ public CipresKeplerRegistry getRegistry() { return registry; } /** * @return Returns the sampleFilesDir. */ public String getSampleFilesDir() { return sampleFilesDir; } /** * @return Returns the tempDir. */ public String getTempDir() { return tempDir; } /** * @return Returns the baseDir. */ public String getBaseDir() { return baseDir; } /** * @return Returns the registryXmlFilepath. */ protected String getRegistryXmlFilepath() { return registryXmlFilepath; } public boolean isWindowsOS() { return osName.equals(OSWINDOWS); } public boolean isMacintoshOS() { return osName.equals(OSMAC); } private void setDirs() throws Exception { this.dirSep = System.getProperty("file.separator"); this.homeDir = System.getProperty("user.home") + this.dirSep + ".kepler" + this.dirSep + "cipres" + this.dirSep; File homeDirectory = new File(homeDir); if (!homeDirectory.exists()) { if (!homeDirectory.mkdirs()) { throw new Exception("could not find or create home directory for cipres " + this.homeDir); } } ClassLoader cl = CipresKeplerRegistry.class.getClassLoader(); URL url = cl.getResource("ptolemy/configs/kepler/cipres/CipresKeplerRegistry.xml"); if (url == null) throw new Exception("Unable to locate CipresKeplerRegistry.xml on the classpath"); // Get the pathname part of the url and decode special characters (e.g // %20 -> space) to get a real filename. String appRegistryXmlFile = URLDecoder.decode(url.getPath(), "UTF-8"); File regFile = new File(appRegistryXmlFile); if (!regFile.exists()) throw new Exception("Unable to open file " + appRegistryXmlFile); // CipresKeplerRegistry.xml is located in $KEPLER/configs/ptolemy/configs/kepler/cipres/. this.registryXmlFilepath = regFile.getAbsolutePath(); this.baseDir = regFile.getParentFile().getParentFile().getParentFile().getParentFile().getParentFile().getParent() + this.dirSep; this.binDir = this.baseDir + "bin" + this.dirSep; this.configDir = regFile.getParent() + this.dirSep; this.guiXmlDir = this.configDir + "gui" + this.dirSep; this.sampleFilesDir = this.baseDir + "sample_data" + this.dirSep; this.tempDir = this.homeDir + "temp" + this.dirSep; //create temp and samplefile dirs if they don't exist // Note: change f.mkdir() to f.mkdirs() since mkdirs() will create the dir and any necessary but nonexistent parent direcoties File f = new File(this.tempDir); if (!f.exists()) { if (!f.mkdirs()) { throw new Exception("could not find or create temp directory " + this.tempDir); } } f = new File(this.sampleFilesDir); if (!f.exists()) { if (!f.mkdirs()) { throw new Exception("could not find or create sample files directory " + this.sampleFilesDir); } } //throw exception if guixml dir does not exist f = new File(this.guiXmlDir); if (!f.exists()) { throw new Exception("could not find gui xml directory at " + this.guiXmlDir); } //set os String os = System.getProperty("os.name"); if (os != null) { if (os.startsWith("Windows")) this.osName = OSWINDOWS; if (os.startsWith("Mac")) this.osName = OSMAC; } } private void initializeRegistry() throws Exception { registry = CipresKeplerRegistry.deserialize(new File( registryXmlFilepath), this.tempDir); // Set dirs that user CAN override if not set. File f; if (registry.getDefaultInputFileDir() == null) { registry.setDefaultInputFileDir(this.sampleFilesDir); } else { //or if set check that it is valid & if not set to default f = new File(registry.getDefaultInputFileDir()); if (!f.exists()) { registry.setDefaultInputFileDir(this.sampleFilesDir); } } if (registry.getDefaultStdErrDir() == null) { registry.setDefaultStdErrDir(this.tempDir); } else { //or if set check that it is valid & if not set to default f = new File(registry.getDefaultStdErrDir()); if (!f.exists()) { registry.setDefaultStdErrDir(this.tempDir); } } if (registry.getDefaultStdOutDir() == null) { registry.setDefaultStdOutDir(this.tempDir); } else { //or if set check that it is valid & if not set to default f = new File(registry.getDefaultStdOutDir()); if (!f.exists()) { registry.setDefaultStdOutDir(this.tempDir); } } } public String getUniqueTmpFilename(String prefix, String suffix) { // this is unique enough for now. todo: use guid? String filename = createTimstampSessionUID(); if (prefix != null) { filename = prefix + filename; } if (suffix != null) { filename = filename + suffix; } return getTempDir() + filename; } /** * Creates a Session UID based on system timestamp
*/ public String createTimstampSessionUID() { String strYear; String strMonth; String strDay; String strHour; String strMinute; String strSecond; Calendar rightNow = Calendar.getInstance(); rightNow.setTime(new Date()); //set Year strYear = Integer.toString(rightNow.get(Calendar.YEAR)); //set Month if (rightNow.get(Calendar.MONTH) + 1 < 10) { strMonth = "0" + Integer.toString(rightNow.get(Calendar.MONTH) + 1); } else { strMonth = Integer.toString(rightNow.get(Calendar.MONTH) + 1); //set Day } if (rightNow.get(Calendar.DAY_OF_MONTH) < 10) { strDay = "0" + Integer.toString(rightNow.get(Calendar.DAY_OF_MONTH)); } else { strDay = Integer.toString(rightNow.get(Calendar.DAY_OF_MONTH)); //set Hour } if (rightNow.get(Calendar.HOUR_OF_DAY) < 10) { strHour = "0" + Integer.toString(rightNow.get(Calendar.HOUR_OF_DAY)); } else { strHour = Integer.toString(rightNow.get(Calendar.HOUR_OF_DAY)); //set Minute } if (rightNow.get(Calendar.MINUTE) < 10) { strMinute = "0" + Integer.toString(rightNow.get(Calendar.MINUTE)); } else { strMinute = Integer.toString(rightNow.get(Calendar.MINUTE)); //set Second } if (rightNow.get(Calendar.SECOND) < 10) { strSecond = "0" + Integer.toString(rightNow.get(Calendar.SECOND)); } else { strSecond = Integer.toString(rightNow.get(Calendar.SECOND)); } return strYear + strMonth + strDay + "_" + strHour + strMinute + strSecond; } /** * queries the CIPRes property service for the value of a given property * * @param name the name of the property * @return the value of the property */ public String getCipresProperty(final String name) { return PropertyManager.getInstance().getProperty(name); } }