Home | History | Annotate | Download | only in sip
      1 package javax.sip;
      2 
      3 import java.lang.reflect.Constructor;
      4 import java.util.HashMap;
      5 import java.util.Map;
      6 import java.util.Properties;
      7 import javax.sip.address.AddressFactory;
      8 import javax.sip.header.HeaderFactory;
      9 import javax.sip.message.MessageFactory;
     10 
     11 public class SipFactory {
     12     private static final String IP_ADDRESS_PROP = "javax.sip.IP_ADDRESS";
     13     private static final String STACK_NAME_PROP = "javax.sip.STACK_NAME";
     14 
     15     private static SipFactory sSipFactory = null;
     16 
     17     public static synchronized SipFactory getInstance() {
     18         if (sSipFactory == null) sSipFactory = new SipFactory();
     19         return sSipFactory;
     20     }
     21 
     22     // name-to-SipStack map; name could be IP address for backward compatibility
     23     private Map<String, SipStack> mNameSipStackMap =
     24             new HashMap<String, SipStack>();
     25 
     26     private SipFactory() {
     27     }
     28 
     29     public synchronized void resetFactory() {
     30         mNameSipStackMap.clear();
     31     }
     32 
     33     public synchronized SipStack createSipStack(Properties properties)
     34             throws PeerUnavailableException {
     35         // for backward compatibility, if IP_ADDRESS_PROP exists, use it and
     36         // ignore STACK_NAME_PROP.
     37         String name = properties.getProperty(IP_ADDRESS_PROP);
     38         if (name == null) {
     39             name = properties.getProperty(STACK_NAME_PROP);
     40             if (name == null ) {
     41                 throw new PeerUnavailableException(
     42                         STACK_NAME_PROP + " property not found");
     43             }
     44         }
     45 
     46         SipStack sipStack = mNameSipStackMap.get(name);
     47         if (sipStack == null) {
     48             String implClassName = "gov.nist."
     49                     + SipStack.class.getCanonicalName() + "Impl";
     50             try {
     51                 sipStack = Class.forName(implClassName)
     52                         .asSubclass(SipStack.class)
     53                         .getConstructor(new Class[] {Properties.class})
     54                         .newInstance(new Object[] {properties});
     55             } catch (Exception e) {
     56                 throw new PeerUnavailableException(
     57                         "Failed to initiate " + implClassName, e);
     58             }
     59             mNameSipStackMap.put(name, sipStack);
     60         }
     61         return sipStack;
     62     }
     63 
     64     public AddressFactory createAddressFactory()
     65             throws PeerUnavailableException {
     66         try {
     67             return new gov.nist.javax.sip.address.AddressFactoryImpl();
     68         } catch (Exception e) {
     69             if (e instanceof PeerUnavailableException) {
     70                 throw (PeerUnavailableException) e;
     71             } else {
     72                 throw new PeerUnavailableException(
     73                         "Failed to create AddressFactory", e);
     74             }
     75         }
     76     }
     77 
     78     public HeaderFactory createHeaderFactory() throws PeerUnavailableException {
     79         try {
     80             return new gov.nist.javax.sip.header.HeaderFactoryImpl();
     81         } catch (Exception e) {
     82             if (e instanceof PeerUnavailableException) {
     83                 throw (PeerUnavailableException) e;
     84             } else {
     85                 throw new PeerUnavailableException(
     86                         "Failed to create HeaderFactory", e);
     87             }
     88         }
     89     }
     90 
     91     public MessageFactory createMessageFactory()
     92             throws PeerUnavailableException {
     93         try {
     94             return new gov.nist.javax.sip.message.MessageFactoryImpl();
     95         } catch (Exception e) {
     96             if (e instanceof PeerUnavailableException) {
     97                 throw (PeerUnavailableException) e;
     98             } else {
     99                 throw new PeerUnavailableException(
    100                         "Failed to create MessageFactory", e);
    101             }
    102         }
    103     }
    104 }
    105