Author: gnodet
Date: Mon Mar  2 17:21:52 2015
New Revision: 1663370

URL: http://svn.apache.org/r1663370
Log:
[FELIX-4812] BundleRepository can be quite CPU intensive when starting a lot of 
bundles

Added:
    
felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/LazyLocalResourceImpl.java
Modified:
    
felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/LocalRepositoryImpl.java

Added: 
felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/LazyLocalResourceImpl.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/LazyLocalResourceImpl.java?rev=1663370&view=auto
==============================================================================
--- 
felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/LazyLocalResourceImpl.java
 (added)
+++ 
felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/LazyLocalResourceImpl.java
 Mon Mar  2 17:21:52 2015
@@ -0,0 +1,120 @@
+/*
+ * 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.
+ */
+package org.apache.felix.bundlerepository.impl;
+
+import java.util.Dictionary;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.StringTokenizer;
+
+import org.apache.felix.bundlerepository.Capability;
+import org.apache.felix.bundlerepository.Requirement;
+import org.apache.felix.bundlerepository.Resource;
+import org.apache.felix.utils.log.Logger;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.Version;
+import org.osgi.framework.wiring.BundleRevision;
+
+public class LazyLocalResourceImpl implements Resource
+{
+    private final Bundle m_bundle;
+    private final Logger m_logger;
+    private volatile Resource m_resource = null;
+
+    LazyLocalResourceImpl(Bundle bundle, Logger logger)
+    {
+        m_bundle = bundle;
+        m_logger = logger;
+    }
+
+    public boolean isLocal()
+    {
+        return true;
+    }
+
+    public Bundle getBundle()
+    {
+        return m_bundle;
+    }
+
+    public String toString()
+    {
+        return m_bundle.toString();
+    }
+
+    private final Resource getResource() {
+        if (m_resource == null) {
+            synchronized (this) {
+                try {
+                    m_resource = new LocalResourceImpl(m_bundle);
+                } catch (InvalidSyntaxException ex) {
+                    // This should never happen since we are generating 
filters,
+                    // but ignore the resource if it does occur.
+                    m_logger.log(Logger.LOG_WARNING, ex.getMessage(), ex);
+                    m_resource = new ResourceImpl();
+                }
+            }
+        }
+        return m_resource;
+    }
+
+    public Map getProperties() {
+        return getResource().getProperties();
+    }
+
+    public String getId() {
+        return getResource().getId();
+    }
+
+    public String getSymbolicName() {
+        return getResource().getSymbolicName();
+    }
+
+    public Version getVersion() {
+        return getResource().getVersion();
+    }
+
+    public String getPresentationName() {
+        return getResource().getPresentationName();
+    }
+
+    public String getURI() {
+        return getResource().getURI();
+    }
+
+    public Long getSize() {
+        return getResource().getSize();
+    }
+
+    public String[] getCategories() {
+        return getResource().getCategories();
+    }
+
+    public Capability[] getCapabilities() {
+        return getResource().getCapabilities();
+    }
+
+    public Requirement[] getRequirements() {
+        return getResource().getRequirements();
+    }
+}

Modified: 
felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/LocalRepositoryImpl.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/LocalRepositoryImpl.java?rev=1663370&r1=1663369&r2=1663370&view=diff
==============================================================================
--- 
felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/LocalRepositoryImpl.java
 (original)
+++ 
felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/LocalRepositoryImpl.java
 Mon Mar  2 17:21:52 2015
@@ -26,7 +26,6 @@ import org.osgi.framework.AllServiceList
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
-import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceEvent;
 import org.osgi.framework.SynchronousBundleListener;
 import org.apache.felix.bundlerepository.*;
@@ -36,7 +35,7 @@ public class LocalRepositoryImpl impleme
     private final BundleContext m_context;
     private final Logger m_logger;
     private long m_snapshotTimeStamp = 0;
-    private Map m_localResourceList = new HashMap();
+    private Map<Long, Resource> m_localResourceList = new HashMap<Long, 
Resource>();
 
     public LocalRepositoryImpl(BundleContext context, Logger logger)
     {
@@ -51,7 +50,7 @@ public class LocalRepositoryImpl impleme
         {
             synchronized (this)
             {
-                addBundle(event.getBundle(), m_logger);
+                addBundle(event.getBundle());
                 m_snapshotTimeStamp = System.currentTimeMillis();
             }
         }
@@ -59,7 +58,7 @@ public class LocalRepositoryImpl impleme
         {
             synchronized (this)
             {
-                removeBundle(event.getBundle(), m_logger);
+                removeBundle(event.getBundle());
                 m_snapshotTimeStamp = System.currentTimeMillis();
             }
         }
@@ -73,14 +72,14 @@ public class LocalRepositoryImpl impleme
         {
             synchronized (this)
             {
-                removeBundle(bundle, m_logger);
-                addBundle(bundle, m_logger);
+                removeBundle(bundle);
+                addBundle(bundle);
                 m_snapshotTimeStamp = System.currentTimeMillis();
             }
         }
     }
 
-    private void addBundle(Bundle bundle, Logger logger)
+    private void addBundle(Bundle bundle)
     {
         /*
          * Concurrency note: This method MUST be called in a context which
@@ -93,19 +92,10 @@ public class LocalRepositoryImpl impleme
         {
             return;
         }
-        try
-        {
-            m_localResourceList.put(new Long(bundle.getBundleId()), new 
LocalResourceImpl(bundle));
-        }
-        catch (InvalidSyntaxException ex)
-        {
-            // This should never happen since we are generating filters,
-            // but ignore the resource if it does occur.
-            m_logger.log(Logger.LOG_WARNING, ex.getMessage(), ex);
-        }
+        m_localResourceList.put(bundle.getBundleId(), new 
LazyLocalResourceImpl(bundle, m_logger));
     }
 
-    private void removeBundle(Bundle bundle, Logger logger)
+    private void removeBundle(Bundle bundle)
     {
         /*
          * Concurrency note: This method MUST be called in a context which
@@ -113,7 +103,7 @@ public class LocalRepositoryImpl impleme
          * corruption.
          */
 
-        m_localResourceList.remove(new Long(bundle.getBundleId()));
+        m_localResourceList.remove(bundle.getBundleId());
     }
 
     public void dispose()
@@ -139,7 +129,7 @@ public class LocalRepositoryImpl impleme
 
     public synchronized Resource[] getResources()
     {
-        return (Resource[]) m_localResourceList.values().toArray(new 
Resource[m_localResourceList.size()]);
+        return m_localResourceList.values().toArray(new 
Resource[m_localResourceList.size()]);
     }
 
     private void initialize()
@@ -159,7 +149,7 @@ public class LocalRepositoryImpl impleme
             bundles = m_context.getBundles();
             for (int i = 0; (bundles != null) && (i < bundles.length); i++)
             {
-                addBundle(bundles[i], m_logger);
+                addBundle(bundles[i]);
             }
 
             m_snapshotTimeStamp = System.currentTimeMillis();


Reply via email to