/*******************************************************************************
* 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 javax.tools.diagnostics.vm.spi.delegates;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.tools.diagnostics.vm.DumpDescriptor;
import javax.tools.diagnostics.vm.DumpHandle;
import javax.tools.diagnostics.vm.DumpInitiatorDelegate;
import javax.tools.diagnostics.vm.spi.DumpInitiatorCapabilities;
/**
* Signal based Dump Initiator relies on the presence of
* org.apache.kato.common142.DumpTrigger in the class path
*
*/
public abstract class AbstractSignalBasedDumpInitiatorDelegate implements
DumpInitiatorDelegate {
private static final String ORG_APACHE_KATO_COMMON142_DUMP_TRIGGER = "org.apache.kato.common142.DumpTrigger";
private Method dumpMethod = null;
public boolean available() {
try {
// check that the trigger support class exists and can be loaded
Class clazz = Class
.forName(ORG_APACHE_KATO_COMMON142_DUMP_TRIGGER);
// now instantiate it so it can do a self check
clazz.newInstance();
// store the dump method for later use
dumpMethod = clazz.getMethod("triggerDump", new Class[0]);
return true;
} catch (Exception e) {
return false;
}
}
@Override
public DumpHandle createDumpHandle() {
return new DumpHandle() {
@Override
public boolean dump() throws IOException {
try {
Boolean result = (Boolean) dumpMethod.invoke(null,
new Object[0]);
return result.booleanValue();
} catch (Exception e) {
throw new IOException("dump failed", e);
}
}
};
}
@Override
public DumpHandle createDumpHandle(DumpDescriptor descriptor) {
return new DumpHandle() {
@Override
public boolean dump() throws IOException {
try {
Boolean result = (Boolean) dumpMethod.invoke(null,
new Object[0]);
return result.booleanValue();
} catch (Exception e) {
throw new IOException("dump failed", e);
}
}
};
}
}