org.glassfish.jersey.server.internal.monitoring.MonitoringStatisticsImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jaxrs-ri Show documentation
Show all versions of jaxrs-ri Show documentation
A bundle project producing JAX-RS RI bundles. The primary artifact is an "all-in-one" OSGi-fied JAX-RS RI bundle
(jaxrs-ri.jar).
Attached to that are two compressed JAX-RS RI archives. The first archive (jaxrs-ri.zip) consists of binary RI bits and
contains the API jar (under "api" directory), RI libraries (under "lib" directory) as well as all external
RI dependencies (under "ext" directory). The secondary archive (jaxrs-ri-src.zip) contains buildable JAX-RS RI source
bundle and contains the API jar (under "api" directory), RI sources (under "src" directory) as well as all external
RI dependencies (under "ext" directory). The second archive also contains "build.xml" ANT script that builds the RI
sources. To build the JAX-RS RI simply unzip the archive, cd to the created jaxrs-ri directory and invoke "ant" from
the command line.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2013-2015 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.glassfish.jersey.server.internal.monitoring;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.SortedMap;
import org.glassfish.jersey.server.model.Resource;
import org.glassfish.jersey.server.model.ResourceMethod;
import org.glassfish.jersey.server.model.ResourceModel;
import org.glassfish.jersey.server.monitoring.ExceptionMapperStatistics;
import org.glassfish.jersey.server.monitoring.ExecutionStatistics;
import org.glassfish.jersey.server.monitoring.MonitoringStatistics;
import org.glassfish.jersey.server.monitoring.ResourceStatistics;
import org.glassfish.jersey.server.monitoring.ResponseStatistics;
import jersey.repackaged.com.google.common.base.Function;
import jersey.repackaged.com.google.common.collect.Maps;
/**
* Monitoring statistics implementation.
*
* @author Miroslav Fuksa
*/
final class MonitoringStatisticsImpl implements MonitoringStatistics {
/**
* Builder of monitoring statistics.
*/
static class Builder {
private static final Function BUILDING_FUNCTION =
new Function() {
@Override
public ResourceStatistics apply(final ResourceStatisticsImpl.Builder builder) {
return builder.build();
}
};
private final ResponseStatisticsImpl.Builder responseStatisticsBuilder;
private final ExceptionMapperStatisticsImpl.Builder exceptionMapperStatisticsBuilder;
private final ResourceMethodStatisticsImpl.Factory methodFactory = new ResourceMethodStatisticsImpl.Factory();
private final SortedMap uriStatistics = Maps.newTreeMap();
private final SortedMap, ResourceStatisticsImpl.Builder> resourceClassStatistics
= Maps.newTreeMap(new Comparator>() {
@Override
public int compare(final Class o1, final Class o2) {
return o1.getName().compareTo(o2.getName());
}
});
private ExecutionStatisticsImpl.Builder requestStatisticsBuilder;
/**
* Create a new builder.
*/
Builder() {
this.responseStatisticsBuilder = new ResponseStatisticsImpl.Builder();
this.exceptionMapperStatisticsBuilder = new ExceptionMapperStatisticsImpl.Builder();
}
/**
* Create a new builder and initialize it from resource model.
* @param resourceModel resource model.
*/
Builder(final ResourceModel resourceModel) {
this();
for (final Resource resource : resourceModel.getRootResources()) {
processResource(resource, "");
for (final Resource child : resource.getChildResources()) {
final String path = resource.getPath();
processResource(child, path.startsWith("/") ? path : "/" + path);
}
}
}
private void processResource(final Resource resource, final String pathPrefix) {
final StringBuilder pathSB = new StringBuilder(pathPrefix);
if (!pathPrefix.endsWith("/") && !resource.getPath().startsWith("/")) {
pathSB.append("/");
}
pathSB.append(resource.getPath());
uriStatistics.put(pathSB.toString(), new ResourceStatisticsImpl.Builder(resource, methodFactory));
for (final ResourceMethod resourceMethod : resource.getResourceMethods()) {
getOrCreateResourceBuilder(resourceMethod).addMethod(resourceMethod);
}
}
private ResourceStatisticsImpl.Builder getOrCreateResourceBuilder(final ResourceMethod resourceMethod) {
final Class clazz = resourceMethod.getInvocable().getHandler().getHandlerClass();
ResourceStatisticsImpl.Builder builder = resourceClassStatistics.get(clazz);
if (builder == null) {
builder = new ResourceStatisticsImpl.Builder(methodFactory);
resourceClassStatistics.put(clazz, builder);
}
return builder;
}
/**
* Get the exception mapper statistics builder.
* @return Builder of internal exception mapper statistics.
*/
ExceptionMapperStatisticsImpl.Builder getExceptionMapperStatisticsBuilder() {
return exceptionMapperStatisticsBuilder;
}
/**
* Add global request execution.
*
* @param startTime time of the execution.
* @param duration duration of the execution.
*/
void addRequestExecution(final long startTime, final long duration) {
if (requestStatisticsBuilder == null) {
requestStatisticsBuilder = new ExecutionStatisticsImpl.Builder();
}
requestStatisticsBuilder.addExecution(startTime, duration);
}
/**
* Add execution of a resource method.
*
* @param uri String uri which was executed.
* @param resourceMethod Resource method.
* @param methodTime Time spent on execution of resource method itself (Unix timestamp format).
* @param methodDuration Time of execution of the resource method.
* @param requestTime Time of whole request processing (from receiving
* the request until writing the response). (Unix timestamp format)
* @param requestDuration Time when the request matching to the executed resource method has been received
* by Jersey.
*/
void addExecution(final String uri, final ResourceMethod resourceMethod,
final long methodTime, final long methodDuration,
final long requestTime, final long requestDuration) {
// Uri resource stats.
ResourceStatisticsImpl.Builder uriStatsBuilder = uriStatistics.get(uri);
if (uriStatsBuilder == null) {
uriStatsBuilder = new ResourceStatisticsImpl.Builder(resourceMethod.getParent(), methodFactory);
uriStatistics.put(uri, uriStatsBuilder);
}
uriStatsBuilder.addExecution(resourceMethod, methodTime, methodDuration, requestTime, requestDuration);
// Class resource stats.
final ResourceStatisticsImpl.Builder classStatsBuilder = getOrCreateResourceBuilder(resourceMethod);
classStatsBuilder.addExecution(resourceMethod, methodTime, methodDuration, requestTime, requestDuration);
// Resource method stats.
methodFactory.getOrCreate(resourceMethod)
.addResourceMethodExecution(methodTime, methodDuration, requestTime, requestDuration);
}
/**
* Add a response status code produces by Jersey.
* @param responseCode Response status code.
*/
void addResponseCode(final int responseCode) {
responseStatisticsBuilder.addResponseCode(responseCode);
}
/**
* Build a new instance of monitoring statistics.
* @return New instance of {@code MonitoringStatisticsImpl}.
*/
MonitoringStatisticsImpl build() {
final Map uriStats = Collections.unmodifiableMap(
Maps.transformValues(uriStatistics, BUILDING_FUNCTION));
final Map, ResourceStatistics> classStats = Collections.unmodifiableMap(
Maps.transformValues(resourceClassStatistics, BUILDING_FUNCTION));
final ExecutionStatistics requestStats = requestStatisticsBuilder == null
? ExecutionStatisticsImpl.EMPTY : requestStatisticsBuilder.build();
return new MonitoringStatisticsImpl(
uriStats, classStats, requestStats,
responseStatisticsBuilder.build(),
exceptionMapperStatisticsBuilder.build());
}
}
private final ExecutionStatistics requestStatistics;
private final ResponseStatistics responseStatistics;
private final ExceptionMapperStatistics exceptionMapperStatistics;
private final Map uriStatistics;
private final Map, ResourceStatistics> resourceClassStatistics;
private MonitoringStatisticsImpl(final Map uriStatistics,
final Map, ResourceStatistics> resourceClassStatistics,
final ExecutionStatistics requestStatistics,
final ResponseStatistics responseStatistics,
final ExceptionMapperStatistics exceptionMapperStatistics) {
this.uriStatistics = uriStatistics;
this.resourceClassStatistics = resourceClassStatistics;
this.requestStatistics = requestStatistics;
this.responseStatistics = responseStatistics;
this.exceptionMapperStatistics = exceptionMapperStatistics;
}
@Override
public ExecutionStatistics getRequestStatistics() {
return requestStatistics;
}
@Override
public ResponseStatistics getResponseStatistics() {
return responseStatistics;
}
@Override
public Map getUriStatistics() {
return uriStatistics;
}
@Override
public Map, ResourceStatistics> getResourceClassStatistics() {
return resourceClassStatistics;
}
@Override
public ExceptionMapperStatistics getExceptionMapperStatistics() {
return exceptionMapperStatistics;
}
@Override
public MonitoringStatistics snapshot() {
// snapshot functionality not yet implemented
return this;
}
}