You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rave.apache.org by mf...@apache.org on 2012/08/20 18:20:22 UTC

svn commit: r1375093 - in /rave/trunk/rave-providers/rave-opensocial-provider/rave-opensocial-core/src: main/java/org/apache/rave/inject/ test/java/org/apache/rave/inject/ test/resources/

Author: mfranklin
Date: Mon Aug 20 16:20:22 2012
New Revision: 1375093

URL: http://svn.apache.org/viewvc?rev=1375093&view=rev
Log:
Fixed RAVE-763

Modified:
    rave/trunk/rave-providers/rave-opensocial-provider/rave-opensocial-core/src/main/java/org/apache/rave/inject/SpringBindingModule.java
    rave/trunk/rave-providers/rave-opensocial-provider/rave-opensocial-core/src/test/java/org/apache/rave/inject/SpringBindingModuleTest.java
    rave/trunk/rave-providers/rave-opensocial-provider/rave-opensocial-core/src/test/resources/rave-shindig-test-applicationContext.xml

Modified: rave/trunk/rave-providers/rave-opensocial-provider/rave-opensocial-core/src/main/java/org/apache/rave/inject/SpringBindingModule.java
URL: http://svn.apache.org/viewvc/rave/trunk/rave-providers/rave-opensocial-provider/rave-opensocial-core/src/main/java/org/apache/rave/inject/SpringBindingModule.java?rev=1375093&r1=1375092&r2=1375093&view=diff
==============================================================================
--- rave/trunk/rave-providers/rave-opensocial-provider/rave-opensocial-core/src/main/java/org/apache/rave/inject/SpringBindingModule.java (original)
+++ rave/trunk/rave-providers/rave-opensocial-provider/rave-opensocial-core/src/main/java/org/apache/rave/inject/SpringBindingModule.java Mon Aug 20 16:20:22 2012
@@ -27,7 +27,9 @@ import org.springframework.context.Appli
 import org.springframework.stereotype.Component;
 
 import java.lang.reflect.Proxy;
+import java.util.Arrays;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 
 /**
@@ -37,9 +39,10 @@ import java.util.Set;
 public class SpringBindingModule extends AbstractModule implements ApplicationContextAware {
 
     public static final String BASE_PACKAGE = "${shindig.spring.base-package}";
+    public static final String DELEMITER = ":";
 
     private ApplicationContext applicationContext;
-    private String basePackage;
+    private List<String> basePackages;
     private Set<Class<?>> mappedClasses;
 
     @Override
@@ -48,6 +51,21 @@ public class SpringBindingModule extends
         bindFromApplicationContext();
     }
 
+    /**
+     * Sets the package name to restrict bean binding to Guice
+     * @param value the base package to bind all beans in sub-packages to Guice
+     */
+    @Value(BASE_PACKAGE)
+    public void setBasePackage(String value) {
+        String replace = value.replace(".", "\\.");
+        this.basePackages = Arrays.asList(replace.split(DELEMITER));
+    }
+
+    @Override
+    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
+        this.applicationContext = applicationContext;
+    }
+
     private void bindFromApplicationContext() {
         for (String beanName : applicationContext.getBeanDefinitionNames()) {
             Object bean = applicationContext.getBean(beanName);
@@ -60,7 +78,7 @@ public class SpringBindingModule extends
     @SuppressWarnings("unchecked")
     private void bindInterfaces(Object bean) {
         String fullClassName = Proxy.isProxyClass(bean.getClass()) ? bean.toString() : bean.getClass().getName();
-        if (fullClassName.matches(basePackage + ".*")) {
+        if (isAddable(fullClassName)) {
             for (final Class clazz : bean.getClass().getInterfaces()) {
                 //Check to see if we have already bound a provider for this interface.  If we have,
                 //then don't attempt to bind it again as the provider will pull from the application context by type
@@ -73,18 +91,13 @@ public class SpringBindingModule extends
         }
     }
 
-    /**
-     * Sets the package name to restrict bean binding to Guice
-     * @param value the base package to bind all beans in sub-packages to Guice
-     */
-    @Value(BASE_PACKAGE)
-    public void setBasePackage(String value) {
-        this.basePackage = value.replace(".", "\\.");
-    }
-
-    @Override
-    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
-        this.applicationContext = applicationContext;
+    private boolean isAddable(String fullClassName) {
+        for(String basePackage : basePackages) {
+            if(fullClassName.matches(basePackage + ".*")) {
+                return true;
+            }
+        }
+        return false;
     }
 
 

Modified: rave/trunk/rave-providers/rave-opensocial-provider/rave-opensocial-core/src/test/java/org/apache/rave/inject/SpringBindingModuleTest.java
URL: http://svn.apache.org/viewvc/rave/trunk/rave-providers/rave-opensocial-provider/rave-opensocial-core/src/test/java/org/apache/rave/inject/SpringBindingModuleTest.java?rev=1375093&r1=1375092&r2=1375093&view=diff
==============================================================================
--- rave/trunk/rave-providers/rave-opensocial-provider/rave-opensocial-core/src/test/java/org/apache/rave/inject/SpringBindingModuleTest.java (original)
+++ rave/trunk/rave-providers/rave-opensocial-provider/rave-opensocial-core/src/test/java/org/apache/rave/inject/SpringBindingModuleTest.java Mon Aug 20 16:20:22 2012
@@ -21,12 +21,14 @@ package org.apache.rave.inject;
 
 import com.google.inject.Guice;
 import com.google.inject.Injector;
+import org.apache.log4j.spi.LoggerRepository;
 import org.apache.rave.opensocial.service.impl.DefaultPersonService;
 import org.apache.shindig.social.opensocial.spi.PersonService;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.jdbc.core.ResultSetExtractor;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
@@ -61,6 +63,17 @@ public class SpringBindingModuleTest {
         assertThat(personService1, is(sameInstance(personService2)));
     }
 
+    @Test
+    public void bindsAlternatePackage() {
+        LoggerRepository string = injector.getInstance(LoggerRepository.class);
+        assertThat(string, is(notNullValue()));
+    }
+
+    @Test(expected = com.google.inject.ConfigurationException.class)
+    public void doesNotBindOthers() {
+        ResultSetExtractor personService1 = injector.getInstance(ResultSetExtractor.class);
+    }
+
 
 
 }

Modified: rave/trunk/rave-providers/rave-opensocial-provider/rave-opensocial-core/src/test/resources/rave-shindig-test-applicationContext.xml
URL: http://svn.apache.org/viewvc/rave/trunk/rave-providers/rave-opensocial-provider/rave-opensocial-core/src/test/resources/rave-shindig-test-applicationContext.xml?rev=1375093&r1=1375092&r2=1375093&view=diff
==============================================================================
--- rave/trunk/rave-providers/rave-opensocial-provider/rave-opensocial-core/src/test/resources/rave-shindig-test-applicationContext.xml (original)
+++ rave/trunk/rave-providers/rave-opensocial-provider/rave-opensocial-core/src/test/resources/rave-shindig-test-applicationContext.xml Mon Aug 20 16:20:22 2012
@@ -19,11 +19,11 @@
 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
-       xmlns:aop="http://www.springframework.org/schema/aop"
+       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
-        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
+        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
 
     <!-- make the the rave.shindig.properties props available to autowire injectors, location of the properties can
      be overridden by setting a system property "rave-shindig.override.properties" -->
@@ -33,6 +33,23 @@
         <property name="location" value="classpath:rave.shindig.properties"/>
     </bean>
 
+    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" name="propertyInjector" autowire-candidate="false" id="propertyInjector">
+        <property name="targetObject">
+            <!-- System.getProperties() -->
+            <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
+                <property name="targetClass" value="java.lang.System" />
+                <property name="targetMethod" value="getProperties" />
+            </bean>
+        </property>
+        <property name="targetMethod" value="putAll" />
+        <property name="arguments">
+            <!-- The new Properties -->
+            <util:properties>
+                <prop key="shindig.spring.base-package">org.apache.rave:org.apache.log4j</prop>
+            </util:properties>
+        </property>
+    </bean>
+
     <!-- bean post-processor for JPA annotations -->
     <context:annotation-config/>
 
@@ -50,6 +67,12 @@
         <constructor-arg value="org.apache.rave.opensocial.repository.OpenSocialPersonRepository"/>
     </bean>
 
+    <bean id="nonRaveBean" class="org.apache.log4j.spi.NOPLoggerRepository" />
+
+    <bean id="mockSpringBean" class="org.easymock.EasyMock" factory-method="createNiceMock">
+        <constructor-arg value="org.springframework.jdbc.core.ResultSetExtractor"/>
+    </bean>
+
     <bean id="personService" class="org.apache.rave.opensocial.service.impl.DefaultPersonService">
         <constructor-arg name="repository" ref="mockPersonRepo" />
     </bean>