Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.glassfish.hk2.internal;
import java.lang.annotation.Annotation;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.glassfish.hk2.api.AOPProxyCtl;
import org.glassfish.hk2.api.ActiveDescriptor;
import org.glassfish.hk2.api.DynamicConfigurationListener;
import org.glassfish.hk2.api.Filter;
import org.glassfish.hk2.api.InstanceLifecycleEvent;
import org.glassfish.hk2.api.InstanceLifecycleListener;
import org.glassfish.hk2.api.IterableProvider;
import org.glassfish.hk2.api.MultiException;
import org.glassfish.hk2.api.PerLookup;
import org.glassfish.hk2.api.Self;
import org.glassfish.hk2.api.ServiceHandle;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.api.Unqualified;
import org.glassfish.hk2.api.messaging.SubscribeTo;
import org.glassfish.hk2.api.messaging.Topic;
import org.glassfish.hk2.api.messaging.TopicDistributionService;
import org.glassfish.hk2.utilities.BuilderHelper;
import org.glassfish.hk2.utilities.DefaultTopicDistributionErrorService;
import org.glassfish.hk2.utilities.InjecteeImpl;
import org.glassfish.hk2.utilities.reflection.ClassReflectionHelper;
import org.glassfish.hk2.utilities.reflection.MethodWrapper;
import org.glassfish.hk2.utilities.reflection.Pretty;
import org.glassfish.hk2.utilities.reflection.ReflectionHelper;
import org.glassfish.hk2.utilities.reflection.TypeChecker;
import org.glassfish.hk2.utilities.reflection.internal.ClassReflectionHelperImpl;
import org.jvnet.hk2.annotations.ContractsProvided;
import org.jvnet.hk2.annotations.Optional;
/**
* This is the default implementation of the TopicDistributionService.
*
* @author jwells
*/
@Singleton
@Named(TopicDistributionService.HK2_DEFAULT_TOPIC_DISTRIBUTOR)
@ContractsProvided({TopicDistributionService.class, InstanceLifecycleListener.class, DynamicConfigurationListener.class})
public class DefaultTopicDistributionService implements
TopicDistributionService, InstanceLifecycleListener, DynamicConfigurationListener {
@Inject
private ServiceLocator locator;
@Inject
private IterableProvider errorHandlers;
private final ClassReflectionHelper reflectionHelper = new ClassReflectionHelperImpl();
private final HashMap, Set>> descriptor2Classes = new HashMap, Set>>();
private final HashMap> class2Subscribers = new HashMap>();
private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private final WriteLock wLock = readWriteLock.writeLock();
private final ReadLock rLock = readWriteLock.readLock();
private static void fire(Object message, Method subscription, SubscriberInfo subscriptionInfo, Object target, ServiceLocator locator) throws Throwable {
Object arguments[] = new Object[subscriptionInfo.otherInjectees.length];
List> destroyMe = new LinkedList>();
try {
for (int lcv = 0; lcv < subscriptionInfo.otherInjectees.length; lcv++) {
InjecteeImpl injectee = subscriptionInfo.otherInjectees[lcv];
if (injectee == null) {
arguments[lcv] = message;
}
else {
if (injectee.isSelf()) {
arguments[lcv] = injectee.getInjecteeDescriptor();
continue;
}
ActiveDescriptor> injecteeDescriptor = locator.getInjecteeDescriptor(injectee);
if (injecteeDescriptor == null) {
if (injectee.isOptional()) {
arguments[lcv] = null;
continue;
}
throw new IllegalStateException("Could not find injectee " + injectee + " for subscriber " +
Pretty.method(subscription) + " on class " + target.getClass().getName());
}
ServiceHandle> handle = locator.getServiceHandle(injecteeDescriptor);
if (injecteeDescriptor.getScope().equals(PerLookup.class.getName())) {
// Only will live as long as the method
destroyMe.add(handle);
}
arguments[lcv] = handle.getService();
}
}
// OK, everything filled in!
ReflectionHelper.invoke(target, subscription, arguments, locator.getNeutralContextClassLoader());
}
finally {
for (ServiceHandle> dead : destroyMe) {
dead.destroy();
}
}
}
private List handleDescriptorToClass(ActiveDescriptor> descriptor, Class> clazz, Type eventType, Topic> topic) {
LinkedList retVal = new LinkedList();
List subscribers = class2Subscribers.get(new ActivatorClassKey(descriptor, clazz));
if (subscribers == null) subscribers = Collections.emptyList();
for (SubscriberInfo subscriberInfo : subscribers) {
Type subscriptionType = subscriberInfo.eventType;
if (!TypeChecker.isRawTypeSafe(subscriptionType, eventType)) {
// Not a type match
continue;
}
if (!subscriberInfo.eventQualifiers.isEmpty()) {
if (!ReflectionHelper.annotationContainsAll(topic.getTopicQualifiers(), subscriberInfo.eventQualifiers)) {
// The qualifiers do not match
continue;
}
}
if ((subscriberInfo.unqualified != null) && !topic.getTopicQualifiers().isEmpty()) {
if (subscriberInfo.unqualified.value().length == 0) {
// publisher must not have any qualifiers,
// but it DOES have some, so forget it!
continue;
}
Set> topicQualifierClasses = new HashSet>();
for (Annotation topicQualifier : topic.getTopicQualifiers()) {
topicQualifierClasses.add(topicQualifier.annotationType());
}
boolean found = false;
for (Class extends Annotation> verbotenQualifier : subscriberInfo.unqualified.value()) {
if (topicQualifierClasses.contains(verbotenQualifier)) {
found = true;
break;
}
}
if (found) {
// Found one of the qualifiers we are not allowed to have!
continue;
}
}
for (WeakReference