1 16 17 package info.jtrac.util; 18 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.FileOutputStream ; 22 import java.io.FilenameFilter ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.io.PrintWriter ; 26 import java.io.Writer ; 27 import java.util.Properties ; 28 import javax.servlet.ServletContext ; 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; 32 import org.springframework.core.io.ClassPathResource; 33 import org.springframework.core.io.FileSystemResource; 34 import org.springframework.web.context.ServletContextAware; 35 36 57 58 public class JtracConfigurer extends PropertyPlaceholderConfigurer implements ServletContextAware { 59 60 private final Log logger = LogFactory.getLog(getClass()); 61 62 private ServletContext servletContext; 63 64 public JtracConfigurer() { 65 } 67 68 public void setServletContext(ServletContext servletContext) { 69 this.servletContext = servletContext; 70 } 71 72 public void init() throws Exception { 73 String jtracHome = null; 74 ClassPathResource cpr = new ClassPathResource("jtrac-init.properties"); 75 File initPropsFile = cpr.getFile(); 76 if (initPropsFile.exists()) { 77 logger.info("found 'jtrac-init.properties' on classpath, processing..."); 78 InputStream is = null; 79 Properties props = new Properties (); 80 try { 81 is = new FileInputStream (initPropsFile); 82 props.load(is); 83 } finally { 84 is.close(); 85 } 86 jtracHome = props.getProperty("jtrac.home"); 87 if (jtracHome != null) { 88 logger.info("'jtrac.home' property initialized from 'jtrac-init.properties' as '" + jtracHome + "'"); 89 } 90 FilenameFilter ff = new FilenameFilter () { 91 public boolean accept(File dir, String name) { 92 return name.startsWith("messages_") && name.endsWith(".properties"); 93 } 94 }; 95 File [] messagePropsFiles = initPropsFile.getParentFile().listFiles(ff); 96 String locales = "en"; 97 for(File f : messagePropsFiles) { 98 int endIndex = f.getName().indexOf('.'); 99 logger.debug("found " + f.getName()); 100 String localeCode = f.getName().substring(9, endIndex); 101 locales += "," + localeCode; 102 } 103 logger.info("locales available configured are '" + locales + "'"); 104 System.setProperty("jtrac.locales", locales); 105 } else { 106 System.setProperty("jtrac.locales", "en"); 107 } 108 if (jtracHome == null) { 109 logger.info("valid 'jtrac.home' property not available in 'jtrac-init.properties', trying system properties."); 110 jtracHome = System.getProperty("jtrac.home"); 111 if (jtracHome != null) { 112 logger.info("'jtrac.home' property initialized from system properties as '" + jtracHome + "'"); 113 } 114 } 115 if (jtracHome == null) { 116 logger.info("valid 'jtrac.home' property not available in system properties, trying servlet init paramters."); 117 jtracHome = servletContext.getInitParameter("jtrac.home"); 118 if (jtracHome != null) { 119 logger.info("Servlet init parameter 'jtrac.home' exists: '" + jtracHome + "'"); 120 } 121 } 122 if (jtracHome == null) { 123 jtracHome = System.getProperty("user.home") + "/.jtrac"; 124 logger.warn("Servlet init paramter 'jtrac.home' does not exist. Will use 'user.home' directory '" + jtracHome + "'"); 125 } 126 System.setProperty("jtrac.home", jtracHome); 127 File homeFile = new File (jtracHome); 128 if (!homeFile.exists()) { 129 homeFile.mkdir(); 130 logger.info("directory does not exist, created '" + homeFile.getPath() + "'"); 131 if (!homeFile.exists()) { 132 String message = "invalid path '" + homeFile.getAbsolutePath() + "', try creating this directory first. Aborting."; 133 logger.fatal(message); 134 throw new RuntimeException (message); 135 } 136 } else { 137 logger.info("directory already exists: '" + homeFile.getPath() + "'"); 138 } 139 System.setProperty("jtrac.home", homeFile.getAbsolutePath()); 140 File attachmentsFile = new File (jtracHome + "/attachments"); 141 if (!attachmentsFile.exists()) { 142 attachmentsFile.mkdir(); 143 logger.info("directory does not exist, created '" + attachmentsFile.getPath() + "'"); 144 } else { 145 logger.info("directory already exists: '" + attachmentsFile.getPath() + "'"); 146 } 147 File indexesFile = new File (jtracHome + "/indexes"); 148 if (!indexesFile.exists()) { 149 indexesFile.mkdir(); 150 logger.info("directory does not exist, created '" + indexesFile.getPath() + "'"); 151 } else { 152 logger.info("directory already exists: '" + indexesFile.getPath() + "'"); 153 } 154 File propFile = new File (homeFile.getPath() + "/jtrac.properties"); 155 if (!propFile.exists()) { 156 propFile.createNewFile(); 157 logger.info("properties file does not exist, created '" + propFile.getPath() + "'"); 158 OutputStream os = new FileOutputStream (propFile); 159 Writer out = new PrintWriter (os); 160 try { 161 out.write("database.driver=org.hsqldb.jdbcDriver\n"); 162 out.write("database.url=jdbc:hsqldb:file:${jtrac.home}/db/jtrac\n"); 163 out.write("database.username=sa\n"); 164 out.write("database.password=\n"); 165 out.write("hibernate.dialect=org.hibernate.dialect.HSQLDialect\n"); 166 out.write("hibernate.show_sql=false\n"); 167 } finally { 168 out.close(); 169 os.close(); 170 } 171 logger.info("HSQLDB will be used. Finished creating '" + propFile.getPath() + "'"); 172 } else { 173 logger.info("'jtrac.properties' file exists: '" + propFile.getPath() + "'"); 174 } 175 FileSystemResource fsr = new FileSystemResource(propFile); 176 logger.info("opening for processing: " + fsr); 177 InputStream is = null; 178 Properties props = new Properties (); 179 try { 180 is = new FileInputStream (fsr.getFile()); 181 props.load(is); 182 } finally { 183 is.close(); 184 } 185 String databaseUrl = props.getProperty("database.url"); 186 if (databaseUrl.trim().startsWith("jdbc:hsqldb:file")) { 187 logger.info("embedded HSQLDB mode detected, switching on spring single connection data source"); 188 System.setProperty("jtrac.datasource", "dataSource"); 189 System.setProperty("database.validationQuery", ""); 190 } else { 191 logger.info("Not using embedded HSQLDB, switching on Apache DBCP data source connection pooling"); 192 System.setProperty("jtrac.datasource", "dataSourceDbcp"); 193 String validationQuery = props.getProperty("database.validationQuery"); 194 if (validationQuery == null) { 195 logger.info("database.validationQuery property not found in 'jtrac.properties', using default 'SELECT 1'"); 196 System.setProperty("database.validationQuery", "SELECT 1"); 197 } else { 198 logger.info("database.validationQuery property found in 'jtrac.properties': '" + validationQuery + "'"); 199 } 200 } 201 setLocation(fsr); 202 } 203 204 } 205 | Popular Tags |