use of java.util.ServiceLoader in project aries by apache.
the class ConsumerHeaderProcessor method processHeader.
/**
* Parses headers of the following syntax:
* <ul>
* <li><tt>org.acme.MyClass#myMethod</tt> - apply the weaving to all overloads of <tt>myMethod()</tt>
* in <tt>MyClass</tt>
* <li><tt>org.acme.MyClass#myMethod(java.lang.String, java.util.List)</tt> - apply the weaving only
* to the <tt>myMethod(String, List)</tt> overload in <tt>MyClass</tt>
* <li><tt>org.acme.MyClass#myMethod()</tt> - apply the weaving only to the noarg overload of
* <tt>myMethod()</tt>
* <li><b>true</b> - equivalent to <tt>java.util.ServiceLoader#load(java.lang.Class)</tt>
* </ul>
* Additionally, it registers the consumer's constraints with the consumer registry in the activator, if the
* consumer is only constrained to a certain set of bundles.<p>
*
* The following attributes are supported:
* <ul>
* <li><tt>bundle</tt> - restrict wiring to the bundle with the specifies Symbolic Name. The attribute value
* is a list of bundle identifiers separated by a '|' sign. The bundle identifier starts with the Symbolic name
* and can optionally contain a version suffix. E.g. bundle=impl2:version=1.2.3 or bundle=impl2|impl4.
* <li><tt>bundleId</tt> - restrict wiring to the bundle with the specified bundle ID. Typically used when
* the service should be forcibly picked up from the system bundle (<tt>bundleId=0</tt>). Multiple bundle IDs
* can be specified separated by a '|' sign.
* </ul>
*
* @param consumerHeaderName the name of the header (either Require-Capability or SPI-Consumer)
* @param consumerHeader the <tt>SPI-Consumer</tt> header.
* @return an instance of the {@link WeavingData} class.
* @throws Exception when a header cannot be parsed.
*/
public static Set<WeavingData> processHeader(String consumerHeaderName, String consumerHeader) throws Exception {
if (SpiFlyConstants.REQUIRE_CAPABILITY.equals(consumerHeaderName)) {
return processRequireCapabilityHeader(consumerHeader);
}
Set<WeavingData> weavingData = new HashSet<WeavingData>();
for (PathElement element : HeaderParser.parseHeader(consumerHeader)) {
List<BundleDescriptor> allowedBundles = new ArrayList<BundleDescriptor>();
String name = element.getName().trim();
String className;
String methodName;
MethodRestriction methodRestriction;
boolean serviceLoader = false;
int hashIdx = name.indexOf('#');
if (hashIdx > 0) {
className = name.substring(0, hashIdx);
int braceIdx = name.substring(hashIdx).indexOf('(');
if (braceIdx > 0) {
methodName = name.substring(hashIdx + 1, hashIdx + braceIdx);
ArgRestrictions argRestrictions = new ArgRestrictions();
int closeIdx = name.substring(hashIdx).indexOf(')');
if (closeIdx > 0) {
String classes = name.substring(hashIdx + braceIdx + 1, hashIdx + closeIdx).trim();
if (classes.length() > 0) {
if (classes.indexOf('[') > 0) {
int argNumber = 0;
for (String s : classes.split(",")) {
int idx = s.indexOf('[');
int end = s.indexOf(']', idx);
if (idx > 0 && end > idx) {
argRestrictions.addRestriction(argNumber, s.substring(0, idx), s.substring(idx + 1, end));
} else {
argRestrictions.addRestriction(argNumber, s);
}
argNumber++;
}
} else {
String[] classNames = classes.split(",");
for (int i = 0; i < classNames.length; i++) {
argRestrictions.addRestriction(i, classNames[i]);
}
}
} else {
argRestrictions = null;
}
}
methodRestriction = new MethodRestriction(methodName, argRestrictions);
} else {
methodName = name.substring(hashIdx + 1);
methodRestriction = new MethodRestriction(methodName);
}
} else {
if ("*".equalsIgnoreCase(name)) {
serviceLoader = true;
className = ServiceLoader.class.getName();
methodName = "load";
ArgRestrictions argRestrictions = new ArgRestrictions();
argRestrictions.addRestriction(0, Class.class.getName());
methodRestriction = new MethodRestriction(methodName, argRestrictions);
} else {
throw new IllegalArgumentException("Must at least specify class name and method name: " + name);
}
}
String bsn = element.getAttribute("bundle");
if (bsn != null) {
bsn = bsn.trim();
if (bsn.length() > 0) {
for (String s : bsn.split("\\|")) {
int colonIdx = s.indexOf(':');
if (colonIdx > 0) {
String sn = s.substring(0, colonIdx);
String versionSfx = s.substring(colonIdx + 1);
if (versionSfx.startsWith("version=")) {
allowedBundles.add(new BundleDescriptor(sn, Version.parseVersion(versionSfx.substring("version=".length()))));
} else {
allowedBundles.add(new BundleDescriptor(sn));
}
} else {
allowedBundles.add(new BundleDescriptor(s));
}
}
}
}
String bid = element.getAttribute("bundleId");
if (bid != null) {
bid = bid.trim();
if (bid.length() > 0) {
for (String s : bid.split("\\|")) {
allowedBundles.add(new BundleDescriptor(Long.parseLong(s)));
}
}
}
weavingData.add(createWeavingData(className, methodName, methodRestriction, allowedBundles));
if (serviceLoader) {
className = ServiceLoader.class.getName();
methodName = "load";
ArgRestrictions argRestrictions = new ArgRestrictions();
argRestrictions.addRestriction(0, Class.class.getName());
argRestrictions.addRestriction(1, ClassLoader.class.getName());
methodRestriction = new MethodRestriction(methodName, argRestrictions);
weavingData.add(createWeavingData(className, methodName, methodRestriction, allowedBundles));
}
}
return weavingData;
}
use of java.util.ServiceLoader in project powermock by powermock.
the class ServiceLoaderTest method supportsMockingServiceLoader.
@Test(expected = IllegalArgumentException.class)
public void supportsMockingServiceLoader() throws Exception {
final ServiceLoader mock = mock(ServiceLoader.class);
doThrow(new IllegalArgumentException("something")).when(mock).reload();
mock.reload();
}
use of java.util.ServiceLoader in project wildfly by wildfly.
the class WeldSubsystemAdd method performBoottime.
@Override
protected void performBoottime(final OperationContext context, ModelNode operation, Resource resource) throws OperationFailedException {
final ModelNode model = resource.getModel();
final boolean requireBeanDescriptor = REQUIRE_BEAN_DESCRIPTOR_ATTRIBUTE.resolveModelAttribute(context, model).asBoolean();
final boolean nonPortableMode = WeldResourceDefinition.NON_PORTABLE_MODE_ATTRIBUTE.resolveModelAttribute(context, model).asBoolean();
final boolean developmentMode = WeldResourceDefinition.DEVELOPMENT_MODE_ATTRIBUTE.resolveModelAttribute(context, model).asBoolean();
final int threadPoolSize = WeldResourceDefinition.THREAD_POOL_SIZE_ATTRIBUTE.resolveModelAttribute(context, model).asInt(WeldExecutorServices.DEFAULT_BOUND);
context.addStep(new AbstractDeploymentChainStep() {
@Override
protected void execute(DeploymentProcessorTarget processorTarget) {
final JBossAllXmlParserRegisteringProcessor<?> jbossAllParsers = JBossAllXmlParserRegisteringProcessor.builder().addParser(WeldJBossAll10Parser.ROOT_ELEMENT, WeldJBossAllConfiguration.ATTACHMENT_KEY, WeldJBossAll10Parser.INSTANCE).addParser(WeldJBossAll11Parser.ROOT_ELEMENT, WeldJBossAllConfiguration.ATTACHMENT_KEY, WeldJBossAll11Parser.INSTANCE).build();
processorTarget.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.STRUCTURE, Phase.STRUCTURE_REGISTER_JBOSS_ALL_WELD, jbossAllParsers);
processorTarget.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.PARSE, Phase.PARSE_WELD_CONFIGURATION, new WeldConfigurationProcessor(requireBeanDescriptor, nonPortableMode, developmentMode));
processorTarget.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.PARSE, Phase.PARSE_CDI_ANNOTATIONS, new CdiAnnotationProcessor());
processorTarget.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.PARSE, Phase.PARSE_CDI_BEAN_DEFINING_ANNOTATIONS, new BeanDefiningAnnotationProcessor());
processorTarget.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.PARSE, Phase.PARSE_WELD_DEPLOYMENT, new BeansXmlProcessor());
processorTarget.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.PARSE, Phase.PARSE_WELD_IMPLICIT_DEPLOYMENT_DETECTION, new WeldImplicitDeploymentProcessor());
processorTarget.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.DEPENDENCIES, Phase.DEPENDENCIES_WELD, new WeldDependencyProcessor());
processorTarget.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.POST_MODULE, Phase.POST_MODULE_WELD_WEB_INTEGRATION, new WebIntegrationProcessor());
processorTarget.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.POST_MODULE, Phase.POST_MODULE_WELD_DEVELOPMENT_MODE, new DevelopmentModeProcessor());
processorTarget.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.POST_MODULE, Phase.POST_MODULE_WELD_BEAN_ARCHIVE, new BeanArchiveProcessor());
processorTarget.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.POST_MODULE, Phase.POST_MODULE_WELD_EXTERNAL_BEAN_ARCHIVE, new ExternalBeanArchiveProcessor());
processorTarget.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.POST_MODULE, Phase.POST_MODULE_WELD_PORTABLE_EXTENSIONS, new WeldPortableExtensionProcessor());
processorTarget.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.POST_MODULE, Phase.POST_MODULE_WELD_COMPONENT_INTEGRATION, new WeldComponentIntegrationProcessor());
processorTarget.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.INSTALL, Phase.INSTALL_WELD_DEPLOYMENT, new WeldDeploymentProcessor(checkJtsEnabled(context)));
processorTarget.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.INSTALL, Phase.INSTALL_WELD_BEAN_MANAGER, new WeldBeanManagerServiceProcessor());
// Add additional deployment processors
ServiceLoader<DeploymentUnitProcessorProvider> processorProviders = ServiceLoader.load(DeploymentUnitProcessorProvider.class, WildFlySecurityManager.getClassLoaderPrivileged(WeldSubsystemAdd.class));
for (DeploymentUnitProcessorProvider provider : processorProviders) {
processorTarget.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, provider.getPhase(), provider.getPriority(), provider.getProcessor());
}
}
}, OperationContext.Stage.RUNTIME);
TCCLSingletonService singleton = new TCCLSingletonService();
context.getServiceTarget().addService(TCCLSingletonService.SERVICE_NAME, singleton).setInitialMode(Mode.ON_DEMAND).install();
context.getServiceTarget().addService(WeldExecutorServices.SERVICE_NAME, new WeldExecutorServices(threadPoolSize)).setInitialMode(Mode.ON_DEMAND).install();
}
use of java.util.ServiceLoader in project JMRI by JMRI.
the class StartupActionModelUtil method prepareActionsHashMap.
private void prepareActionsHashMap() {
if (this.actions == null) {
this.actions = new HashMap<>();
this.overrides = new HashMap<>();
ResourceBundle rb = ResourceBundle.getBundle("apps.ActionListBundle");
rb.keySet().stream().filter((key) -> (!key.isEmpty())).forEach((key) -> {
try {
Class<?> clazz = Class.forName(key);
ActionAttributes attrs = new ActionAttributes(rb.getString(key), clazz);
this.actions.put(clazz, attrs);
} catch (ClassNotFoundException ex) {
log.error("Did not find class \"{}\"", key);
}
});
ServiceLoader<StartupActionFactory> loader = ServiceLoader.load(StartupActionFactory.class);
loader.forEach(factory -> {
for (Class<?> clazz : factory.getActionClasses()) {
ActionAttributes attrs = new ActionAttributes(factory.getTitle(clazz), clazz);
this.actions.put(clazz, attrs);
for (String overridden : factory.getOverriddenClasses(clazz)) {
this.overrides.put(overridden, clazz);
}
}
});
// allow factories to be garbage collected
loader.reload();
}
}
use of java.util.ServiceLoader in project jdk8u_jdk by JetBrains.
the class CMSManager method getModule.
public static synchronized PCMM getModule() {
if (cmmImpl != null) {
return cmmImpl;
}
CMMServiceProvider spi = AccessController.doPrivileged(new PrivilegedAction<CMMServiceProvider>() {
public CMMServiceProvider run() {
String cmmClass = System.getProperty("sun.java2d.cmm", "sun.java2d.cmm.lcms.LcmsServiceProvider");
ServiceLoader<CMMServiceProvider> cmmLoader = ServiceLoader.loadInstalled(CMMServiceProvider.class);
CMMServiceProvider spi = null;
for (CMMServiceProvider cmm : cmmLoader) {
spi = cmm;
if (cmm.getClass().getName().equals(cmmClass)) {
break;
}
}
return spi;
}
});
cmmImpl = spi.getColorManagementModule();
if (cmmImpl == null) {
throw new CMMException("Cannot initialize Color Management System." + "No CM module found");
}
GetPropertyAction gpa = new GetPropertyAction("sun.java2d.cmm.trace");
String cmmTrace = (String) AccessController.doPrivileged(gpa);
if (cmmTrace != null) {
cmmImpl = new CMMTracer(cmmImpl);
}
return cmmImpl;
}
Aggregations