001/* ***** BEGIN LICENSE BLOCK ***** 002 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 003 * 004 * The contents of this file are subject to the Mozilla Public License Version 005 * 1.1 (the "License"); you may not use this file except in compliance with 006 * the License. You may obtain a copy of the License at 007 * http://www.mozilla.org/MPL/ 008 * 009 * Software distributed under the License is distributed on an "AS IS" basis, 010 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 011 * for the specific language governing rights and limitations under the 012 * License. 013 * 014 * The Original Code is part of dcm4che, an implementation of DICOM(TM) in 015 * Java(TM), hosted at https://github.com/gunterze/dcm4che. 016 * 017 * The Initial Developer of the Original Code is 018 * Agfa Healthcare. 019 * Portions created by the Initial Developer are Copyright (C) 2012 020 * the Initial Developer. All Rights Reserved. 021 * 022 * Contributor(s): 023 * See @authors listed below 024 * 025 * Alternatively, the contents of this file may be used under the terms of 026 * either the GNU General Public License Version 2 or later (the "GPL"), or 027 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 028 * in which case the provisions of the GPL or the LGPL are applicable instead 029 * of those above. If you wish to allow use of your version of this file only 030 * under the terms of either the GPL or the LGPL, and not to allow others to 031 * use your version of this file under the terms of the MPL, indicate your 032 * decision by deleting the provisions above and replace them with the notice 033 * and other provisions required by the GPL or the LGPL. If you do not delete 034 * the provisions above, a recipient may use your version of this file under 035 * the terms of any one of the MPL, the GPL or the LGPL. 036 * 037 * ***** END LICENSE BLOCK ***** */ 038 039package org.dcm4che3.tool.syslogd; 040 041import java.io.File; 042import java.io.FileOutputStream; 043import java.io.IOException; 044import java.net.InetAddress; 045import java.net.MalformedURLException; 046import java.util.ResourceBundle; 047import java.util.concurrent.ExecutorService; 048import java.util.concurrent.Executors; 049import java.util.concurrent.ScheduledExecutorService; 050 051import org.apache.commons.cli.CommandLine; 052import org.apache.commons.cli.MissingOptionException; 053import org.apache.commons.cli.OptionBuilder; 054import org.apache.commons.cli.Options; 055import org.apache.commons.cli.ParseException; 056import org.dcm4che3.net.Connection; 057import org.dcm4che3.net.Connection.Protocol; 058import org.dcm4che3.net.Device; 059import org.dcm4che3.net.audit.AuditRecordHandler; 060import org.dcm4che3.net.audit.AuditRecordRepository; 061import org.dcm4che3.tool.common.CLIUtils; 062import org.dcm4che3.util.StringUtils; 063 064/** 065 * @author Gunter Zeilinger <gunterze@gmail.com> 066 * 067 */ 068public class Syslogd { 069 070 private static final ResourceBundle rb = 071 ResourceBundle.getBundle("org.dcm4che3.tool.syslogd.messages"); 072 073 private final Device device = new Device("syslogd"); 074 private final AuditRecordRepository arr = new AuditRecordRepository(); 075 private final Connection conn = new Connection(); 076 private File storageDir; 077 private final AuditRecordHandler handler = new AuditRecordHandler() { 078 079 @Override 080 public void onMessage(byte[] data, int xmlOffset, int xmlLength, 081 Connection conn, InetAddress from) { 082 Syslogd.this.onMessage(data, xmlOffset, xmlLength, conn, from); 083 } 084 }; 085 086 public Syslogd() { 087 device.addDeviceExtension(arr); 088 device.addConnection(conn); 089 arr.setAuditRecordHandler(handler); 090 } 091 092 public void setStorageDirectory(String storageDir) { 093 this.storageDir = storageDir != null ? new File(storageDir) : null; 094 } 095 096 private static CommandLine parseComandLine(String[] args) 097 throws ParseException { 098 Options opts = new Options(); 099 addOptions(opts); 100 CLIUtils.addSocketOptions(opts); 101 CLIUtils.addTLSOptions(opts); 102 CLIUtils.addCommonOptions(opts); 103 return CLIUtils.parseComandLine(args, opts, rb, Syslogd.class); 104 } 105 106 @SuppressWarnings("static-access") 107 public static void addOptions(Options opts) { 108 opts.addOption(null, "ignore", false, rb.getString("ignore")); 109 opts.addOption(OptionBuilder 110 .hasArg() 111 .withArgName("path") 112 .withDescription(rb.getString("directory")) 113 .withLongOpt("directory") 114 .create(null)); 115 opts.addOption(OptionBuilder 116 .hasArg() 117 .withArgName("[ip:]port") 118 .withDescription(rb.getString("bind-server")) 119 .withLongOpt("bind") 120 .create("b")); 121 opts.addOption(OptionBuilder 122 .hasArg() 123 .withArgName("ms") 124 .withDescription(rb.getString("idle-timeout")) 125 .withLongOpt("idle-timeout") 126 .create(null)); 127 opts.addOption(null, "udp", false, rb.getString("udp")); 128 } 129 130 public static void main(String[] args) { 131 try { 132 CommandLine cl = parseComandLine(args); 133 Syslogd main = new Syslogd(); 134 configure(main, cl); 135 ExecutorService executorService = Executors.newCachedThreadPool(); 136 ScheduledExecutorService scheduledExecutorService = 137 Executors.newSingleThreadScheduledExecutor(); 138 main.device.setScheduledExecutor(scheduledExecutorService); 139 main.device.setExecutor(executorService); 140 main.device.bindConnections(); 141 } catch (ParseException e) { 142 System.err.println("syslogd: " + e.getMessage()); 143 System.err.println(rb.getString("try")); 144 System.exit(2); 145 } catch (Exception e) { 146 System.err.println("syslogd: " + e.getMessage()); 147 e.printStackTrace(); 148 System.exit(2); 149 } 150 } 151 152 private static void configure(Syslogd main, CommandLine cl) 153 throws Exception, MalformedURLException, ParseException, 154 IOException { 155 if (!cl.hasOption("ignore")) 156 main.setStorageDirectory( 157 cl.getOptionValue("directory", ".")); 158 configureBindServer(main.conn, cl); 159 main.setProtocol(toProtocol(cl)); 160 main.arr.addConnection(main.conn); 161 CLIUtils.configure(main.conn, cl); 162 } 163 164 private void setProtocol(Protocol protocol) { 165 conn.setProtocol(protocol); 166 arr.addConnection(conn); 167 } 168 169 private static void configureBindServer(Connection conn, CommandLine cl) 170 throws ParseException { 171 if (!cl.hasOption("b")) 172 throw new MissingOptionException( 173 CLIUtils.rb.getString("missing-bind-opt")); 174 String aeAtHostPort = cl.getOptionValue("b"); 175 String[] hostAndPort = StringUtils.split(aeAtHostPort, ':'); 176 int portIndex = hostAndPort.length - 1; 177 conn.setPort(Integer.parseInt(hostAndPort[portIndex])); 178 if (portIndex > 0) 179 conn.setHostname(hostAndPort[0]); 180 } 181 182 private static Protocol toProtocol(CommandLine cl) { 183 return cl.hasOption("udp") 184 ? Connection.Protocol.SYSLOG_UDP 185 : Connection.Protocol.SYSLOG_TLS; 186 } 187 188 private void onMessage(byte[] data, int off, int len, Connection conn, 189 InetAddress from) { 190 if (storageDir != null) 191 try { 192 storeToFile(data, off, len, 193 File.createTempFile("syslog", ".xml", storageDir)); 194 } catch (IOException e) { 195 Connection.LOG.warn("Failed to store received message", e); 196 } 197 } 198 199 private void storeToFile(byte[] data, int off, int len, File f) 200 throws IOException { 201 Connection.LOG.info("M-WRITE {}", f); 202 f.getParentFile().mkdirs(); 203 FileOutputStream out = new FileOutputStream(f); 204 try { 205 out.write(data, off, len); 206 } finally { 207 out.close(); 208 } 209 } 210 211}