001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.web.monitor.data;
043:
044: import org.w3c.dom.*;
045: import org.xml.sax.InputSource;
046: import org.netbeans.modules.schema2beans.*;
047: import java.beans.*;
048: import java.util.*;
049: import java.io.*;
050:
051: public class MonitorData extends BaseBean implements DataRecord {
052:
053: private final static boolean debug = false;
054:
055: static Vector comparators = new Vector();
056:
057: static public final String CLIENTDATA = "ClientData"; // NOI18N
058: static public final String SESSIONDATA = "SessionData"; // NOI18N
059: static public final String COOKIESDATA = "CookiesData"; // NOI18N
060: static public final String REQUESTDATA = "RequestData"; // NOI18N
061: static public final String SERVLETDATA = "ServletData"; // NOI18N
062: static public final String CONTEXTDATA = "ContextData"; // NOI18N
063: static public final String ENGINEDATA = "EngineData"; // NOI18N
064: static public final String MONITORDATA = "MonitorData"; // NOI18N
065: static public final String DISPATCHES = "Dispatches"; // NOI18N
066:
067: public MonitorData() {
068: this (null, Common.USE_DEFAULT_VALUES);
069: }
070:
071: public MonitorData(Node doc, int options) {
072: this (Common.NO_DEFAULT_VALUES);
073: if (doc == null) {
074: doc = GraphManager.createRootElementNode(MONITORDATA);
075: if (doc == null)
076: throw new RuntimeException(
077: "failed to create a new DOM root!"); // NOI18N
078: }
079: Node n = GraphManager.getElementNode(MONITORDATA, doc);
080: if (n == null)
081: throw new RuntimeException(
082: "doc root not found in the DOM graph!"); // NOI18N
083:
084: this .graphManager.setXmlDocument(doc);
085:
086: // Entry point of the createBeans() recursive calls
087: this .createBean(n, this .graphManager());
088: this .initialize(options);
089: }
090:
091: public MonitorData(int options) {
092: super (MonitorData.comparators,
093: new org.netbeans.modules.schema2beans.Version(1, 0, 6));
094: // The graph manager is allocated in the bean root
095: this .graphManager = new GraphManager(this );
096:
097: this .createRoot(MONITORDATA, MONITORDATA, Common.TYPE_1
098: | Common.TYPE_BEAN, MonitorData.class);
099:
100: // Properties (see root bean comments for the bean graph)
101: this .createProperty("ClientData",
102: CLIENTDATA, // NOI18N
103: Common.TYPE_1 | Common.TYPE_BEAN | Common.TYPE_KEY,
104: ClientData.class);
105: this .createAttribute(CLIENTDATA, "protocol", "Protocol", // NOI18N
106: AttrProp.CDATA | AttrProp.REQUIRED, null, null);
107: this .createAttribute(CLIENTDATA, "remoteAddress",
108: "RemoteAddress", // NOI18N
109: AttrProp.CDATA | AttrProp.REQUIRED, null, null);
110: this .createAttribute(CLIENTDATA, "software", "Software", // NOI18N
111: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
112: this .createAttribute(CLIENTDATA, "locale", "Locale", // NOI18N
113: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
114: this .createAttribute(CLIENTDATA, "formatsAccepted",
115: "FormatsAccepted", // NOI18N
116: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
117: this .createAttribute(CLIENTDATA, "encodingsAccepted",
118: "EncodingsAccepted", // NOI18N
119: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
120: this .createAttribute(CLIENTDATA, "charsetsAccepted",
121: "CharsetsAccepted", // NOI18N
122: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
123: this .createProperty("SessionData",
124: SESSIONDATA, // NOI18N
125: Common.TYPE_0_1 | Common.TYPE_BEAN | Common.TYPE_KEY,
126: SessionData.class);
127: this .createAttribute(SESSIONDATA, "before", "Before", // NOI18N
128: AttrProp.ENUM | AttrProp.REQUIRED, new String[] {
129: "false", // NOI18N
130: "true" // NOI18N
131: }, "false"); // NOI18N
132: this .createAttribute(SESSIONDATA, "after", "After", // NOI18N
133: AttrProp.ENUM | AttrProp.REQUIRED, new String[] {
134: "false", // NOI18N
135: "true" // NOI18N
136: }, "false"); // NOI18N
137: this .createAttribute(SESSIONDATA, "id", "Id", // NOI18N
138: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
139: this .createAttribute(SESSIONDATA, "created", "Created", // NOI18N
140: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
141: this .createProperty("CookiesData",
142: COOKIESDATA, // NOI18N
143: Common.TYPE_0_1 | Common.TYPE_BEAN | Common.TYPE_KEY,
144: CookiesData.class);
145:
146: // PENDING - I think I need to move this to the request data
147: // class to make it an independent class that can be handled
148: // on its own (if I want to pass less data about).
149: this .createProperty("RequestData",
150: REQUESTDATA, // NOI18N
151: Common.TYPE_1 | Common.TYPE_BEAN | Common.TYPE_KEY,
152: RequestData.class);
153: this .createAttribute(REQUESTDATA, "uri", "Uri", // NOI18N
154: AttrProp.CDATA | AttrProp.REQUIRED, null, null);
155: this .createAttribute(REQUESTDATA, "method", "Method", // NOI18N
156: AttrProp.CDATA | AttrProp.REQUIRED, null, null);
157: this .createAttribute(REQUESTDATA, "urlencoded", "Urlencoded", // NOI18N
158: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
159: this .createAttribute(REQUESTDATA, "queryString", "QueryString", // NOI18N
160: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
161: this .createAttribute(REQUESTDATA, "replace", "Replace", // NOI18N
162: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
163: this .createAttribute(REQUESTDATA, "protocol", "Protocol", // NOI18N
164: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
165: this .createAttribute(REQUESTDATA, "ipaddress", "Ipaddress", // NOI18N
166: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
167: this .createAttribute(REQUESTDATA, "scheme", "Scheme", // NOI18N
168: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
169: this .createAttribute(REQUESTDATA, "status", "Status", // NOI18N
170: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
171:
172: this .createProperty("ServletData",
173: SERVLETDATA, // NOI18N
174: Common.TYPE_0_1 | Common.TYPE_BEAN | Common.TYPE_KEY,
175: ServletData.class);
176: this .createAttribute(SERVLETDATA, "name", "Name", // NOI18N
177: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
178: this .createAttribute(SERVLETDATA, "className", "ClassName", // NOI18N
179: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
180: this .createAttribute(SERVLETDATA, "packageName", "PackageName", // NOI18N
181: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
182: this .createAttribute(SERVLETDATA, "servletInfo", "ServletInfo", // NOI18N
183: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
184: this .createAttribute(SERVLETDATA, "relPath", "RelPath", // NOI18N
185: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
186: this .createAttribute(SERVLETDATA, "transPath", "TransPath", // NOI18N
187: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
188: this .createAttribute(SERVLETDATA, "contextName", "ContextName", // NOI18N
189: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
190: this .createAttribute(SERVLETDATA, "absPath", "AbsPath", // NOI18N
191: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
192: this .createAttribute(SERVLETDATA, "jre", "Jre", // NOI18N
193: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
194: this .createAttribute(SERVLETDATA, "platform", "Platform", // NOI18N
195: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
196: this .createAttribute(SERVLETDATA, "serverPort", "ServerPort", // NOI18N
197: AttrProp.NMTOKEN | AttrProp.IMPLIED, null, null);
198: this .createAttribute(SERVLETDATA, "serverName", "ServerName", // NOI18N
199: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
200:
201: this .createAttribute(SERVLETDATA, "collected", "Collected", // NOI18N
202: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
203:
204: this .createProperty("ContextData",
205: CONTEXTDATA, // NOI18N
206: Common.TYPE_0_1 | Common.TYPE_BEAN | Common.TYPE_KEY,
207: ContextData.class);
208: this .createAttribute(CONTEXTDATA, "contextName", "ContextName", // NOI18N
209: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
210: this .createAttribute(CONTEXTDATA, "absPath", "AbsPath", // NOI18N
211: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
212:
213: this .createProperty("EngineData",
214: ENGINEDATA, // NOI18N
215: Common.TYPE_0_1 | Common.TYPE_BEAN | Common.TYPE_KEY,
216: EngineData.class);
217:
218: this .createAttribute(ENGINEDATA, "jre", "Jre", // NOI18N
219: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
220: this .createAttribute(ENGINEDATA, "platform", "Platform", // NOI18N
221: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
222: this .createAttribute(ENGINEDATA, "serverPort", "ServerPort", // NOI18N
223: AttrProp.NMTOKEN | AttrProp.IMPLIED, null, null);
224: this .createAttribute(ENGINEDATA, "serverName", "ServerName", // NOI18N
225: AttrProp.CDATA | AttrProp.IMPLIED, null, null);
226:
227: this .createAttribute("resource", "Resource", // NOI18N
228: AttrProp.CDATA | AttrProp.REQUIRED, null, null);
229: this .createAttribute("timestamp", "Timestamp", // NOI18N
230: AttrProp.CDATA | AttrProp.REQUIRED, null, null);
231: this .createAttribute("id", "Id", // NOI18N
232: AttrProp.CDATA | AttrProp.REQUIRED, null, null);
233: this .createAttribute("method", "Metod", // NOI18N
234: AttrProp.CDATA | AttrProp.REQUIRED, null, null);
235:
236: this .createProperty("Dispatches",
237: DISPATCHES, // NOI18N
238: Common.TYPE_0_1 | Common.TYPE_BEAN | Common.TYPE_KEY,
239: Dispatches.class);
240:
241: this .initialize(options);
242: }
243:
244: // Setting the default values of the properties
245: void initialize(int options) {
246: }
247:
248: // This attribute is mandatory
249: public void setClientData(ClientData value) {
250: this .setValue(CLIENTDATA, value);
251: }
252:
253: //
254: public ClientData getClientData() {
255: return (ClientData) this .getValue(CLIENTDATA);
256: }
257:
258: // This attribute is optional
259: public void setSessionData(SessionData value) {
260: this .setValue(SESSIONDATA, value);
261: }
262:
263: //
264: public SessionData getSessionData() {
265: return (SessionData) this .getValue(SESSIONDATA);
266: }
267:
268: // This attribute is optional
269: public void setCookiesData(CookiesData value) {
270: this .setValue(COOKIESDATA, value);
271: }
272:
273: //
274: public CookiesData getCookiesData() {
275: return (CookiesData) this .getValue(COOKIESDATA);
276: }
277:
278: // This attribute is optional
279: public void setDispatches(Dispatches value) {
280: this .setValue(DISPATCHES, value);
281: }
282:
283: //
284: public Dispatches getDispatches() {
285: if (debug)
286: System.out.println("Running getDispatches"); //NOI18N
287: return (Dispatches) this .getValue(DISPATCHES);
288: }
289:
290: // This attribute is mandatory
291: public void setRequestData(RequestData value) {
292: this .setValue(REQUESTDATA, value);
293: }
294:
295: //
296: public RequestData getRequestData() {
297: return (RequestData) this .getValue(REQUESTDATA);
298: }
299:
300: // This attribute is optional
301: public void setServletData(ServletData value) {
302: this .setValue(SERVLETDATA, value);
303: }
304:
305: //
306: public ServletData getServletData() {
307: return (ServletData) this .getValue(SERVLETDATA);
308: }
309:
310: // This attribute is optional
311: public void setEngineData(EngineData value) {
312: this .setValue(ENGINEDATA, value);
313: }
314:
315: //
316: public EngineData getEngineData() {
317: return (EngineData) this .getValue(ENGINEDATA);
318: }
319:
320: // This attribute is optional
321: public void setContextData(ContextData value) {
322: this .setValue(CONTEXTDATA, value);
323: }
324:
325: //
326: public ContextData getContextData() {
327: return (ContextData) this .getValue(CONTEXTDATA);
328: }
329:
330: // This method verifies that the mandatory properties are set
331: public boolean verify() {
332: return true;
333: }
334:
335: public String getServerAndPort() {
336: String server = null;
337: String port = null;
338:
339: try {
340: server = getEngineData().getAttributeValue("serverName"); // NOI18N
341: port = getEngineData().getAttributeValue("serverPort"); // NOI18N
342: return server.concat(":").concat(port); //NOI18N
343: } catch (NullPointerException npe) {
344: }
345:
346: // Backwards compatibility
347: server = getServletData().getAttributeValue("serverName"); // NOI18N
348: port = getServletData().getAttributeValue("serverPort"); // NOI18N
349: return server.concat(":").concat(port); //NOI18N
350: }
351:
352: public String getServerName() {
353:
354: try {
355: return getEngineData().getAttributeValue("serverName"); // NOI18N
356: } catch (NullPointerException npe) {
357: }
358:
359: // Backwards compatibility
360: return getServletData().getAttributeValue("serverName"); // NOI18N
361: }
362:
363: public int getServerPort() {
364:
365: String portS = null;
366: try {
367: portS = getEngineData().getAttributeValue("serverPort"); // NOI18N
368: } catch (NullPointerException npe) {
369: }
370: if (portS == null)
371: portS = getServletData().getAttributeValue("serverPort"); // NOI18N
372: return Integer.parseInt(portS);
373: }
374:
375: public String getServerPortAsString() {
376:
377: String portS = null;
378: try {
379: portS = getEngineData().getAttributeValue("serverPort"); // NOI18N
380: } catch (NullPointerException npe) {
381: }
382: if (portS == null)
383: portS = getServletData().getAttributeValue("serverPort"); // NOI18N
384: return portS;
385:
386: }
387:
388: public void setServerName(String server) {
389:
390: try {
391: getEngineData().setAttributeValue("serverName", server); // NOI18N
392: return;
393: } catch (NullPointerException npe) {
394: }
395:
396: try {
397: getServletData().getAttributeValue("serverName, server"); // NOI18N
398: return;
399: } catch (NullPointerException npe) {
400: }
401: }
402:
403: public void setServerPort(int port) {
404:
405: try {
406: getEngineData().setAttributeValue("serverPort", // NOI18N
407: String.valueOf(port));
408: return;
409: } catch (NullPointerException npe) {
410: }
411:
412: try {
413: getServletData().setAttributeValue("serverPort", // NOI18N
414: String.valueOf(port));
415: return;
416: } catch (NullPointerException npe) {
417: }
418: }
419:
420: public void setServerPort(String port) {
421:
422: try {
423: getEngineData().setAttributeValue("serverPort", // NOI18N
424: port);
425: return;
426: } catch (NullPointerException npe) {
427: }
428:
429: try {
430: getServletData().setAttributeValue("serverPort", // NOI18N
431: port);
432: return;
433: } catch (NullPointerException npe) {
434: }
435: }
436:
437: //
438: static public void addComparator(BeanComparator c) {
439: MonitorData.comparators.add(c);
440: }
441:
442: //
443: static public void removeComparator(BeanComparator c) {
444: MonitorData.comparators.remove(c);
445: }
446:
447: //
448: public void addPropertyChangeListener(PropertyChangeListener l) {
449: BeanProp p = this .beanProp();
450: if (p != null)
451: p.addPCListener(l);
452: }
453:
454: //
455: public void removePropertyChangeListener(PropertyChangeListener l) {
456: BeanProp p = this .beanProp();
457: if (p != null)
458: p.removePCListener(l);
459: }
460:
461: //
462: public void addPropertyChangeListener(String n,
463: PropertyChangeListener l) {
464: BeanProp p = this .beanProp(n);
465: if (p != null)
466: p.addPCListener(l);
467: }
468:
469: //
470: public void removePropertyChangeListener(String n,
471: PropertyChangeListener l) {
472: BeanProp p = this .beanProp(n);
473: if (p != null)
474: p.removePCListener(l);
475: }
476:
477: //
478: // This method returns the root of the bean graph
479: // Each call creates a new bean graph from the specified DOM graph
480: //
481: public static MonitorData createGraph(Node doc) {
482: return new MonitorData(doc, Common.NO_DEFAULT_VALUES);
483: }
484:
485: public static MonitorData createGraph(java.io.Reader reader)
486: throws IOException {
487: try {
488: return MonitorData.createGraph(reader, false);
489: } catch (IOException ioe) {
490: throw ioe;
491: }
492: }
493:
494: public static MonitorData createGraph(java.io.Reader reader,
495: boolean validate) throws IOException {
496: try {
497: InputSource insource = new InputSource(reader);
498: insource.setEncoding("UTF-8"); // NOI18N
499: Document doc = GraphManager.createXmlDocument(insource,
500: validate);
501: return MonitorData.createGraph(doc);
502: } catch (Throwable t) {
503: throw new IOException();
504: }
505: }
506:
507: //
508: // This method returns the root for a new empty bean graph
509: //
510: public static MonitorData createGraph() {
511: return new MonitorData();
512: }
513:
514: public void write(OutputStream out) throws IOException {
515: throw new RuntimeException("Don't do this!"); // NOI18N
516: }
517:
518: public void write(Writer writer) throws IOException {
519: try {
520: this .write(writer, "UTF-8"); // NOI18N
521: } catch (Exception e) {
522: throw new RuntimeException(e.getMessage());
523: }
524: }
525:
526: // Dump the content of this bean returning it as a String
527: public void dump(StringBuffer str, String indent) {
528: String s;
529: BaseBean n;
530: str.append(indent);
531: str.append("ClientData"); // NOI18N
532: n = this .getClientData();
533: if (n != null)
534: n.dump(str, indent + "\t"); // NOI18N
535: else
536: str.append(indent + "\tnull"); // NOI18N
537: this .dumpAttributes(CLIENTDATA, 0, str, indent);
538:
539: str.append(indent);
540: str.append("SessionData"); // NOI18N
541: n = this .getSessionData();
542: if (n != null)
543: n.dump(str, indent + "\t"); // NOI18N
544: else
545: str.append(indent + "\tnull"); // NOI18N
546: this .dumpAttributes(SESSIONDATA, 0, str, indent);
547:
548: str.append(indent);
549: str.append("CookiesData"); // NOI18N
550: n = this .getCookiesData();
551: if (n != null)
552: n.dump(str, indent + "\t"); // NOI18N
553: else
554: str.append(indent + "\tnull"); // NOI18N
555: this .dumpAttributes(COOKIESDATA, 0, str, indent);
556:
557: str.append(indent);
558: str.append("RequestData"); // NOI18N
559: n = this .getRequestData();
560: if (n != null)
561: n.dump(str, indent + "\t"); // NOI18N
562: else
563: str.append(indent + "\tnull"); // NOI18N
564: this .dumpAttributes(REQUESTDATA, 0, str, indent);
565:
566: str.append(indent);
567: str.append("ServletData"); // NOI18N
568: n = this .getServletData();
569: if (n != null)
570: n.dump(str, indent + "\t"); // NOI18N
571: else
572: str.append(indent + "\tnull"); // NOI18N
573: this .dumpAttributes(SERVLETDATA, 0, str, indent);
574:
575: str.append("ContextData"); // NOI18N
576: n = this .getContextData();
577: if (n != null)
578: n.dump(str, indent + "\t"); // NOI18N
579: else
580: str.append(indent + "\tnull"); // NOI18N
581: this .dumpAttributes(CONTEXTDATA, 0, str, indent);
582:
583: str.append(indent);
584: str.append("Dispatches"); // NOI18N
585: n = this .getDispatches();
586: if (n != null)
587: n.dump(str, indent + "\t"); // NOI18N
588: else
589: str.append(indent + "\tnull"); // NOI18N
590: this .dumpAttributes(DISPATCHES, 0, str, indent);
591: }
592:
593: public String dumpBeanNode() {
594: StringBuffer str = new StringBuffer();
595: str.append("MonitorData\n"); // NOI18N
596: this .dump(str, "\n "); // NOI18N
597: return str.toString();
598: }
599:
600: /** Writes the data to a temp file.
601: * @param filename The name of the file to which the data is
602: * written
603: * @return the absolute path to the file as a String
604: *
605: */
606: public String createTempFile(String filename) {
607:
608: StringBuffer buf = new StringBuffer(System
609: .getProperty("java.io.tmpdir")); // NOI18N
610: buf.append(System.getProperty("file.separator")); // NOI18N
611: buf.append(filename);
612:
613: try {
614: File file = new File(buf.toString());
615: FileOutputStream fout = new FileOutputStream(file);
616: PrintWriter pw = new PrintWriter(fout);
617: this .write(pw);
618: pw.close();
619: fout.close();
620: return file.getAbsolutePath();
621: } catch (Throwable throwable) {
622: return "null"; //NOI18N
623: }
624: }
625:
626: /*
627: public TransactionNode createTransactionNode(boolean current) {
628: TransactionNode node =
629: new TransactionNode(this.getAttributeValue("id"), // NOI18N
630: this.getAttributeValue("method"), // NOI18N
631: this.getAttributeValue("resource")); // NOI18N
632: node.setCurrent(current);
633: return node;
634: }
635: */
636: }
|