List of usage examples for org.springframework.context.support FileSystemXmlApplicationContext close
@Override public void close()
From source file:net.phoenix.thrift.server.Server.java
/** * ? spring?/*from w w w . j a v a2 s . c o m*/ * @param args * @throws Exception */ public static int main(String[] args) throws Exception { if (args.length == 0 || StringUtils.isEmpty(args[0])) { LOG.error("Please assign spring configuration file . "); return 0; } FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(); context.setConfigLocation(args[0]); context.registerShutdownHook(); context.refresh(); context.close(); return 1; }
From source file:de.uniwue.dmir.heatmap.EntryPoint.java
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) {
String settingsFile;//ww w .j a va2s . co m
if (args.length > 0 && args[0] != null) {
settingsFile = args[0];
} else {
settingsFile = SETTINGS_FILE;
}
LOGGER.debug("Reading settings file: {}", settingsFile);
FileSystemXmlApplicationContext appContext = new FileSystemXmlApplicationContext(settingsFile);
IHeatmap heatmap = appContext.getBean(HEATMAP_BEAN, IHeatmap.class);
ITileProcessor tileProcessor = appContext.getBean(WRITER_BEAN, ITileProcessor.class);
heatmap.processTiles(tileProcessor);
tileProcessor.close();
appContext.close();
}
From source file:de.uniwue.dmir.heatmap.EntryPointIncremental.java
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) throws IOException, ParseException {
DateFormat df = new SimpleDateFormat(DATE_FORMAT);
SimpleDateFormat backupDf = new SimpleDateFormat(BACKUP_DATE_FORMAT);
String workDir = System.getProperty("workDir", ".");
LOGGER.debug("Work dir: {}", workDir);
String configDir = System.getProperty("configDir", ".");
LOGGER.debug("Config dir: {}", configDir);
File seedDir = new File(workDir, SEED_DIR);
LOGGER.debug("Seed dir: {}", seedDir);
File currentDir = new File(workDir, CURRENT_DIR);
LOGGER.debug("Current dir: {}", currentDir);
File backupDir = new File(workDir, BACKUP_DIR);
LOGGER.debug("Backup dir: {}", backupDir);
String initialMinTimeString = System.getProperty("minTime");
LOGGER.debug("Initial minimal time parameter: {}", initialMinTimeString);
Date initialMinTime = initialMinTimeString == null ? new Date(0) : df.parse(initialMinTimeString);
LOGGER.debug("Initial minimal time: {}", df.format(initialMinTime));
String absoluteMaxTimeString = System.getProperty("maxTime");
LOGGER.debug("Absolute maximal time parameter: {}", absoluteMaxTimeString);
Date absoluteMaxTime = absoluteMaxTimeString == null ? new Date()
: new SimpleDateFormat(DATE_FORMAT).parse(absoluteMaxTimeString);
LOGGER.debug("Absolute maximal time: {}", df.format(absoluteMaxTime));
String incrementalFile = new File("file:" + configDir, INCREMENTAL_FILE).getPath();
String settingsFile = new File("file:" + configDir, HEATMAP_PROCESSOR__FILE).getPath();
LOGGER.debug("Initializing incremental control file: {}", incrementalFile);
FileSystemXmlApplicationContext incrementalContext = new FileSystemXmlApplicationContext(incrementalFile);
// get point limit
int pointLimit = Integer
.parseInt(incrementalContext.getBeanFactory().resolveEmbeddedValue("${point.limit}"));
LOGGER.debug("Print limit: {}", pointLimit);
// get backups to keep
int backupsToKeep = Integer
.parseInt(incrementalContext.getBeanFactory().resolveEmbeddedValue("${backups.to.keep}"));
LOGGER.debug("Backups to keep: {}", pointLimit);
LOGGER.debug("Initializing process components (manager and limiter).");
IProcessManager processManager = incrementalContext.getBean(IProcessManager.class);
IProcessLimiter processLimiter = incrementalContext.getBean(IProcessLimiter.class);
LOGGER.debug("Starting incremental loop.");
while (true) { // break as soon as no new points are available
// cleanup --- just in case
LOGGER.debug("Deleting \"current\" dir.");
FileUtils.deleteDirectory(currentDir);
// copy from seed to current
LOGGER.debug("Copying seed.");
seedDir.mkdirs();/*from w ww .ja va 2 s . co m*/
FileUtils.copyDirectory(seedDir, currentDir);
// get min time
LOGGER.debug("Getting minimal time ...");
Date minTime = initialMinTime;
ProcessManagerEntry entry = processManager.getEntry();
if (entry != null && entry.getMaxTime() != null) {
minTime = entry.getMaxTime();
}
LOGGER.debug("Minimal time: {}", new SimpleDateFormat(DATE_FORMAT).format(minTime));
// break if we processed all available points (minTime is greater than or equal to absoluteMaxTime)
if (minTime.getTime() >= absoluteMaxTime.getTime()) {
LOGGER.debug("Processed all points.");
break;
}
// get the maximal time
LOGGER.debug("Get maximal time.");
// get the time from the newest point in our point range (pointMaxTime) ...
Date pointMaxTime = processLimiter.getMaxTime(minTime, pointLimit);
// ... and possibly break the loop if no new points are available
if (pointMaxTime == null)
break;
// set the max time and make sure we are not taking to many points
// (set max time to the minimum of pointMaxTime and absoluteMaxTime)
Date maxTime = pointMaxTime.getTime() > absoluteMaxTime.getTime() ? absoluteMaxTime : pointMaxTime;
LOGGER.debug("Maximal time: {}", new SimpleDateFormat(DATE_FORMAT).format(maxTime));
// start process
processManager.start(minTime);
System.setProperty("minTimestamp", new SimpleDateFormat(DATE_FORMAT).format(minTime));
System.setProperty("maxTimestamp", new SimpleDateFormat(DATE_FORMAT).format(maxTime));
FileSystemXmlApplicationContext heatmapContext = new FileSystemXmlApplicationContext(settingsFile);
IHeatmap heatmap = heatmapContext.getBean(HEATMAP_BEAN, IHeatmap.class);
ITileProcessor tileProcessor = heatmapContext.getBean(WRITER_BEAN, ITileProcessor.class);
heatmap.processTiles(tileProcessor);
tileProcessor.close();
heatmapContext.close();
// finish process
processManager.finish(maxTime);
// move old seed
if (backupsToKeep > 0) {
FileUtils.moveDirectory(seedDir, new File(backupDir, backupDf.format(minTime))); // minTime is the maxTime of the seed
// cleanup backups
String[] backups = backupDir.list(DirectoryFileFilter.DIRECTORY);
File oldestBackup = null;
if (backups.length > backupsToKeep) {
for (String bs : backups) {
File b = new File(backupDir, bs);
if (oldestBackup == null || oldestBackup.lastModified() > b.lastModified()) {
oldestBackup = b;
}
}
FileUtils.deleteDirectory(oldestBackup);
}
} else {
FileUtils.deleteDirectory(seedDir);
}
// move new seed
FileUtils.moveDirectory(currentDir, seedDir);
}
incrementalContext.close();
}
From source file:net.darkmist.alib.spring.Main.java
public static void main(String[] args) { FileSystemXmlApplicationContext ctx; Runnable mb;/* w w w . j a v a 2s . c o m*/ boolean doExit = false; int exitCode = 0; if (args.length < 1) usage(); ctx = new FileSystemXmlApplicationContext(args[0]); mb = (Runnable) ctx.getBean(MAIN_BEAN); if (mb instanceof MainBean) ((MainBean) mb).setArgs(args, 1, args.length - 1); else if (args.length > 1) throw new IllegalArgumentException("main bean does not take arguments"); mb.run(); if (mb instanceof MainBean) { exitCode = ((MainBean) mb).getExitCode(); doExit = true; } mb = null; ctx.close(); if (doExit) System.exit(exitCode); }
From source file:org.craftercms.cstudio.publishing.StopServiceMain.java
public static void main(String[] args) throws Exception { FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext( "classpath:spring/shutdown-context.xml"); ReadablePropertyPlaceholderConfigurer properties = (ReadablePropertyPlaceholderConfigurer) context .getBean("cstudioShutdownProperties"); if (properties != null) { String url = getProperty(properties, PROP_URL); String path = getProperty(properties, PROP_SERVICE_PATH); String port = getProperty(properties, PROP_PORT); String password = URLEncoder.encode(getProperty(properties, PROP_PASSWORD), "UTF-8"); String target = url + ":" + port + path; if (LOGGER.isDebugEnabled()) { LOGGER.debug("Sending a stop request to " + target); }/*from w w w . j a v a 2s. c o m*/ target = target + "?" + StopServiceServlet.PARAM_PASSWORD + "=" + password; URL serviceUrl = new URL(target); HttpURLConnection connection = (HttpURLConnection) serviceUrl.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(10000); connection.connect(); try { connection.getContent(); } catch (ConnectException e) { // ignore this error (server will terminate as soon as the request is sent out) } } else { if (LOGGER.isErrorEnabled()) { LOGGER.error(PROPERTIES_NAME + " is not present in shutdown-context.xml"); } } context.close(); }
From source file:com.px100systems.data.utility.RestoreUtility.java
public static void main(String[] args) { if (args.length < 3) { System.err.println("Usage: java -cp ... com.px100systems.data.utility.RestoreUtility " + "<springXmlConfigFile> <persisterBeanName> <backupDirectory> [compare]"); return;//from w w w. jav a 2 s.c o m } FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext("file:" + args[0]); try { PersistenceProvider persister = ctx.getBean(args[1], PersistenceProvider.class); File directory = new File(args[2]); if (!directory.isDirectory()) { System.err.println(directory.getName() + " is not a directory"); return; } List<File> files = new ArrayList<File>(); //noinspection ConstantConditions for (File file : directory.listFiles()) if (BackupFile.isBackup(file)) files.add(file); if (files.isEmpty()) { System.err.println(directory.getName() + " directory has no backup files"); return; } if (args.length == 4 && args[3].equalsIgnoreCase("compare")) { final Map<String, Map<Long, RawRecord>> units = new HashMap<String, Map<Long, RawRecord>>(); for (String storage : persister.storage()) { System.out.println("Storage " + storage); persister.loadByStorage(storage, new PersistenceProvider.LoadCallback() { @Override public void process(RawRecord record) { Map<Long, RawRecord> unitList = units.get(record.getUnitName()); if (unitList == null) { unitList = new HashMap<Long, RawRecord>(); units.put(record.getUnitName(), unitList); } unitList.put(record.getId(), record); } }); for (final Map.Entry<String, Map<Long, RawRecord>> unit : units.entrySet()) { BackupFile file = null; for (int i = 0, n = files.size(); i < n; i++) if (BackupFile.isBackup(files.get(i), unit.getKey())) { file = new BackupFile(files.get(i)); files.remove(i); break; } if (file == null) throw new RuntimeException("Could not find backup file for unit " + unit.getKey()); final Long[] count = new Long[] { 0L }; file.read(new PersistenceProvider.LoadCallback() { @Override public void process(RawRecord record) { RawRecord r = unit.getValue().get(record.getId()); if (r == null) throw new RuntimeException("Could not find persisted record " + record.getId() + " for unit " + unit.getKey()); if (!r.equals(record)) throw new RuntimeException( "Record " + record.getId() + " mismatch for unit " + unit.getKey()); count[0] = count[0] + 1; } }); if (count[0] != unit.getValue().size()) throw new RuntimeException("Extra persisted records for unit " + unit.getKey()); System.out.println(" Unit " + unit.getKey() + ": OK"); } units.clear(); } if (!files.isEmpty()) { System.err.println("Extra backups: "); for (File file : files) System.err.println(" " + file.getName()); } } else { persister.init(); for (File file : files) { InMemoryDatabase.readBackupFile(file, persister); System.out.println("Loaded " + file.getName()); } } } catch (Exception e) { throw new RuntimeException(e); } finally { ctx.close(); } }
From source file:org.craftercms.cstudio.publishing.PublishingReceiverMain.java
private static Server initializeContext() throws IOException { System.setProperty("org.terracotta.quartz.skipUpdateCheck", "true"); FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext( "classpath:spring/application-context.xml"); Server server = (Server) context.getBean("Server"); context.close(); return server; }
From source file:cn.vlabs.umt.ui.UMTStartupListener.java
public void contextDestroyed(ServletContextEvent event) { ServletContext context = event.getServletContext(); FileSystemXmlApplicationContext factory = (FileSystemXmlApplicationContext) context .getAttribute(Attributes.APPLICATION_CONTEXT_KEY); if (factory != null) { context.removeAttribute(Attributes.APPLICATION_CONTEXT_KEY); factory.close(); factory = null;// w w w . jav a2 s.c om } }
From source file:org.activiti.crystalball.anttasks.GenerateGraphTask.java
public void execute() { log("Starting generator [" + generatorBean + "] from application context [" + appContext + "].", Project.MSG_INFO);/*from w w w. j a v a 2s.c o m*/ // seting app context if (appContext == null) { throw new BuildException("No application context set."); } FileSystemXmlApplicationContext applicationContext = new FileSystemXmlApplicationContext(appContext); try { // getting generator AbstractProcessEngineGraphGenerator generator = null; if (generatorBean != null) generator = (AbstractProcessEngineGraphGenerator) applicationContext.getBean(generatorBean); if (generator == null) { applicationContext.close(); throw new BuildException("unable to get generator bean"); } // running report generate try { generator.generateReport(processDefinitionId, getStartDate(), getEndDate(), reportFileName); } catch (IOException e) { log("Generator exception", Project.MSG_ERR); throw new BuildException(e); } catch (ParseException e) { log("Generator exception - parsing dates", Project.MSG_ERR); throw new BuildException(e); } } finally { applicationContext.close(); } log("Generator [" + generatorBean + "] execution from application context [" + appContext + "] done.", Project.MSG_INFO); }