1 package net.walend.somnifugi; 2 3 import java.lang.reflect.Constructor ; 4 import java.lang.reflect.InvocationTargetException ; 5 6 import java.util.Map ; 7 import java.util.HashMap ; 8 import java.util.Hashtable ; 9 10 import javax.naming.Context ; 11 import javax.naming.NamingException ; 12 13 import javax.jms.Message ; 14 15 import net.walend.somnifugi.channel.ChannelFactory; 16 import net.walend.somnifugi.channel.FanOutFactory; 17 18 import net.walend.somnifugi.juc.SimpleFanOutFactory; 19 20 25 26 class ChannelFactoryCache 27 { 28 private static final String DEFAULTFACTORYCLASSNAMEPROP = SomniProperties.DEFAULT+"."+SomniProperties.CHANNELFACTORYCLASSNAMEPROP; 29 30 public static final ChannelFactoryCache IT = new ChannelFactoryCache(); 31 32 private Map <String ,ChannelFactory<Message >> contextNamesToChannelFactories = new HashMap <String ,ChannelFactory<Message >>(); 33 private Map <String ,FanOutFactory<Message >> contextNamesToFanOutFactories = new HashMap <String ,FanOutFactory<Message >>(); 34 35 private final Object guard = new Object (); 36 37 private ChannelFactoryCache() 38 {} 39 40 ChannelFactory<Message > getChannelFactory(String name,Context context,boolean isQueue) 41 throws SomniNamingException 42 { 43 try 44 { 45 synchronized(guard) 46 { 47 if(containsChannelFactory(name)) 48 { 49 return contextNamesToChannelFactories.get(name); 50 } 51 else 52 { 53 ChannelFactory<Message > channelFactory = createChannelFactory(name,context.getEnvironment(),isQueue); 54 55 contextNamesToChannelFactories.put(name,channelFactory); 56 SomniLogger.IT.config("Added "+name+" ChannelFactory."); 57 return channelFactory; 58 } 59 } 60 } 61 catch(NamingException ne) 62 { 63 throw new SomniNamingException(ne); 64 } 65 } 66 @SuppressWarnings ("unchecked") 67 private ChannelFactory<Message > createChannelFactory(String destName,Hashtable env,boolean isQueue) 68 throws SomniNamingException 69 { 70 String channelFactoryClassNameKey = destName+"."+SomniProperties.CHANNELFACTORYCLASSNAMEPROP; 71 72 String className = (String )env.get(channelFactoryClassNameKey); 73 if(className == null) 74 { 75 className = (String )env.get(DEFAULTFACTORYCLASSNAMEPROP); 76 } 77 if(className == null) 78 { 79 if(isQueue) 80 { 81 className = SomniProperties.DEFAULTQUEUECHANNELFACTORYCLASSNAME; 82 } 83 else 84 { 85 className = SomniProperties.DEFAULTTOPICCHANNELFACTORYCLASSNAME; 86 } 87 } 88 89 try 90 { 91 Class <ChannelFactory<Message >> channelFactoryClass = (Class <ChannelFactory<Message >>)Class.forName(className); 92 ChannelFactory<Message > factory = (ChannelFactory<Message >)channelFactoryClass.newInstance(); 93 94 return factory; 95 } 96 catch(ClassNotFoundException cnfe) 97 { 98 throw new SomniNamingException("Looking for "+className,cnfe); 99 } 100 catch(InstantiationException ie) 101 { 102 throw new SomniNamingException("Instantiating "+className,ie); 103 } 104 catch(IllegalAccessException iae) 105 { 106 throw new SomniNamingException("Instantiating "+className,iae); 107 } 108 } 109 110 private boolean containsChannelFactory(String key) 111 { 112 synchronized(guard) 113 { 114 return contextNamesToChannelFactories.containsKey(key); 115 } 116 } 117 @SuppressWarnings ("unchecked") 118 FanOutFactory<Message > getFanOutFactory(String name,Context context) 119 throws SomniNamingException,NamingException 120 { 121 synchronized(guard) 122 { 123 if(containsFanOutFactory(name)) 124 { 125 return contextNamesToFanOutFactories.get(name); 126 } 127 else 128 { 129 Hashtable <?,?> props = context.getEnvironment(); 130 131 String fanOutFactoryClassName = SimpleFanOutFactory.class.getName(); 132 133 String fanOutFactoryClassNameKey = SomniProperties.DEFAULT+"."+SomniProperties.FANOUTFACTORYCLASSNAMEPROP; 134 if(props.containsKey(fanOutFactoryClassNameKey)) 135 { 136 fanOutFactoryClassName = (String )props.get(fanOutFactoryClassNameKey); 137 } 138 139 fanOutFactoryClassNameKey = name+"."+SomniProperties.FANOUTFACTORYCLASSNAMEPROP; 140 if(props.containsKey(fanOutFactoryClassNameKey)) 141 { 142 fanOutFactoryClassName = (String )props.get(fanOutFactoryClassNameKey); 143 } 144 try 145 { 146 Class fanOutFactoryClass = Class.forName(fanOutFactoryClassName); 147 148 Constructor fanOutConstructor = fanOutFactoryClass.getConstructor(); 149 150 FanOutFactory<Message > fanOutFactory = (FanOutFactory<Message >)fanOutConstructor.newInstance(); 151 152 contextNamesToFanOutFactories.put(name,fanOutFactory); 153 SomniLogger.IT.config("Added "+name+" FanOutFactory."); 154 return fanOutFactory; 155 } 156 catch(ClassNotFoundException cnfe) 157 { 158 throw new SomniNamingException(cnfe); 159 } 160 catch(IllegalAccessException cnfe) 161 { 162 throw new SomniNamingException(cnfe); 163 } 164 catch(NoSuchMethodException nsme) 165 { 166 throw new SomniNamingException(nsme); 167 } 168 catch(InvocationTargetException ite) 169 { 170 throw new SomniNamingException(ite); 171 } 172 catch(InstantiationException ie) 173 { 174 throw new SomniNamingException(ie); 175 } 176 } 177 } 178 } 179 180 private boolean containsFanOutFactory(String name) 181 { 182 synchronized(guard) 183 { 184 return contextNamesToFanOutFactories.containsKey(name); 185 } 186 } 187 } 188 189 209 | Popular Tags |