com.rapiddweller.common.debug.ResourceMonitor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rd-lib-common Show documentation
Show all versions of rd-lib-common Show documentation
'rapiddweller Common' is an open source Java library
forked from Databene Commons by Volker Bergmann.
It provides extensions to the Java core library by utility classes, abstract concepts
and concrete implementations.
/*
* Copyright (C) 2004-2015 Volker Bergmann ([email protected]).
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rapiddweller.common.debug;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.List;
/**
* Class that monitors resources as a collection of {@link MonitoredResource}s.
* Resources are registered and unregistered with {@link #register(Object)} and
* {@link #unregister(Object)} and in the end you can assert that all resources
* have been unregistered by calling {@link #assertNoRegistrations(boolean)}.
* Created: 14.04.2011 17:16:20
*
* @author Volker Bergmann
* @since 0.5.8
*/
public class ResourceMonitor {
private static final Logger LOGGER = LogManager.getLogger(ResourceMonitor.class);
private final List registrations;
/**
* Instantiates a new Resource monitor.
*/
public ResourceMonitor() {
registrations = new ArrayList<>();
}
/**
* Register.
*
* @param object the object
*/
public void register(Object object) {
Throwable t = new Throwable();
registrations.add(new MonitoredResource(object, t.getStackTrace()));
}
/**
* Unregister.
*
* @param object the object
*/
public void unregister(Object object) {
for (int i = registrations.size() - 1; i >= 0; i--) {
MonitoredResource candidate = registrations.get(i);
if (candidate.resource == object) {
registrations.remove(i);
return;
}
}
throw new IllegalStateException("Object '" + object + "' was not registered");
}
/**
* Gets registrations.
*
* @return the registrations
*/
public List getRegistrations() {
return registrations;
}
/**
* Reset.
*/
public void reset() {
this.registrations.clear();
}
/**
* Assert no registrations boolean.
*
* @param critical the critical
* @return the boolean
*/
public boolean assertNoRegistrations(boolean critical) {
if (registrations.size() == 0) {
return true;
}
String message = "There are resources which have not been unregistered:";
LOGGER.warn(message);
logRegistrations();
if (critical) {
throw new IllegalStateException(message);
}
return false;
}
private void logRegistrations() {
for (MonitoredResource resource : registrations) {
LOGGER.warn(resource.toString());
}
}
}