/**
  * Extracts the entry name for the grammar from a given JAR or ZIP URL. The name is either given
  * in the JAR entry part of the URL, or it is taken to be the name of the archive file.
  *
  * @param url the URL to be parsed; guaranteed to be a JAR or ZIP
  * @return the name; non-null
  * @throws IllegalArgumentException if no name can be found according to the above rules
  * @throws IOException if the URL cannot be opened
  */
 private String extractEntryName(URL url) throws IOException {
   String result;
   JarURLConnection connection = (JarURLConnection) url.openConnection();
   result = connection.getEntryName();
   if (result == null) {
     result = FileType.getPureName(new File(connection.getJarFileURL().getPath()));
   }
   return result;
 }
Esempio n. 2
0
  /*
   * Scans the given JarURLConnection for TLD files located in META-INF
   * (or a subdirectory of it), adding an implicit map entry to the taglib
   * map for any TLD that has a <uri> element.
   *
   * @param jarConn The JarURLConnection to the JAR file to scan
   *
   * Keep in sync with o.a.c.startup.TldConfig
   */
  private void tldScanJar(JarURLConnection jarConn) throws IOException {

    Jar jar = null;
    InputStream is;
    boolean foundTld = false;

    URL resourceURL = jarConn.getJarFileURL();
    String resourcePath = resourceURL.toString();

    try {
      jar = JarFactory.newInstance(jarConn.getURL());

      jar.nextEntry();
      String entryName = jar.getEntryName();
      while (entryName != null) {
        if (entryName.startsWith("META-INF/") && entryName.endsWith(".tld")) {
          is = null;
          try {
            is = jar.getEntryInputStream();
            foundTld = true;
            tldScanStream(resourcePath, entryName, is);
          } finally {
            if (is != null) {
              try {
                is.close();
              } catch (IOException ioe) {
                // Ignore
              }
            }
          }
        }
        jar.nextEntry();
        entryName = jar.getEntryName();
      }
    } finally {
      if (jar != null) {
        jar.close();
      }
    }

    if (!foundTld) {
      if (log.isDebugEnabled()) {
        log.debug(Localizer.getMessage("jsp.tldCache.noTldInJar", resourcePath));
      } else if (showTldScanWarning) {
        // Not entirely thread-safe but a few duplicate log messages are
        // not a huge issue
        showTldScanWarning = false;
        log.info(Localizer.getMessage("jsp.tldCache.noTldSummary"));
      }
    }
  }
 /**
  * Determine the URL path to the persistence unit
  *
  * @param pxmlURL - Encoded URL containing the pu
  * @return
  * @throws IOException
  */
 public static URL computePURootURL(URL pxmlURL, String descriptorLocation)
     throws IOException, URISyntaxException {
   StringTokenizer tokenizer = new StringTokenizer(descriptorLocation, "/\\");
   int descriptorDepth = tokenizer.countTokens() - 1;
   URL result;
   String protocol = pxmlURL.getProtocol();
   if ("file".equals(protocol)) { // NOI18N
     StringBuffer path = new StringBuffer();
     boolean firstElement = true;
     for (int i = 0; i < descriptorDepth; i++) {
       if (!firstElement) {
         path.append("/"); // 315097 URL use standard separators
       }
       path.append("..");
       firstElement = false;
     }
     // e.g. file:/tmp/META-INF/persistence.xml
     // 210280: any file url will be assumed to always reference a file (not a directory)
     result = new URL(pxmlURL, path.toString()); // NOI18N
   } else if ("jar".equals(protocol)) { // NOI18N
     // e.g. jar:file:/tmp/a_ear/b.jar!/META-INF/persistence.xml
     JarURLConnection conn = JarURLConnection.class.cast(pxmlURL.openConnection());
     result = conn.getJarFileURL();
   } else if ("zip".equals(protocol)) { // NOI18N
     // e.g. zip:/tmp/a_ear/b.jar!/META-INF/persistence.xml
     // stolen from java.net.JarURLConnection.parseSpecs method
     String spec = pxmlURL.getFile();
     int separator = spec.lastIndexOf("!/");
     if (separator == -1) {
       separator = spec.length() - 1;
     }
     result = new File(spec.substring(0, separator++)).toURL();
   } else if ("wsjar".equals(protocol)) { // NOI18N
     // e.g. wsjar:file:/tmp/a_ear/b.jar!/META-INF/persistence.xml
     // but WS gives use jar:file:..., so we need to match it.
     String spec = pxmlURL.getFile();
     int separator = spec.lastIndexOf("!/");
     if (separator == -1) {
       separator = spec.length();
     } else {
       // If this doesn't reference a war file with a properly located persistence.xml,
       // then chop off everything after the "!/" marker and assume it is a normal jar.
       // Else, if the wsjar URL references a file with a ".war" extension, and its entry
       // starts with WEB-INF/classes/, then the calculated persistence unit root should
       // be wsjar:path/to/a.war!/WEB-INF/classes/ as per JPA 2.1 Spec section 8.2 "Persistence
       // Unit Packaging".
       separator += 2;
       // Filter out invalid scenarios such as
       // wsjar:file:/a/path/to/my.war!/foo/WEB-INF/classes/META-INF/persistence.xml
       if (spec.regionMatches(true, separator - 6, ".war", 0, 4)
           && spec.regionMatches(true, separator, WEBINF_CLASSES_STR, 0, WEBINF_CLASSES_LEN)) {
         separator += WEBINF_CLASSES_LEN;
       }
     }
     result = new URL("jar", "", spec.substring(0, separator));
   } else if ("bundleentry".equals(protocol)) {
     // mkeith - add bundle protocol cases
     result = new URL("bundleentry://" + pxmlURL.getAuthority());
   } else if ("bundleresource".equals(protocol)) {
     result = new URL("bundleresource://" + pxmlURL.getAuthority());
   } else {
     StringBuffer path = new StringBuffer();
     for (int i = 0; i < descriptorDepth; i++) {
       path.append("../"); // 315097 URL use standard separators
     }
     // some other protocol
     result = new URL(pxmlURL, path.toString()); // NOI18N
   }
   result = fixUNC(result);
   return result;
 }
  private Manifest _calculateManifest(URL url, Manifest manifest) {
    Analyzer analyzer = new Analyzer();

    Jar jar = null;

    try {
      URLConnection urlConnection = url.openConnection();

      String fileName = url.getFile();

      if (urlConnection instanceof JarURLConnection) {
        JarURLConnection jarURLConnection = (JarURLConnection) urlConnection;

        URL jarFileURL = jarURLConnection.getJarFileURL();

        fileName = jarFileURL.getFile();
      }

      File file = new File(fileName);

      if (!file.exists() || !file.canRead()) {
        return manifest;
      }

      fileName = file.getName();

      analyzer.setJar(new Jar(fileName, file));

      jar = analyzer.getJar();

      String bundleSymbolicName = fileName;

      Matcher matcher = _bundleSymbolicNamePattern.matcher(bundleSymbolicName);

      if (matcher.matches()) {
        bundleSymbolicName = matcher.group(1);
      }

      analyzer.setProperty(Analyzer.BUNDLE_SYMBOLICNAME, bundleSymbolicName);

      String exportPackage = _calculateExportPackage(jar);

      analyzer.setProperty(Analyzer.EXPORT_PACKAGE, exportPackage);

      analyzer.mergeManifest(manifest);

      String bundleVersion = analyzer.getProperty(Analyzer.BUNDLE_VERSION);

      if (bundleVersion != null) {
        bundleVersion = Builder.cleanupVersion(bundleVersion);

        analyzer.setProperty(Analyzer.BUNDLE_VERSION, bundleVersion);
      }

      return analyzer.calcManifest();
    } catch (Exception e) {
      _log.error(e, e);

      return manifest;
    } finally {
      if (jar != null) {
        jar.close();
      }

      analyzer.close();
    }
  }