View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  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;
20  import java.net.URL;
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;
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 classLoader = WebServicesFactory.class.getClassLoader();
37          URL 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 e) {
44              throw (IllegalStateException)new IllegalStateException("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 webservicesURL) throws DeploymentException {
56          InputStream in = null;
57          WebServices webservice = null;
58          try {
59              in = webservicesURL.openStream();
60              webservice = (WebServices) unmarshaller.unmarshal(new InputSource(in));
61          } catch (Exception e) {
62              throw new DeploymentException(e);
63          } finally {
64              if (in != null) {
65                  try {
66                      in.close();
67                  } catch(Exception ignored) {
68                      // Don't care
69                  }
70              }
71          }
72          return webservice;
73      }
74  
75  }