List of usage examples for org.apache.commons.io FileUtils toFile
public static File toFile(URL url)
URL
to a File
. From source file:org.jamwiki.utils.Utilities.java
/** * Given a file name for a file that is located somewhere in the application * classpath, return a File object representing the file. * * @param filename The name of the file (relative to the classpath) that is * to be retrieved./*from w ww . j a v a2s . co m*/ * @return A file object representing the requested filename * @throws FileNotFoundException Thrown if the classloader can not be found or if * the file can not be found in the classpath. */ public static File getClassLoaderFile(String filename) throws FileNotFoundException { // note that this method is used when initializing logging, so it must // not attempt to log anything. File file = null; ClassLoader loader = Utilities.getClassLoader(); URL url = loader.getResource(filename); if (url == null) { url = ClassLoader.getSystemResource(filename); } if (url == null) { throw new FileNotFoundException("Unable to find " + filename); } file = FileUtils.toFile(url); if (file == null || !file.exists()) { try { // url exists but file cannot be read, so perhaps it's not a "file:" url (an example // would be a "jar:" url). as a workaround, copy the file to a temp file and return // the temp file. file = File.createTempFile(filename, null); FileUtils.copyURLToFile(url, file); } catch (IOException e) { throw new FileNotFoundException("Unable to load file with URL " + url); } } return file; }
From source file:org.jamwiki.utils.Utilities.java
/** * Utility method for reading a file from a classpath directory and returning * its contents as a String./*from w ww. ja v a 2 s . c o m*/ * * @param filename The name of the file to be read, either as an absolute file * path or relative to the classpath. * @return A string representation of the file contents. * @throws FileNotFoundException Thrown if the file cannot be found or if an I/O exception * occurs. */ public static String readFile(String filename) throws IOException { File file = new File(filename); if (file.exists()) { // file passed in as full path return FileUtils.readFileToString(file, "UTF-8"); } // look for file in resource directories ClassLoader loader = Utilities.getClassLoader(); URL url = loader.getResource(filename); file = FileUtils.toFile(url); if (file == null || !file.exists()) { throw new FileNotFoundException("File " + filename + " is not available for reading"); } return FileUtils.readFileToString(file, "UTF-8"); }
From source file:org.jboss.arquillian.container.mobicents.servlet.sip.tomcat.embedded_7.MobicentsSipServletsContainer.java
@Override public void startTomcatEmbedded(Properties sipStackProperties) throws UnknownHostException, org.apache.catalina.LifecycleException, LifecycleException { System.setProperty("javax.servlet.sip.ar.spi.SipApplicationRouterProvider", configuration.getSipApplicationRouterProviderClassName()); if (bindAddress != null) { System.setProperty("org.mobicents.testsuite.testhostaddr", bindAddress); } else {/*from w w w . j a va 2 s . c om*/ System.setProperty("org.mobicents.testsuite.testhostaddr", "127.0.0.1"); } //Required in order to read the dar conf of each file //The Router in the arquillian.xml should be <property name="sipApplicationRouterProviderClassName">org.mobicents.servlet.sip.router.DefaultApplicationRouterProvider</property> String darConfiguration = Thread.currentThread().getContextClassLoader().getResource("test-dar.properties") .toString(); if (darConfiguration != null) { System.setProperty("javax.servlet.sip.dar", Thread.currentThread().getContextClassLoader().getResource("test-dar.properties").toString()); } else { System.setProperty("javax.servlet.sip.dar", Thread.currentThread().getContextClassLoader().getResource("empty-dar.properties").toString()); } // creating the tomcat embedded == service tag in server.xml MobicentsSipServletsEmbeddedImpl embedded = new MobicentsSipServletsEmbeddedImpl(); this.embedded = embedded; sipStandardService = (SipStandardService) embedded.getService(); sipStandardService .setSipApplicationDispatcherClassName(SipApplicationDispatcherImpl.class.getCanonicalName()); sipStandardService.setCongestionControlCheckingInterval(-1); sipStandardService.setAdditionalParameterableHeaders("additionalParameterableHeader"); sipStandardService.setUsePrettyEncoding(true); sipStandardService.setName(serverName); if (sipStackProperties != null) sipStandardService.setSipStackProperties(sipStackProperties); // TODO this needs to be a lot more robust String tomcatHome = configuration.getTomcatHome(); File tomcatHomeFile = null; if (tomcatHome != null) { if (tomcatHome.startsWith(ENV_VAR)) { String sysVar = tomcatHome.substring(ENV_VAR.length(), tomcatHome.length() - 1); tomcatHome = System.getProperty(sysVar); if (tomcatHome != null && tomcatHome.length() > 0 && new File(tomcatHome).isAbsolute()) { tomcatHomeFile = new File(tomcatHome); log.info("Using tomcat home from environment variable: " + tomcatHome); } } else { tomcatHomeFile = new File(tomcatHome); } } if (tomcatHomeFile == null) { tomcatHomeFile = new File(System.getProperty(TMPDIR_SYS_PROP), "mss-tomcat-embedded-7"); } tomcatHomeFile.mkdirs(); embedded.setCatalinaBase(tomcatHomeFile.getAbsolutePath()); embedded.setCatalinaHome(tomcatHomeFile.getAbsolutePath()); // creates the engine, i.e., <engine> element in server.xml Engine engine = embedded.createEngine(); this.engine = engine; engine.setName(serverName); engine.setDefaultHost(bindAddress); engine.setService(sipStandardService); sipStandardService.setContainer(engine); embedded.addEngine(engine); // creates the host, i.e., <host> element in server.xml appBase = new File(tomcatHomeFile, configuration.getAppBase()); appBase.mkdirs(); //Host configuration StandardHost host = (StandardHost) embedded.createHost(bindAddress, appBase.getAbsolutePath()); this.standardHost = host; if (configuration.getTomcatWorkDir() != null) { host.setWorkDir(configuration.getTomcatWorkDir()); } host.setUnpackWARs(configuration.isUnpackArchive()); host.setDeployOnStartup(false); host.setAutoDeploy(false); embedded.getEngine().addChild(host); //Copy the license file try { System.setProperty("telscale.license.dir", tomcatHomeFile.getPath()); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { classLoader = Class.class.getClassLoader(); } File license = FileUtils.toFile(classLoader.getResource("telestax-license.xml").toURI().toURL()); if (license != null) { FileUtils.copyFileToDirectory(license, tomcatHomeFile, false); } else { try { InputStream is = classLoader.getResourceAsStream("telestax-license.xml"); OutputStream os = new FileOutputStream( tomcatHomeFile + File.separator + "telestax-license.xml"); byte[] buffer = new byte[1024]; int bytesRead; //read from is to buffer while ((bytesRead = is.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } is.close(); //flush OutputStream to write any buffered data to file os.flush(); os.close(); } catch (IOException e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } // creates an http connector, i.e., <connector> element in server.xml Connector connector = embedded.createConnector(InetAddress.getByName(bindAddress), bindPort, false); embedded.setPort(bindPort); embedded.setHostname(bindAddress); embedded.addConnector(connector); embedded.setConnector(connector); // Enable JNDI - it is disabled by default. embedded.enableNaming(); // // starts embedded Mobicents Sip Serlvets embedded.start(); embedded.getService().start(); // creates an sip connector, i.e., <connector> element in server.xml for (SipConnector sipConnector : sipConnectors) { addSipConnector(sipConnector); } wasStarted = true; }
From source file:org.jboss.pressgang.ccms.contentspec.client.ClientTest.java
@Before public void setUp() { File file = FileUtils.toFile(ClassLoader.getSystemResource("")); System.setProperty("user.home", file.getAbsolutePath()); }
From source file:org.jboss.pressgang.ccms.contentspec.client.commands.AssembleCommandTest.java
@Before public void setUp() throws IOException { bindStdOut();//from w w w.j a v a2 s . c o m command = new AssembleCommand(parser, cspConfig, clientConfig); // Only test the assemble command and not the build command content. command.setNoBuild(true); rootTestDirectory = FileUtils.toFile(ClassLoader.getSystemResource("")); when(cspConfig.getRootOutputDirectory()).thenReturn(rootTestDirectory.getAbsolutePath() + File.separator); // Make the book title directory bookDir = new File(rootTestDirectory, BOOK_TITLE); bookDir.mkdir(); // Make a empty file in that directory emptyFile = new File(bookDir, DUMMY_BUILD_FILE_NAME); emptyFile.createNewFile(); // Make the project book directory projectBookDir = new File(rootTestDirectory, BOOK_TITLE + File.separator + "assembly"); projectBookDir.mkdirs(); // Make a empty file in that directory projectEmptyFile = new File(projectBookDir, DUMMY_PROJECT_BUILD_FILE_NAME); projectEmptyFile.createNewFile(); PowerMockito.mockStatic(FileUtilities.class); when(FileUtilities.deleteDirContents(any(File.class))).thenCallRealMethod(); }
From source file:org.jboss.pressgang.ccms.contentspec.client.commands.BuildCommandTest.java
@Before public void setUp() { bindStdOut();/*from w w w . ja v a2 s. c o m*/ when(clientConfig.getDefaults()).thenReturn(defaults); command = spy(new BuildCommand(parser, cspConfig, clientConfig)); // Authentication is tested in the base implementation so assume all users are valid TestUtil.setUpAuthorisedUser(command, userProvider, users, user, username); rootTestDirectory = FileUtils.toFile(ClassLoader.getSystemResource("")); when(cspConfig.getRootOutputDirectory()).thenReturn(rootTestDirectory.getAbsolutePath() + File.separator); when(contentSpecWrapper.getTranslationDetails()).thenReturn(translationDetailWrapper); // Make the book title directory bookDir = new File(rootTestDirectory, BOOK_TITLE); bookDir.mkdir(); }
From source file:org.jboss.pressgang.ccms.contentspec.client.commands.CheckoutCommandTest.java
@Before public void setUp() throws IOException { bindStdOut();/*from w w w.jav a2 s. c o m*/ PowerMockito.mockStatic(RESTProviderFactory.class); when(RESTProviderFactory.create(anyString())).thenReturn(providerFactory); when(providerFactory.getProvider(ContentSpecProvider.class)).thenReturn(contentSpecProvider); command = new CheckoutCommand(parser, cspConfig, clientConfig); // Return the test directory as the root directory rootTestDirectory = FileUtils.toFile(ClassLoader.getSystemResource("")); when(cspConfig.getRootOutputDirectory()).thenReturn(rootTestDirectory.getAbsolutePath() + File.separator); // Make the book title directory bookDir = new File(rootTestDirectory, BOOK_TITLE); bookDir.mkdir(); // Make a empty file in that directory emptyFile = new File(bookDir, EMPTY_FILE_NAME); emptyFile.createNewFile(); }
From source file:org.jboss.pressgang.ccms.contentspec.client.commands.CreateCommandTest.java
@Before public void setUp() { bindStdOut();/*from w w w . j av a2 s . c o m*/ command = new CreateCommand(parser, cspConfig, clientConfig); when(textContentSpecProvider.newTextContentSpec()).thenReturn(textContentSpecWrapper); when(textContentSpecProvider.newTextProcessingOptions()).thenReturn(textCSProcessingOptionsWrapper); when(textContentSpecProvider.createTextContentSpec(any(TextContentSpecWrapper.class), any(TextCSProcessingOptionsWrapper.class), any(LogMessageWrapper.class))) .thenReturn(textContentSpecWrapper); // Authentication is tested in the base implementation so assume all users are valid TestUtil.setUpAuthorisedUser(command, userProvider, users, user, username); // Return the test directory as the root directory rootTestDirectory = FileUtils.toFile(ClassLoader.getSystemResource("")); when(cspConfig.getRootOutputDirectory()).thenReturn(rootTestDirectory.getAbsolutePath() + File.separator); // Make the book title directory bookDir = new File(rootTestDirectory, BOOK_TITLE); bookDir.mkdir(); }
From source file:org.jboss.pressgang.ccms.contentspec.client.commands.PreviewCommandTest.java
@Before public void setUp() throws IOException { bindStdOut();// w ww.j a v a 2 s . c o m command = spy(new PreviewCommand(parser, cspConfig, clientConfig)); // Only test the preview command and not the build or assemble command content. command.setNoAssemble(true); rootTestDirectory = FileUtils.toFile(ClassLoader.getSystemResource("")); when(cspConfig.getRootOutputDirectory()).thenReturn(rootTestDirectory.getAbsolutePath() + File.separator); // Make the book title directory bookDir = new File(rootTestDirectory, BOOK_TITLE); bookDir.mkdir(); // Make the preview dummy file htmlSingleDir = new File(bookDir.getAbsolutePath() + File.separator + "tmp" + File.separator + "en-US" + File.separator + "html-single"); htmlSingleDir.mkdirs(); previewFile = new File(htmlSingleDir, "index.html"); previewFile.createNewFile(); when(contentSpecWrapper.getLocale()).thenReturn(localeWrapper); }
From source file:org.jboss.pressgang.ccms.contentspec.client.commands.PublishCommandTest.java
@Before public void setUp() { bindStdOut();/*from w w w. jav a 2 s . co m*/ command = new PublishCommand(parser, cspConfig, clientConfig); // Only test the publish command and not the build or assemble command content. command.setNoAssemble(true); rootTestDirectory = FileUtils.toFile(ClassLoader.getSystemResource("")); when(cspConfig.getRootOutputDirectory()).thenReturn(rootTestDirectory.getAbsolutePath() + File.separator); }