KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > webservices > WebServicesFactory


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.webservices;
18
19 import java.io.InputStream JavaDoc;
20 import java.net.URL JavaDoc;
21
22 import org.apache.geronimo.common.DeploymentException;
23 import org.exolab.castor.mapping.Mapping;
24 import org.exolab.castor.xml.Unmarshaller;
25 import org.exolab.castor.xml.Marshaller;
26 import org.xml.sax.InputSource JavaDoc;
27
28 public class WebServicesFactory {
29
30     private static WebServicesFactory webServicesFactory;
31
32     private final Mapping mapping;
33     private final Unmarshaller unmarshaller;
34
35     private WebServicesFactory() {
36         ClassLoader JavaDoc classLoader = WebServicesFactory.class.getClassLoader();
37         URL JavaDoc mappingUrl = classLoader.getResource("org/apache/geronimo/webservices/webservices_1_1.xml");
38
39         try {
40             mapping = new Mapping(classLoader);
41             mapping.loadMapping(mappingUrl);
42             unmarshaller = new Unmarshaller(mapping);
43         } catch (Exception JavaDoc e) {
44             throw (IllegalStateException JavaDoc)new IllegalStateException JavaDoc("Unable to initialize xml unmarshaller").initCause(e);
45         }
46     }
47
48     public static WebServicesFactory getInstance() {
49         if (webServicesFactory == null){
50             webServicesFactory = new WebServicesFactory();
51         }
52         return webServicesFactory;
53     }
54
55     public WebServices readXML(URL JavaDoc webservicesURL) throws DeploymentException {
56         InputStream JavaDoc in = null;
57         WebServices webservice = null;
58         try {
59             in = webservicesURL.openStream();
60             webservice = (WebServices) unmarshaller.unmarshal(new InputSource JavaDoc(in));
61         } catch (Exception JavaDoc e) {
62             throw new DeploymentException(e);
63         } finally {
64             if (in != null) {
65                 try {
66                     in.close();
67                 } catch(Exception JavaDoc ignored) {
68                     // Don't care
69
}
70             }
71         }
72         return webservice;
73     }
74
75 }
76
Popular Tags