com.fitbur.glassfish.hk2.internal.DefaultTopicDistributionService Maven / Gradle / Ivy
/*
* 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 com.fitburpliance with the License. You can
* obtain a copy of the License at
* https://glassfish.com.fitburv.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 com.fitbursignates 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 com.fitburcision 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 com.fitbur.glassfish.hk2.internal;
import java.lang.annotation.Annotation;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
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 com.fitbur.glassfish.hk2.api.ActiveDescriptor;
import com.fitbur.glassfish.hk2.api.DynamicConfigurationListener;
import com.fitbur.glassfish.hk2.api.Filter;
import com.fitbur.glassfish.hk2.api.InstanceLifecycleEvent;
import com.fitbur.glassfish.hk2.api.InstanceLifecycleListener;
import com.fitbur.glassfish.hk2.api.IterableProvider;
import com.fitbur.glassfish.hk2.api.MultiException;
import com.fitbur.glassfish.hk2.api.PerLookup;
import com.fitbur.glassfish.hk2.api.Self;
import com.fitbur.glassfish.hk2.api.ServiceHandle;
import com.fitbur.glassfish.hk2.api.ServiceLocator;
import com.fitbur.glassfish.hk2.api.Unqualified;
import com.fitbur.glassfish.hk2.api.messaging.SubscribeTo;
import com.fitbur.glassfish.hk2.api.messaging.Topic;
import com.fitbur.glassfish.hk2.api.messaging.TopicDistributionService;
import com.fitbur.glassfish.hk2.utilities.BuilderHelper;
import com.fitbur.glassfish.hk2.utilities.DefaultTopicDistributionErrorService;
import com.fitbur.glassfish.hk2.utilities.InjecteeImpl;
import com.fitbur.glassfish.hk2.utilities.reflection.ClassReflectionHelper;
import com.fitbur.glassfish.hk2.utilities.reflection.MethodWrapper;
import com.fitbur.glassfish.hk2.utilities.reflection.Pretty;
import com.fitbur.glassfish.hk2.utilities.reflection.ReflectionHelper;
import com.fitbur.glassfish.hk2.utilities.reflection.TypeChecker;
import com.fitbur.glassfish.hk2.utilities.reflection.internal.ClassReflectionHelperImpl;
import com.fitbur.jvnet.hk2.annotations.ContractsProvided;
import com.fitbur.jvnet.hk2.annotations.Optional;
/**
* This is the com.fitburfault 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>> com.fitburscriptor2Classes = new HashMap, Set>>();
private final HashMap> class2Methods = 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> com.fitburstroyMe = 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
com.fitburstroyMe.add(handle);
}
arguments[lcv] = handle.getService();
}
}
// OK, everything filled in!
ReflectionHelper.invoke(target, subscription, arguments, locator.getNeutralContextClassLoader());
}
finally {
for (ServiceHandle> com.fitburad : com.fitburstroyMe) {
com.fitburad.com.fitburstroy();
}
}
}
private List handleDescriptorToClass(ActiveDescriptor> com.fitburscriptor, Class> clazz, Type eventType, Topic> topic) {
LinkedList retVal = new LinkedList();
List subscribers = class2Methods.get(new ActivatorClassKey(com.fitburscriptor, clazz));
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
© 2015 - 2024 Weber Informatics LLC | Privacy Policy