1 25 26 package com.j2biz.blogunity; 27 28 import java.io.File ; 29 import java.util.Calendar ; 30 import java.util.Date ; 31 32 import javax.servlet.ServletContext ; 33 import javax.servlet.ServletContextEvent ; 34 import javax.servlet.ServletContextListener ; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 import org.quartz.JobDetail; 39 import org.quartz.Scheduler; 40 import org.quartz.SchedulerException; 41 import org.quartz.SimpleTrigger; 42 import org.quartz.impl.StdSchedulerFactory; 43 44 import com.j2biz.blogunity.dao.SystemConfigurationDAO; 45 import com.j2biz.blogunity.exception.BlogunityException; 46 import com.j2biz.blogunity.exception.BlogunityRuntimeException; 47 import com.j2biz.blogunity.i18n.I18N; 48 import com.j2biz.blogunity.i18n.I18NStatusFactory; 49 import com.j2biz.blogunity.jobs.RefererAndVisitedPagesRemover; 50 import com.j2biz.blogunity.pojo.SystemConfiguration; 51 import com.j2biz.blogunity.util.HibernateUtil; 52 53 60 public class BlogunityManager implements ServletContextListener { 61 64 private static final Log log = LogFactory.getLog(BlogunityManager.class); 65 66 private static SystemConfiguration blogunityConfiguration = null; 67 68 70 private static ServletContext servletContext; 71 72 private static String host; 73 74 private static String base; 75 76 private static String contextPath; 77 78 private static Scheduler scheduler; 79 80 private static Date startupTime; 81 82 public BlogunityManager() { 83 } 84 85 90 public void contextInitialized(ServletContextEvent e) { 91 if (log.isDebugEnabled()) { 92 log.debug("contextInitialized(ServletContextEvent) - start"); 93 } 94 95 try { 96 97 startupTime = new Date (); 98 99 servletContext = e.getServletContext(); 100 101 initConfiguration(); 102 103 if (log.isDebugEnabled()) { 105 log.debug("Check, if the data-directory for the blog's content exists. " 106 + "If not, create new one..."); 107 } 108 String dataDir = blogunityConfiguration.getDataDir(); 109 File _dataDir = new File (dataDir); 110 if (!_dataDir.exists()) { 111 112 boolean result = _dataDir.mkdir(); 113 if (!result) { 114 115 throw new BlogunityRuntimeException(I18NStatusFactory.create( 116 I18N.ERRORS.CREATE_DIRECTORY, new String []{_dataDir.getAbsolutePath()})); } 117 118 } else { 119 if (!_dataDir.isDirectory() || !_dataDir.canRead() || !_dataDir.canWrite()) { 120 121 throw new BlogunityRuntimeException(I18NStatusFactory.create( 122 I18N.ERRORS.READ_DIRECTORY, new String []{_dataDir.getAbsolutePath()})); } 123 124 } 125 126 File _blogsDataDir = new File (_dataDir, "blogs"); 128 if (!_blogsDataDir.exists()) { 129 boolean result = _blogsDataDir.mkdir(); 130 if (!result) { 131 132 throw new BlogunityRuntimeException(I18NStatusFactory 133 .create(I18N.ERRORS.CREATE_DIRECTORY, new String []{_blogsDataDir 134 .getAbsolutePath()})); } 135 } 136 137 File _usersDataDir = new File (_dataDir, "users"); 138 if (!_usersDataDir.exists()) { 139 boolean result = _usersDataDir.mkdir(); 140 if (!result) { 141 142 throw new BlogunityRuntimeException(I18NStatusFactory 143 .create(I18N.ERRORS.CREATE_DIRECTORY, new String []{_usersDataDir 144 .getAbsolutePath()})); } 145 } 146 147 148 if (log.isDebugEnabled()) { 150 log.debug("Check, if the temp-directory exists. " + "If not, create new one..."); 151 } 152 String tempDir = blogunityConfiguration.getTempDir(); 153 File _tempDir = new File (tempDir); 154 if (!_tempDir.exists()) { 155 156 boolean result = _tempDir.mkdir(); 157 if (!result) { 158 159 throw new BlogunityRuntimeException(I18NStatusFactory.create( 160 I18N.ERRORS.CREATE_DIRECTORY, new String []{_tempDir.getAbsolutePath()})); } 161 162 } else { 163 if (!_tempDir.isDirectory() || !_tempDir.canRead() || !_tempDir.canWrite()) { throw new BlogunityRuntimeException( 164 I18NStatusFactory.create(I18N.ERRORS.READ_DIRECTORY, new String []{_tempDir 165 .getAbsolutePath()})); } 166 } 167 168 try { 169 scheduler = StdSchedulerFactory.getDefaultScheduler(); 171 scheduler.start(); 172 173 scheduleJobs(); 174 175 } catch (SchedulerException e1) { 176 177 throw new BlogunityRuntimeException(I18NStatusFactory.createUnknown(e1)); 178 } 179 180 if (log.isInfoEnabled()) { 181 log.info("Blogunity started at " + startupTime); 182 } 183 184 } catch (BlogunityException e2) { 185 if (log.isErrorEnabled()) { 186 log.error("Error loading configuration!", e2); 187 } 188 189 throw new BlogunityRuntimeException(I18NStatusFactory.create( 190 I18N.ERRORS.LOADING_CONFIGURATION_FAILED, e2)); 191 } 192 193 if (log.isDebugEnabled()) { 194 log.debug("contextInitialized(ServletContextEvent) - end"); 195 } 196 } 197 198 202 private void initConfiguration() throws BlogunityException { 203 SystemConfigurationDAO dao = new SystemConfigurationDAO(); 204 blogunityConfiguration = dao.getSystemConfiguration(); 205 206 } 207 208 private void scheduleJobs() throws SchedulerException { 209 210 JobDetail jobDetail = new JobDetail("RefererAndVisitedPagesRemover", 212 Scheduler.DEFAULT_GROUP, RefererAndVisitedPagesRemover.class); 213 214 Date d = new Date (); 215 Calendar calendar = Calendar.getInstance(); 216 calendar.setTime(d); 217 calendar.add(Calendar.DATE, 1); 218 calendar.set(Calendar.HOUR, 0); 219 calendar.set(Calendar.MINUTE, 0); 220 calendar.set(Calendar.SECOND, 1); 221 222 if (log.isDebugEnabled()) { 223 log.debug("!!!>>>>> Execute RefererAndVisitedPagesRemover for the first time at " 224 + calendar.getTime()); 225 } 226 227 int millis = 24 * 60 * 60 * 1000; 229 SimpleTrigger trigger = new SimpleTrigger("RefererAndVisitedPagesTrigger", 230 Scheduler.DEFAULT_GROUP, calendar.getTime(), null, 231 SimpleTrigger.REPEAT_INDEFINITELY, millis); 232 233 scheduler.scheduleJob(jobDetail, trigger); 234 235 } 236 237 240 public static SystemConfiguration getSystemConfiguration() { 241 return blogunityConfiguration; 242 } 243 244 249 public void contextDestroyed(ServletContextEvent e) { 250 if (log.isDebugEnabled()) { 251 log.debug("contextDestroyed(ServletContextEvent) - start"); 252 } 253 254 HibernateUtil.closeSession(); 256 257 try { 258 if (scheduler != null) scheduler.shutdown(); 260 } catch (SchedulerException e1) { 261 log.error("Error shutting down scheduler!", e1); 262 } 263 264 if (log.isDebugEnabled()) { 265 log.debug("contextDestroyed(ServletContextEvent) - end"); 266 } 267 } 268 269 274 277 public static ServletContext getServletContext() { 278 return servletContext; 279 } 280 281 284 public static String getContextPath() { 285 return contextPath; 286 } 287 288 292 public static void setContextPath(String contextPath) { 293 BlogunityManager.contextPath = contextPath; 294 } 295 296 303 public static String getRealPath(String path) { 304 return servletContext.getRealPath(path); 305 } 306 307 public static Scheduler getScheduler() { 308 return scheduler; 309 } 310 311 316 public static Date getStartupTime() { 317 return startupTime; 318 } 319 320 325 public static void setHost(String host) { 326 BlogunityManager.host = host; 327 } 328 329 335 public static String getHost() { 336 return host; 337 } 338 339 348 public static String getBase() { 349 if (base == null) { 350 base = getHost() + getContextPath(); 351 if (base.endsWith("/")) base = base.substring(0, base.length() - 1); 352 } 353 return base; 354 } 355 } | Popular Tags |