org.glassfish.jersey.server.wadl.config.WadlGeneratorLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jaxrs-ri Show documentation
Show all versions of jaxrs-ri Show documentation
A bundle project producing JAX-RS RI bundles. The primary artifact is an "all-in-one" OSGi-fied JAX-RS RI bundle
(jaxrs-ri.jar).
Attached to that are two compressed JAX-RS RI archives. The first archive (jaxrs-ri.zip) consists of binary RI bits and
contains the API jar (under "api" directory), RI libraries (under "lib" directory) as well as all external
RI dependencies (under "ext" directory). The secondary archive (jaxrs-ri-src.zip) contains buildable JAX-RS RI source
bundle and contains the API jar (under "api" directory), RI sources (under "src" directory) as well as all external
RI dependencies (under "ext" directory). The second archive also contains "build.xml" ANT script that builds the RI
sources. To build the JAX-RS RI simply unzip the archive, cd to the created jaxrs-ri directory and invoke "ant" from
the command line.
/*
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.server.wadl.config;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.jersey.internal.inject.InjectionManager;
import org.glassfish.jersey.internal.inject.Injections;
import org.glassfish.jersey.internal.util.ReflectionHelper;
import org.glassfish.jersey.server.wadl.WadlGenerator;
import org.glassfish.jersey.server.wadl.internal.generators.WadlGeneratorJAXBGrammarGenerator;
/**
* Loads {@link WadlGenerator}s from a provided list of {@link WadlGeneratorDescription}s.
* The properties of the {@link WadlGeneratorDescription}s can refer to {@link WadlGenerator} properties
* of these types:
*
*
* - exact match: if the WadlGenerator property is of type
org.example.Foo
and the
* property value provided by the {@link WadlGeneratorDescription} is of type org.example.Foo
*
* - java.io.InputStream: The {@link InputStream} can e.g. represent a file. The stream is loaded from the
* property value (provided by the {@link WadlGeneratorDescription}) via
* {@link ClassLoader#getResourceAsStream(String)} or via OSGi API means if OSGi runtime is detected.
* The stream will be closed after {@link WadlGenerator#init()} was called.
*
*
* - Types that provide a constructor for the provided type (mostly java.lang.String)
*
* - Deprecated, will be removed in future versions from the {@link WadlGeneratorLoader}:
* java.lang.File: The property value can contain the prefix classpath: to denote, that the
* path to the file is relative to the classpath. In this case, the property value is stripped by
* the prefix classpath: and the java.lang.File is created via
* new File( generator.getClass().getResource( strippedFilename ).toURI() )
*
*
*
* @author Martin Grotzke (martin.grotzke at freiheit.com)
*/
class WadlGeneratorLoader {
private static final Logger LOGGER = Logger.getLogger(WadlGeneratorLoader.class.getName());
static WadlGenerator loadWadlGenerators(
List wadlGenerators) throws Exception {
WadlGenerator wadlGenerator = new WadlGeneratorJAXBGrammarGenerator();
if (wadlGenerators != null && !wadlGenerators.isEmpty()) {
for (WadlGenerator generator : wadlGenerators) {
generator.setWadlGeneratorDelegate(wadlGenerator);
wadlGenerator = generator;
}
}
wadlGenerator.init();
return wadlGenerator;
}
static WadlGenerator loadWadlGeneratorDescriptions(InjectionManager injectionManager,
WadlGeneratorDescription... wadlGeneratorDescriptions) throws Exception {
final List list = wadlGeneratorDescriptions != null
? Arrays.asList(wadlGeneratorDescriptions) : null;
return loadWadlGeneratorDescriptions(injectionManager, list);
}
static WadlGenerator loadWadlGeneratorDescriptions(InjectionManager injectionManager,
List wadlGeneratorDescriptions)
throws Exception {
WadlGenerator wadlGenerator = new WadlGeneratorJAXBGrammarGenerator();
final CallbackList callbacks = new CallbackList();
try {
if (wadlGeneratorDescriptions != null && !wadlGeneratorDescriptions.isEmpty()) {
for (WadlGeneratorDescription wadlGeneratorDescription : wadlGeneratorDescriptions) {
final WadlGeneratorControl control = loadWadlGenerator(injectionManager, wadlGeneratorDescription,
wadlGenerator);
wadlGenerator = control.wadlGenerator;
callbacks.add(control.callback);
}
}
wadlGenerator.init();
} finally {
callbacks.callback();
}
return wadlGenerator;
}
private static WadlGeneratorControl loadWadlGenerator(InjectionManager injectionManager,
WadlGeneratorDescription wadlGeneratorDescription,
WadlGenerator wadlGeneratorDelegate) throws Exception {
LOGGER.info("Loading wadlGenerator " + wadlGeneratorDescription.getGeneratorClass().getName());
final WadlGenerator generator = Injections.getOrCreate(injectionManager, wadlGeneratorDescription.getGeneratorClass());
generator.setWadlGeneratorDelegate(wadlGeneratorDelegate);
CallbackList callbacks = null;
if (wadlGeneratorDescription.getProperties() != null
&& !wadlGeneratorDescription.getProperties().isEmpty()) {
callbacks = new CallbackList();
final Properties wadlGeneratorProperties = wadlGeneratorDescription.getProperties();
Class osgiConfiguratorClass = wadlGeneratorDescription.getConfiguratorClass();
for (Entry