Changeset 5330

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

Add support for live modules when using file URLs

Location:
branches/release-0.15.x
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • branches/release-0.15.x/pustefix-core/src/main/java/de/schlund/pfixxml/resources/ModuleResourceProvider.java

    r5014 r5330  
    6060        ModuleDescriptor desc = ModuleInfo.getInstance().getModuleDescriptor(module); 
    6161        if (desc != null) { 
    62             URL url = getJarURL(desc.getURL()); 
     62            URL url = desc.getURL().getProtocol().equals("jar") ? getJarURL(desc.getURL()) : getFileUrl(desc.getURL()); 
    6363            // Ensure module resources are read from classpath in production environment 
    6464            boolean checkLive = !BuildTimeProperties.getProperties().getProperty("mode").equals("prod"); 
     
    8989    } 
    9090     
     91    private static URL getFileUrl(URL url) { 
     92        if (!url.getProtocol().equals("file")) 
     93            throw new PustefixRuntimeException("Invalid protocol: " + url); 
     94        String urlStr = url.toString(); 
     95        int ind = urlStr.indexOf("META-INF"); 
     96        if (ind > -1) { 
     97            urlStr = urlStr.substring(0, ind); 
     98        } else 
     99            throw new PustefixRuntimeException("Unexpected module descriptor URL: " + url); 
     100        try { 
     101            return new URL(urlStr); 
     102        } catch (MalformedURLException x) { 
     103            throw new PustefixRuntimeException("Invalid module URL: " + urlStr); 
     104        } 
     105    } 
     106 
    91107    private static URL getJarURL(URL url) { 
    92108        if(!url.getProtocol().equals("jar")) throw new PustefixRuntimeException("Invalid protocol: "+url); 
  • 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    } 
  • branches/release-0.15.x/pustefix-live/src/main/java/org/pustefixframework/live/LiveResolver.java

    r5014 r5330  
    8282    /** 
    8383     * Resolves the module base. 
    84      * @param jarUrl 
    85      *            the original module jar URL 
     84     * @param url 
     85     *            the original module jar URL or file target URL 
    8686     * @param path 
    87      *            the resource path, relative to jarUrl 
     87     *            the resource path, relative to the URL 
    8888     * @return the resolved live module root, or null if no live module root was found 
    8989     * @throws Exception 
    9090     */ 
    91     public URL resolveLiveModuleRoot(URL jarUrl, String path) throws Exception { 
     91    public URL resolveLiveModuleRoot(URL url, String path) throws Exception { 
    9292        if (LOG.isDebugEnabled()) { 
    93             LOG.debug("Resolving live module root from live.xml for " + jarUrl + ":" + path); 
     93            LOG.debug("Resolving live module root from live.xml for " + url + ":" + path); 
    9494        } 
    9595         
     
    9797            path = path.substring(1); 
    9898        } 
    99         File moduleRoot = getLiveJarInfo().getLiveModuleRoot(new URL(jarUrl + path)); 
     99        File moduleRoot = getLiveJarInfo().getLiveModuleRoot(url, path); 
    100100        if (moduleRoot != null) { 
    101101            if (LOG.isDebugEnabled()) {