001package jmri.managers; 002 003import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 004import javax.annotation.CheckForNull; 005import javax.annotation.Nonnull; 006import jmri.IdTag; 007import jmri.InstanceManager; 008import jmri.RailCom; 009import jmri.RailComManager; 010import jmri.implementation.DefaultRailCom; 011import jmri.managers.configurexml.DefaultIdTagManagerXml; 012import org.slf4j.Logger; 013import org.slf4j.LoggerFactory; 014 015/** 016 * Concrete implementation for the Internal {@link jmri.RailComManager} 017 * interface. 018 * 019 * @author Kevin Dickerson Copyright (C) 2012 020 * @since 2.99.4 021 */ 022public class DefaultRailComManager extends DefaultIdTagManager 023 implements RailComManager { 024 025 public DefaultRailComManager() { 026 super(new jmri.jmrix.CaptiveSystemConnectionMemo("R", "RailCom")); // NOI18N 027 setInstances(); 028 } 029 030 final void setInstances() { 031 InstanceManager.store(this, RailComManager.class); 032 InstanceManager.setIdTagManager(this); 033 } 034 035 @Override 036 protected RailCom createNewIdTag(@Nonnull String systemName, String userName) { 037 // we've decided to enforce that IdTag system 038 // names start with RD by prepending if not present 039 if (!systemName.startsWith(getSystemPrefix() + "D")) { 040 systemName = getSystemPrefix() + "D" + systemName; 041 } 042 return new DefaultRailCom(systemName, userName); 043 } 044 045 @SuppressFBWarnings(value="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE", justification="defensive programming check of @Nonnull argument") 046 private void checkSystemName(@Nonnull String systemName, @CheckForNull String userName) { 047 if (systemName == null) { 048 log.error("SystemName cannot be null. UserName was {}", 049 (userName == null ? "null" : userName)); 050 throw new IllegalArgumentException("SystemName cannot be null. UserName was " 051 + ((userName == null) ? "null" : userName)); 052 } 053 } 054 055 /** 056 * Provide by userName, then SystemName, else create new. 057 * {@inheritDoc} 058 */ 059 @Override 060 @Nonnull 061 public IdTag newIdTag(@Nonnull String systemName, @CheckForNull String userName) throws IllegalArgumentException { 062 log.debug("new IdTag: {};{}", systemName, (userName == null ? "null" : userName)); 063 checkSystemName(systemName, userName); 064 065 // return existing if there is one 066 RailCom s; 067 if ((userName != null) && ((s = (RailCom)getByUserName(userName)) != null)) { 068 if (getBySystemName(systemName) != s) { 069 log.error("inconsistent user ({}) and system name ({}) results; userName related to ({})", 070 userName, systemName, s.getSystemName()); 071 } 072 return s; 073 } 074 if ((s = (RailCom) getBySystemName(systemName)) != null) { 075 if ((s.getUserName() == null) && (userName != null)) { 076 s.setUserName(userName); 077 } else if (userName != null) { 078 log.warn("Found IdTag via system name ({}) with non-null user name ({})", systemName, userName); 079 } 080 return s; 081 } 082 083 // doesn't exist, make a new one 084 s = createNewIdTag(systemName, userName); 085 // save in the maps 086 register(s); 087 088 // if that failed, blame it on the input arguments 089 if (s == null) { 090 throw new IllegalArgumentException(); 091 } 092 093 return s; 094 } 095 096 @Override 097 public void writeIdTagDetails() throws java.io.IOException { 098 if (this.dirty) { 099 new DefaultIdTagManagerXml(this,"RailComIdTags.xml").store(); // NOI18N 100 this.dirty = false; 101 log.debug("...done writing IdTag details"); 102 } 103 } 104 105 @Override 106 public void readIdTagDetails() { 107 log.debug("reading idTag Details"); 108 new DefaultIdTagManagerXml(this,"RailComIdTags.xml").load(); // NOI18N 109 this.dirty = false; 110 log.debug("...done reading IdTag details"); 111 } 112 113 private static final Logger log = LoggerFactory.getLogger(DefaultRailComManager.class); 114 115}