pkriens commented on a change in pull request #36: URL: https://github.com/apache/felix-dev/pull/36#discussion_r468534733
########## File path: scr/src/main/java/org/apache/felix/scr/impl/logger/LogManager.java ########## @@ -0,0 +1,203 @@ +/* + * 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.scr.impl.logger; + +import java.io.Closeable; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.BundleEvent; +import org.osgi.framework.BundleListener; +import org.osgi.framework.ServiceReference; +import org.osgi.service.log.Logger; +import org.osgi.service.log.LoggerFactory; +import org.osgi.util.tracker.ServiceTracker; + +/* + * A class that tracks the LoggerFactory of a bundle and its associated Logger objects. + * When a bundle is stopped and/or the service is unregistered, the logger objects are cleaned. + * + * The basic technique is to use a facade. Instead of returning a log object, we return a facade. The + * methods delegate to the actual logger object. If there is no logger object, we create one. + * + * The LogDomain represents every bundle. Per LogDomain, we keep the facades. If the factory goes, + * we reset the facades. + */ +class LogManager extends ServiceTracker<LoggerFactory, LoggerFactory> { + + // Guard is itself. Don't replace it with a ConcurrentHashMap since the + // LogDomain value is not stateless. + + final Map<Bundle, LogDomain> domains = new HashMap<>(); + final BundleContext scrContext; + final AtomicBoolean closed = new AtomicBoolean(false); + volatile int trackingCount; + volatile LoggerFactory factory; + + LogManager(BundleContext context) { + super(context, LoggerFactory.class, null); + this.scrContext = context; + } + + private LogDomain getLogDomain(Bundle bundle) { + synchronized (domains) { + LogDomain domain = domains.get(bundle); + if (domain == null) { + domain = new LogDomain(bundle); + domains.put(bundle, domain); + } + return domain; + } + } + + @Override + public void removedService(ServiceReference<LoggerFactory> reference, LoggerFactory service) { + if (!closed.get()) { + reset(); + } + } + + private void reset() { + for (LogDomain domain : domains.values()) { Review comment: Since the single service tracker & the higher priority made the locking harder, I rewrote these parts. Good catch ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
