001package jmri.jmrix.lenz.xntcp.configurexml;
002
003import jmri.jmrix.configurexml.AbstractNetworkConnectionConfigXml;
004import jmri.jmrix.lenz.xntcp.ConnectionConfig;
005import jmri.jmrix.lenz.xntcp.XnTcpAdapter;
006import org.jdom2.Element;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010import javax.annotation.Nonnull;
011
012/**
013 * Handle XML persistence of layout connections by persisting the XnTcpAdapter
014 * (and connections). Note this is named as the XML version of a
015 * ConnectionConfig object, but it's actually persisting the XnTcpAdapter.
016 * <p>
017 * This class is invoked from jmrix.JmrixConfigPaneXml on write, as that class
018 * is the one actually registered. Reads are brought here directly via the class
019 * attribute in the XML.
020 *
021 * @author Giorgio Terdina Copyright (C) 2008, based on LI100 Action by Bob
022 * Jacobsen, Copyright (C) 2003
023 */
024public class ConnectionConfigXml extends AbstractNetworkConnectionConfigXml {
025
026    public ConnectionConfigXml() {
027        super();
028    }
029
030    @Override
031    protected void getInstance() {
032        if (adapter == null) {
033            adapter = new XnTcpAdapter();
034        }
035    }
036
037    @Override
038    protected void getInstance(Object object) {
039        adapter = ((ConnectionConfig) object).getAdapter();
040    }
041
042    @Override
043    public boolean load(@Nonnull Element shared, Element perNode) {
044        boolean result = true;
045        try {
046            result = super.load(shared, perNode);
047            if (log.isDebugEnabled()) {
048                log.debug("result {}", result);
049            }
050        } catch (NullPointerException ex) {
051            // If the standard configuration fails, try the original 
052            // original configurations method for XnTcp which used a
053            // string name as the port name to designate automatic or
054            // manual configuration.  
055            // If manual was manual, option 1 contained the host name
056            // and option 2 contained the port.  We now use option 1 to 
057            // designate the manual option.
058            if (log.isDebugEnabled()) {
059                log.debug("Null Pointer Exception Occured");
060            }
061            try {
062                String manualOption = shared.getAttribute("port").getValue();
063                adapter.configureOption1(manualOption);
064            } catch (NullPointerException e1) {
065                // it is considered normal if this fails when the 
066                //attributes are not present.
067            }
068
069            try {
070                String hostName = shared.getAttribute("option1").getValue();
071                adapter.setHostName(hostName);
072            } catch (NullPointerException e1) {
073                // it is considered normal if this fails when the 
074                //attributes are not present.
075            }
076
077            try {
078                int portNumber = shared.getAttribute("option2").getIntValue();
079                adapter.setPort(portNumber);
080            } catch (org.jdom2.DataConversionException e2) {
081                log.warn("Could not parse port attribute");
082            } catch (NullPointerException e1) {
083                // it is considered normal if this fails when the 
084                //attributes are not present.
085            }
086
087            String manufacturer;
088            try {
089                manufacturer = shared.getAttribute("manufacturer").getValue();
090                adapter.setManufacturer(manufacturer);
091            } catch (NullPointerException e1) { //Considered normal if not present
092
093            }
094
095            if (adapter.getSystemConnectionMemo() != null) {
096                if (shared.getAttribute("userName") != null) {
097                    adapter.getSystemConnectionMemo().setUserName(shared.getAttribute("userName").getValue());
098                }
099
100                if (shared.getAttribute("systemPrefix") != null) {
101                    adapter.getSystemConnectionMemo().setSystemPrefix(shared.getAttribute("systemPrefix").getValue());
102                }
103            }
104
105            if (shared.getAttribute("disabled") != null) {
106                String yesno = shared.getAttribute("disabled").getValue();
107                if ((yesno != null) && (!yesno.equals(""))) {
108                    if (yesno.equals("no")) {
109                        adapter.setDisabled(false);
110                    } else if (yesno.equals("yes")) {
111                        adapter.setDisabled(true);
112                    }
113                }
114            }
115            // register, so can be picked up next time
116            register();
117
118            if (adapter.getDisabled()) {
119                unpackElement(shared, perNode);
120                return result;
121            }
122            try {
123                adapter.connect();
124            } catch (Exception e1) {
125                handleException(e1.getMessage(), "opening connection", null, null, e1);
126                return false;
127            }
128
129            // if successful so far, go ahead and configure
130            adapter.configure();
131
132            // once all the configure processing has happened, do any
133            // extra config
134            unpackElement(shared, perNode);
135
136        }
137        return result;
138    }
139
140    @Override
141    protected void register() {
142        this.register(new ConnectionConfig(adapter));
143    }
144
145    // initialize logging
146    private static final Logger log = LoggerFactory.getLogger(ConnectionConfigXml.class);
147
148}