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

Deterministic guess of fallback docroot, refactored some code to LiveUtils?

Files:
1 modified

Legend:

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

    r5330 r5334  
    4848import org.w3c.dom.Element; 
    4949import org.w3c.dom.Node; 
    50 import org.w3c.dom.NodeList; 
    5150 
    5251/** 
     
    6160    /** The live.xml file */ 
    6261    private File file; 
    63      
     62 
    6463    private long lastReadTimestamp; 
    6564 
     
    103102                oldFile = new File(homeDir + "/.m2/life.xml"); // support old misspelled name 
    104103            } 
    105             if(oldFile.exists()) { 
     104            if (oldFile.exists()) { 
    106105                file = oldFile; 
    107106                LOG.warn("Using live.xml from old location: " + file); 
    108107            } 
    109108        } 
    110         if(file == null) { 
     109        if (file == null) { 
    111110            LOG.warn("No live.xml detected, default settings for live resources may be used!"); 
    112111        } 
     
    126125        rootsWithNoLocation = new HashSet<String>(); 
    127126 
    128         if(file != null) { 
     127        if (file != null) { 
    129128            try { 
    130129                read(); 
     
    146145            if (root.getLocalName().equals("live") || root.getLocalName().equals("life")) { // support old misspelled 
    147146                // name 
    148                 for (Element jarElem : getChildElements(root, "jar")) { 
     147                for (Element jarElem : LiveUtils.getChildElements(root, "jar")) { 
    149148                    Entry entry = readEntry(jarElem); 
    150149                    jarEntries.put(entry.getId(), entry); 
    151150                } 
    152                 for (Element warElem : getChildElements(root, "war")) { 
     151                for (Element warElem : LiveUtils.getChildElements(root, "war")) { 
    153152                    Entry entry = readEntry(warElem); 
    154153                    warEntries.put(entry.getId(), entry); 
     
    162161    private Entry readEntry(Element jarElem) throws Exception { 
    163162        Entry entry = new Entry(); 
    164         Element idElem = getSingleChildElement(jarElem, "id", true); 
    165         Element groupElem = getSingleChildElement(idElem, "group", true); 
     163        Element idElem = LiveUtils.getSingleChildElement(jarElem, "id", true); 
     164        Element groupElem = LiveUtils.getSingleChildElement(idElem, "group", true); 
    166165        entry.groupId = groupElem.getTextContent().trim(); 
    167         Element artifactElem = getSingleChildElement(idElem, "artifact", true); 
     166        Element artifactElem = LiveUtils.getSingleChildElement(idElem, "artifact", true); 
    168167        entry.artifactId = artifactElem.getTextContent().trim(); 
    169         Element versionElem = getSingleChildElement(idElem, "version", true); 
     168        Element versionElem = LiveUtils.getSingleChildElement(idElem, "version", true); 
    170169        entry.version = versionElem.getTextContent().trim(); 
    171         List<Element> dirElems = getChildElements(jarElem, "directory"); 
     170        List<Element> dirElems = LiveUtils.getChildElements(jarElem, "directory"); 
    172171        if (dirElems.size() == 0) 
    173             dirElems = getChildElements(jarElem, "directorie"); // support old misspelled name 
     172            dirElems = LiveUtils.getChildElements(jarElem, "directorie"); // support old misspelled name 
    174173        for (Element dirElem : dirElems) { 
    175174            File dir = new File(dirElem.getTextContent().trim()); 
     
    189188    /** 
    190189     * Writes the live information to the live.xml file. 
    191      *  
    192      * @throws Exception the exception 
     190     * @throws Exception 
     191     *             the exception 
    193192     */ 
    194193    public void write() throws Exception { 
     
    257256    } 
    258257 
    259     private static Element getSingleChildElement(Element parent, String localName, boolean mandatory) throws Exception { 
    260         Element elem = null; 
    261         NodeList nodes = parent.getChildNodes(); 
    262         for (int i = 0; i < nodes.getLength(); i++) { 
    263             Node node = nodes.item(i); 
    264             if (node.getNodeType() == Node.ELEMENT_NODE && node.getLocalName().equals(localName)) { 
    265                 if (elem != null) 
    266                     throw new Exception("Multiple '" + localName + "' child elements aren't allowed."); 
    267                 elem = (Element) node; 
    268             } 
    269         } 
    270         if (mandatory && elem == null) 
    271             throw new Exception("Missing '" + localName + "' child element."); 
    272         return elem; 
    273     } 
    274  
    275     private static List<Element> getChildElements(Element parent, String localName) { 
    276         List<Element> elems = new ArrayList<Element>(); 
    277         NodeList nodes = parent.getChildNodes(); 
    278         for (int i = 0; i < nodes.getLength(); i++) { 
    279             Node node = nodes.item(i); 
    280             if (node.getNodeType() == Node.ELEMENT_NODE && node.getLocalName().equals(localName)) { 
    281                 elems.add((Element) node); 
    282             } 
    283         } 
    284         return elems; 
    285     } 
    286  
    287258    /** 
    288259     * Gets the live docroot. 
     
    313284 
    314285        // find pom.xml, retrieve groupId, artifactId, version from pom.xml 
    315         File pomFile = guessPom(docroot); 
     286        File pomFile = LiveUtils.guessPom(docroot); 
    316287        if (pomFile != null) { 
    317288            if (LOG.isDebugEnabled()) { 
     
    323294            Document document = builder.parse(pomFile); 
    324295            Element root = document.getDocumentElement(); 
    325             Element groupElem = getSingleChildElement(root, "groupId", true); 
     296            Element groupElem = LiveUtils.getSingleChildElement(root, "groupId", true); 
    326297            String groupId = groupElem.getTextContent().trim(); 
    327             Element artifactElem = getSingleChildElement(root, "artifactId", true); 
     298            Element artifactElem = LiveUtils.getSingleChildElement(root, "artifactId", true); 
    328299            String artifactId = artifactElem.getTextContent().trim(); 
    329             Element versionElem = getSingleChildElement(root, "version", true); 
     300            Element versionElem = LiveUtils.getSingleChildElement(root, "version", true); 
    330301            String version = versionElem.getTextContent().trim(); 
    331302            String entryKey = groupId + "+" + artifactId + "+" + version; 
     
    348319 
    349320        rootsWithNoLocation.add(docroot); 
    350         return null; 
    351     } 
    352  
    353     private static File guessPom(String docroot) throws Exception { 
    354         if (LOG.isDebugEnabled()) { 
    355             LOG.debug("Guessing pom.xml for " + docroot); 
    356         } 
    357          
    358         /* 
    359          * Here is a project layout including a pustefix editor. The editor has its 
    360          * own pom.xml blow META-INF. Otherwise we walk up the parent path till we  
    361          * find the project pom.xml. 
    362          *  
    363          * <pre> 
    364          * pfixui 
    365          * |-- pom.xml                                        <-- pom of application 
    366          * |-- target 
    367          *     |-- pfixui-0.2.29-SNAPSHOT                     <-- docroot of application 
    368          *         |-- WEB-INF 
    369          *     |-- editor                                     <-- docroot of editor 
    370          *         |-- META-INF 
    371          *             |-- maven 
    372          *                 |-- org.pustefixframework.editor 
    373          *                     |-- pustefix-editor-webui 
    374          *                         |-- pom.xml                <-- pom of editor 
    375          *      
    376          * </pre> 
    377          */ 
    378          
    379         // search pom.xml below META-INF 
    380         File docrootDir = new File(docroot); 
    381         if (docrootDir.exists() && docrootDir.isDirectory()) { 
    382             File metaInfDir = new File(docrootDir, "META-INF"); 
    383             if(LOG.isTraceEnabled()) LOG.trace(metaInfDir); 
    384             if (metaInfDir.exists() && metaInfDir.isDirectory()) { 
    385                 File mavenDir = new File(metaInfDir, "maven"); 
    386                 if(LOG.isTraceEnabled()) LOG.trace(mavenDir); 
    387                 if (mavenDir.exists() && mavenDir.isDirectory()) { 
    388                     File[] groupIdDirs = mavenDir.listFiles(); 
    389                     for (File groupIdDir : groupIdDirs) { 
    390                         if(LOG.isTraceEnabled()) LOG.trace(groupIdDir); 
    391                         if (groupIdDir.exists() && groupIdDir.isDirectory()) { 
    392                             File[] artifactIdDirs = groupIdDir.listFiles(); 
    393                             for (File artifiactIdDir : artifactIdDirs) { 
    394                                 if(LOG.isTraceEnabled()) LOG.trace(artifiactIdDir); 
    395                                 if (artifiactIdDir.exists() && artifiactIdDir.isDirectory()) { 
    396                                     File pomFile = new File(artifiactIdDir, "pom.xml"); 
    397                                     if(LOG.isTraceEnabled()) LOG.trace(pomFile); 
    398                                     if (pomFile.exists() && pomFile.isFile()) { 
    399                                         return pomFile; 
    400                                     } 
    401                                 } 
    402                             } 
    403                         } 
    404                     } 
    405                 } 
    406             } 
    407         } 
    408  
    409         // search project pom.xml 
    410         File pomDir = new File(docroot); 
    411         while (pomDir != null && pomDir.exists() && pomDir.isDirectory()) { 
    412             File pomFile = new File(pomDir, "pom.xml"); 
    413             if (pomFile != null && pomFile.exists() && pomFile.isFile()) { 
    414                 return pomFile; 
    415             } 
    416             pomDir = pomDir.getParentFile(); 
    417         } 
    418321        return null; 
    419322    } 
     
    499402            // find pom.xml, retrieve groupId, artifactId, version from pom.xml 
    500403            try { 
    501                 File pomFile = guessPom(url.getFile()); 
     404                File pomFile = LiveUtils.guessPom(url.getFile()); 
    502405                if (pomFile != null) { 
    503406                    if (LOG.isDebugEnabled()) { 
    504407                        LOG.debug("Found pom.xml: " + pomFile); 
    505408                    } 
    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; 
     409                    String entryKey = LiveUtils.getKeyFromPom(pomFile); 
    518410 
    519411                    Entry jarEntry = jarEntries.get(entryKey); 
     
    593485            this.directories = directories; 
    594486        } 
    595          
     487 
    596488    } 
    597489