List of usage examples for org.springframework.core.io Resource getFile
File getFile() throws IOException;
From source file:org.openmrs.messagesource.impl.MutableResourceBundleMessageSource.java
/** * Presumes to append the messages to a message.properties file which is already being monitored * by the super ReloadableResourceBundleMessageSource. This is a blind, trusting hack. * * @see org.openmrs.messagesource.MutableMessageSource#publishProperties(java.util.Properties, * java.lang.String, java.lang.String, java.lang.String, java.lang.String) * @deprecated use {@linkplain #merge(MutableMessageSource, boolean)} *//*from ww w . j a va 2s . c om*/ @Deprecated public void publishProperties(Properties props, String locale, String namespace, String name, String version) { String filePrefix = (namespace.length() > 0) ? (namespace + "_") : ""; String propertiesPath = "/WEB-INF/" + filePrefix + "messages" + locale + ".properties"; Resource propertiesResource = applicationContext.getResource(propertiesPath); try { File propertiesFile = propertiesResource.getFile(); if (!propertiesFile.exists()) { propertiesFile.createNewFile(); } // append the properties to the appropriate messages file OpenmrsUtil.storeProperties(props, propertiesFile, namespace + ": " + name + " v" + version); } catch (Exception ex) { log.error("Error creating new properties file"); } }
From source file:org.openmrs.messagesource.impl.MutableResourceBundleMessageSource.java
/** * Searches the filesystem for message properties files. ABKTODO: consider caching this, rather * than searching every time/*from www .ja va 2s . c o m*/ * * @return collection of property file names */ private Collection<File> findPropertiesFiles() { Collection<File> propertiesFiles = new Vector<File>(); try { for (String basename : basenames) { File basefilename = new File(basename); basename = basefilename.getPath(); int nameIndex = basename.lastIndexOf(File.separatorChar) + 1; String basedir = (nameIndex > 0) ? basename.substring(0, nameIndex) : ""; String namePrefix = basename.substring(nameIndex); Resource propertiesDir = applicationContext.getResource(basedir); boolean filesFound = false; if (propertiesDir.exists()) { for (File possibleFile : propertiesDir.getFile().listFiles()) { if (possibleFile.getName().startsWith(namePrefix) && possibleFile.getName().endsWith(".properties")) { propertiesFiles.add(possibleFile); filesFound = true; } } } else { if (log.isDebugEnabled()) { log.debug("Parent directory " + propertiesDir + " does not exist"); } } if (log.isDebugEnabled() && !filesFound) { log.debug("No messages for basename " + basename); } } } catch (IOException e) { log.error("Error generated", e); } if (log.isWarnEnabled() && (propertiesFiles.size() == 0)) { log.warn("No properties files found."); } return propertiesFiles; }
From source file:org.opennms.netmgt.config.DefaultEventConfDao.java
/** * <p>saveCurrent</p>//from w w w . ja v a2s .com */ public synchronized void saveCurrent() { for (Entry<Resource, Events> entry : getEventConfiguration().getEventFiles().entrySet()) { Resource resource = entry.getKey(); Events fileEvents = entry.getValue(); StringWriter stringWriter = new StringWriter(); try { CastorUtils.marshalWithTranslatedExceptions(fileEvents, stringWriter); } catch (DataAccessException e) { throw new DataAccessResourceFailureException( "Could not marshal configuration file for " + resource + ": " + e, e); } if (stringWriter.toString() != null) { File file; try { file = resource.getFile(); } catch (IOException e) { throw new DataAccessResourceFailureException("Event resource '" + resource + "' is not a file resource and cannot be saved. Nested exception: " + e, e); } Writer fileWriter; try { fileWriter = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); } catch (IOException e) { throw new DataAccessResourceFailureException( "Event file '" + file + "' could not be opened. Nested exception: " + e, e); } try { fileWriter.write(stringWriter.toString()); } catch (IOException e) { throw new DataAccessResourceFailureException( "Event file '" + file + "' could not be written to. Nested exception: " + e, e); } try { fileWriter.close(); } catch (IOException e) { throw new DataAccessResourceFailureException( "Event file '" + file + "' could not be closed. Nested exception: " + e, e); } } } File programmaticStoreFile; try { programmaticStoreFile = getProgrammaticStoreConfigResource().getFile(); } catch (IOException e) { log().info("Programmatic store resource '" + getProgrammaticStoreConfigResource() + "'; not attempting to delete an unused programmatic store file if it exists (since we can't test for it)."); programmaticStoreFile = null; } if (programmaticStoreFile != null) { // Delete the programmatic store if it exists on disk, but isn't in the main store. This is for cleanliness if (programmaticStoreFile.exists() && (!getEventConfiguration().getEventFiles() .containsKey(getProgrammaticStoreConfigResource()))) { log().info( "Deleting programmatic store configuration file because it is no longer referenced in the root config file " + getConfigResource()); if (!programmaticStoreFile.delete()) { LogUtils.warnf(this, "Attempted to delete %s, but failed.", programmaticStoreFile); } } } /* * XXX Should we call reload so that the EventConfData object is updated * without the caller having to call reload() themselves? */ //reload(); }
From source file:org.opennms.netmgt.config.SnmpPeerFactory.java
/** * <p>Constructor for SnmpPeerFactory.</p> * * @param resource a {@link org.springframework.core.io.Resource} object. *///from w w w . ja va 2 s . c o m public SnmpPeerFactory(final Resource resource) { SnmpPeerFactory.getWriteLock().lock(); try { final SnmpConfig config = JaxbUtils.unmarshal(SnmpConfig.class, resource); try { final File file = resource.getFile(); if (file != null) { m_callback = new FileReloadCallback<SnmpConfig>() { @Override public SnmpConfig reload(final SnmpConfig object, final Resource resource) throws IOException { return JaxbUtils.unmarshal(SnmpConfig.class, resource); } }; m_container = new FileReloadContainer<SnmpConfig>(config, resource, m_callback); return; } } catch (final IOException e) { LOG.debug("No file associated with resource {}, skipping reload container initialization.", resource); } // if we fall through to here, then the file was null, or something else went wrong store the config directly m_config = config; } finally { SnmpPeerFactory.getWriteLock().unlock(); } }
From source file:org.opennms.netmgt.dao.support.PropertiesGraphDao.java
private void scanIncludeDirectory(PrefabGraphTypeDao type) throws IOException { Resource includeDirectoryResource = type.getIncludeDirectoryResource(); if (includeDirectoryResource != null) { File includeDirectory = includeDirectoryResource.getFile(); // Include all the files in the directory, knowing that the // format is slightly different (no report name required in // each property name, and report.id is expected) FilenameFilter propertyFilesFilter = new FilenameFilter() { @Override/*ww w. j a v a 2 s . co m*/ public boolean accept(File dir, String name) { return (name.endsWith(".properties")); } }; final File[] propertyFiles = includeDirectory.listFiles(propertyFilesFilter); Arrays.sort(propertyFiles); for (final File file : propertyFiles) { loadIncludedFile(type, file); } } type.setLastIncludeScan(System.currentTimeMillis()); }
From source file:org.opennms.netmgt.provision.persist.DirectoryWatcherTest.java
@Before public void setUp() throws Exception { MockLogAppender.setupLogging();//from w w w . ja va 2s. c o m m_bldr = new FileSystemBuilder("target", "DirectoryWatcherTest"); m_bldr.file("file1.xml", "file1Contents").file("file2.xml", "file2Contents"); File dir = m_bldr.getCurrentDir(); FileReloadCallback<String> loader = new FileReloadCallback<String>() { @Override public String reload(String object, Resource resource) throws IOException { return FileUtils.readFileToString(resource.getFile()); } }; m_watcher = new DirectoryWatcher<String>(dir, loader); }
From source file:org.opennms.netmgt.provision.persist.FasterFilesystemForeignSourceRepository.java
private FileReloadCallback<ForeignSource> fsLoader() { return new FileReloadCallback<ForeignSource>() { @Override/*from www . ja v a2s .co m*/ public ForeignSource reload(ForeignSource object, Resource resource) throws IOException { return RequisitionFileUtils.getForeignSourceFromFile(resource.getFile()); } }; }
From source file:org.opennms.netmgt.provision.persist.FasterFilesystemForeignSourceRepository.java
private FileReloadCallback<Requisition> reqLoader() { return new FileReloadCallback<Requisition>() { @Override/* w ww. java2 s . co m*/ public Requisition reload(Requisition object, Resource resource) throws IOException { return RequisitionFileUtils.getRequisitionFromFile(resource.getFile()); } }; }
From source file:org.opennms.netmgt.provision.persist.RequisitionFileUtils.java
public static void deleteResourceIfSnapshot(final Requisition requisition) { final Resource resource = requisition.getResource(); if (resource == null) return;/*from www . j av a 2 s . co m*/ try { final File resourceFile = resource.getFile(); if (isSnapshot(requisition.getForeignSource(), resourceFile)) { LOG.trace("Deleting {}", resourceFile); if (!resourceFile.delete()) { LOG.debug("Failed to delete {}", resourceFile); } } } catch (final IOException e) { LOG.debug("Resource {} can't be turned into a file, skipping snapshot delete detection.", resource, e); return; } }
From source file:org.opennms.reporting.availability.render.HTMLReportRenderer.java
/** {@inheritDoc} */ @Override// w w w .j ava 2s. c om public void render(final String inputFileName, final OutputStream outputStream, final Resource xsltResource) throws ReportRenderException { try { Logging.withPrefix(LOG4J_CATEGORY, new Callable<Void>() { @Override public Void call() throws Exception { LOG.debug("Rendering {} with XSL File {} to OutputStream", inputFileName, xsltResource.getDescription()); FileInputStream in = null, xslt = null; try { in = new FileInputStream(xsltResource.getFile()); Reader xsl = new InputStreamReader(in, "UTF-8"); xslt = new FileInputStream(inputFileName); Reader xml = new InputStreamReader(xslt, "UTF-8"); render(xml, outputStream, xsl); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(xslt); } return null; } }); } catch (final Exception e) { if (e instanceof ReportRenderException) throw (ReportRenderException) e; throw new ReportRenderException("Failed to render " + inputFileName, e); } }