001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.webservices;
018    
019    import java.io.InputStream;
020    import java.net.URL;
021    
022    import org.apache.geronimo.common.DeploymentException;
023    import org.exolab.castor.mapping.Mapping;
024    import org.exolab.castor.xml.Unmarshaller;
025    import org.exolab.castor.xml.Marshaller;
026    import org.xml.sax.InputSource;
027    
028    public class WebServicesFactory {
029    
030        private static WebServicesFactory webServicesFactory;
031    
032        private final Mapping mapping;
033        private final Unmarshaller unmarshaller;
034    
035        private WebServicesFactory() {
036            ClassLoader classLoader = WebServicesFactory.class.getClassLoader();
037            URL mappingUrl = classLoader.getResource("org/apache/geronimo/webservices/webservices_1_1.xml");
038    
039            try {
040                mapping = new Mapping(classLoader);
041                mapping.loadMapping(mappingUrl);
042                unmarshaller = new Unmarshaller(mapping);
043            } catch (Exception e) {
044                throw (IllegalStateException)new IllegalStateException("Unable to initialize xml unmarshaller").initCause(e);
045            }
046        }
047    
048        public static WebServicesFactory getInstance() {
049            if (webServicesFactory == null){
050                webServicesFactory = new WebServicesFactory();
051            }
052            return webServicesFactory;
053        }
054    
055        public WebServices readXML(URL webservicesURL) throws DeploymentException {
056            InputStream in = null;
057            WebServices webservice = null;
058            try {
059                in = webservicesURL.openStream();
060                webservice = (WebServices) unmarshaller.unmarshal(new InputSource(in));
061            } catch (Exception e) {
062                throw new DeploymentException(e);
063            } finally {
064                if (in != null) {
065                    try {
066                        in.close();
067                    } catch(Exception ignored) {
068                        // Don't care
069                    }
070                }
071            }
072            return webservice;
073        }
074    
075    }