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.audit;
040
041import java.net.InetAddress;
042import java.util.ArrayList;
043import java.util.List;
044
045import org.dcm4che3.conf.core.api.ConfigurableClass;
046import org.dcm4che3.conf.core.api.ConfigurableProperty;
047import org.dcm4che3.conf.core.api.LDAP;
048import org.dcm4che3.net.Connection;
049import org.dcm4che3.net.DeviceExtension;
050
051/**
052 * @author Gunter Zeilinger <gunterze@gmail.com>
053 *
054 */
055@LDAP(objectClasses = "dcmAuditRecordRepository")
056@ConfigurableClass
057public class AuditRecordRepository extends DeviceExtension {
058
059    private static final long serialVersionUID = -2279487409324427161L;
060
061    static {
062        Connection.registerTCPProtocolHandler(
063                Connection.Protocol.SYSLOG_TLS, SyslogProtocolHandler.INSTANCE);
064        Connection.registerUDPProtocolHandler(
065                Connection.Protocol.SYSLOG_UDP, SyslogProtocolHandler.INSTANCE);
066    }
067
068    @ConfigurableProperty(name="dicomInstalled")
069    private Boolean arrInstalled;
070
071    @ConfigurableProperty(name="dicomNetworkConnectionReference", collectionOfReferences = true)
072    private final List<Connection> connections = new ArrayList<Connection>(1);
073    
074    @ConfigurableProperty(name="auditRecordCodeCachingEnabled", defaultValue = "false")
075    private boolean auditRecordCodeCachingEnabled;
076
077    private transient AuditRecordHandler handler;
078
079    public boolean isInstalled() {
080        return device != null && device.isInstalled() 
081                && (arrInstalled == null || arrInstalled.booleanValue());
082    }
083
084    public final Boolean getArrInstalled() {
085        return arrInstalled;
086    }
087
088    public void setArrInstalled(Boolean installed) {
089        if (installed != null && installed.booleanValue()
090                && device != null && !device.isInstalled())
091            throw new IllegalStateException("owning device not installed");
092        this.arrInstalled = installed;
093    }
094
095    public void addConnection(Connection conn) {
096        if (!conn.getProtocol().isSyslog())
097            throw new IllegalArgumentException(
098                    "Audit Record Repository does not support protocol " + conn.getProtocol());
099        if (device != null && device != conn.getDevice())
100            throw new IllegalStateException(conn + " not contained by " + 
101                    device.getDeviceName());
102        connections.add(conn);
103    }
104    
105    public boolean removeConnection(Connection conn) {
106        return connections.remove(conn);
107    }
108
109    public List<Connection> getConnections() {
110        return connections;
111    }
112
113    public void setConnections(List<Connection> connections) {
114        this.connections.clear();
115        for (Connection connection : connections) addConnection(connection);
116    }
117
118    public AuditRecordHandler getAuditRecordHandler() {
119        return handler;
120    }
121
122    public void setAuditRecordHandler(AuditRecordHandler handler) {
123        this.handler = handler;
124    }
125    
126    public boolean isAuditRecordCodeCachingEnabled() {
127        return auditRecordCodeCachingEnabled;
128    }
129
130    public void setAuditRecordCodeCachingEnabled(boolean auditRecordCodeCachingEnabled) {
131        this.auditRecordCodeCachingEnabled = auditRecordCodeCachingEnabled;
132    }
133
134    @Override
135    public void reconfigure(DeviceExtension from)  {
136        reconfigure((AuditRecordRepository) from);
137    }
138
139    private void reconfigure(AuditRecordRepository from) {
140        setArrInstalled(from.arrInstalled);
141        device.reconfigureConnections(connections, from.connections);
142    }
143
144    public void onMessage(byte[] data, int xmlOffset, int xmlLength,
145            Connection conn, InetAddress from) {
146        if (handler == null)
147            throw new IllegalStateException("No AuditRecordHandler initialized");
148
149        handler.onMessage(data, xmlOffset, xmlLength, conn, from);
150    }
151}