aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject svn commit: r1628505 - in /aries/trunk/jpa/jpa-blueprint-aries/src: main/java/org/apache/aries/jpa/blueprint/aries/impl/NSHandler.java test/java/org/apache/aries/jpa/blueprint/aries/test/NSHandlerTest.java
Date Tue, 30 Sep 2014 18:08:04 GMT
Author: cschneider
Date: Tue Sep 30 18:08:04 2014
New Revision: 1628505

URL: http://svn.apache.org/r1628505
Log:
ARIES-1251 Small refactoring of NSHandler and test

Modified:
    aries/trunk/jpa/jpa-blueprint-aries/src/main/java/org/apache/aries/jpa/blueprint/aries/impl/NSHandler.java
    aries/trunk/jpa/jpa-blueprint-aries/src/test/java/org/apache/aries/jpa/blueprint/aries/test/NSHandlerTest.java

Modified: aries/trunk/jpa/jpa-blueprint-aries/src/main/java/org/apache/aries/jpa/blueprint/aries/impl/NSHandler.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-blueprint-aries/src/main/java/org/apache/aries/jpa/blueprint/aries/impl/NSHandler.java?rev=1628505&r1=1628504&r2=1628505&view=diff
==============================================================================
--- aries/trunk/jpa/jpa-blueprint-aries/src/main/java/org/apache/aries/jpa/blueprint/aries/impl/NSHandler.java
(original)
+++ aries/trunk/jpa/jpa-blueprint-aries/src/main/java/org/apache/aries/jpa/blueprint/aries/impl/NSHandler.java
Tue Sep 30 18:08:04 2014
@@ -47,7 +47,6 @@ import org.apache.aries.jpa.container.co
 import org.apache.aries.jpa.container.sync.Synchronization;
 import org.apache.aries.util.nls.MessageUtil;
 import org.osgi.framework.Bundle;
-import org.osgi.framework.FrameworkUtil;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.blueprint.reflect.BeanMetadata;
@@ -59,6 +58,7 @@ import org.osgi.service.blueprint.reflec
 import org.osgi.service.blueprint.reflect.ValueMetadata;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
@@ -69,6 +69,7 @@ import org.w3c.dom.NodeList;
  * namespace handler also registers clients of managed persistence contexts with
  * the {@link GlobalPersistenceManager}.
  */
+@SuppressWarnings("rawtypes")
 public class NSHandler implements NamespaceHandler {
     private static final String ATTR_INDEX = "index";
 
@@ -131,39 +132,8 @@ public class NSHandler implements Namesp
      */
     public ComponentMetadata decorate(Node node, ComponentMetadata component,
             ParserContext context) {
-        // The node should always be an element
-        if (node.getNodeType() != Node.ELEMENT_NODE) {
-            _logger.error(MESSAGES.getMessage("unexpected.node", node));
-            throw new IllegalArgumentException(node.toString());
-        }
-
-        Element element = (Element) node;
-        // The surrounding component should always be a bean
-        if (!(component instanceof BeanMetadata)) {
-            _logger.error(MESSAGES.getMessage("incorrect.component.type", component));
-            throw new IllegalArgumentException(component.toString());
-        }
-        
-        if (!(component instanceof MutableBeanMetadata)) {
-            _logger.error(MESSAGES.getMessage("non.mutable.bean", component));
-            throw new IllegalArgumentException(component.toString());
-        }
-
-        MutableBeanMetadata bean = (MutableBeanMetadata) component;
-
-        if (!NS_URI_100.equals(element.getNamespaceURI())
-            && !NS_URI_110.equals(element.getNamespaceURI())) {
-            String message = MESSAGES.getMessage("unexpected.namespace", element.getNamespaceURI());
-            _logger.error(message);
-            throw new IllegalArgumentException(message);
-        }
-
-        if (!TAG_UNIT.equals(element.getLocalName())
-                && !TAG_CONTEXT.equals(element.getLocalName())) {
-            String message = MESSAGES.getMessage("unexpected.element", element.getLocalName());
-            _logger.error(message);
-            throw new IllegalArgumentException(message);
-        }
+        Element element = getValidNode(node, component);
+        MutableBeanMetadata bean = getValidBean(component, element);
 
         String property = element.getAttribute(ATTR_PROPERTY);
         property = property.isEmpty() ? null : property;
@@ -246,28 +216,7 @@ public class NSHandler implements Namesp
                     Collection<ServiceReference<Synchronization>> refs = client.getBundleContext().getServiceReferences(Synchronization.class,
"(" + PersistenceUnitConstants.OSGI_UNIT_NAME + "=" + unitName + ")");
                     if (refs.size() > 0) {
                         final Synchronization sync = client.getBundleContext().getService(refs.iterator().next());
-                        context.getComponentDefinitionRegistry().registerInterceptorWithComponent(
-                                component,
-                                new Interceptor() {
-                                    @Override
-                                    public Object preCall(ComponentMetadata componentMetadata,
Method method, Object... objects) throws Throwable {
-                                        sync.preCall();
-                                        return null;
-                                    }
-                                    @Override
-                                    public void postCallWithReturn(ComponentMetadata componentMetadata,
Method method, Object o, Object o2) throws Throwable {
-                                        sync.postCall();
-                                    }
-                                    @Override
-                                    public void postCallWithException(ComponentMetadata componentMetadata,
Method method, Throwable throwable, Object o) throws Throwable {
-                                        sync.postCall();
-                                    }
-                                    @Override
-                                    public int getRank() {
-                                        return 0;
-                                    }
-                                }
-                        );
+                        context.getComponentDefinitionRegistry().registerInterceptorWithComponent(component,
createSyncInterceptor(sync));
                         foundSync = true;
                     }
                 } catch (InvalidSyntaxException e) {
@@ -284,7 +233,68 @@ public class NSHandler implements Namesp
         return bean;
     }
 
-    @SuppressWarnings("unchecked")
+    private MutableBeanMetadata getValidBean(ComponentMetadata component, Element element)
{
+        MutableBeanMetadata bean = (MutableBeanMetadata) component;
+
+        if (!NS_URI_100.equals(element.getNamespaceURI())
+            && !NS_URI_110.equals(element.getNamespaceURI())) {
+            String message = MESSAGES.getMessage("unexpected.namespace", element.getNamespaceURI());
+            _logger.error(message);
+            throw new IllegalArgumentException(message);
+        }
+
+        if (!TAG_UNIT.equals(element.getLocalName())
+                && !TAG_CONTEXT.equals(element.getLocalName())) {
+            String message = MESSAGES.getMessage("unexpected.element", element.getLocalName());
+            _logger.error(message);
+            throw new IllegalArgumentException(message);
+        }
+        return bean;
+    }
+
+    private Element getValidNode(Node node, ComponentMetadata component) {
+        // The node should always be an element
+        if (node.getNodeType() != Node.ELEMENT_NODE) {
+            _logger.error(MESSAGES.getMessage("unexpected.node", node));
+            throw new IllegalArgumentException(node.toString());
+        }
+
+        Element element = (Element) node;
+        // The surrounding component should always be a bean
+        if (!(component instanceof BeanMetadata)) {
+            _logger.error(MESSAGES.getMessage("incorrect.component.type", component));
+            throw new IllegalArgumentException(component.toString());
+        }
+        
+        if (!(component instanceof MutableBeanMetadata)) {
+            _logger.error(MESSAGES.getMessage("non.mutable.bean", component));
+            throw new IllegalArgumentException(component.toString());
+        }
+        return element;
+    }
+
+    private Interceptor createSyncInterceptor(final Synchronization sync) {
+        return new Interceptor() {
+            @Override
+            public Object preCall(ComponentMetadata componentMetadata, Method method, Object...
objects) throws Throwable {
+                sync.preCall();
+                return null;
+            }
+            @Override
+            public void postCallWithReturn(ComponentMetadata componentMetadata, Method method,
Object o, Object o2) throws Throwable {
+                sync.postCall();
+            }
+            @Override
+            public void postCallWithException(ComponentMetadata componentMetadata, Method
method, Throwable throwable, Object o) throws Throwable {
+                sync.postCall();
+            }
+            @Override
+            public int getRank() {
+                return 0;
+            }
+        };
+    }
+
     public Set<Class> getManagedClasses() {
         // This is a no-op
         return null;
@@ -328,6 +338,7 @@ public class NSHandler implements Namesp
         _logger.warn(MESSAGES.getMessage("jpa.support.gone"));
     }
     
+    @SuppressWarnings("unchecked")
     private ComponentMetadata createTargetMetadata(boolean isPersistenceUnit,
         ParserContext ctx, String unitName) {
       // Create a service reference for the EMF (it is an EMF for persistence

Modified: aries/trunk/jpa/jpa-blueprint-aries/src/test/java/org/apache/aries/jpa/blueprint/aries/test/NSHandlerTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-blueprint-aries/src/test/java/org/apache/aries/jpa/blueprint/aries/test/NSHandlerTest.java?rev=1628505&r1=1628504&r2=1628505&view=diff
==============================================================================
--- aries/trunk/jpa/jpa-blueprint-aries/src/test/java/org/apache/aries/jpa/blueprint/aries/test/NSHandlerTest.java
(original)
+++ aries/trunk/jpa/jpa-blueprint-aries/src/test/java/org/apache/aries/jpa/blueprint/aries/test/NSHandlerTest.java
Tue Sep 30 18:08:04 2014
@@ -261,10 +261,7 @@ public class NSHandlerTest {
     Element e = getTestElement("context", root);
     BeanMetadata bean = 
       (BeanMetadata) sut.decorate(e, new BeanMetadataImpl(), parserCtx);
-    BeanMetadata innerBean = (BeanMetadata) ((BeanProperty) bean.getProperties().get(0)).getValue();
-
-    assertEquals("createEntityManager", innerBean.getFactoryMethod());
-    assertEquals("internalClose", innerBean.getDestroyMethod());
+    assertInnerBeanCorrect(bean);
 
     assertEquals(1, registeredComponents.size());
     ReferenceMetadata reference = (ReferenceMetadata) registeredComponents.get(0);
@@ -277,16 +274,20 @@ public class NSHandlerTest {
     Skeleton.getSkeleton(manager).assertCalled(
         new MethodCall(PersistenceContextProvider.class, "registerContext", "myUnit", clientBundle,
props));
   }
+
+private void assertInnerBeanCorrect(BeanMetadata bean) {
+    BeanMetadata innerBean = (BeanMetadata) ((BeanProperty) bean.getProperties().get(0)).getValue();
+
+    assertEquals("createEntityManager", innerBean.getFactoryMethod());
+    assertEquals("internalClose", innerBean.getDestroyMethod());
+}
   
   @Test
   public void testDefaultContext_110() {
     Element e = getTestElement("context", root_110);
     BeanMetadata bean = 
       (BeanMetadata) sut.decorate(e, new BeanMetadataImpl(), parserCtx);
-    BeanMetadata innerBean = (BeanMetadata) ((BeanProperty) bean.getProperties().get(0)).getValue();
-
-    assertEquals("createEntityManager", innerBean.getFactoryMethod());
-    assertEquals("internalClose", innerBean.getDestroyMethod());
+    assertInnerBeanCorrect(bean);
 
     assertEquals(1, registeredComponents.size());
     ReferenceMetadata reference = (ReferenceMetadata) registeredComponents.get(0);
@@ -307,10 +308,7 @@ public class NSHandlerTest {
     Element e = getTestElement("context", root);
     BeanMetadata bean = 
       (BeanMetadata) sut.decorate(e, new BeanMetadataImpl(), parserCtx);
-    BeanMetadata innerBean = (BeanMetadata) ((BeanProperty) bean.getProperties().get(0)).getValue();
-
-    assertEquals("createEntityManager", innerBean.getFactoryMethod());
-    assertEquals("internalClose", innerBean.getDestroyMethod());
+    assertInnerBeanCorrect(bean);
 
     assertEquals(1, registeredComponents.size());
     ReferenceMetadata reference = (ReferenceMetadata) registeredComponents.get(0);
@@ -331,10 +329,7 @@ public class NSHandlerTest {
     Element e = getTestElement("context", root_110);
     BeanMetadata bean = 
       (BeanMetadata) sut.decorate(e, new BeanMetadataImpl(), parserCtx);
-    BeanMetadata innerBean = (BeanMetadata) ((BeanProperty) bean.getProperties().get(0)).getValue();
-
-    assertEquals("createEntityManager", innerBean.getFactoryMethod());
-    assertEquals("internalClose", innerBean.getDestroyMethod());
+    assertInnerBeanCorrect(bean);
 
     assertEquals(1, registeredComponents.size());
     ReferenceMetadata reference = (ReferenceMetadata) registeredComponents.get(0);
@@ -442,10 +437,7 @@ public class NSHandlerTest {
       assertEquals("Wrong index", -1,
           ((BeanArgument)input.getArguments().get(0)).getIndex());
       
-      BeanMetadata innerBean = (BeanMetadata) ((BeanArgument)input.getArguments().get(0)).getValue();
-
-      assertEquals("createEntityManager", innerBean.getFactoryMethod());
-      assertEquals("internalClose", innerBean.getDestroyMethod());
+      assertConstructorInnerBean(input);
 
       assertEquals(1, registeredComponents.size());
       reference = (ReferenceMetadata) registeredComponents.get(0);
@@ -458,6 +450,13 @@ public class NSHandlerTest {
       Skeleton.getSkeleton(manager).assertCalled(
           new MethodCall(PersistenceContextProvider.class, "registerContext", "myUnit", clientBundle,
Map.class));
   }
+
+private void assertConstructorInnerBean(BeanMetadata input) {
+    BeanMetadata innerBean = (BeanMetadata) ((BeanArgument)input.getArguments().get(0)).getValue();
+
+      assertEquals("createEntityManager", innerBean.getFactoryMethod());
+      assertEquals("internalClose", innerBean.getDestroyMethod());
+}
   
   @Test
   public void testIndexedArgs_110() {
@@ -489,10 +488,7 @@ public class NSHandlerTest {
       assertEquals("Wrong index", 1,
           ((BeanArgument)input.getArguments().get(0)).getIndex());
       
-      BeanMetadata innerBean = (BeanMetadata) ((BeanArgument)input.getArguments().get(0)).getValue();
-
-      assertEquals("createEntityManager", innerBean.getFactoryMethod());
-      assertEquals("internalClose", innerBean.getDestroyMethod());
+      assertConstructorInnerBean(input);
 
       assertEquals(1, registeredComponents.size());
       reference = (ReferenceMetadata) registeredComponents.get(0);



Mime
View raw message