View Javadoc
1 package com.codestreet.messageforge; 2 3 import java.util.*; 4 5 /*** 6 * Factory implementation. 7 * 8 * @author Jawaid Hakim. 9 */ 10 public class RMsgFactoryImpl implements RMsgFactory 11 { 12 /*** 13 * Get <tt>singleton</tt> instance. 14 * 15 * @return Singleton instance. 16 */ 17 public static RMsgFactoryImpl getInstance() 18 { 19 return instance_; 20 } 21 22 /*** 23 * Register message factories. 24 * 25 * @param factories 26 * Message factoies. 27 * @throws ConverterException 28 */ 29 public void setDefaultMsgFactories(RMsgFactory[] factories) 30 throws ConverterException 31 { 32 if (factories == null) 33 throw new NullPointerException("Null factories"); 34 35 for (int i = 0; i < factories.length; ++i) 36 { 37 RMsgFactory factory = factories[i]; 38 String[] msgNames = factory.getMsgNames(); 39 for (int j = 0; j < msgNames.length; ++j) 40 { 41 String msgName = msgNames[j]; 42 if (msgDefs_.containsKey(msgName)) 43 { 44 if (msgDefs_.get(msgName) != factory) 45 throw new ConverterException( 46 "Message defined in multiple factories: " 47 + msgNames[j]); 48 try 49 { 50 String qualifiedName = factory 51 .getQualifiedName(msgName); 52 Class beanCls = Class.forName(qualifiedName); 53 BeanFactory.createBeanInfo(msgName, beanCls); 54 } 55 catch (FactoryException ex) 56 { 57 throw new ConverterException(ex); 58 } 59 catch (ClassNotFoundException ex) 60 { 61 throw new ConverterException(ex); 62 } 63 } 64 else 65 { 66 msgDefs_.put(msgName, factory); 67 try 68 { 69 String qualifiedName = factory 70 .getQualifiedName(msgName); 71 Class beanCls = Class.forName(qualifiedName); 72 BeanFactory.createBeanInfo(msgName, beanCls); 73 } 74 catch (FactoryException ex) 75 { 76 throw new ConverterException(ex); 77 } 78 catch (ClassNotFoundException ex) 79 { 80 throw new ConverterException(ex); 81 } 82 } 83 } 84 String[] beanNames = factory.getBeanFieldNames(); 85 for (int j = 0; j < beanNames.length; ++j) 86 { 87 String beanName = beanNames[j]; 88 if (beanFieldDefs_.containsKey(beanName)) 89 { 90 if (beanFieldDefs_.get(beanName) != factory) 91 throw new ConverterException( 92 "Bean defined in multiple factories: " 93 + beanNames[j]); 94 } 95 else 96 { 97 beanFieldDefs_.put(beanName, factory); 98 try 99 { 100 String qualifiedName = factory 101 .getFullBeanFieldName(beanName); 102 Class beanCls = Class.forName(qualifiedName); 103 BeanFactory.createBeanInfo(beanName, beanCls); 104 } 105 catch (ClassNotFoundException ex) 106 { 107 throw new ConverterException(ex); 108 } 109 } 110 } 111 } 112 msgNames_ = new String[msgDefs_.size()]; 113 int index = 0; 114 for (Iterator iter = msgDefs_.keySet().iterator(); iter.hasNext();) 115 { 116 String key = (String) iter.next(); 117 msgNames_[index++] = key; 118 } 119 beanFieldNames_ = new String[beanFieldDefs_.size()]; 120 index = 0; 121 for (Iterator iter = beanFieldDefs_.keySet().iterator(); iter.hasNext();) 122 { 123 String key = (String) iter.next(); 124 beanFieldNames_[index++] = key; 125 } 126 } 127 128 private RMsgFactoryImpl() 129 { 130 } 131 132 public RMapMessage createMsgObject(String msgName) throws FactoryException 133 { 134 return createMsgObject(msgName, false); 135 } 136 137 public RMapMessage createMsgObject(String msgName, boolean create) 138 throws FactoryException 139 { 140 RMsgFactory factory = (RMsgFactory) msgDefs_.get(msgName); 141 if (factory == null) 142 throw new FactoryException( 143 "Message not found in registered factories: " + msgName); 144 145 return factory.createMsgObject(msgName, create); 146 } 147 148 public RMapMessage createMsgObject(Class cls) throws FactoryException 149 { 150 return createMsgObject(cls, false); 151 } 152 153 public RMapMessage createMsgObject(Class cls, boolean create) 154 throws FactoryException 155 { 156 try 157 { 158 RMapMessage msgObj = (RMapMessage) cls.newInstance(); 159 if (create) 160 ((RMsg) msgObj).createNested(); 161 return msgObj; 162 } 163 catch (IllegalAccessException ex) 164 { 165 throw new FactoryException(ex); 166 } 167 catch (InstantiationException ex) 168 { 169 throw new FactoryException(ex); 170 } 171 } 172 173 public String[] getMsgNames() 174 { 175 return msgNames_; 176 } 177 178 public String getQualifiedName(String msgName) throws FactoryException 179 { 180 RMsgFactory factory = (RMsgFactory) msgDefs_.get(msgName); 181 if (factory == null) 182 throw new FactoryException( 183 "Message not found in registered factories: " + msgName); 184 185 return factory.getQualifiedName(msgName); 186 } 187 188 public String[] getBeanFieldNames() 189 { 190 return beanFieldNames_; 191 } 192 193 public String getFullBeanFieldName(String beanField) 194 { 195 RMsgFactory fact = (RMsgFactory) beanFieldDefs_.get(beanField); 196 if (fact != null) 197 return fact.getFullBeanFieldName(beanField); 198 return null; 199 } 200 201 private static RMsgFactoryImpl instance_ = new RMsgFactoryImpl(); 202 203 private Map msgDefs_ = new HashMap(); 204 205 private Map beanFieldDefs_ = new HashMap(); 206 207 private String[] msgNames_ = new String[0]; 208 209 private String[] beanFieldNames_ = new String[0]; 210 }

This page was automatically generated by Maven