Improved the logging option allowing to log bad performing service lookups (filters possible eligible for indexing).

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1522614 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/index/BundleContextInterceptor.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/index/BundleContextInterceptor.java
index 62dc4e2..9224898 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/index/BundleContextInterceptor.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/index/BundleContextInterceptor.java
@@ -21,7 +21,6 @@
 import java.util.Arrays;
 import java.util.Iterator;
 import java.util.List;
-import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.felix.dm.FilterIndex;
 import org.apache.felix.dm.impl.Logger;
@@ -36,16 +35,19 @@
  * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
  */
 public class BundleContextInterceptor extends BundleContextInterceptorBase {
-	private static final String INDEX_LOG_BAD_PERFORMING_FILTERS = "org.apache.felix.dependencymanager.index.logbadperformingfilters";
-	private static long maxLookupTime = 0L;
+	protected static final String INDEX_LOG_TRESHOLD = "org.apache.felix.dm.index.log.treshold";
     private final ServiceRegistryCache m_cache;
-    private final boolean perfmon = "true".equals(System.getProperty(INDEX_LOG_BAD_PERFORMING_FILTERS));
-	private final Logger m_logger;
+    private final boolean m_perfmon = System.getProperty(INDEX_LOG_TRESHOLD) != null;
+	private Logger m_logger;
+	private long m_treshold;
 
-    public BundleContextInterceptor(ServiceRegistryCache cache, BundleContext context, Logger logger) {
+    public BundleContextInterceptor(ServiceRegistryCache cache, BundleContext context) {
         super(context);
         m_cache = cache;
-		m_logger = logger;
+		if (m_perfmon) {
+			m_treshold = Long.parseLong(System.getProperty(INDEX_LOG_TRESHOLD));
+			m_logger = new Logger(context);
+		}
     }
 
     public void addServiceListener(ServiceListener listener, String filter) throws InvalidSyntaxException {
@@ -82,7 +84,7 @@
 
     public ServiceReference[] getServiceReferences(String clazz, String filter) throws InvalidSyntaxException {
     	long start = 0L;
-    	if (perfmon) {
+    	if (m_perfmon) {
     		start = System.currentTimeMillis();
     	}
         // first we ask the cache if there is an index for our request (class and filter combination)
@@ -100,11 +102,10 @@
                     }
                 }
             }
-            if (perfmon) {
+            if (m_perfmon) {
 	        	long duration = System.currentTimeMillis() - start;
-	        	if (maxLookupTime < duration) {
-	        		maxLookupTime = duration;
-	        		m_logger.log(org.apache.felix.dm.impl.Logger.LOG_DEBUG, "new worst performing filter (" + duration + "ms.): " + clazz + " " + filter);
+	        	if (duration > m_treshold) {
+	        		m_logger.log(org.apache.felix.dm.impl.Logger.LOG_DEBUG, "Indexed filter exceeds lookup time treshold (" + duration + "ms.): " + clazz + " " + filter);
 	        	}
             }
             if (result == null || result.size() == 0) {
@@ -115,11 +116,10 @@
         else {
             // if they don't know, we ask the real bundle context instead
             ServiceReference[] serviceReferences = m_context.getServiceReferences(clazz, filter);
-            if (perfmon) {
+            if (m_perfmon) {
 	        	long duration = System.currentTimeMillis() - start;
-	        	if (maxLookupTime < duration) {
-	        		maxLookupTime = duration;
-	        		System.out.println("new worst performing filter (" + duration + "ms.): " + clazz + " " + filter);
+	        	if (duration > m_treshold) {
+	        		m_logger.log(org.apache.felix.dm.impl.Logger.LOG_DEBUG, "Unindexed filter exceeds lookup time treshold (" + duration + "ms.): " + clazz + " " + filter);
 	        	}
             }
         	return serviceReferences;
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/index/ServiceRegistryCache.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/index/ServiceRegistryCache.java
index bbb0995..f97637c 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/index/ServiceRegistryCache.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/index/ServiceRegistryCache.java
@@ -36,7 +36,6 @@
  * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
  */
 public class ServiceRegistryCache implements ServiceListener/*, CommandProvider*/ {
-    private static final String INDEX_PERFLOG = "org.apache.felix.dependencymanager.index.logmissingindices";
 	private final List /* <FilterIndex> */ m_filterIndexList = new CopyOnWriteArrayList();
     private final BundleContext m_context;
     private final FilterIndexBundleContext m_filterIndexBundleContext;
@@ -45,16 +44,9 @@
     private long m_arrayVersion = -1;
     private BundleContextInterceptor[] m_interceptors = null;
     private ServiceRegistration m_registration;
-    private boolean m_dumpUnIndexedFilters = "true".equals(System.getProperty(INDEX_PERFLOG));
-    private List m_unindexedFilters = new ArrayList();
-	private Logger m_logger;
     
     public ServiceRegistryCache(BundleContext context) {
         m_context = context;
-        // only obtain the logservice when we actually want to log something.
-        if (System.getProperty(INDEX_PERFLOG) != null) {
-        	m_logger = new Logger(context);
-        }
         m_filterIndexBundleContext = new FilterIndexBundleContext(m_context);
     }
     
@@ -98,7 +90,7 @@
         synchronized (m_bundleContextInterceptorMap) {
             BundleContextInterceptor bundleContextInterceptor = (BundleContextInterceptor) m_bundleContextInterceptorMap.get(context);
             if (bundleContextInterceptor == null) {
-                bundleContextInterceptor = new BundleContextInterceptor(this, context, m_logger);
+                bundleContextInterceptor = new BundleContextInterceptor(this, context);
                 m_bundleContextInterceptorMap.put(context, bundleContextInterceptor);
                 m_currentVersion++;
                 // TODO figure out a good way to clean up bundle contexts that are no longer valid so they can be garbage collected
@@ -115,13 +107,6 @@
                 return filterIndex;
             }
         }
-        if (m_dumpUnIndexedFilters) {
-        	String filterStr = clazz + ":" + filter;
-	        if (!m_unindexedFilters.contains(filterStr)) {
-	        	m_unindexedFilters.add(filterStr);
-	        	m_logger.log(Logger.LOG_DEBUG, "No filter index for " + filterStr);
-	        }
-        }
         return null;
     }