jaxws/src/java.xml.ws/share/classes/com/sun/xml/internal/ws/assembler/MetroConfigLoader.java
changeset 30994 6efa17f32dcb
parent 25871 b80b84e87032
equal deleted inserted replaced
30850:56166ce66037 30994:6efa17f32dcb
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    39 import javax.xml.bind.JAXBContext;
    39 import javax.xml.bind.JAXBContext;
    40 import javax.xml.bind.JAXBElement;
    40 import javax.xml.bind.JAXBElement;
    41 import javax.xml.bind.Unmarshaller;
    41 import javax.xml.bind.Unmarshaller;
    42 import javax.xml.stream.XMLInputFactory;
    42 import javax.xml.stream.XMLInputFactory;
    43 import javax.xml.ws.WebServiceException;
    43 import javax.xml.ws.WebServiceException;
       
    44 import java.io.IOException;
       
    45 import java.io.InputStream;
    44 import java.lang.reflect.Method;
    46 import java.lang.reflect.Method;
    45 import java.net.MalformedURLException;
    47 import java.net.MalformedURLException;
    46 import java.net.URI;
    48 import java.net.URI;
    47 import java.net.URISyntaxException;
    49 import java.net.URISyntaxException;
    48 import java.net.URL;
    50 import java.net.URL;
    62  * @author Marek Potociar <marek.potociar at sun.com>
    64  * @author Marek Potociar <marek.potociar at sun.com>
    63  */
    65  */
    64 // TODO Move the logic of this class directly into MetroConfig class.
    66 // TODO Move the logic of this class directly into MetroConfig class.
    65 class MetroConfigLoader {
    67 class MetroConfigLoader {
    66 
    68 
       
    69     private static final String JAXWS_TUBES_JDK_XML_RESOURCE = "jaxws-tubes-default.xml";
    67     private static final Logger LOGGER = Logger.getLogger(MetroConfigLoader.class);
    70     private static final Logger LOGGER = Logger.getLogger(MetroConfigLoader.class);
    68 
    71 
    69     private MetroConfigName defaultTubesConfigNames;
    72     private MetroConfigName defaultTubesConfigNames;
    70 
    73 
    71     private static interface TubeFactoryListResolver {
    74     private static interface TubeFactoryListResolver {
   120 
   123 
   121         if (defaultFileName == null) {
   124         if (defaultFileName == null) {
   122             defaultFileName = defaultTubesConfigNames.getDefaultFileName();
   125             defaultFileName = defaultTubesConfigNames.getDefaultFileName();
   123         }
   126         }
   124         this.defaultConfigUrl = locateResource(defaultFileName, loaders);
   127         this.defaultConfigUrl = locateResource(defaultFileName, loaders);
   125         if (defaultConfigUrl == null) {
   128         if (defaultConfigUrl != null) {
   126             throw LOGGER.logSevereException(new IllegalStateException(TubelineassemblyMessages.MASM_0001_DEFAULT_CFG_FILE_NOT_FOUND(defaultFileName)));
   129             LOGGER.config(TubelineassemblyMessages.MASM_0002_DEFAULT_CFG_FILE_LOCATED(defaultFileName, defaultConfigUrl));
   127         }
   130         }
   128 
   131 
   129         LOGGER.config(TubelineassemblyMessages.MASM_0002_DEFAULT_CFG_FILE_LOCATED(defaultFileName, defaultConfigUrl));
       
   130         this.defaultConfig = MetroConfigLoader.loadMetroConfig(defaultConfigUrl);
   132         this.defaultConfig = MetroConfigLoader.loadMetroConfig(defaultConfigUrl);
   131         if (defaultConfig == null) {
   133         if (defaultConfig == null) {
   132             throw LOGGER.logSevereException(new IllegalStateException(TubelineassemblyMessages.MASM_0003_DEFAULT_CFG_FILE_NOT_LOADED(defaultFileName)));
   134             throw LOGGER.logSevereException(new IllegalStateException(TubelineassemblyMessages.MASM_0003_DEFAULT_CFG_FILE_NOT_LOADED(defaultFileName)));
   133         }
   135         }
   134         if (defaultConfig.getTubelines() == null) {
   136         if (defaultConfig.getTubelines() == null) {
   233         }
   235         }
   234         return null;
   236         return null;
   235     }
   237     }
   236 
   238 
   237     private static MetroConfig loadMetroConfig(@NotNull URL resourceUrl) {
   239     private static MetroConfig loadMetroConfig(@NotNull URL resourceUrl) {
   238         MetroConfig result = null;
   240         try (InputStream is = getConfigInputStream(resourceUrl)) {
   239         try {
       
   240             JAXBContext jaxbContext = createJAXBContext();
   241             JAXBContext jaxbContext = createJAXBContext();
   241             Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
   242             Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
   242             XMLInputFactory factory = XmlUtil.newXMLInputFactory(true);
   243             XMLInputFactory factory = XmlUtil.newXMLInputFactory(true);
   243             final JAXBElement<MetroConfig> configElement = unmarshaller.unmarshal(factory.createXMLStreamReader(resourceUrl.openStream()), MetroConfig.class);
   244             JAXBElement<MetroConfig> configElement = unmarshaller.unmarshal(factory.createXMLStreamReader(is), MetroConfig.class);
   244             result = configElement.getValue();
   245             return configElement.getValue();
   245         } catch (Exception e) {
   246         } catch (Exception e) {
   246             LOGGER.warning(TubelineassemblyMessages.MASM_0010_ERROR_READING_CFG_FILE_FROM_LOCATION(resourceUrl.toString()), e);
   247             String message = TubelineassemblyMessages.MASM_0010_ERROR_READING_CFG_FILE_FROM_LOCATION(
   247         }
   248                     resourceUrl != null ? resourceUrl.toString() : null);
   248         return result;
   249             InternalError error = new InternalError(message);
       
   250             LOGGER.logException(error, e, Level.SEVERE);
       
   251             throw error;
       
   252         }
       
   253     }
       
   254 
       
   255     private static InputStream getConfigInputStream(URL resourceUrl) throws IOException {
       
   256         InputStream is;
       
   257         if (resourceUrl != null) {
       
   258             is = resourceUrl.openStream();
       
   259         } else {
       
   260             is = MetroConfigLoader.class.getResourceAsStream(JAXWS_TUBES_JDK_XML_RESOURCE);
       
   261 
       
   262             if (is == null)
       
   263                 throw LOGGER.logSevereException(
       
   264                         new IllegalStateException(
       
   265                                 TubelineassemblyMessages.MASM_0001_DEFAULT_CFG_FILE_NOT_FOUND(JAXWS_TUBES_JDK_XML_RESOURCE)));
       
   266         }
       
   267 
       
   268         return is;
   249     }
   269     }
   250 
   270 
   251     private static JAXBContext createJAXBContext() throws Exception {
   271     private static JAXBContext createJAXBContext() throws Exception {
   252         if (isJDKInternal()) {
   272         if (isJDKInternal()) {
   253             // since jdk classes are repackaged, extra privilege is necessary to create JAXBContext
   273             // since jdk classes are repackaged, extra privilege is necessary to create JAXBContext