Show
Ignore:
Timestamp:
09/01/10 10:24:14 (21 months ago)
Author:
seelmann
Message:

Add support for live modules when using file URLs

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/release-0.15.x/pustefix-live/src/main/java/org/pustefixframework/live/LiveJarInfo.java

    r5023 r5330  
    421421    /** 
    422422     * Gets the live module root. 
    423      * @param jarUrl 
    424      *            the URL to the module JAR with the path to the resource appended 
     423     * @param url 
     424     *            the URL to the module JAR or file target URL 
     425     * @param path 
     426     *            the resource path, relative to the URL 
    425427     * @return the live location for the module resource, or null if no live location is available 
    426428     */ 
    427     public File getLiveModuleRoot(URL jarUrl) { 
     429    public File getLiveModuleRoot(URL url, String path) { 
    428430        checkFileModified(); 
    429431 
    430432        // TODO: excludes and includes, depending on path, per directory 
    431433 
    432         File location = rootToLocation.get(jarUrl.toString()); 
    433         if (location != null || rootsWithNoLocation.contains(jarUrl.toString())) { 
     434        File location = rootToLocation.get(url.toString()); 
     435        if (location != null || rootsWithNoLocation.contains(url.toString())) { 
    434436            return location; 
    435437        } 
    436438 
    437         String path = jarUrl.getPath(); 
    438         int ind = path.indexOf('!'); 
    439         path = path.substring(0, ind); 
    440         ind = path.lastIndexOf('/'); 
    441         path = path.substring(ind + 1); 
    442         ind = path.lastIndexOf('.'); 
    443         path = path.substring(0, ind); 
    444         // look for entry by jar file name 
    445         Entry entry = jarEntries.get(path); 
    446         if (entry != null) { 
    447             for (File dir : entry.directories) { 
    448                 if (dir.getName().equals("resources")) { 
    449                     if (LOG.isDebugEnabled()) { 
    450                         LOG.debug("Found live location by jar file name: " + path); 
    451                     } 
    452                     rootToLocation.put(jarUrl.toString(), dir); 
    453                     return dir; 
    454                 } 
    455             } 
    456         } 
    457         // look for entry by artifact name and MANIFEST attributes 
    458         try { 
    459             URL manifestUrl = new URL(jarUrl, "/META-INF/MANIFEST.MF"); 
    460             URLConnection con = manifestUrl.openConnection(); 
    461             if (con != null) { 
    462                 InputStream in = con.getInputStream(); 
    463                 if (in != null) { 
    464                     Manifest manifest = new Manifest(in); 
    465                     Attributes attrs = manifest.getMainAttributes(); 
    466                     String groupId = attrs.getValue("Implementation-Vendor-Id"); 
    467                     String version = attrs.getValue("Implementation-Version"); 
    468                     if (groupId != null && version != null) { 
    469                         int endInd = path.indexOf(version); 
    470                         if (endInd > 2) { 
    471                             String artifactId = path.substring(0, endInd - 1); 
    472                             String entryKey = groupId + "+" + artifactId + "+" + version; 
    473                             entry = jarEntries.get(entryKey); 
    474                             if (entry != null) { 
    475                                 for (File dir : entry.directories) { 
    476                                     // if (dir.getName().equals("resources")) { 
    477                                     if (LOG.isDebugEnabled()) { 
    478                                         LOG.debug("Found live location by artifact name and MANIFEST attributes: " 
    479                                                 + entryKey); 
     439        if (url.getProtocol().equals("jar")) { 
     440            String jarFileName = url.getPath(); 
     441            int ind = jarFileName.indexOf('!'); 
     442            jarFileName = jarFileName.substring(0, ind); 
     443            ind = jarFileName.lastIndexOf('/'); 
     444            jarFileName = jarFileName.substring(ind + 1); 
     445            ind = jarFileName.lastIndexOf('.'); 
     446            jarFileName = jarFileName.substring(0, ind); 
     447            // look for entry by jar file name 
     448            Entry entry = jarEntries.get(jarFileName); 
     449            if (entry != null) { 
     450                for (File dir : entry.directories) { 
     451                    if (dir.getName().equals("resources")) { 
     452                        if (LOG.isDebugEnabled()) { 
     453                            LOG.debug("Found live module location by jar file name: " + jarFileName); 
     454                        } 
     455                        rootToLocation.put(url.toString(), dir); 
     456                        return dir; 
     457                    } 
     458                } 
     459            } 
     460            // look for entry by artifact name and MANIFEST attributes 
     461            try { 
     462                URL manifestUrl = new URL(url, "/META-INF/MANIFEST.MF"); 
     463                URLConnection con = manifestUrl.openConnection(); 
     464                if (con != null) { 
     465                    InputStream in = con.getInputStream(); 
     466                    if (in != null) { 
     467                        Manifest manifest = new Manifest(in); 
     468                        Attributes attrs = manifest.getMainAttributes(); 
     469                        String groupId = attrs.getValue("Implementation-Vendor-Id"); 
     470                        String version = attrs.getValue("Implementation-Version"); 
     471                        if (groupId != null && version != null) { 
     472                            int endInd = jarFileName.indexOf(version); 
     473                            if (endInd > 2) { 
     474                                String artifactId = jarFileName.substring(0, endInd - 1); 
     475                                String entryKey = groupId + "+" + artifactId + "+" + version; 
     476                                entry = jarEntries.get(entryKey); 
     477                                if (entry != null) { 
     478                                    for (File dir : entry.directories) { 
     479                                        if (LOG.isDebugEnabled()) { 
     480                                            LOG.debug("Found live module location by artifact name and MANIFEST attributes: " 
     481                                                    + entryKey); 
     482                                        } 
     483                                        rootToLocation.put(url.toString(), dir); 
     484                                        return dir; 
    480485                                    } 
    481                                     rootToLocation.put(jarUrl.toString(), dir); 
    482                                     return dir; 
    483                                     // } 
    484486                                } 
    485487                            } 
     
    487489                    } 
    488490                } 
    489             } 
    490         } catch (FileNotFoundException x) { 
    491             LOG.warn("Module contains no MANIFEST.MF: " + jarUrl.toString()); 
    492         } catch (MalformedURLException x) { 
    493             LOG.warn("Illegal module URL: " + jarUrl.toString(), x); 
    494         } catch (IOException x) { 
    495             LOG.warn("IO error reading module data: " + jarUrl.toString(), x); 
    496         } 
     491            } catch (FileNotFoundException x) { 
     492                LOG.warn("Module contains no MANIFEST.MF: " + url.toString()); 
     493            } catch (MalformedURLException x) { 
     494                LOG.warn("Illegal module URL: " + url.toString(), x); 
     495            } catch (IOException x) { 
     496                LOG.warn("IO error reading module data: " + url.toString(), x); 
     497            } 
     498        } else if (url.getProtocol().equals("file")) { 
     499            // find pom.xml, retrieve groupId, artifactId, version from pom.xml 
     500            try { 
     501                File pomFile = guessPom(url.getFile()); 
     502                if (pomFile != null) { 
     503                    if (LOG.isDebugEnabled()) { 
     504                        LOG.debug("Found pom.xml: " + pomFile); 
     505                    } 
     506                    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     507                    factory.setNamespaceAware(true); 
     508                    DocumentBuilder builder = factory.newDocumentBuilder(); 
     509                    Document document = builder.parse(pomFile); 
     510                    Element root = document.getDocumentElement(); 
     511                    Element groupElem = getSingleChildElement(root, "groupId", true); 
     512                    String groupId = groupElem.getTextContent().trim(); 
     513                    Element artifactElem = getSingleChildElement(root, "artifactId", true); 
     514                    String artifactId = artifactElem.getTextContent().trim(); 
     515                    Element versionElem = getSingleChildElement(root, "version", true); 
     516                    String version = versionElem.getTextContent().trim(); 
     517                    String entryKey = groupId + "+" + artifactId + "+" + version; 
     518 
     519                    Entry jarEntry = jarEntries.get(entryKey); 
     520                    if (jarEntry != null) { 
     521                        for (File dir : jarEntry.directories) { 
     522                            if (LOG.isDebugEnabled()) { 
     523                                LOG.debug("Found live module location by pom.xml: " + entryKey); 
     524                            } 
     525                            rootToLocation.put(url.toString(), dir); 
     526                            return dir; 
     527                        } 
     528                    } 
     529                } 
     530            } catch (Exception e) { 
     531                LOG.warn("Exceptions reading module live POM: " + url.toString(), e); 
     532            } 
     533        } 
     534 
    497535        if (LOG.isDebugEnabled()) { 
    498             LOG.debug("Found no live location: " + jarUrl.toString()); 
    499         } 
    500         rootsWithNoLocation.add(jarUrl.toString()); 
     536            LOG.debug("Found no live location: " + url.toString()); 
     537        } 
     538        rootsWithNoLocation.add(url.toString()); 
    501539        return null; 
    502540    }