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.ianscp;
040
041import java.io.File;
042import java.io.IOException;
043import java.util.Properties;
044import java.util.ResourceBundle;
045import java.util.concurrent.ExecutorService;
046import java.util.concurrent.Executors;
047import java.util.concurrent.ScheduledExecutorService;
048
049import org.apache.commons.cli.CommandLine;
050import org.apache.commons.cli.OptionBuilder;
051import org.apache.commons.cli.Options;
052import org.apache.commons.cli.ParseException;
053import org.dcm4che3.net.ApplicationEntity;
054import org.dcm4che3.net.Connection;
055import org.dcm4che3.net.Device;
056import org.dcm4che3.net.TransferCapability;
057import org.dcm4che3.tool.common.CLIUtils;
058
059/**
060 * Command line interface for {@link IanSCPTool}
061 *
062 * @author Gunter Zeilinger <gunterze@gmail.com>
063 * @author Hermann Czedik-Eysenberg <hermann-agfa@czedik.net>
064 */
065public class IanSCP {
066
067    private static ResourceBundle rb =
068            ResourceBundle.getBundle("org.dcm4che3.tool.ianscp.messages");
069
070    public static void main(String[] args) {
071       try {
072           CommandLine cl = parseComandLine(args);
073           Device device = new Device("ianscp");
074           ApplicationEntity ae = new ApplicationEntity("*");
075           device.addApplicationEntity(ae);
076           Connection conn = new Connection();
077           device.addConnection(conn);
078           ae.addConnection(conn);
079           IanSCPTool main = new IanSCPTool(device, ae);
080           CLIUtils.configureBindServer(conn, ae, cl);
081           CLIUtils.configure(conn, cl);
082           configureTransferCapability(ae, cl);
083           main.setResponseStatus(CLIUtils.getIntOption(cl, "status", 0));
084           main.setStorageDirectory(getStorageDirectory(cl));
085           ExecutorService executorService = Executors.newCachedThreadPool();
086           ScheduledExecutorService scheduledExecutorService = 
087                   Executors.newSingleThreadScheduledExecutor();
088           device.setScheduledExecutor(scheduledExecutorService);
089           device.setExecutor(executorService);
090           device.bindConnections();
091       } catch (ParseException e) {
092           System.err.println("ianscp: " + e.getMessage());
093           System.err.println(rb.getString("try"));
094           System.exit(2);
095       } catch (Exception e) {
096           System.err.println("ianscp: " + e.getMessage());
097           e.printStackTrace();
098           System.exit(2);
099       }
100   }
101
102    private static CommandLine parseComandLine(String[] args) throws ParseException {
103        Options opts = new Options();
104        CLIUtils.addBindServerOption(opts);
105        CLIUtils.addAEOptions(opts);
106        CLIUtils.addCommonOptions(opts);
107        addStorageDirectoryOptions(opts);
108        addTransferCapabilityOptions(opts);
109        addStatusOption(opts);
110        return CLIUtils.parseComandLine(args, opts, rb, IanSCP.class);
111    }
112
113    @SuppressWarnings("static-access")
114    private static void addStorageDirectoryOptions(Options opts) {
115        opts.addOption(null, "ignore", false,
116                rb.getString("ignore"));
117        opts.addOption(OptionBuilder
118                .hasArg()
119                .withArgName("path")
120                .withDescription(rb.getString("directory"))
121                .withLongOpt("directory")
122                .create(null));
123    }
124
125    @SuppressWarnings("static-access")
126    private static void addTransferCapabilityOptions(Options opts) {
127        opts.addOption(OptionBuilder
128                .hasArg()
129                .withArgName("file|url")
130                .withDescription(rb.getString("sop-classes"))
131                .withLongOpt("sop-classes")
132                .create(null));
133    }
134
135    @SuppressWarnings("static-access")
136    private static void addStatusOption(Options opts) {
137        opts.addOption(OptionBuilder
138                .hasArg()
139                .withArgName("code")
140                .withDescription(rb.getString("status"))
141                .withLongOpt("status")
142                .create(null));
143    }
144
145    private static File getStorageDirectory(CommandLine cl) {
146        return cl.hasOption("ignore")
147                ? null
148                : new File(cl.getOptionValue("directory", "."));
149    }
150
151    private static void configureTransferCapability(ApplicationEntity ae,
152            CommandLine cl) throws IOException {
153        Properties p = CLIUtils.loadProperties(
154                cl.getOptionValue("sop-classes", 
155                        "resource:sop-classes.properties"),
156                null);
157        for (String cuid : p.stringPropertyNames()) {
158            String ts = p.getProperty(cuid);
159            ae.addTransferCapability(
160                    new TransferCapability(null,
161                            CLIUtils.toUID(cuid),
162                            TransferCapability.Role.SCP,
163                            CLIUtils.toUIDs(ts)));
164        }
165    }
166
167}