1 30 31 package org.jboss.axis.components.encoding; 32 33 34 import org.apache.commons.discovery.ResourceNameIterator; 35 import org.apache.commons.discovery.resource.ClassLoaders; 36 import org.apache.commons.discovery.resource.names.DiscoverServiceNames; 37 import org.jboss.axis.utils.JavaUtils; 38 import org.jboss.axis.utils.Messages; 39 import org.jboss.logging.Logger; 40 41 import java.io.UnsupportedEncodingException ; 42 import java.util.HashMap ; 43 import java.util.Map ; 44 45 46 52 53 public class XMLEncoderFactory 54 { 55 56 private static Logger log = Logger.getLogger(XMLEncoderFactory.class.getName()); 57 58 59 public static final String ENCODING_UTF_8 = "UTF-8"; 60 61 public static final String ENCODING_UTF_16 = "UTF-16"; 62 63 public static final String DEFAULT_ENCODING = ENCODING_UTF_8; 64 65 66 private static Map encoderMap = new HashMap (); 67 68 private static final String PLUGABLE_PROVIDER_FILENAME = "org.jboss.axis.components.encoding.XMLEncoder"; 69 70 71 static 72 { 73 74 encoderMap.put(ENCODING_UTF_8, new UTF8Encoder()); 75 76 encoderMap.put(ENCODING_UTF_16, new UTF16Encoder()); 77 78 try 79 { 80 81 loadPluggableEncoders(); 82 83 } 84 catch (Throwable t) 85 { 86 87 String msg = t + JavaUtils.LS + JavaUtils.stackToString(t); 88 89 log.info(Messages.getMessage("exception01", msg)); 90 91 } 92 93 } 94 95 96 101 102 public static XMLEncoder getDefaultEncoder() 103 { 104 105 try 106 { 107 108 return getEncoder(DEFAULT_ENCODING); 109 110 } 111 catch (UnsupportedEncodingException e) 112 { 113 114 116 throw new IllegalStateException (Messages.getMessage("unsupportedDefaultEncoding00", DEFAULT_ENCODING)); 117 118 } 119 120 } 121 122 123 130 131 public static XMLEncoder getEncoder(String encoding) throws UnsupportedEncodingException 132 { 133 134 XMLEncoder encoder = (XMLEncoder)encoderMap.get(encoding.toUpperCase()); 135 136 if (encoder == null) 137 { 138 139 throw new UnsupportedEncodingException (Messages.getMessage("unsupportedEncoding00", encoding)); 140 141 } 142 143 145 147 "test".getBytes(encoder.getEncoding()); 148 149 return encoder; 150 151 } 152 153 154 183 184 private static void loadPluggableEncoders() 185 { 186 187 ClassLoader clzLoader = XMLEncoder.class.getClassLoader(); 188 189 ClassLoaders loaders = new ClassLoaders(); 190 191 loaders.put(clzLoader); 192 193 DiscoverServiceNames dsn = new DiscoverServiceNames(loaders); 194 195 ResourceNameIterator iter = dsn.findResourceNames(PLUGABLE_PROVIDER_FILENAME); 196 197 while (iter.hasNext()) 198 { 199 200 String className = iter.nextResourceName(); 201 202 try 203 { 204 205 Object o = Class.forName(className).newInstance(); 206 207 if (o instanceof XMLEncoder) 208 { 209 210 XMLEncoder encoder = (XMLEncoder)o; 211 212 encoderMap.put(encoder.getEncoding(), encoder); 213 214 } 215 216 } 217 catch (Exception e) 218 { 219 220 String msg = e + JavaUtils.LS + JavaUtils.stackToString(e); 221 222 log.info(Messages.getMessage("exception01", msg)); 223 224 continue; 225 226 } 227 228 } 229 230 } 231 232 } 233 234 | Popular Tags |