KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > components > encoding > XMLEncoderFactory


1 /*
2
3  * Copyright 2001-2004 The Apache Software Foundation.
4
5  *
6
7  * Licensed under the Apache License, Version 2.0 (the "License");
8
9  * you may not use this file except in compliance with the License.
10
11  * You may obtain a copy of the License at
12
13  *
14
15  * http://www.apache.org/licenses/LICENSE-2.0
16
17  *
18
19  * Unless required by applicable law or agreed to in writing, software
20
21  * distributed under the License is distributed on an "AS IS" BASIS,
22
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
25  * See the License for the specific language governing permissions and
26
27  * limitations under the License.
28
29  */

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 JavaDoc;
42 import java.util.HashMap JavaDoc;
43 import java.util.Map JavaDoc;
44
45
46 /**
47  * Factory for XMLEncoder
48  *
49  * @author <a HREF="mailto:jens@void.fm">Jens Schumann</a>
50  * @author <a HREF="mailto:dims@yahoo.com">Davanum Srinivas</a>
51  */

52
53 public class XMLEncoderFactory
54 {
55
56    private static Logger log = Logger.getLogger(XMLEncoderFactory.class.getName());
57
58
59    public static final String JavaDoc ENCODING_UTF_8 = "UTF-8";
60
61    public static final String JavaDoc ENCODING_UTF_16 = "UTF-16";
62
63    public static final String JavaDoc DEFAULT_ENCODING = ENCODING_UTF_8;
64
65
66    private static Map JavaDoc encoderMap = new HashMap JavaDoc();
67
68    private static final String JavaDoc 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 JavaDoc t)
85       {
86
87          String JavaDoc msg = t + JavaUtils.LS + JavaUtils.stackToString(t);
88
89          log.info(Messages.getMessage("exception01", msg));
90
91       }
92
93    }
94
95
96    /**
97     * Returns the default encoder
98     *
99     * @return
100     */

101
102    public static XMLEncoder getDefaultEncoder()
103    {
104
105       try
106       {
107
108          return getEncoder(DEFAULT_ENCODING);
109
110       }
111       catch (UnsupportedEncodingException JavaDoc e)
112       {
113
114          // as far I know J++ VMs will throw this exception
115

116          throw new IllegalStateException JavaDoc(Messages.getMessage("unsupportedDefaultEncoding00", DEFAULT_ENCODING));
117
118       }
119
120    }
121
122
123    /**
124     * Returns the requested encoder
125     *
126     * @param encoding
127     * @return
128     * @throws UnsupportedEncodingException
129     */

130
131    public static XMLEncoder getEncoder(String JavaDoc encoding) throws UnsupportedEncodingException JavaDoc
132    {
133
134       XMLEncoder encoder = (XMLEncoder)encoderMap.get(encoding.toUpperCase());
135
136       if (encoder == null)
137       {
138
139          throw new UnsupportedEncodingException JavaDoc(Messages.getMessage("unsupportedEncoding00", encoding));
140
141       }
142
143       // test local encoding so we can ensure that getBytes()
144

145       // will not fail later
146

147       "test".getBytes(encoder.getEncoding());
148
149       return encoder;
150
151    }
152
153
154    /**
155     * Look for file META-INF/services/org.jboss.axis.components.encoding.XMLEncoder
156     * <p/>
157     * in all the JARS, get the classes listed in those files and add them to
158     * <p/>
159     * encoders list if they are valid encoders.
160     * <p/>
161     * <p/>
162     * <p/>
163     * Here is how the scheme would work.
164     * <p/>
165     * <p/>
166     * <p/>
167     * A company providing a new encoder will jar up their encoder related
168     * <p/>
169     * classes in a JAR file. The following file containing the name of the new
170     * <p/>
171     * encoder class is also made part of this JAR file.
172     * <p/>
173     * <p/>
174     * <p/>
175     * META-INF/services/org.jboss.axis.components.encoding.XMLEncoder
176     * <p/>
177     * <p/>
178     * <p/>
179     * By making this JAR part of the webapp, the new encoder will be
180     * <p/>
181     * automatically discovered.
182     */

183
184    private static void loadPluggableEncoders()
185    {
186
187       ClassLoader JavaDoc 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 JavaDoc className = iter.nextResourceName();
201
202          try
203          {
204
205             Object JavaDoc 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 JavaDoc e)
218          {
219
220             String JavaDoc 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