Example usage for java.net URL getFile

List of usage examples for java.net URL getFile

Introduction

In this page you can find the example usage for java.net URL getFile.

Prototype

public String getFile() 

Source Link

Document

Gets the file name of this URL .

Usage

From source file:info.dolezel.fatrat.plugins.helpers.JarClassLoader.java

public Set<Class> findAnnotatedClassesLocal(String packageName, Class annotation)
        throws IOException, ClassNotFoundException {
    String path = packageName.replace('.', '/');
    Enumeration<URL> resources = this.getResources(path);
    Set<File> dirs = new HashSet<File>();
    while (resources.hasMoreElements()) {
        URL resource = resources.nextElement();
        dirs.add(new File(resource.getFile()));
    }//ww w  . ja v a2 s.co  m
    for (URL url : this.getURLs()) {
        dirs.add(new File(url.getFile()));
    }

    HashSet<Class> classes = new HashSet<Class>();
    for (File directory : dirs) {
        classes.addAll(findClasses(directory, packageName, annotation));
    }

    return classes;
}

From source file:net.grinder.util.AbstractGrinderClassPathProcessor.java

/**
 * Construct the foremost classPath from current classLoader.
 *
 * @param logger logger/*from www  . j  a  v a  2  s . c  om*/
 * @return classpath optimized for grinder.
 */
public String buildForemostClasspathBasedOnCurrentClassLoader(Logger logger) {
    URL[] urLs = ((URLClassLoader) AbstractGrinderClassPathProcessor.class.getClassLoader()).getURLs();
    StringBuilder builder = new StringBuilder();
    for (URL each : urLs) {
        builder.append(each.getFile()).append(File.pathSeparator);
    }
    return filterForeMostClassPath(builder.toString(), logger);
}

From source file:net.grinder.util.AbstractGrinderClassPathProcessor.java

/**
 * Construct the patch classPath from current classLoader.
 *
 * @param logger logger/*from ww w.  j  a  v a2 s . c  o  m*/
 * @return classpath optimized for grinder.
 */
public String buildPatchClasspathBasedOnCurrentClassLoader(Logger logger) {
    URL[] urLs = ((URLClassLoader) AbstractGrinderClassPathProcessor.class.getClassLoader()).getURLs();
    StringBuilder builder = new StringBuilder();
    for (URL each : urLs) {
        builder.append(each.getFile()).append(File.pathSeparator);
    }
    return filterPatchClassPath(builder.toString(), logger);
}

From source file:com.brokenmodel.swats.RouterServlet.java

private void handleRequest(HttpServletRequest request, HttpServletResponse response,
        ControllerRequest.Type type) {
    try {/*from  ww w  .j a  v a  2s  .c om*/
        URL rootURL = new URL(request.getScheme(), request.getServerName(), request.getServerPort(),
                request.getContextPath() + request.getServletPath());

        String appRoot = rootURL.getFile();
        String htmlRoot = request.getContextPath();

        MatchedRoute matchedRoute = controllers.matchRoute(request.getPathInfo());
        ControllerRequest controllerRequest = new ControllerRequest(request, response, appRoot, htmlRoot,
                matchedRoute.getUrlParams(), type, handleMultipart(request), getDataSource());
        AbstractController controller = matchedRoute.getController();
        controller.doRequest(controllerRequest);
    } catch (Throwable t) {
        log(t);
        try {
            // only will work if output stream has not been opened
            PrintWriter pw = new PrintWriter(response.getWriter());
            pw.append("<pre>");
            pw.append("We're sorry - an error has occurred:\n\n");
            t.printStackTrace(pw);
            pw.append("</pre>");
        } catch (Throwable t2) {
        }
    }
}

From source file:com.santhoshknn.sudoku.GridExtractor.java

/**
 * <p>//from www. ja  va 2 s  .c  o m
 * Parses the supplied file to extract a 9x9 grid of integers substituting
 * the supplied x with a 0
 * </p>
 * <b>Note:</b>Used internally for testing with various data. REST API uses
 * the operation above
 *
 * @param input
 * @return extracted grid if valid input, null otherwise
 */
public GridResponse parseFromFile(final String fileName) {
    int[][] grid = new int[9][9]; // default 0 vals
    GridResponse response = new GridResponse();
    Scanner scanner = null;
    String error = null;
    try {
        URL url = getClass().getResource(fileName);
        log.info("Reading input file [{}]", url.getFile());
        scanner = new Scanner(new File(url.getPath()));
        int row = 0;
        while (scanner.hasNext()) {
            int col = 0;
            String line = scanner.nextLine();
            // delete whitespaces added for cosmetic purpose
            line = StringUtils.deleteWhitespace(line);
            if (line.isEmpty())
                continue; // Sanitize input. Remove line added for
                          // readability
                          // fail if line's length!=9
            if (line.length() != 9) {
                error = INVALID_CHARS_IN_FILE + ":" + (row + 1);
                break;
            }

            for (int i = 0; i < line.length(); i++) {
                //log.info("Row [{}] Char is [{}]",row,line.charAt(i));
                if (Character.isDigit(line.charAt(i))) {
                    int number = Character.getNumericValue(line.charAt(i));
                    grid[row][col] = number;
                } else {
                    grid[row][col] = 0;
                }
                col++;
            }
            if (row == 9)
                break;
            row++;
        }
    } catch (FileNotFoundException e) {
        log.error("Error reading file [{}]", fileName, e);
    } finally {
        if (scanner != null)
            scanner.close();
    }
    if (null == error) {
        response.setGrid(grid);
    } else {
        response.setError(error);
        log.error(error);
    }
    return response;
}

From source file:org.jboss.pnc.buildagent.server.BootstrapUndertowBuildAgentHandlers.java

private String getManifestInformation() {
    String result = "";
    try {/*from  ww  w .j  a  v a  2s  .  co m*/
        final Enumeration<URL> resources = Welcome.class.getClassLoader().getResources("META-INF/MANIFEST.MF");

        while (resources.hasMoreElements()) {
            final URL jarUrl = resources.nextElement();

            log.trace("Processing jar resource " + jarUrl);
            if (jarUrl.getFile().contains("build-agent")) {
                final Manifest manifest = new Manifest(jarUrl.openStream());
                result = manifest.getMainAttributes().getValue("Implementation-Version");
                result += " ( SHA: " + manifest.getMainAttributes().getValue("Scm-Revision") + " ) ";
                break;
            }
        }
    } catch (final IOException e) {
        log.trace("Error retrieving information from manifest", e);
    }

    return result;
}

From source file:io.neba.core.util.ReverseFileByLineReaderTest.java

private File getFile(String resourcePath) {
    URL resource = getClass().getClassLoader().getResource("io/neba/core/logviewer/" + resourcePath);
    if (resource != null) {
        return new File(resource.getFile());
    }//w  w  w  .j a va2  s .c om
    return null;
}

From source file:com.enonic.cms.core.plugin.container.OsgiContainer.java

private void copyFrameworkJar(final File targetFile) throws Exception {
    URL location = FrameworkProperties.class.getProtectionDomain().getCodeSource().getLocation();

    final String locationFile = location.getFile();

    LOG.info("Location of framework.jar : " + locationFile);

    if (locationFile.endsWith(".jar!/")) // for IBM Websphere 8.5 Liberty Profile
    {/* w  w w .  j  a  va2  s .c  om*/
        String absolutePath = locationFile.substring(0, locationFile.length() - 2);

        location = new URL(absolutePath);
    }

    else if (ResourceUtils.URL_PROTOCOL_VFS.equals(location.getProtocol())) // JBOSS 7.1.1 VFS
    {
        final URI uri = ResourceUtils.toURI(location);
        final UrlResource urlResource = new UrlResource(uri);
        final File file = urlResource.getFile();

        String absolutePath = file.getAbsolutePath();

        if (!absolutePath.endsWith(urlResource.getFilename())) {
            // removing /contents folder from path and adding unpacked jar to path.
            absolutePath = absolutePath.substring(0, absolutePath.length() - VFS_CONTENTS_FOLDER.length())
                    + urlResource.getFilename();
        }

        final StringBuilder stringBuilder = new StringBuilder("file:/");
        if (!SystemUtils.IS_OS_WINDOWS) // windows already has one slash in path like /c:/Program Files/....
        {
            stringBuilder.append('/');
        }
        stringBuilder.append(absolutePath);

        location = new URL(stringBuilder.toString());
    }

    LOG.info("Copying " + location.toString() + " to " + targetFile.toString());
    Files.copy(Resources.newInputStreamSupplier(location), targetFile);
}

From source file:nc.noumea.mairie.distiller.DistillerTest.java

@Test
public void cUploadImage() {
    try {/*  w ww. j  a v  a  2 s  .  c  om*/
        logger.info("testFileName : " + testFileName);
        logger.info("destinationPath : " + destinationPath);
        logger.info("as400User : " + as400User);

        URL url = getClass().getResource("/" + testFileName);
        File testFile = new File(url.getFile());
        Assert.assertTrue(testFile.exists());

        FileInputStream stream = new FileInputStream(testFile);

        SmbFile dFile = new SmbFile(destinationPath + "/" + testFileName, auth);
        SmbFileOutputStream outStream = new SmbFileOutputStream(dFile);
        outStream.write(IOUtils.toByteArray(stream));
        outStream.flush();
        outStream.close();
        stream.close();
        Assert.assertTrue(dFile.exists());
        Assert.assertTrue(dFile.canRead());
        Assert.assertNotNull(dFile.getUncPath());
    } catch (Exception ex) {
        ex.printStackTrace();
        Assert.assertNull(ex);
    }

    // image should have been uploaded

}

From source file:org.motechproject.server.osgi.RuleBundleLoader.java

@SuppressWarnings("unchecked")
@Override/*from  www. j  a va  2s.  c  o  m*/
public void loadBundle(Bundle bundle) throws Exception {
    Enumeration<URL> e = bundle.findEntries(ruleFolder, "*", false);
    String symbolicName = bundle.getSymbolicName();
    if (e != null) {
        while (e.hasMoreElements()) {
            URL url = e.nextElement();
            URLConnection conn = url.openConnection();
            InputStream inputStream = conn.getInputStream();
            knowledgeBaseManager.addOrUpdateRule(FilenameUtils.getName(url.getFile()), symbolicName,
                    inputStream);
            inputStream.close();
        }
    }
}