Advertisement
Michael-O

Update Jersey 2.7 to Spring 4.0.x and multi-context

Apr 23rd, 2014
1,713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 19.93 KB | None | 0 0
  1. Index: src/main/resources/jersey-spring-applicationContext.xml
  2. ===================================================================
  3. --- src/main/resources/jersey-spring-applicationContext.xml (revision 312)
  4. +++ src/main/resources/jersey-spring-applicationContext.xml (revision 313)
  5. @@ -1,61 +0,0 @@
  6. -<?xml version="1.0" encoding="UTF-8"?>
  7. -<!--
  8. -
  9. -    DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  10. -
  11. -    Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
  12. -
  13. -    The contents of this file are subject to the terms of either the GNU
  14. -    General Public License Version 2 only ("GPL") or the Common Development
  15. -    and Distribution License("CDDL") (collectively, the "License").  You
  16. -    may not use this file except in compliance with the License.  You can
  17. -    obtain a copy of the License at
  18. -    http://glassfish.java.net/public/CDDL+GPL_1_1.html
  19. -    or packager/legal/LICENSE.txt.  See the License for the specific
  20. -    language governing permissions and limitations under the License.
  21. -
  22. -    When distributing the software, include this License Header Notice in each
  23. -    file and include the License file at packager/legal/LICENSE.txt.
  24. -
  25. -    GPL Classpath Exception:
  26. -    Oracle designates this particular file as subject to the "Classpath"
  27. -    exception as provided by Oracle in the GPL Version 2 section of the License
  28. -    file that accompanied this code.
  29. -
  30. -    Modifications:
  31. -    If applicable, add the following below the License Header, with the fields
  32. -    enclosed by brackets [] replaced by your own identifying information:
  33. -    "Portions Copyright [year] [name of copyright owner]"
  34. -
  35. -    Contributor(s):
  36. -    If you wish your version of this file to be governed by only the CDDL or
  37. -    only the GPL Version 2, indicate your decision by adding "[Contributor]
  38. -    elects to include this software in this distribution under the [CDDL or GPL
  39. -    Version 2] license."  If you don't indicate a single choice of license, a
  40. -    recipient has the option to distribute your version of this file under
  41. -    either the CDDL, the GPL Version 2 or to extend the choice of license to
  42. -    its licensees as provided above.  However, if you add GPL Version 2 code
  43. -    and therefore, elected the GPL Version 2 license, then the option applies
  44. -    only if the new code is made subject to such option by the copyright
  45. -    holder.
  46. -
  47. --->
  48. -<beans xmlns="http://www.springframework.org/schema/beans"
  49. -       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  50. -       xmlns:context="http://www.springframework.org/schema/context"
  51. -       xsi:schemaLocation="http://www.springframework.org/schema/beans
  52. -  http://www.springframework.org/schema/beans/spring-beans.xsd
  53. -  http://www.springframework.org/schema/context
  54. -  http://www.springframework.org/schema/context/spring-context.xsd"
  55. -        >
  56. -
  57. -    <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
  58. -        <property name="scopes">
  59. -            <map>
  60. -                <entry key="request">
  61. -                    <bean class="org.springframework.web.context.request.RequestScope"/>
  62. -                </entry>
  63. -            </map>
  64. -        </property>
  65. -    </bean>
  66. -</beans>
  67. \ No newline at end of file
  68. Index: src/main/java/org/glassfish/jersey/server/spring/SpringWebApplicationInitializer.java
  69. ===================================================================
  70. --- src/main/java/org/glassfish/jersey/server/spring/SpringWebApplicationInitializer.java   (revision 312)
  71. +++ src/main/java/org/glassfish/jersey/server/spring/SpringWebApplicationInitializer.java   (revision 313)
  72. @@ -1,72 +0,0 @@
  73. -/*
  74. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  75. - *
  76. - * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
  77. - *
  78. - * The contents of this file are subject to the terms of either the GNU
  79. - * General Public License Version 2 only ("GPL") or the Common Development
  80. - * and Distribution License("CDDL") (collectively, the "License").  You
  81. - * may not use this file except in compliance with the License.  You can
  82. - * obtain a copy of the License at
  83. - * http://glassfish.java.net/public/CDDL+GPL_1_1.html
  84. - * or packager/legal/LICENSE.txt.  See the License for the specific
  85. - * language governing permissions and limitations under the License.
  86. - *
  87. - * When distributing the software, include this License Header Notice in each
  88. - * file and include the License file at packager/legal/LICENSE.txt.
  89. - *
  90. - * GPL Classpath Exception:
  91. - * Oracle designates this particular file as subject to the "Classpath"
  92. - * exception as provided by Oracle in the GPL Version 2 section of the License
  93. - * file that accompanied this code.
  94. - *
  95. - * Modifications:
  96. - * If applicable, add the following below the License Header, with the fields
  97. - * enclosed by brackets [] replaced by your own identifying information:
  98. - * "Portions Copyright [year] [name of copyright owner]"
  99. - *
  100. - * Contributor(s):
  101. - * If you wish your version of this file to be governed by only the CDDL or
  102. - * only the GPL Version 2, indicate your decision by adding "[Contributor]
  103. - * elects to include this software in this distribution under the [CDDL or GPL
  104. - * Version 2] license."  If you don't indicate a single choice of license, a
  105. - * recipient has the option to distribute your version of this file under
  106. - * either the CDDL, the GPL Version 2 or to extend the choice of license to
  107. - * its licensees as provided above.  However, if you add GPL Version 2 code
  108. - * and therefore, elected the GPL Version 2 license, then the option applies
  109. - * only if the new code is made subject to such option by the copyright
  110. - * holder.
  111. - */
  112. -package org.glassfish.jersey.server.spring;
  113. -
  114. -import java.util.logging.Logger;
  115. -
  116. -import javax.servlet.ServletContext;
  117. -import javax.servlet.ServletException;
  118. -
  119. -import org.springframework.web.WebApplicationInitializer;
  120. -
  121. -/**
  122. - * Spring WebApplicationInitializer implementation initializes Spring context by
  123. - * adding a Spring ContextLoaderListener to the ServletContext.
  124. - *
  125. - * @author Marko Asplund (marko.asplund at yahoo.com)
  126. - */
  127. -public class SpringWebApplicationInitializer implements WebApplicationInitializer {
  128. -
  129. -    private static final Logger LOGGER = Logger.getLogger(SpringWebApplicationInitializer.class.getName());
  130. -
  131. -    private static final String PAR_NAME_CTX_CONFIG_LOCATION = "contextConfigLocation";
  132. -
  133. -    @Override
  134. -    public void onStartup(ServletContext sc) throws ServletException {
  135. -        if(sc.getInitParameter(PAR_NAME_CTX_CONFIG_LOCATION) == null) {
  136. -            LOGGER.config(LocalizationMessages.REGISTERING_CTX_LOADER_LISTENER());
  137. -            sc.setInitParameter(PAR_NAME_CTX_CONFIG_LOCATION, "classpath:applicationContext.xml");
  138. -            sc.addListener("org.springframework.web.context.ContextLoaderListener");
  139. -            sc.addListener("org.springframework.web.context.request.RequestContextListener");
  140. -        } else {
  141. -            LOGGER.config(LocalizationMessages.SKIPPING_CTX_LODAER_LISTENER_REGISTRATION());
  142. -        }
  143. -    }
  144. -}
  145. Index: src/main/java/org/glassfish/jersey/server/spring/SpringComponentProvider.java
  146. ===================================================================
  147. --- src/main/java/org/glassfish/jersey/server/spring/SpringComponentProvider.java   (revision 312)
  148. +++ src/main/java/org/glassfish/jersey/server/spring/SpringComponentProvider.java   (revision 313)
  149. @@ -43,6 +43,7 @@
  150.  import java.util.logging.Level;
  151.  import java.util.logging.Logger;
  152.  
  153. +import javax.servlet.ServletConfig;
  154.  import javax.servlet.ServletContext;
  155.  
  156.  import org.glassfish.hk2.api.DynamicConfiguration;
  157. @@ -51,16 +52,18 @@
  158.  import org.glassfish.hk2.utilities.ServiceLocatorUtilities;
  159.  import org.glassfish.hk2.utilities.binding.ServiceBindingBuilder;
  160.  import org.glassfish.jersey.internal.inject.Injections;
  161. -import org.glassfish.jersey.server.ApplicationHandler;
  162.  import org.glassfish.jersey.server.spi.ComponentProvider;
  163. -
  164.  import org.jvnet.hk2.spring.bridge.api.SpringBridge;
  165.  import org.jvnet.hk2.spring.bridge.api.SpringIntoHK2Bridge;
  166. -
  167. +import org.springframework.beans.BeanUtils;
  168.  import org.springframework.context.ApplicationContext;
  169. -import org.springframework.context.support.ClassPathXmlApplicationContext;
  170. +import org.springframework.core.env.ConfigurableEnvironment;
  171.  import org.springframework.stereotype.Component;
  172. +import org.springframework.web.context.ConfigurableWebApplicationContext;
  173. +import org.springframework.web.context.ConfigurableWebEnvironment;
  174. +import org.springframework.web.context.WebApplicationContext;
  175.  import org.springframework.web.context.support.WebApplicationContextUtils;
  176. +import org.springframework.web.context.support.XmlWebApplicationContext;
  177.  
  178.  /**
  179.   * Custom ComponentProvider class.
  180. @@ -72,12 +75,9 @@
  181.  public class SpringComponentProvider implements ComponentProvider {
  182.  
  183.      private static final Logger LOGGER = Logger.getLogger(SpringComponentProvider.class.getName());
  184. -    private static final String DEFAULT_CONTEXT_CONFIG_LOCATION = "applicationContext.xml";
  185. -    private static final String PARAM_CONTEXT_CONFIG_LOCATION = "contextConfigLocation";
  186. -    private static final String PARAM_SPRING_CONTEXT = "contextConfig";
  187.  
  188.      private volatile ServiceLocator locator;
  189. -    private volatile ApplicationContext ctx;
  190. +    private volatile WebApplicationContext ctx;
  191.  
  192.      @Override
  193.      public void initialize(ServiceLocator locator) {
  194. @@ -88,18 +88,36 @@
  195.          }
  196.  
  197.          ServletContext sc = locator.getService(ServletContext.class);
  198. +        ServletConfig config = locator.getService(ServletConfig.class);
  199.  
  200. -        if(sc != null) {
  201. -            // servlet container
  202. -            ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
  203. -        } else {
  204. -            // non-servlet container
  205. -            ctx = createSpringContext();
  206. -        }
  207. -        if(ctx == null) {
  208. -            LOGGER.severe(LocalizationMessages.CTX_LOOKUP_FAILED());
  209. -            return;
  210. -        }
  211. +        WebApplicationContext rootContext = WebApplicationContextUtils
  212. +               .getRequiredWebApplicationContext(sc);
  213. +
  214. +       WebApplicationContext wac = null;
  215. +
  216. +       ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) BeanUtils
  217. +               .instantiateClass(XmlWebApplicationContext.class);
  218. +
  219. +       cwac.setParent(rootContext);
  220. +       cwac.setConfigLocation("/WEB-INF/spring/jaxrs/*.xml");
  221. +
  222. +       cwac.setId("jaxrs-context");
  223. +       cwac.setServletContext(sc);
  224. +       cwac.setServletConfig(config);
  225. +       cwac.setNamespace(config.getServletName());
  226. +
  227. +       ConfigurableEnvironment env = cwac.getEnvironment();
  228. +
  229. +       if (env instanceof ConfigurableWebEnvironment) {
  230. +           ((ConfigurableWebEnvironment) env).initPropertySources(
  231. +                   sc, config);
  232. +       }
  233. +
  234. +       cwac.refresh();
  235. +
  236. +       wac = cwac;
  237. +       this.ctx = wac;
  238. +
  239.          LOGGER.config(LocalizationMessages.CTX_LOOKUP_SUCESSFUL());
  240.  
  241.          // initialize HK2 spring-bridge
  242. @@ -146,24 +164,6 @@
  243.      public void done() {
  244.      }
  245.  
  246. -    private ApplicationContext createSpringContext() {
  247. -        ApplicationHandler applicationHandler = locator.getService(ApplicationHandler.class);
  248. -        ApplicationContext springContext = (ApplicationContext) applicationHandler.getConfiguration().getProperty(PARAM_SPRING_CONTEXT);
  249. -        if (springContext == null) {
  250. -            String contextConfigLocation = (String) applicationHandler.getConfiguration().getProperty(PARAM_CONTEXT_CONFIG_LOCATION);
  251. -            springContext = createXmlSpringConfiguration(contextConfigLocation);
  252. -        }
  253. -        return springContext;
  254. -    }
  255. -
  256. -    private ApplicationContext createXmlSpringConfiguration(String contextConfigLocation) {
  257. -        if (contextConfigLocation == null) {
  258. -            contextConfigLocation = DEFAULT_CONTEXT_CONFIG_LOCATION;
  259. -        }
  260. -        return ctx = new ClassPathXmlApplicationContext(contextConfigLocation, "jersey-spring-applicationContext.xml");
  261. -    }
  262. -
  263. -
  264.      private static class SpringManagedBeanFactory implements Factory {
  265.          private final ApplicationContext ctx;
  266.          private final ServiceLocator locator;
  267. Index: src/main/java/org/glassfish/jersey/server/spring/AutowiredInjectResolver.java
  268. ===================================================================
  269. --- src/main/java/org/glassfish/jersey/server/spring/AutowiredInjectResolver.java   (revision 312)
  270. +++ src/main/java/org/glassfish/jersey/server/spring/AutowiredInjectResolver.java   (revision 313)
  271. @@ -40,12 +40,10 @@
  272.  package org.glassfish.jersey.server.spring;
  273.  
  274.  import java.lang.reflect.AnnotatedElement;
  275. -import java.lang.reflect.ParameterizedType;
  276. -import java.lang.reflect.Type;
  277. -import java.util.ArrayList;
  278. -import java.util.Collections;
  279. +import java.lang.reflect.Constructor;
  280. +import java.lang.reflect.Field;
  281.  import java.util.HashSet;
  282. -import java.util.Map;
  283. +import java.util.Set;
  284.  import java.util.logging.Logger;
  285.  
  286.  import javax.inject.Singleton;
  287. @@ -53,15 +51,18 @@
  288.  import org.glassfish.hk2.api.Injectee;
  289.  import org.glassfish.hk2.api.InjectionResolver;
  290.  import org.glassfish.hk2.api.ServiceHandle;
  291. -
  292. +import org.springframework.beans.factory.NoSuchBeanDefinitionException;
  293.  import org.springframework.beans.factory.annotation.Autowired;
  294.  import org.springframework.beans.factory.annotation.Qualifier;
  295. +import org.springframework.beans.factory.config.DependencyDescriptor;
  296.  import org.springframework.context.ApplicationContext;
  297. +import org.springframework.core.MethodParameter;
  298.  
  299.  /**
  300.   * HK2 injection resolver for Spring framework {@link Autowired} annotation injection.
  301.   *
  302.   * @author Marko Asplund (marko.asplund at yahoo.com)
  303. + * @author Vetle Leinonen-Roeim (vetle at roeim.net)
  304.   */
  305.  @Singleton
  306.  public class AutowiredInjectResolver implements InjectionResolver<Autowired> {
  307. @@ -70,6 +71,7 @@
  308.  
  309.      /**
  310.       * Create a new instance.
  311. +     *
  312.       * @param ctx Spring application context.
  313.       */
  314.      public AutowiredInjectResolver(ApplicationContext ctx) {
  315. @@ -86,42 +88,31 @@
  316.                  beanName = an.value();
  317.              }
  318.          }
  319. -        return getBeanFromSpringContext(beanName, injectee.getRequiredType());
  320. +        return getBeanFromSpringContext(beanName, injectee);
  321.      }
  322.  
  323. -    private Object getBeanFromSpringContext(String beanName, Type beanType) {
  324. -        Class<?> bt = getClassFromType(beanType);
  325. -        if (beanName != null) {
  326. -            return ctx.getBean(beanName, bt);
  327. +    private Object getBeanFromSpringContext(String beanName, Injectee injectee) {
  328. +        try {
  329. +            DependencyDescriptor dependencyDescriptor = createSpringDependencyDescriptor(injectee);
  330. +            Set<String> autowiredBeanNames = new HashSet<>(1);
  331. +            autowiredBeanNames.add(beanName);
  332. +            return ctx.getAutowireCapableBeanFactory().resolveDependency(dependencyDescriptor, null,
  333. +                    autowiredBeanNames, null);
  334. +        } catch (NoSuchBeanDefinitionException e) {
  335. +            LOGGER.warning(e.getMessage());
  336. +            throw e;
  337.          }
  338. -        Map<String, ?> beans = ctx.getBeansOfType(bt);
  339. -        if (beans == null || beans.size() < 1) {
  340. -            LOGGER.warning(LocalizationMessages.NO_BEANS_FOUND_FOR_TYPE(beanType));
  341. -            return null;
  342. -        }
  343. -
  344. -        if (beanType instanceof ParameterizedType) {
  345. -            final Class<?> rawType = (Class<?>) ((ParameterizedType) beanType).getRawType();
  346. -            if (rawType.isAssignableFrom(ArrayList.class)) {
  347. -                return Collections.unmodifiableList(new ArrayList(beans.values()));
  348. -            } else if (rawType.isAssignableFrom(HashSet.class)) {
  349. -                return new HashSet<>(beans.values());
  350. -            }
  351. -        }
  352. -
  353. -        return beans.values().iterator().next();
  354.      }
  355.  
  356. -    private Class<?> getClassFromType(Type type) {
  357. -        if (type instanceof Class) {
  358. -            return (Class<?>) type;
  359. +    private DependencyDescriptor createSpringDependencyDescriptor(final Injectee injectee) {
  360. +        AnnotatedElement annotatedElement = injectee.getParent();
  361. +        if (annotatedElement.getClass().isAssignableFrom(Field.class)) {
  362. +            return new DependencyDescriptor((Field) annotatedElement,
  363. +                    !injectee.isOptional());
  364. +        } else {
  365. +            return new DependencyDescriptor(
  366. +                    new MethodParameter((Constructor) annotatedElement, injectee.getPosition()), !injectee.isOptional());
  367.          }
  368. -        if (type instanceof ParameterizedType) {
  369. -            ParameterizedType pt = (ParameterizedType) type;
  370. -
  371. -            return (Class<?>) pt.getActualTypeArguments()[0];
  372. -        }
  373. -        return null;
  374.      }
  375.  
  376.      @Override
  377. Index: pom.xml
  378. ===================================================================
  379. --- pom.xml (revision 312)
  380. +++ pom.xml (revision 313)
  381. @@ -49,46 +49,45 @@
  382.          <version>2.7</version>
  383.      </parent>
  384.  
  385. -    <artifactId>jersey-spring3</artifactId>
  386. -    <name>jersey-spring3</name>
  387. +    <artifactId>jersey-spring4</artifactId>
  388. +    <name>jersey-spring4</name>
  389. +    <version>2.7-dynamowerk</version>
  390.  
  391.      <packaging>jar</packaging>
  392.  
  393.      <description>
  394. -        Jersey extension module providing support for Spring 3 integration.
  395. +        Jersey extension module providing support for Spring 4 integration.
  396.      </description>
  397.  
  398.      <dependencies>
  399.          <dependency>
  400.              <groupId>org.glassfish.jersey.core</groupId>
  401.              <artifactId>jersey-server</artifactId>
  402. -            <version>${project.version}</version>
  403. +            <version>${project.parent.version}</version>
  404.          </dependency>
  405.  
  406.          <dependency>
  407.              <groupId>org.glassfish.jersey.containers</groupId>
  408.              <artifactId>jersey-container-servlet-core</artifactId>
  409. -            <version>${project.version}</version>
  410. +            <version>${project.parent.version}</version>
  411.          </dependency>
  412.  
  413.          <dependency>
  414.              <groupId>org.glassfish.jersey.test-framework.providers</groupId>
  415.              <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
  416. -            <version>${project.version}</version>
  417. +            <version>${project.parent.version}</version>
  418.              <scope>test</scope>
  419.          </dependency>
  420.  
  421.          <dependency>
  422.              <groupId>commons-logging</groupId>
  423.              <artifactId>commons-logging</artifactId>
  424. -            <version>1.1.1</version>
  425.              <scope>test</scope>
  426.          </dependency>
  427.  
  428.          <dependency>
  429.              <groupId>org.glassfish.hk2</groupId>
  430.              <artifactId>hk2</artifactId>
  431. -            <version>${hk2.version}</version>
  432.          </dependency>
  433.  
  434.          <dependency>
  435. @@ -110,13 +109,13 @@
  436.          <dependency>
  437.              <groupId>org.springframework</groupId>
  438.              <artifactId>spring-beans</artifactId>
  439. -            <version>${spring3.version}</version>
  440. +            <version>${spring4.version}</version>
  441.          </dependency>
  442.  
  443.          <dependency>
  444.              <groupId>org.springframework</groupId>
  445.              <artifactId>spring-core</artifactId>
  446. -            <version>${spring3.version}</version>
  447. +            <version>${spring4.version}</version>
  448.              <exclusions>
  449.                  <exclusion>
  450.                      <groupId>commons-logging</groupId>
  451. @@ -128,7 +127,7 @@
  452.          <dependency>
  453.              <groupId>org.springframework</groupId>
  454.              <artifactId>spring-web</artifactId>
  455. -            <version>${spring3.version}</version>
  456. +            <version>${spring4.version}</version>
  457.              <scope>compile</scope>
  458.          </dependency>
  459.  
  460. @@ -142,14 +141,14 @@
  461.          <dependency>
  462.              <groupId>org.glassfish.jersey.test-framework</groupId>
  463.              <artifactId>jersey-test-framework-core</artifactId>
  464. -            <version>${project.version}</version>
  465. +            <version>${project.parent.version}</version>
  466.              <scope>test</scope>
  467.          </dependency>
  468.  
  469.      </dependencies>
  470.  
  471.      <properties>
  472. -        <spring3.version>3.2.3.RELEASE</spring3.version>
  473. +        <spring4.version>4.0.3.RELEASE</spring4.version>
  474.      </properties>
  475.  
  476.      <build>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement