Java Code Examples for java.net.JarURLConnection#setUseCaches()

The following examples show how to use java.net.JarURLConnection#setUseCaches() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JarURLConnectionUseCaches.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main( String[] args ) throws IOException {
    JarOutputStream out = new JarOutputStream(
            new FileOutputStream("usecache.jar"));
    out.putNextEntry(new JarEntry("test.txt"));
    out.write("Test txt file".getBytes());
    out.closeEntry();
    out.close();

    URL url = new URL("jar:"
        + new File(".").toURI().toString()
        + "/usecache.jar!/test.txt");

    JarURLConnection c1 = (JarURLConnection)url.openConnection();
    c1.setDefaultUseCaches( false );
    c1.setUseCaches( true );
    c1.connect();

    JarURLConnection c2 = (JarURLConnection)url.openConnection();
    c2.setDefaultUseCaches( false );
    c2.setUseCaches( true );
    c2.connect();

    c1.getInputStream().close();
    c2.getInputStream().read();
    c2.getInputStream().close();
}
 
Example 2
Source File: JarURLConnectionUseCaches.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main( String[] args ) throws IOException {
    JarOutputStream out = new JarOutputStream(
            new FileOutputStream("usecache.jar"));
    out.putNextEntry(new JarEntry("test.txt"));
    out.write("Test txt file".getBytes());
    out.closeEntry();
    out.close();

    URL url = new URL("jar:"
        + new File(".").toURI().toString()
        + "/usecache.jar!/test.txt");

    JarURLConnection c1 = (JarURLConnection)url.openConnection();
    c1.setDefaultUseCaches( false );
    c1.setUseCaches( true );
    c1.connect();

    JarURLConnection c2 = (JarURLConnection)url.openConnection();
    c2.setDefaultUseCaches( false );
    c2.setUseCaches( true );
    c2.connect();

    c1.getInputStream().close();
    c2.getInputStream().read();
    c2.getInputStream().close();
}
 
Example 3
Source File: JarURLConnectionUseCaches.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main( String[] args ) throws IOException {
    JarOutputStream out = new JarOutputStream(
            new FileOutputStream("usecache.jar"));
    out.putNextEntry(new JarEntry("test.txt"));
    out.write("Test txt file".getBytes());
    out.closeEntry();
    out.close();

    URL url = new URL("jar:"
        + new File(".").toURI().toString()
        + "/usecache.jar!/test.txt");

    JarURLConnection c1 = (JarURLConnection)url.openConnection();
    c1.setDefaultUseCaches( false );
    c1.setUseCaches( true );
    c1.connect();

    JarURLConnection c2 = (JarURLConnection)url.openConnection();
    c2.setDefaultUseCaches( false );
    c2.setUseCaches( true );
    c2.connect();

    c1.getInputStream().close();
    c2.getInputStream().read();
    c2.getInputStream().close();
}
 
Example 4
Source File: JarURLConnectionUseCaches.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main( String[] args ) throws IOException {
    JarOutputStream out = new JarOutputStream(
            new FileOutputStream("usecache.jar"));
    out.putNextEntry(new JarEntry("test.txt"));
    out.write("Test txt file".getBytes());
    out.closeEntry();
    out.close();

    URL url = new URL("jar:"
        + new File(".").toURI().toString()
        + "/usecache.jar!/test.txt");

    JarURLConnection c1 = (JarURLConnection)url.openConnection();
    c1.setDefaultUseCaches( false );
    c1.setUseCaches( true );
    c1.connect();

    JarURLConnection c2 = (JarURLConnection)url.openConnection();
    c2.setDefaultUseCaches( false );
    c2.setUseCaches( true );
    c2.connect();

    c1.getInputStream().close();
    c2.getInputStream().read();
    c2.getInputStream().close();
}
 
Example 5
Source File: JarURLConnectionUseCaches.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main( String[] args ) throws IOException {
    JarOutputStream out = new JarOutputStream(
            new FileOutputStream("usecache.jar"));
    out.putNextEntry(new JarEntry("test.txt"));
    out.write("Test txt file".getBytes());
    out.closeEntry();
    out.close();

    URL url = new URL("jar:"
        + new File(".").toURI().toString()
        + "/usecache.jar!/test.txt");

    JarURLConnection c1 = (JarURLConnection)url.openConnection();
    c1.setDefaultUseCaches( false );
    c1.setUseCaches( true );
    c1.connect();

    JarURLConnection c2 = (JarURLConnection)url.openConnection();
    c2.setDefaultUseCaches( false );
    c2.setUseCaches( true );
    c2.connect();

    c1.getInputStream().close();
    c2.getInputStream().read();
    c2.getInputStream().close();
}
 
Example 6
Source File: JarURLConnectionUseCaches.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main( String[] args ) throws IOException {
    JarOutputStream out = new JarOutputStream(
            new FileOutputStream("usecache.jar"));
    out.putNextEntry(new JarEntry("test.txt"));
    out.write("Test txt file".getBytes());
    out.closeEntry();
    out.close();

    URL url = new URL("jar:"
        + new File(".").toURI().toString()
        + "/usecache.jar!/test.txt");

    JarURLConnection c1 = (JarURLConnection)url.openConnection();
    c1.setDefaultUseCaches( false );
    c1.setUseCaches( true );
    c1.connect();

    JarURLConnection c2 = (JarURLConnection)url.openConnection();
    c2.setDefaultUseCaches( false );
    c2.setUseCaches( true );
    c2.connect();

    c1.getInputStream().close();
    c2.getInputStream().read();
    c2.getInputStream().close();
}
 
Example 7
Source File: JarURLConnectionUseCaches.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void main( String[] args ) throws IOException {
    JarOutputStream out = new JarOutputStream(
            new FileOutputStream("usecache.jar"));
    out.putNextEntry(new JarEntry("test.txt"));
    out.write("Test txt file".getBytes());
    out.closeEntry();
    out.close();

    URL url = new URL("jar:"
        + new File(".").toURI().toString()
        + "/usecache.jar!/test.txt");

    JarURLConnection c1 = (JarURLConnection)url.openConnection();
    c1.setDefaultUseCaches( false );
    c1.setUseCaches( true );
    c1.connect();

    JarURLConnection c2 = (JarURLConnection)url.openConnection();
    c2.setDefaultUseCaches( false );
    c2.setUseCaches( true );
    c2.connect();

    c1.getInputStream().close();
    c2.getInputStream().read();
    c2.getInputStream().close();
}
 
Example 8
Source File: JarFileUrlNestedJar.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
public JarFileUrlNestedJar(URL url) throws IOException {
    super(url);
    JarURLConnection jarConn = (JarURLConnection) url.openConnection();
    jarConn.setUseCaches(false);
    warFile = jarConn.getJarFile();

    String urlAsString = url.toString();
    int pathStart = urlAsString.indexOf("!/") + 2;
    String jarPath = urlAsString.substring(pathStart);
    jarEntry = warFile.getJarEntry(jarPath);
}
 
Example 9
Source File: ParserController.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
private JarFile getJarFile(URL jarFileUrl) throws IOException {
JarFile jarFile = null;

if (jarFileUrl != null) {
    JarURLConnection conn = (JarURLConnection) jarFileUrl.openConnection();
    conn.setUseCaches(false);
    conn.connect();
    jarFile = conn.getJarFile();
}

return jarFile;
   }
 
Example 10
Source File: JarURLResource.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public JarFile getJarFile() throws IOException {
    URL jarFileUrl = new URL("jar:" + jarUrl + "!/");
    JarURLConnection conn = (JarURLConnection) jarFileUrl.openConnection();
    conn.setUseCaches(false);
    conn.connect();
    return conn.getJarFile();
}
 
Example 11
Source File: JarURLResource.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public JarFile getJarFile() throws IOException {
    URL jarFileUrl = new URL("jar:" + jarUrl + "!/");
    JarURLConnection conn = (JarURLConnection) jarFileUrl.openConnection();
    conn.setUseCaches(false);
    conn.connect();
    return conn.getJarFile();
}
 
Example 12
Source File: ExpandWar.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Validate the WAR file found at the specified URL.
 *
 * @param host Host war is being installed for
 * @param war URL of the web application archive to be validated
 *  (must start with "jar:")
 * @param pathname Context path name for web application
 *
 * @exception IllegalArgumentException if this is not a "jar:" URL or if the
 *            WAR file is invalid
 * @exception IOException if an input/output error was encountered
 *            during validation
 */
public static void validate(Host host, URL war, String pathname) throws IOException {

    File docBase = new File(host.getAppBaseFile(), pathname);

    // Calculate the document base directory
    String canonicalDocBasePrefix = docBase.getCanonicalPath();
    if (!canonicalDocBasePrefix.endsWith(File.separator)) {
        canonicalDocBasePrefix += File.separator;
    }
    JarURLConnection juc = (JarURLConnection) war.openConnection();
    juc.setUseCaches(false);
    try (JarFile jarFile = juc.getJarFile()) {
        Enumeration<JarEntry> jarEntries = jarFile.entries();
        while (jarEntries.hasMoreElements()) {
            JarEntry jarEntry = jarEntries.nextElement();
            String name = jarEntry.getName();
            File expandedFile = new File(docBase, name);
            if (!expandedFile.getCanonicalPath().startsWith(
                    canonicalDocBasePrefix)) {
                // Entry located outside the docBase
                // Throw an exception to stop the deployment
                throw new IllegalArgumentException(
                        sm.getString("expandWar.illegalPath",war, name,
                                expandedFile.getCanonicalPath(),
                                canonicalDocBasePrefix));
            }
        }
    } catch (IOException e) {
        throw e;
    }
}
 
Example 13
Source File: FileUrlJar.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
public FileUrlJar(URL url) throws IOException {
    JarURLConnection jarConn = (JarURLConnection) url.openConnection();
    jarConn.setUseCaches(false);
    jarFile = jarConn.getJarFile();
}
 
Example 14
Source File: ClassScanner.java    From startup-os with Apache License 2.0 4 votes vote down vote up
private JarFile getJarFile(URL url) throws IOException {
  JarURLConnection urlConnection = (JarURLConnection) url.openConnection();
  urlConnection.setUseCaches(false);
  return urlConnection.getJarFile();
}
 
Example 15
Source File: SpecialAgentUtil.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
/**
   * Fills the specified {@code fileToPluginManifest} map with JAR files having
   * a prefix path that match {@code path}, and the associated
   * {@link PluginManifest}.
   * <p>
   * This method will add a shutdown hook to delete any temporary directory and
   * file resources it created.
   *
   * @param path The prefix path to match when finding resources.
   * @param destDir Callback that supplies the destDir.
   * @param callback Callback function to process resource files.
   * @throws IllegalStateException If an illegal state occurs due to an
   *           {@link IOException}.
   */
  static void findJarResources(final String path, final Supplier<File> destDir, Predicate<File> callback) {
    try {
      final Enumeration<URL> resources = ClassLoader.getSystemClassLoader().getResources(path);
      if (!resources.hasMoreElements())
        return;

      final Set<URL> visitedResources = new HashSet<>();
      File outDir = null;
      do {
        final URL resource = resources.nextElement();
        if (visitedResources.contains(resource))
          continue;

        visitedResources.add(resource);
        final URLConnection connection = resource.openConnection();
        // Only consider resources that are inside JARs
        if (!(connection instanceof JarURLConnection))
          continue;

//        if (logger.isLoggable(Level.FINEST))
//          logger.finest("SpecialAgent Rule Path: " + resource);

        if (outDir == null)
          outDir = destDir.get();

        if (outDir == null) {
//          logger.severe("Unable to continue with null output directory");
          return;
        }

        final JarURLConnection jarURLConnection = (JarURLConnection)connection;
        jarURLConnection.setUseCaches(false);
        final JarFile jarFile = jarURLConnection.getJarFile();
        final Enumeration<JarEntry> jarEntries = jarFile.entries();
        while (jarEntries.hasMoreElements()) {
          final String jarEntry = jarEntries.nextElement().getName();
          if (jarEntry.length() <= path.length() || !jarEntry.startsWith(path))
            continue;

          final int slash = jarEntry.lastIndexOf('/');
          final String jarFileName = jarEntry.substring(slash + 1);

          // First, extract the JAR into a temp dir
          final File subDir = new File(outDir, jarEntry.substring(0, slash));
          subDir.mkdirs();
          final File file = new File(subDir, jarFileName);
          if (!file.isDirectory() && !file.getName().endsWith(".jar"))
            continue;

          final URL jarUrl = new URL(resource, jarEntry.substring(path.length()));
          Files.copy(jarUrl.openStream(), file.toPath(), StandardCopyOption.REPLACE_EXISTING);

          if (!callback.test(file))
            file.delete();
        }
      }
      while (resources.hasMoreElements());

      if (outDir != null)
        deleteOnShutdown(outDir);
    }
    catch (final IOException e) {
      throw new IllegalStateException(e);
    }
  }
 
Example 16
Source File: FileUrlJar.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
public FileUrlJar(URL url) throws IOException {
    JarURLConnection jarConn = (JarURLConnection) url.openConnection();
    jarConn.setUseCaches(false);
    jarFile = jarConn.getJarFile();
}
 
Example 17
Source File: ResourceTool.java    From sds with Apache License 2.0 4 votes vote down vote up
public static List<URL> getJarUrl(URL rootUrl, String extensionName) throws IOException {
    List<URL> result = ListUtils.newArrayList();

    if (rootUrl == null || !"jar".equals(rootUrl.getProtocol())) {
        return result;
    }

    if (StringUtils.isNotBlank(extensionName)) {
        extensionName = "." + extensionName;
    }

    if (extensionName == null) {
        extensionName = "";
    }

    URLConnection con = rootUrl.openConnection();

    JarURLConnection jarCon = (JarURLConnection) con;
    jarCon.setUseCaches(false);
    JarFile jarFile = jarCon.getJarFile();
    JarEntry jarEntry = jarCon.getJarEntry();
    String rootEntryPath = (jarEntry != null ? jarEntry.getName() : "");

    if (!"".equals(rootEntryPath) && !rootEntryPath.endsWith("/")) {
        rootEntryPath = rootEntryPath + "/";
    }

    for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) {
        JarEntry entry = entries.nextElement();
        String entryPath = entry.getName();

        if (entryPath.startsWith(rootEntryPath)) {
        	
            String relativePath = entryPath.substring(rootEntryPath.length());
            if (relativePath.endsWith(extensionName)) {
                result.add(createRelative(rootUrl, relativePath));

            }
        }
    }

    return result;

}