/* The MIT License (MIT) Copyright (c) 2015 Pierre Lindenbaum Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. History: * 2015 creation */ package com.github.lindenb.jvarkit.tools.jmx; import java.io.File; import java.io.IOException; import java.lang.management.ManagementFactory; import java.util.List; import javax.management.MBeanServer; import javax.management.ObjectName; import com.beust.jcommander.Parameter; import com.beust.jcommander.ParametersDelegate; import com.github.lindenb.jvarkit.util.jcommander.Launcher; import com.github.lindenb.jvarkit.util.jcommander.Program; import com.github.lindenb.jvarkit.util.log.Logger; import htsjdk.samtools.SAMFileWriter; import htsjdk.samtools.SAMRecord; import htsjdk.samtools.SAMRecordIterator; import htsjdk.samtools.SamReader; import htsjdk.samtools.util.CloserUtil; @Program(name="samjmx",description="Monitor/interrupt/break a BAM/SAM stream with java JMX http://www.oracle.com/technetwork/articles/java/javamanagement-140525.html") public class SamJmx extends Launcher { private static final Logger LOG=Logger.build(SamJmx.class).make(); @Parameter(names={"-o","--out"},description="Output vcf , or stdout") private File output=null; @ParametersDelegate private WritingBamArgs writingBamArgs = new WritingBamArgs(); @Parameter(names={"-p"},required=false,description="Stream identifier") private String projectName=null; public SamJmx() { } public void setProjectName(String projectName) { this.projectName = projectName; } private int doWork( SamReader in) throws IOException { String name=this.projectName; if(name==null || name.trim().isEmpty()) { name= "undefined"; } MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer(); LocatableStreamInfo dynamicMBean=new LocatableStreamInfo( name ); LocatableStreamInfo.Status status = LocatableStreamInfo.Status.IDLE; ObjectName objectMBean = null; SAMFileWriter out=null; SAMRecordIterator iter=null; try { out = this.writingBamArgs.openSAMFileWriter(output,in.getFileHeader(), true); objectMBean = new ObjectName( dynamicMBean.getClass().getPackage() .getName() + ":type=" + dynamicMBean.getClass().getSimpleName() ); mbeanServer.registerMBean(dynamicMBean,objectMBean); iter = in.iterator(); while(iter.hasNext()) { SAMRecord rec = iter.next(); out.addAlignment(rec); status = dynamicMBean.watch(rec); if( status == LocatableStreamInfo.Status.ABORT || status == LocatableStreamInfo.Status.BREAK) { LOG.error("#### Process \""+name+"\" received message "+status.name()); break; } } if(status == LocatableStreamInfo.Status.ABORT) { LOG.error("#### Process \""+name+"\" : Exit failure"); mbeanServer.unregisterMBean(objectMBean); if(this.output!=null) { CloserUtil.close(out); out=null; this.output.delete(); } System.exit(-1); } return 0; } catch(Exception err) { LOG.error(err); return -1; } finally { CloserUtil.close(iter); CloserUtil.close(out); if(objectMBean!=null) { try { mbeanServer.unregisterMBean(objectMBean);} catch(Exception err2) {} } } } @Override public int doWork(List<String> args) { SamReader in=null; try { in = super.openSamReader(super.oneFileOrNull(args)); return doWork(in); } catch(final Exception err) { LOG.error(err); return -1; } finally { CloserUtil.close(in); } } public static void main(String[] args) throws IOException { new SamJmx().instanceMain(args); } }