001package jmri.jmrix.loconet; 002 003import javax.annotation.CheckForNull; 004import javax.annotation.Nonnull; 005import jmri.IdTag; 006import jmri.InstanceManager; 007import jmri.managers.configurexml.DefaultIdTagManagerXml; 008import org.slf4j.Logger; 009import org.slf4j.LoggerFactory; 010 011/** 012 * Concrete implementation for the Internal {@link jmri.IdTagManager} 013 * interface that manages TranspondingTags. 014 * 015 * @author Kevin Dickerson Copyright (C) 2012 016 * @since 2.99.4 017 */ 018public class TranspondingTagManager extends jmri.managers.DefaultIdTagManager { 019 020 public TranspondingTagManager() { 021 super(new jmri.jmrix.CaptiveSystemConnectionMemo("L", "LocoNet")); // NOI18N 022 storeInstance(); 023 } 024 025 final void storeInstance(){ 026 InstanceManager.store(this, TranspondingTagManager.class); 027 } 028 029 @Override 030 @Nonnull 031 protected TranspondingTag createNewIdTag(String systemName, String userName) throws IllegalArgumentException { 032 if (!systemName.startsWith(getSystemPrefix() + typeLetter() )) { 033 systemName = getSystemPrefix() + typeLetter() + systemName; 034 } 035 return new TranspondingTag(systemName, userName); 036 } 037 038 @Override 039 @Nonnull 040 public IdTag newIdTag(@Nonnull String systemName, @CheckForNull String userName) throws IllegalArgumentException { 041 log.debug("new IdTag:{};{}", systemName, (userName == null) ? "null" : userName); 042 // return existing if there is one 043 TranspondingTag s; 044 if (userName != null) { 045 s = (TranspondingTag)getByUserName(userName); 046 if (s != null) { 047 if (getBySystemName(systemName) != s) { 048 log.error("inconsistent user ({}) and system name ({}) results; userName related to ({})", userName, systemName, s.getSystemName()); 049 } 050 return s; 051 } 052 } 053 s = (TranspondingTag) getBySystemName(systemName); 054 if (s != null) { 055 if ((s.getUserName() == null) && (userName != null)) { 056 s.setUserName(userName); 057 } else if (userName != null) { 058 log.warn("Found IdTag via system name ({}) with non-null user name ({})", systemName, userName); 059 } 060 return s; 061 } 062 063 // doesn't exist, make a new one 064 s = createNewIdTag(systemName, userName); 065 066 // save in the maps 067 register(s); 068 069 return s; 070 } 071 072 @Override 073 public void writeIdTagDetails() throws java.io.IOException { 074 if (this.dirty) { 075 new DefaultIdTagManagerXml(this,"TranspondingIdTags.xml").store(); // NOI18N 076 this.dirty = false; 077 log.debug("...done writing IdTag details"); 078 } 079 } 080 081 @Override 082 public void readIdTagDetails() { 083 log.debug("reading idTag Details"); 084 new DefaultIdTagManagerXml(this,"TranspondingIdTags.xml").load(); // NOI18N 085 this.dirty = false; 086 log.debug("...done reading IdTag details"); 087 } 088 089 private static final Logger log = LoggerFactory.getLogger(TranspondingTagManager.class); 090 091}