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.net.hl7; 040 041import org.dcm4che3.conf.core.api.ConfigurableClass; 042import org.dcm4che3.conf.core.api.ConfigurableProperty; 043import org.dcm4che3.conf.core.api.LDAP; 044import org.dcm4che3.hl7.HL7Exception; 045import org.dcm4che3.hl7.HL7Segment; 046import org.dcm4che3.net.Connection; 047import org.dcm4che3.net.DeviceExtension; 048 049import java.net.Socket; 050import java.util.Collection; 051import java.util.Map; 052import java.util.TreeMap; 053 054/** 055 * @author Gunter Zeilinger <gunterze@gmail.com> 056 * 057 */ 058@LDAP(noContainerNode = true) 059@ConfigurableClass 060public class HL7DeviceExtension extends DeviceExtension { 061 062 private static final long serialVersionUID = -411853996726542266L; 063 064 static { 065 Connection.registerTCPProtocolHandler( 066 Connection.Protocol.HL7, HL7ProtocolHandler.INSTANCE); 067 } 068 069 @LDAP(noContainerNode = true, distinguishingField = "hl7ApplicationName") 070 @ConfigurableProperty(name="hl7Apps", label = "HL7 Applications") 071 private Map<String, HL7Application> hl7apps = new TreeMap<String, HL7Application>(); 072 073 private transient HL7MessageListener hl7MessageListener; 074 075 076 public Map<String, HL7Application> getHl7apps() { 077 return hl7apps; 078 } 079 080 public void setHl7apps(Map<String, HL7Application> hl7apps) { 081 // remove old 082 for (String appName : this.hl7apps.keySet()) removeHL7Application(appName); 083 // add new 084 for (HL7Application hl7Application : hl7apps.values()) addHL7Application(hl7Application); 085 } 086 087 @Override 088 public void verifyNotUsed(Connection conn) { 089 for (HL7Application app : hl7apps.values()) 090 if (app.getConnections().contains(conn)) 091 throw new IllegalStateException(conn 092 + " used by HL7 Application: " 093 + app.getApplicationName()); 094 } 095 096 public void addHL7Application(HL7Application hl7App) { 097 hl7App.setDevice(device); 098 hl7apps.put(hl7App.getApplicationName(), hl7App); 099 } 100 101 public HL7Application removeHL7Application(String name) { 102 HL7Application hl7App = hl7apps.remove(name); 103 if (hl7App != null) 104 hl7App.setDevice(null); 105 106 return hl7App; 107 } 108 109 public boolean removeHL7Application(HL7Application hl7App) { 110 return removeHL7Application(hl7App.getApplicationName()) != null; 111 } 112 113 public HL7Application getHL7Application(String name) { 114 HL7Application hl7App = hl7apps.get(name); 115 if (hl7App == null) 116 hl7App = hl7apps.get("*"); 117 return hl7App; 118 } 119 120 public boolean containsHL7Application(String name) { 121 return hl7apps.containsKey(name); 122 } 123 124 public Collection<String> getHL7ApplicationNames() { 125 return hl7apps.keySet(); 126 } 127 128 public Collection<HL7Application> getHL7Applications() { 129 return hl7apps.values(); 130 } 131 132 public final HL7MessageListener getHL7MessageListener() { 133 return hl7MessageListener; 134 } 135 136 public final void setHL7MessageListener(HL7MessageListener listener) { 137 this.hl7MessageListener = listener; 138 } 139 140 byte[] onMessage(HL7Segment msh, byte[] msg, int off, int len, int mshlen, 141 Connection conn, Socket s) throws HL7Exception { 142 HL7Application hl7App = getHL7Application(msh.getReceivingApplicationWithFacility()); 143 if (hl7App == null) 144 throw new HL7Exception(HL7Exception.AR, "Receiving Application not recognized"); 145 return hl7App.onMessage(conn, s, msh, msg, off, len, mshlen); 146 } 147 148 @Override 149 public void reconfigure(DeviceExtension from) { 150 reconfigureHL7Applications((HL7DeviceExtension) from); 151 } 152 153 private void reconfigureHL7Applications(HL7DeviceExtension from) { 154 hl7apps.keySet().retainAll(from.hl7apps.keySet()); 155 for (HL7Application src : from.hl7apps.values()) { 156 HL7Application hl7app = hl7apps.get(src.getApplicationName()); 157 if (hl7app == null) 158 addHL7Application(hl7app = new HL7Application(src.getApplicationName())); 159 hl7app.reconfigure(src); 160 } 161 } 162}