001: /*
002: * <copyright>
003: *
004: * Copyright 2002-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.util;
028:
029: import java.io.BufferedReader;
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.io.InputStreamReader;
033: import java.io.PrintWriter;
034: import java.net.URL;
035: import java.net.URLClassLoader;
036: import java.util.ArrayList;
037: import java.util.HashMap;
038: import java.util.List;
039: import java.util.Map;
040: import java.util.regex.Matcher;
041: import java.util.regex.Pattern;
042:
043: import javax.servlet.http.HttpServlet;
044: import javax.servlet.http.HttpServletRequest;
045: import javax.servlet.http.HttpServletResponse;
046:
047: import org.cougaar.bootstrap.XURLClassLoader;
048: import org.cougaar.core.servlet.ComponentServlet;
049:
050: /**
051: * This component loads the "/showJars" servlet, which displays
052: * the jars loaded by the agent's ClassLoader.
053: * <p>
054: * Load with:<pre>
055: * <component class="org.cougaar.core.util.CheckJarsServlet">
056: * <argument>/showJars</argument>
057: * </component>
058: * </pre>
059: */
060: public class CheckJarsServlet extends ComponentServlet {
061:
062: public void doGet(HttpServletRequest request,
063: HttpServletResponse response) throws IOException {
064: response.setContentType("text/html");
065: PrintWriter out = response.getWriter();
066: out.println("<html>");
067: out.println("<head><title>");
068: out.println("Jar version information from agent "
069: + getEncodedAgentName());
070: out.println("</title></head>");
071: out.println("<body>");
072: ClassLoader cl = this .getClass().getClassLoader();
073: while (cl != null) {
074: out.println("<h2>");
075: out.println("Classloader <em>" + cl + "</em>");
076: out.println("</h2>");
077: List info = scanClassLoader(cl);
078: report(out, info);
079: cl = cl.getParent();
080: }
081: out.println("</body>");
082: out.println("</html>");
083: out.close();
084: }
085:
086: private static void report(PrintWriter out, List info) {
087: if (info.size() == 0)
088: return;
089: out.println("<table border=1>");
090: out
091: .println("<tr><th>NAME</th><th>URL</th><th>COMMENT</th><th>TAG</th><th>TIME</th><th>ATIME</th></tr>");
092: for (int i = 0, l = info.size(); i < l; i++) {
093: Map m = (Map) info.get(i);
094: out.println("<tr>");
095: out.println("<td>" + empty(m.get("NAME")) + "</td>");
096: out.println("<td>" + empty(m.get("URL")) + "</td>");
097: out.println("<td>" + empty(m.get("COMMENT")) + "</td>");
098: out.println("<td>" + empty(m.get("REPOSITORY_TAG"))
099: + "</td>");
100: out.println("<td>" + empty(m.get("REPOSITORY_TIME"))
101: + "</td>");
102: out
103: .println("<td>" + empty(m.get("ARCHIVE_TIME"))
104: + "</td>");
105: out.println("</tr>");
106: }
107: out.println("</table>");
108: }
109:
110: private static String empty(Object s) {
111: return (s == null) ? "" : (s.toString());
112: }
113:
114: private static List scanClassLoader(ClassLoader cl) {
115: ArrayList list = new ArrayList(11);
116:
117: URL[] urls = null;
118: if (cl instanceof URLClassLoader) {
119: urls = ((URLClassLoader) cl).getURLs();
120: } else if (cl instanceof XURLClassLoader) {
121: urls = ((XURLClassLoader) cl).getURLs();
122: }
123:
124: if (urls != null) {
125: for (int i = 0; i < urls.length; i++) {
126: Map m = checkURL(cl, urls[i]);
127: if (m != null) {
128: list.add(m);
129: }
130: }
131: }
132:
133: return list;
134: }
135:
136: private static Pattern jarPattern = Pattern.compile(
137: ".*[:/]([^:/]+)\\.(?:jar|zip|plugin)",
138: Pattern.CASE_INSENSITIVE);
139: private static Pattern varPattern = Pattern.compile("(\\w+)=(.*)");
140:
141: private static Map checkURL(ClassLoader cl, URL url) {
142: String us = url.toString();
143:
144: HashMap map = new HashMap(7);
145: // add the url info
146: map.put("URL", us);
147:
148: Matcher m = jarPattern.matcher(us);
149: if (m.lookingAt()) {
150: String arg = m.group(1);
151: map.put("NAME", arg);
152: try {
153: InputStream is = cl.getResourceAsStream("Manifest/"
154: + arg + ".version");
155: if (is != null) {
156: BufferedReader r = new BufferedReader(
157: new InputStreamReader(is));
158: String line;
159: while ((line = r.readLine()) != null) {
160: Matcher lm = varPattern.matcher(line);
161: if (lm.lookingAt()) {
162: String var = lm.group(1);
163: String val = lm.group(2);
164: if ("NAME".equals(var)) {
165: if (!(arg.equals(val))) {
166: map.put(var, val + "!");
167: } // else no-op
168: } else {
169: map.put(var, val);
170: }
171: }
172: }
173: r.close();
174: }
175: } catch (Exception e) {
176: map.put("NAME", arg + "?");
177: }
178: } else {
179: map.put("NAME", "?");
180: }
181: return map;
182: }
183: }
|