/// <summary>Writes a JSON string field only if the value is non-null.</summary> /// <param name="json">JsonGenerator to receive output</param> /// <param name="key">String key to put</param> /// <param name="value">String value to put</param> /// <exception cref="System.IO.IOException">if there is an I/O error</exception> private static void WriteStringFieldIfNotNull(JsonGenerator json, string key, string value) { if (value != null) { json.WriteStringField(key, value); } }
/// <summary>Build a JSON entry from the parameters.</summary> /// <remarks>Build a JSON entry from the parameters. This is public for testing.</remarks> /// <param name="writer">destination</param> /// <param name="loggerName">logger name</param> /// <param name="timeStamp">time_t value</param> /// <param name="level">level string</param> /// <param name="threadName">name of the thread</param> /// <param name="message">rendered message</param> /// <param name="ti">nullable thrown information</param> /// <returns>the writer</returns> /// <exception cref="System.IO.IOException">on any problem</exception> public virtual TextWriter ToJson(TextWriter writer, string loggerName, long timeStamp , string level, string threadName, string message, ThrowableInformation ti) { JsonGenerator json = factory.CreateJsonGenerator(writer); json.WriteStartObject(); json.WriteStringField(Name, loggerName); json.WriteNumberField(Time, timeStamp); DateTime date = Extensions.CreateDate(timeStamp); json.WriteStringField(Date, dateFormat.Format(date)); json.WriteStringField(Level, level); json.WriteStringField(Thread, threadName); json.WriteStringField(Message, message); if (ti != null) { //there is some throwable info, but if the log event has been sent over the wire, //there may not be a throwable inside it, just a summary. Exception thrown = ti.GetThrowable(); string eclass = (thrown != null) ? thrown.GetType().FullName : string.Empty; json.WriteStringField(ExceptionClass, eclass); string[] stackTrace = ti.GetThrowableStrRep(); json.WriteArrayFieldStart(Stack); foreach (string row in stackTrace) { json.WriteString(row); } json.WriteEndArray(); } json.WriteEndObject(); json.Flush(); json.Close(); return(writer); }
/// <summary> /// method to perform depth-first search and write the parameters of every /// queue in JSON format. /// </summary> /// <param name="dumpGenerator"> /// JsonGenerator object which takes the dump and flushes /// to a writer object /// </param> /// <param name="rootQueues">the top-level queues</param> /// <exception cref="Org.Codehaus.Jackson.JsonGenerationException"/> /// <exception cref="System.IO.IOException"/> private static void DumpConfiguration(JsonGenerator dumpGenerator, ICollection <Queue > rootQueues) { foreach (Queue queue in rootQueues) { dumpGenerator.WriteStartObject(); dumpGenerator.WriteStringField("name", queue.GetName()); dumpGenerator.WriteStringField("state", queue.GetState().ToString()); AccessControlList submitJobList = null; AccessControlList administerJobsList = null; if (queue.GetAcls() != null) { submitJobList = queue.GetAcls()[ToFullPropertyName(queue.GetName(), QueueACL.SubmitJob .GetAclName())]; administerJobsList = queue.GetAcls()[ToFullPropertyName(queue.GetName(), QueueACL .AdministerJobs.GetAclName())]; } string aclsSubmitJobValue = " "; if (submitJobList != null) { aclsSubmitJobValue = submitJobList.GetAclString(); } dumpGenerator.WriteStringField("acl_submit_job", aclsSubmitJobValue); string aclsAdministerValue = " "; if (administerJobsList != null) { aclsAdministerValue = administerJobsList.GetAclString(); } dumpGenerator.WriteStringField("acl_administer_jobs", aclsAdministerValue); dumpGenerator.WriteFieldName("properties"); dumpGenerator.WriteStartArray(); if (queue.GetProperties() != null) { foreach (KeyValuePair <object, object> property in queue.GetProperties()) { dumpGenerator.WriteStartObject(); dumpGenerator.WriteStringField("key", (string)property.Key); dumpGenerator.WriteStringField("value", (string)property.Value); dumpGenerator.WriteEndObject(); } } dumpGenerator.WriteEndArray(); ICollection <Queue> childQueues = queue.GetChildren(); dumpGenerator.WriteFieldName("children"); dumpGenerator.WriteStartArray(); if (childQueues != null && childQueues.Count > 0) { DumpConfiguration(dumpGenerator, childQueues); } dumpGenerator.WriteEndArray(); dumpGenerator.WriteEndObject(); } }
// --------------------------------------------------------- Private Methods /// <exception cref="System.IO.IOException"/> private void ListBeans(JsonGenerator jg, ObjectName qry, string attribute, HttpServletResponse response) { Log.Debug("Listing beans for " + qry); ICollection <ObjectName> names = null; names = mBeanServer.QueryNames(qry, null); jg.WriteArrayFieldStart("beans"); IEnumerator <ObjectName> it = names.GetEnumerator(); while (it.HasNext()) { ObjectName oname = it.Next(); MBeanInfo minfo; string code = string.Empty; object attributeinfo = null; try { minfo = mBeanServer.GetMBeanInfo(oname); code = minfo.GetClassName(); string prs = string.Empty; try { if ("org.apache.commons.modeler.BaseModelMBean".Equals(code)) { prs = "modelerType"; code = (string)mBeanServer.GetAttribute(oname, prs); } if (attribute != null) { prs = attribute; attributeinfo = mBeanServer.GetAttribute(oname, prs); } } catch (AttributeNotFoundException e) { // If the modelerType attribute was not found, the class name is used // instead. Log.Error("getting attribute " + prs + " of " + oname + " threw an exception", e); } catch (MBeanException e) { // The code inside the attribute getter threw an exception so log it, // and fall back on the class name Log.Error("getting attribute " + prs + " of " + oname + " threw an exception", e); } catch (RuntimeException e) { // For some reason even with an MBeanException available to them // Runtime exceptionscan still find their way through, so treat them // the same as MBeanException Log.Error("getting attribute " + prs + " of " + oname + " threw an exception", e); } catch (ReflectionException e) { // This happens when the code inside the JMX bean (setter?? from the // java docs) threw an exception, so log it and fall back on the // class name Log.Error("getting attribute " + prs + " of " + oname + " threw an exception", e); } } catch (InstanceNotFoundException) { //Ignored for some reason the bean was not found so don't output it continue; } catch (IntrospectionException e) { // This is an internal error, something odd happened with reflection so // log it and don't output the bean. Log.Error("Problem while trying to process JMX query: " + qry + " with MBean " + oname, e); continue; } catch (ReflectionException e) { // This happens when the code inside the JMX bean threw an exception, so // log it and don't output the bean. Log.Error("Problem while trying to process JMX query: " + qry + " with MBean " + oname, e); continue; } jg.WriteStartObject(); jg.WriteStringField("name", oname.ToString()); jg.WriteStringField("modelerType", code); if ((attribute != null) && (attributeinfo == null)) { jg.WriteStringField("result", "ERROR"); jg.WriteStringField("message", "No attribute with name " + attribute + " was found." ); jg.WriteEndObject(); jg.WriteEndArray(); jg.Close(); response.SetStatus(HttpServletResponse.ScNotFound); return; } if (attribute != null) { WriteAttribute(jg, attribute, attributeinfo); } else { MBeanAttributeInfo[] attrs = minfo.GetAttributes(); for (int i = 0; i < attrs.Length; i++) { WriteAttribute(jg, oname, attrs[i]); } } jg.WriteEndObject(); } jg.WriteEndArray(); }
/// <summary>Process a GET request for the specified resource.</summary> /// <param name="request">The servlet request we are processing</param> /// <param name="response">The servlet response we are creating</param> protected override void DoGet(HttpServletRequest request, HttpServletResponse response ) { string jsonpcb = null; PrintWriter writer = null; try { if (!IsInstrumentationAccessAllowed(request, response)) { return; } JsonGenerator jg = null; try { writer = response.GetWriter(); response.SetContentType("application/json; charset=utf8"); response.SetHeader(AccessControlAllowMethods, "GET"); response.SetHeader(AccessControlAllowOrigin, "*"); JsonFactory jsonFactory = new JsonFactory(); jg = jsonFactory.CreateJsonGenerator(writer); jg.Disable(JsonGenerator.Feature.AutoCloseTarget); jg.UseDefaultPrettyPrinter(); jg.WriteStartObject(); if (mBeanServer == null) { jg.WriteStringField("result", "ERROR"); jg.WriteStringField("message", "No MBeanServer could be found"); jg.Close(); Log.Error("No MBeanServer could be found."); response.SetStatus(HttpServletResponse.ScNotFound); return; } // query per mbean attribute string getmethod = request.GetParameter("get"); if (getmethod != null) { string[] splitStrings = getmethod.Split("\\:\\:"); if (splitStrings.Length != 2) { jg.WriteStringField("result", "ERROR"); jg.WriteStringField("message", "query format is not as expected."); jg.Close(); response.SetStatus(HttpServletResponse.ScBadRequest); return; } ListBeans(jg, new ObjectName(splitStrings[0]), splitStrings[1], response); jg.Close(); return; } // query per mbean string qry = request.GetParameter("qry"); if (qry == null) { qry = "*:*"; } ListBeans(jg, new ObjectName(qry), null, response); } finally { if (jg != null) { jg.Close(); } if (writer != null) { writer.Close(); } } } catch (IOException e) { Log.Error("Caught an exception while processing JMX request", e); response.SetStatus(HttpServletResponse.ScInternalServerError); } catch (MalformedObjectNameException e) { Log.Error("Caught an exception while processing JMX request", e); response.SetStatus(HttpServletResponse.ScBadRequest); } finally { if (writer != null) { writer.Close(); } } }