All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.osgi.internal.serviceregistry.PrototypeServiceFactoryUse Maven / Gradle / Ivy

Go to download

AspectJ tools most notably contains the AspectJ compiler (AJC). AJC applies aspects to Java classes during compilation, fully replacing Javac for plain Java classes and also compiling native AspectJ or annotation-based @AspectJ syntax. Furthermore, AJC can weave aspects into existing class files in a post-compile binary weaving step. This library is a superset of AspectJ weaver and hence also of AspectJ runtime.

There is a newer version: 1.9.22.1
Show newest version
/*******************************************************************************
 * Copyright (c) 2013, 2022 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.osgi.internal.serviceregistry;

import java.util.IdentityHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import org.eclipse.osgi.internal.debug.Debug;
import org.eclipse.osgi.internal.framework.BundleContextImpl;
import org.eclipse.osgi.internal.messages.Msg;
import org.osgi.framework.Bundle;
import org.osgi.framework.PrototypeServiceFactory;
import org.osgi.framework.ServiceException;
import org.osgi.framework.ServiceRegistration;

/**
 * This class represents the use of a service by a bundle. One is created for each
 * service acquired by a bundle.
 *
 * 

* This class manages a prototype service factory. * * @ThreadSafe */ public class PrototypeServiceFactoryUse extends ServiceFactoryUse { /** Service objects returned by PrototypeServiceFactory.getService() and their use count. */ /* @GuardedBy("getLock()") */ private final Map serviceObjects; /** * Constructs a service use encapsulating the service object. * * @param context bundle getting the service * @param registration ServiceRegistration of the service */ PrototypeServiceFactoryUse(BundleContextImpl context, ServiceRegistrationImpl registration) { super(context, registration); this.serviceObjects = new IdentityHashMap<>(); } /** * Create a new service object for the service. * *

* * @return The service object. */ /* @GuardedBy("getLock()") */ @Override S newServiceObject() { assert getLock().isHeldByCurrentThread(); if (debug.DEBUG_SERVICES) { Debug.println('[' + Thread.currentThread().getName() + "] getServiceObject[PSfactory=" //$NON-NLS-1$ + registration.getBundle() + "](" + context.getBundleImpl() + "," + registration + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } final S service = factoryGetService(); if (service == null) { return null; } AtomicInteger useCount = serviceObjects.get(service); if (useCount == null) { serviceObjects.put(service, new AtomicInteger(1)); } else { if (useCount.getAndIncrement() == Integer.MAX_VALUE) { useCount.getAndDecrement(); throw new ServiceException(Msg.SERVICE_USE_OVERFLOW); } } return service; } /** * Release a service object for the service. * * @param service The service object to release. * @return true if the service was released; otherwise false. * @throws IllegalArgumentException If the specified service was not * provided by this object. */ /* @GuardedBy("getLock()") */ @Override boolean releaseServiceObject(final S service) { assert getLock().isHeldByCurrentThread(); if ((service == null) || !serviceObjects.containsKey(service)) { throw new IllegalArgumentException(Msg.SERVICE_OBJECTS_UNGET_ARGUMENT_EXCEPTION); } if (debug.DEBUG_SERVICES) { Debug.println( '[' + Thread.currentThread().getName() + "] ungetService[PSfactory=" + registration.getBundle() //$NON-NLS-1$ + "](" + context.getBundleImpl() + "," + registration + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } AtomicInteger useCount = serviceObjects.get(service); if (useCount.decrementAndGet() < 1) { serviceObjects.remove(service); factoryUngetService(service); } return true; } /** * Release all uses of the service and reset the use count to zero. * *

    *
  1. The bundle's use count for this service is set to zero. *
  2. The {@link PrototypeServiceFactory#ungetService(Bundle, ServiceRegistration, Object)} method * is called to release the service object for the bundle. *
*/ /* @GuardedBy("getLock()") */ @Override void release() { super.release(); for (S service : serviceObjects.keySet()) { if (debug.DEBUG_SERVICES) { Debug.println('[' + Thread.currentThread().getName() + "] releaseService[PSfactory=" //$NON-NLS-1$ + registration.getBundle() + "](" + context.getBundleImpl() + "," + registration + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } factoryUngetService(service); } serviceObjects.clear(); } /** * Is this service use using any services? * * @return true if no services are being used and this service use can be discarded. */ /* @GuardedBy("getLock()") */ @Override boolean isEmpty() { return super.isEmpty() && serviceObjects.isEmpty(); } }