From 89f728384cd9941fbc045e01b5f4cb2758cc1aea Mon Sep 17 00:00:00 2001 From: Lyubomir Marinov Date: Sat, 13 Dec 2008 11:32:24 +0000 Subject: [PATCH] Minor elimination of code duplication. --- .../ConfigurationServiceImpl.java | 21 ++++ .../impl/protocol/sip/SipStackSharing.java | 98 +++++-------------- .../configuration/ConfigurationService.java | 2 + 3 files changed, 46 insertions(+), 75 deletions(-) diff --git a/src/net/java/sip/communicator/impl/configuration/ConfigurationServiceImpl.java b/src/net/java/sip/communicator/impl/configuration/ConfigurationServiceImpl.java index 07f31c44c..ee46df178 100644 --- a/src/net/java/sip/communicator/impl/configuration/ConfigurationServiceImpl.java +++ b/src/net/java/sip/communicator/impl/configuration/ConfigurationServiceImpl.java @@ -1071,6 +1071,27 @@ public boolean getBoolean(String propertyName, boolean defaultValue) .parseBoolean(stringValue); } + public int getInt(String propertyName, int defaultValue) + { + String stringValue = getString(propertyName); + int intValue = defaultValue; + + if (stringValue != null) + { + try + { + intValue = Integer.parseInt(stringValue); + } + catch (NumberFormatException ex) + { + logger.error(propertyName + + " does not appear to be an integer. " + "Defaulting to " + + defaultValue + ".", ex); + } + } + return intValue; + } + /** * We use property references when we'd like to store system properties. * Simply storing System properties in our properties Map would not be diff --git a/src/net/java/sip/communicator/impl/protocol/sip/SipStackSharing.java b/src/net/java/sip/communicator/impl/protocol/sip/SipStackSharing.java index 47febcc63..51a5d09b4 100644 --- a/src/net/java/sip/communicator/impl/protocol/sip/SipStackSharing.java +++ b/src/net/java/sip/communicator/impl/protocol/sip/SipStackSharing.java @@ -7,7 +7,9 @@ package net.java.sip.communicator.impl.protocol.sip; import gov.nist.javax.sip.stack.*; + import java.util.*; + import javax.sip.*; import javax.sip.address.*; import javax.sip.header.*; @@ -43,7 +45,7 @@ public class SipStackSharing /** * Our SIP stack (provided by JAIN-SIP) */ - private SipStack stack = null; + private final SipStack stack; /** * The JAIN-SIP provider that we use for clear UDP/TCP. @@ -59,7 +61,7 @@ public class SipStackSharing * the listeners to choose from when dispatching * messages from the SipProvider-s */ - private Set listeners + private final Set listeners = new HashSet(); /** @@ -139,8 +141,10 @@ private void removeSipListener(ProtocolProviderServiceSipImpl listener) synchronized(this.listeners) { this.listeners.remove(listener); - logger.trace(this.listeners.size() + " listeners left"); - if(this.listeners.size() == 0) + + int listenerCount = listeners.size(); + logger.trace(listenerCount + " listeners left"); + if(listenerCount == 0) stopListening(); } } @@ -209,10 +213,12 @@ private void startListening() { try { + int bindRetriesValue = getBindRetriesValue(); + this.createProvider(this.getPreferredClearPort() - , this.getBindRetriesValue(), false); + , bindRetriesValue, false); this.createProvider(this.getPreferredSecurePort() - , this.getBindRetriesValue(), true); + , bindRetriesValue, true); this.stack.start(); logger.trace("started listening"); } @@ -385,28 +391,8 @@ else if(transport.equalsIgnoreCase(ListeningPoint.TLS)) */ private int getPreferredClearPort() { - String clearPortStr - = SipActivator.getConfigurationService().getString( - PREFERRED_CLEAR_PORT_PROPERTY_NAME); - - int clearPort = ListeningPoint.PORT_5060; - - if (clearPortStr != null) - { - try - { - clearPort = Integer.parseInt(clearPortStr); - } - catch (NumberFormatException ex) - { - logger.error(clearPortStr - + " does not appear to be an integer. " - + "Defaulting port bind retries to " - + clearPort + ".", ex); - } - } - - return clearPort; + return SipActivator.getConfigurationService().getInt( + PREFERRED_CLEAR_PORT_PROPERTY_NAME, ListeningPoint.PORT_5060); } /** @@ -415,28 +401,8 @@ private int getPreferredClearPort() */ private int getPreferredSecurePort() { - String securePortStr - = SipActivator.getConfigurationService().getString( - PREFERRED_SECURE_PORT_PROPERTY_NAME); - - int securePort = ListeningPoint.PORT_5061; - - if (securePortStr != null) - { - try - { - securePort = Integer.parseInt(securePortStr); - } - catch (NumberFormatException ex) - { - logger.error(securePortStr - + " does not appear to be an integer. " - + "Defaulting port bind retries to " - + securePort + ".", ex); - } - } - - return securePort; + return SipActivator.getConfigurationService().getInt( + PREFERRED_SECURE_PORT_PROPERTY_NAME, ListeningPoint.PORT_5061); } /** @@ -446,28 +412,9 @@ private int getPreferredSecurePort() */ private int getBindRetriesValue() { - String bindRetriesStr - = SipActivator.getConfigurationService().getString( - ProtocolProviderService.BIND_RETRIES_PROPERTY_NAME); - - int bindRetries = ProtocolProviderService.BIND_RETRIES_DEFAULT_VALUE; - - if (bindRetriesStr != null) - { - try - { - bindRetries = Integer.parseInt(bindRetriesStr); - } - catch (NumberFormatException ex) - { - logger.error(bindRetriesStr - + " does not appear to be an integer. " - + "Defaulting port bind retries to " - + bindRetries + ".", ex); - } - } - - return bindRetries; + return SipActivator.getConfigurationService().getInt( + ProtocolProviderService.BIND_RETRIES_PROPERTY_NAME, + ProtocolProviderService.BIND_RETRIES_DEFAULT_VALUE); } /** @@ -655,9 +602,11 @@ private ProtocolProviderServiceSipImpl findTargetFor(Request request) // every other case is approximation if(candidates.size() == 1) { + ProtocolProviderServiceSipImpl perfectMatch = candidates.get(0); + logger.trace("(0) will dispatch to: " - + candidates.get(0).getAccountID()); - return candidates.get(0); + + perfectMatch.getAccountID()); + return perfectMatch; } // past this point, our guess is not reliable @@ -909,4 +858,3 @@ private ProtocolProviderServiceSipImpl getListenerFor(Address localParty, return null; } } - diff --git a/src/net/java/sip/communicator/service/configuration/ConfigurationService.java b/src/net/java/sip/communicator/service/configuration/ConfigurationService.java index 6b6f5ad80..6ded4de8b 100644 --- a/src/net/java/sip/communicator/service/configuration/ConfigurationService.java +++ b/src/net/java/sip/communicator/service/configuration/ConfigurationService.java @@ -150,6 +150,8 @@ public List getPropertyNamesByPrefix(String prefix, boolean getBoolean(String propertyName, boolean defaultValue); + int getInt(String propertyName, int defaultValue); + /** * Adds a PropertyChangeListener to the listener list. The listener is * registered for all properties in the current configuration.