Changeset 5334 for branches/release-0.15.x/pustefix-live/src/main/java/org/pustefixframework/live/LiveJarInfo.java
- Timestamp:
- 09/01/10 16:16:44 (21 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
branches/release-0.15.x/pustefix-live/src/main/java/org/pustefixframework/live/LiveJarInfo.java
r5330 r5334 48 48 import org.w3c.dom.Element; 49 49 import org.w3c.dom.Node; 50 import org.w3c.dom.NodeList;51 50 52 51 /** … … 61 60 /** The live.xml file */ 62 61 private File file; 63 62 64 63 private long lastReadTimestamp; 65 64 … … 103 102 oldFile = new File(homeDir + "/.m2/life.xml"); // support old misspelled name 104 103 } 105 if (oldFile.exists()) {104 if (oldFile.exists()) { 106 105 file = oldFile; 107 106 LOG.warn("Using live.xml from old location: " + file); 108 107 } 109 108 } 110 if (file == null) {109 if (file == null) { 111 110 LOG.warn("No live.xml detected, default settings for live resources may be used!"); 112 111 } … … 126 125 rootsWithNoLocation = new HashSet<String>(); 127 126 128 if (file != null) {127 if (file != null) { 129 128 try { 130 129 read(); … … 146 145 if (root.getLocalName().equals("live") || root.getLocalName().equals("life")) { // support old misspelled 147 146 // name 148 for (Element jarElem : getChildElements(root, "jar")) {147 for (Element jarElem : LiveUtils.getChildElements(root, "jar")) { 149 148 Entry entry = readEntry(jarElem); 150 149 jarEntries.put(entry.getId(), entry); 151 150 } 152 for (Element warElem : getChildElements(root, "war")) {151 for (Element warElem : LiveUtils.getChildElements(root, "war")) { 153 152 Entry entry = readEntry(warElem); 154 153 warEntries.put(entry.getId(), entry); … … 162 161 private Entry readEntry(Element jarElem) throws Exception { 163 162 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); 166 165 entry.groupId = groupElem.getTextContent().trim(); 167 Element artifactElem = getSingleChildElement(idElem, "artifact", true);166 Element artifactElem = LiveUtils.getSingleChildElement(idElem, "artifact", true); 168 167 entry.artifactId = artifactElem.getTextContent().trim(); 169 Element versionElem = getSingleChildElement(idElem, "version", true);168 Element versionElem = LiveUtils.getSingleChildElement(idElem, "version", true); 170 169 entry.version = versionElem.getTextContent().trim(); 171 List<Element> dirElems = getChildElements(jarElem, "directory");170 List<Element> dirElems = LiveUtils.getChildElements(jarElem, "directory"); 172 171 if (dirElems.size() == 0) 173 dirElems = getChildElements(jarElem, "directorie"); // support old misspelled name172 dirElems = LiveUtils.getChildElements(jarElem, "directorie"); // support old misspelled name 174 173 for (Element dirElem : dirElems) { 175 174 File dir = new File(dirElem.getTextContent().trim()); … … 189 188 /** 190 189 * Writes the live information to the live.xml file. 191 * 192 * @throws Exceptionthe exception190 * @throws Exception 191 * the exception 193 192 */ 194 193 public void write() throws Exception { … … 257 256 } 258 257 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 287 258 /** 288 259 * Gets the live docroot. … … 313 284 314 285 // find pom.xml, retrieve groupId, artifactId, version from pom.xml 315 File pomFile = guessPom(docroot);286 File pomFile = LiveUtils.guessPom(docroot); 316 287 if (pomFile != null) { 317 288 if (LOG.isDebugEnabled()) { … … 323 294 Document document = builder.parse(pomFile); 324 295 Element root = document.getDocumentElement(); 325 Element groupElem = getSingleChildElement(root, "groupId", true);296 Element groupElem = LiveUtils.getSingleChildElement(root, "groupId", true); 326 297 String groupId = groupElem.getTextContent().trim(); 327 Element artifactElem = getSingleChildElement(root, "artifactId", true);298 Element artifactElem = LiveUtils.getSingleChildElement(root, "artifactId", true); 328 299 String artifactId = artifactElem.getTextContent().trim(); 329 Element versionElem = getSingleChildElement(root, "version", true);300 Element versionElem = LiveUtils.getSingleChildElement(root, "version", true); 330 301 String version = versionElem.getTextContent().trim(); 331 302 String entryKey = groupId + "+" + artifactId + "+" + version; … … 348 319 349 320 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 its360 * own pom.xml blow META-INF. Otherwise we walk up the parent path till we361 * find the project pom.xml.362 *363 * <pre>364 * pfixui365 * |-- pom.xml <-- pom of application366 * |-- target367 * |-- pfixui-0.2.29-SNAPSHOT <-- docroot of application368 * |-- WEB-INF369 * |-- editor <-- docroot of editor370 * |-- META-INF371 * |-- maven372 * |-- org.pustefixframework.editor373 * |-- pustefix-editor-webui374 * |-- pom.xml <-- pom of editor375 *376 * </pre>377 */378 379 // search pom.xml below META-INF380 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.xml410 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 }418 321 return null; 419 322 } … … 499 402 // find pom.xml, retrieve groupId, artifactId, version from pom.xml 500 403 try { 501 File pomFile = guessPom(url.getFile());404 File pomFile = LiveUtils.guessPom(url.getFile()); 502 405 if (pomFile != null) { 503 406 if (LOG.isDebugEnabled()) { 504 407 LOG.debug("Found pom.xml: " + pomFile); 505 408 } 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); 518 410 519 411 Entry jarEntry = jarEntries.get(entryKey); … … 593 485 this.directories = directories; 594 486 } 595 487 596 488 } 597 489
