You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2012/01/27 17:00:43 UTC

svn commit: r1236726 - /commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/PropertyDescriptorsRegistry.java

Author: simonetripodi
Date: Fri Jan 27 16:00:42 2012
New Revision: 1236726

URL: http://svn.apache.org/viewvc?rev=1236726&view=rev
Log:
first checkin of PropertyDescriptor registry - it will be needed to get/set properties

Added:
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/PropertyDescriptorsRegistry.java   (with props)

Added: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/PropertyDescriptorsRegistry.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/PropertyDescriptorsRegistry.java?rev=1236726&view=auto
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/PropertyDescriptorsRegistry.java (added)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/PropertyDescriptorsRegistry.java Fri Jan 27 16:00:42 2012
@@ -0,0 +1,100 @@
+package org.apache.commons.beanutils2;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import static java.beans.Introspector.getBeanInfo;
+
+import java.beans.BeanInfo;
+import java.beans.IntrospectionException;
+import java.beans.PropertyDescriptor;
+import java.lang.ref.Reference;
+import java.lang.ref.WeakReference;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.WeakHashMap;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+/**
+ *
+ */
+final class PropertyDescriptorsRegistry
+{
+
+    private static final PropertyDescriptorsRegistry INSTANCE = new PropertyDescriptorsRegistry();
+
+    public static PropertyDescriptorsRegistry getInstance()
+    {
+        return INSTANCE;
+    }
+
+    private final ReadWriteLock lock = new ReentrantReadWriteLock();
+
+    private final Map<Class<?>, WeakReference<Map<String, PropertyDescriptor>>> cache =
+                    new WeakHashMap<Class<?>, WeakReference<Map<String, PropertyDescriptor>>>();
+
+    /**
+     * This class cann not be instantiated.
+     */
+    private PropertyDescriptorsRegistry()
+    {
+        // do nothing
+    }
+
+    public PropertyDescriptor getPropertyDescriptor( Class<?> beanType, String propertyName )
+        throws IntrospectionException
+    {
+        Lock readLock = lock.readLock();
+        readLock.lock();
+        try
+        {
+            Reference<Map<String, PropertyDescriptor>> methodReference = cache.get( beanType );
+            if ( methodReference != null )
+            {
+                return methodReference.get().get( propertyName );
+            }
+        }
+        finally
+        {
+            readLock.unlock();
+        }
+
+        Lock writeLock = lock.writeLock();
+        writeLock.lock();
+        try
+        {
+            final Map<String, PropertyDescriptor> propertiesIndex = new HashMap<String, PropertyDescriptor>();
+            BeanInfo beanInfo = getBeanInfo( beanType );
+
+            for ( PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors() )
+            {
+                propertiesIndex.put( propertyDescriptor.getName(), propertyDescriptor );
+            }
+
+            cache.put( beanType, new WeakReference<Map<String, PropertyDescriptor>>( propertiesIndex ) );
+
+            return propertiesIndex.get( propertyName );
+        }
+        finally
+        {
+            writeLock.unlock();
+        }
+    }
+
+}

Propchange: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/PropertyDescriptorsRegistry.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/PropertyDescriptorsRegistry.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/PropertyDescriptorsRegistry.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain