@Override public <U> U findOrCreate(ActiveDescriptor<U> activeDescriptor, ServiceHandle<?> root) { Hk2RequestScope.Instance instance = (Hk2RequestScope.Instance) requestScope.current(); U retVal = instance.get(ForeignDescriptor.wrap(activeDescriptor)); if (retVal == null) { retVal = activeDescriptor.create(root); instance.put(ForeignDescriptor.wrap(activeDescriptor, obj -> activeDescriptor.dispose((U) obj)), retVal); } return retVal; }
@Override @SuppressWarnings("unchecked") public <T> List<ServiceHolder<T>> getAllServiceHolders(Class<T> contract, Annotation... qualifiers) { return getServiceLocator().getAllServiceHandles(contract, qualifiers).stream() .map(sh -> new ServiceHolderImpl<>( sh.getService(), (Class<T>) sh.getActiveDescriptor().getImplementationClass(), sh.getActiveDescriptor().getContractTypes(), sh.getActiveDescriptor().getRanking())) .collect(Collectors.toList()); }
public Object set(int index, Object element) { Dom child = Dom.unwrap((ConfigBeanProxy) element); String name = "*".equals(xmlName) ? child.model.injector.getName() : xmlName; dom.replaceChild(v.get(index), name, child); return v.set(index,child).createProxy(); }
/** * @param in * If provided, this is used to record the source location where this DOM object is loaded from. * Otherwise this can be null. */ public Dom(ServiceLocator habitat, DomDocument document, Dom parent, ConfigModel model, XMLStreamReader in) { super(createDescriptor( model.targetTypeName, model.injector.getLoader(), model.injector.getMetadata())); this.habitat = habitat; if (in!=null) { this.location = new LocationImpl(in.getLocation()); } else { this.location=null; } this.model = model; this.document = document; this.parent = parent; // TODO: This code is disabled as it does fail from time to time when assertions are enabled // assert (parent==null || parent.document==document); // all the nodes in the tree must belong to the same document }
ServiceLocator locator = getServiceLocator(); ActiveDescriptor<?> myselfReified = locator.reifyDescriptor(this); DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class); DynamicConfiguration dc = dcs.createDynamicConfiguration(); ctrs.add(myselfReified.getImplementationClass()); serviceHandle = getHabitat().getServiceHandle(domDescriptor);
@Override public Injectee apply(CacheKey cacheKey) { Injectee injectee = cacheKey.getInjectee(); if (injectee.getParent() != null) { if (Field.class.isAssignableFrom(injectee.getParent().getClass())) { Field f = (Field) injectee.getParent(); if (foreignRequestScopedComponents.get().contains(f.getDeclaringClass())) { final Class<?> clazz = f.getType(); if (serviceLocator.getServiceHandle(clazz).getActiveDescriptor().getScopeAnnotation() == RequestScoped.class) { final AbstractActiveDescriptor<Object> descriptor = BuilderHelper.activeLink(clazz) .to(clazz) .in(RequestScoped.class) .build(); return new DescriptorOverridingInjectee(injectee, descriptor); } } } } return injectee; } });
public void lookupInjectorByName() { ServiceHandle inhabitant1 = habitat.getServiceHandle(ConfigInjector.class, "simple-connector"); ServiceHandle inhabitant2 = habitat.getServiceHandle(ConfigInjector.class, "ejb-container-availability"); assert(inhabitant1 != null && inhabitant2 != null && inhabitant1.getActiveDescriptor().getImplementation().equals(SimpleConnectorInjector.class.getName()) && inhabitant2.getActiveDescriptor().getImplementation().equals(EjbContainerAvailabilityInjector.class.getName())); }
public static List<ConfigurationDeploymentDescriptorFile> getConfigurationDeploymentDescriptorFiles(ServiceLocator habitat, String containerType) { List<ConfigurationDeploymentDescriptorFile> confDDFiles = new ArrayList<ConfigurationDeploymentDescriptorFile>(); for (ServiceHandle<?> serviceHandle : habitat.getAllServiceHandles(ConfigurationDeploymentDescriptorFileFor.class)) { ActiveDescriptor<?> descriptor = serviceHandle.getActiveDescriptor(); String indexedType = descriptor.getMetadata().get(ConfigurationDeploymentDescriptorFileFor.DESCRIPTOR_FOR).get(0); if(indexedType.equals(containerType)) { ConfigurationDeploymentDescriptorFile confDD = (ConfigurationDeploymentDescriptorFile) serviceHandle.getService(); confDDFiles.add(confDD); } } return confDDFiles; }
proxyClass = Utilities.getFactoryAwareImplementationClass(root); iFaces = Utilities.getInterfacesForProxy(root.getContractTypes()); " in scope " + root.getScope() + " an error occured while creating the proxy"); me.addError(addMe); MultiException me = new MultiException(th); me.addError(addMe); throw me;
@SuppressWarnings("unchecked") private void reupClassAnalyzers() { List<ServiceHandle<?>> allAnalyzers = protectedGetAllServiceHandles(ClassAnalyzer.class); synchronized (classAnalyzerLock) { classAnalyzers.clear(); for (ServiceHandle<?> handle : allAnalyzers) { ActiveDescriptor<?> descriptor = handle.getActiveDescriptor(); String name = descriptor.getName(); if (name == null) continue; ClassAnalyzer created = ((ServiceHandle<ClassAnalyzer>) handle).getService(); if (created == null) continue; classAnalyzers.put(name, created); } } }
private Object internalInvoke(Object target, Method method, Method proceed, Object[] params) throws Throwable { Context<?> context; Object service; context = locator.resolveContext(descriptor.getScopeAnnotation()); service = context.findOrCreate(descriptor, root); if (service == null) { throw new MultiException(new IllegalStateException("Proxiable context " + context + " findOrCreate returned a null for descriptor " + descriptor + " and handle " + root)); } if (method.getName().equals(PROXY_MORE_METHOD_NAME)) { // We did what we came here to do return service; } if (isEquals(method) && (params.length == 1) && (params[0] != null) && (params[0] instanceof ProxyCtl)) { ProxyCtl equalsProxy = (ProxyCtl) params[0]; params = new Object[1]; params[0] = equalsProxy.__make(); } return ReflectionHelper.invoke(service, method, params, locator.getNeutralContextClassLoader()); }
public void lookupAllInjectors() { String[] expected = { SimpleConnectorInjector.class.getName(), EjbContainerAvailabilityInjector.class.getName(), WebContainerAvailabilityInjector.class.getName(), GenericContainerInjector.class.getName(), GenericConfigInjector.class.getName() }; List<String> expectedInjectors = Arrays.asList(expected); List<ServiceHandle<ConfigInjector>> inhabitants = habitat.getAllServiceHandles(ConfigInjector.class); Set<String> inhabitantNames = new HashSet<String>(); for (ServiceHandle<?> inh : inhabitants) { inhabitantNames.add(inh.getActiveDescriptor().getImplementation()); } assert(inhabitants.size() == expected.length); assert(inhabitantNames.containsAll(expectedInjectors)); }
if (!initialized) { if (!descriptor.isReified()) { descriptor = (ActiveDescriptor<T>) locator.reifyDescriptor(descriptor); HK2Loader loader = descriptor.getLoader(); Class<?> ic = descriptor.getImplementationClass(); ClassLoader cl = null; if (ic != null) {
/** * Given a command name, return the name of the class that implements * that command in the server. */ private static String getCommandClass(String cmdName) { ServiceLocator h = getManHabitat(); String cname = "org.glassfish.api.admin.AdminCommand"; for (ActiveDescriptor<?> ad : h.getDescriptors(BuilderHelper.createContractFilter(cname))) { if (ad.getName() != null && ad.getName().equals(cmdName)) { return ad.getImplementation(); } } return null; }
/** * This method returns the class associated with the type of the factory. * * @param descriptor The descriptor (reified and not null) that will be used to find the implementation. * @return the CLASS version of what the factory produces. Will not be null. * @throws MultiException if there was an error analyzing the class. */ private static Class<?> getFactoryProductionClass(ActiveDescriptor<?> descriptor) { Class<?> factoryClass = descriptor.getImplementationClass(); Type factoryProvidedType = getFactoryProductionType(factoryClass); Class<?> retVal = ReflectionHelper.getRawClass(factoryProvidedType); if (retVal == null && descriptor.getContractTypes().size() == 1) { // If the type was not resolved from a generic type, try to use // contract type if only one was registered. Type contract = descriptor.getContractTypes().iterator().next(); retVal = ReflectionHelper.getRawClass(contract); } if (retVal == null) { throw new MultiException(new AssertionError("Could not find true produced type of factory " + factoryClass.getName())); } return retVal; }
public void lookupInjectorByFilter() { ActiveDescriptor desc = habitat.getBestDescriptor( new InjectionTargetFilter(EjbContainerAvailability.class.getName())); assert(desc != null && desc.getImplementation().equals(EjbContainerAvailabilityInjector.class.getName())); }
/** * Given a command name, return the name of the class that implements that * command in the server. */ private static String getCommandClass(String cmdName) { ServiceLocator h = getManHabitat(); String cname = "org.glassfish.api.admin.AdminCommand"; ActiveDescriptor<?> ad = h.getBestDescriptor( BuilderHelper.createNameAndContractFilter(cname, cmdName)); if (ad == null) return null; return ad.getImplementation(); }
@SuppressWarnings("unchecked") @Override public void shutdown() { List<ActiveDescriptor<?>> all = locator.getDescriptors(BuilderHelper.allFilter()); long myLocatorId = locator.getLocatorId(); TreeSet<SystemDescriptor<Object>> singlesOnly = new TreeSet<SystemDescriptor<Object>>( new GenerationComparator()); for (ActiveDescriptor<?> one : all) { if (one.getScope() == null || !one.getScope().equals(Singleton.class.getName())) continue; synchronized (this) { if (one.getCache() == null) continue; } if (one.getLocatorId() == null || one.getLocatorId().longValue() != myLocatorId) continue; SystemDescriptor<Object> oneAsObject = (SystemDescriptor<Object>) one; singlesOnly.add(oneAsObject); } for (SystemDescriptor<Object> one : singlesOnly) { destroyOne(one); } }
ServiceLocator locator) { if(description==null) throw new ConfigurationException("%s doesn't have any metadata",injector.getImplementation()); this.classLoaderHolder = new SafeHk2Loader((injector.getLoader() == null) ? new HK2LoaderImpl() : injector.getLoader() ); throw new ConfigurationException("%s doesn't have the mandatory '%s' metadata", injector.getImplementation(), ConfigMetadata.TARGET); if(key==null ^ indexTypeName==null) throw new ConfigurationException("%s has inconsistent '%s=%s' and '%s=%s' metadata", tagName = v.substring(ELEMENT_NAME_PREFIX.length()); this.tagName = tagName == null ? injector.getName() : tagName;
private static String oneLineDescriptor(ActiveDescriptor<?> descriptor) { if (descriptor == null) return "null-descriptor"; return descriptor.getImplementation() + ((descriptor.getName() != null) ? "/" + descriptor.getName() : "") + "(" + descriptor.getServiceId() + "," + descriptor.getLocatorId() + "," + System.identityHashCode(descriptor) + ")"; }