001 /*
002 * Copyright 2011-2012 UnboundID Corp.
003 *
004 * This program is free software; you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License (GPLv2 only)
006 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
007 * as published by the Free Software Foundation.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program; if not, see <http://www.gnu.org/licenses>.
016 */
017
018 package com.unboundid.scim.wink;
019
020 import com.unboundid.scim.sdk.Debug;
021 import com.unboundid.scim.sdk.Version;
022 import org.json.JSONException;
023 import org.json.JSONStringer;
024 import org.json.JSONWriter;
025
026 import javax.ws.rs.GET;
027 import javax.ws.rs.Path;
028 import javax.ws.rs.Produces;
029 import javax.ws.rs.core.MediaType;
030 import javax.ws.rs.core.Response;
031 import java.util.Map;
032
033
034 /**
035 * This class is a JAX-RS resource to allow monitor data to be fetched and
036 * reset.
037 */
038 @Path("monitor")
039 public class MonitorResource
040 {
041 private final SCIMApplication application;
042 private final ResourceStats resourceStats;
043
044 /**
045 * Create a new JAX-RS resource.
046 *
047 * @param application The SCIM JAX-RS application associated with this
048 * resource.
049 * @param resourceStats The ResourceStats instance to use.
050 */
051 public MonitorResource(final SCIMApplication application,
052 final ResourceStats resourceStats) {
053 this.application = application;
054 this.resourceStats = resourceStats;
055 }
056
057 /**
058 * Implement the GET operation on the monitor resource to fetch the monitor
059 * data in JSON format.
060 *
061 * @return The response to the request.
062 */
063 @GET
064 @Produces(MediaType.APPLICATION_JSON)
065 public Response doJsonGet()
066 {
067 try
068 {
069 final JSONStringer writer = new JSONStringer();
070 writeMonitorData(writer);
071 resourceStats.incrementStat(ResourceStats.GET_RESPONSE_JSON);
072 resourceStats.incrementStat(ResourceStats.GET_OK);
073 return Response.ok(writer.toString(), MediaType.APPLICATION_JSON).build();
074 }
075 catch (JSONException e)
076 {
077 Debug.debugException(e);
078 resourceStats.incrementStat(ResourceStats.GET_INTERNAL_SERVER_ERROR);
079 return Response.serverError().entity(e.getMessage()).build();
080 }
081 }
082
083
084
085 /**
086 * Write the monitor data in JSON format.
087 *
088 * @param writer A JSON writer where the monitor data is to be written.
089 *
090 * @throws JSONException If an error occurs while formatting the data.
091 */
092 private void writeMonitorData(final JSONWriter writer)
093 throws JSONException
094 {
095 writer.object();
096 writer.key("version");
097 writer.value(Version.VERSION);
098 writer.key("build");
099 writer.value(Version.BUILD_TIMESTAMP);
100 writer.key("revision");
101 writer.value(Version.REVISION_NUMBER);
102
103 writer.key("resources");
104 writer.array();
105 for(ResourceStats stats : application.getResourceStats())
106 {
107 writer.object();
108 writer.key("name");
109 writer.value(stats.getName());
110 for(Map.Entry<String, Long> stat : stats.getStats().entrySet())
111 {
112 writer.key(stat.getKey());
113 writer.value(stat.getValue());
114 }
115 writer.endObject();
116 }
117 writer.endArray();
118 writer.endObject();
119 }
120 }