/** * Installs all Tentackle Look and Feels */ public void installTentackleLookAndFeels() { for (String plafName: ServiceFactory.getServiceFinder().findServiceConfigurations(TentackleLookAndFeel.class.getName()).keySet()) { installLookAndFeel(plafName); } }
/** * Creates a class mapper for a given mapped service name. * * @param name the mapper's name * @param mappedServiceName the mapped name */ public DefaultClassMapper(String name, String mappedServiceName) { this(name, ServiceFactory.getClassLoader(Constants.MAPPED_SERVICE_PATH, mappedServiceName), ServiceFactory.getServiceFinder().createNameMap(mappedServiceName), null); }
/** * Utility method to create a service instance with a default if not found.<br> * * @param <T> the service type * @param serviceClass the service class * @param defaultClass the default class if no service found * @return an instance of the service */ public static <T> T createService(Class<T> serviceClass, Class<? extends T> defaultClass) { try { return createService(serviceClass); } catch (TentackleRuntimeException re) { try { // we cannot use LOGGER here because this may use the createService as well java.util.logging.Logger.getLogger(ServiceFactory.class.getName()).log( java.util.logging.Level.WARNING, "creating service for " + serviceClass.getName() + " failed -> creating default " + defaultClass.getName(), re); return defaultClass.getDeclaredConstructor().newInstance(); } catch (IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException | SecurityException | InvocationTargetException ex) { throw new TentackleRuntimeException("cannot create default instance " + defaultClass + " for " + serviceClass, ex); } } }
/** * Gets the classloader for a given service path and name.<br> * If an explicit classloader is set, the last defined classloader for the given path and name is returned. * Otherwise the default classloader is returned. * * @param servicePath the service path * @param serviceName the service name * @return the classloader, never null */ protected ClassLoader getClassLoaderImpl(String servicePath, String serviceName) { ClassLoader cl = getExplicitClassLoader(servicePath, serviceName); if (cl == null) { cl = getClassLoader(); } return cl; }
/** * Gets a service finder for a given service path.<br> * If the finder does not exist yet, it will be created. * The classloader to use is determined as follows: * <ol> * <li> use the fixed classloader set by {@link #setFixedClassLoader(java.lang.ClassLoader)}, if not null</li> * <li>else try {@code Thread.currentThread().getContextClassLoader()}</li> * <li>if the context classloader is null, use the classloader that loaded the service factory</li> * </ol> * * @param servicePath the service path prefix * @return the finder, never null */ protected ServiceFinder getServiceFinderImpl(String servicePath) { return getServiceFinderImpl(getClassLoader(), servicePath); }
/** * Utility method to create a service instance.<br> * This is the standard way to instantiate singletons. * Finds the first service implementation along the classpath. * * @param <T> the service type * @param serviceClass the service class * @return an instance of the service */ public static <T> T createService(Class<T> serviceClass) { try { return createServiceClass(serviceClass).getDeclaredConstructor().newInstance(); } catch (IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException | SecurityException | InvocationTargetException ex) { throw new TentackleRuntimeException("cannot create instance for " + serviceClass, ex); } }
addExplicitClassLoader(servicePath, serviceName, classLoader); removeExplicitClassLoader(servicePath, serviceName, classLoader);
return new ServiceFactory();
private static Class<? extends Logger> getConfiguredLoggerClass() { Logger logger = new DefaultLogger(DefaultLoggerFactory.class.getName()); try { Class<Logger> loggerClass = ServiceFactory.createServiceClass(Logger.class); logger.info("configured logger is " + loggerClass.getName()); return loggerClass; } catch (RuntimeException ex) { String msg = "no logging backend configured -> using DefaultLogger"; if (logger.isFineLoggable()) { logger.fine(msg, ex); } else { logger.info(msg); } return DefaultLogger.class; } }
/** * Creates a utility instance. */ public PdoUtilities() { ServiceFinder finder = ServiceFactory.getServiceFinder(); singularMap = new HashMap<>(); for (Map.Entry<String,String> entry: finder.createNameMap(Singular.class.getName()).entrySet()) { String singleName = StringHelper.stripEnclosingDoubleQuotes(entry.getKey()); singularMap.put(entry.getValue(), singleName); } pluralMap = new HashMap<>(); for (Map.Entry<String,String> entry: finder.createNameMap(Plural.class.getName()).entrySet()) { String multiName = StringHelper.stripEnclosingDoubleQuotes(entry.getKey()); pluralMap.put(entry.getValue(), multiName); } }
controllerClasses = ServiceFactory.getServiceFinder().findServiceProviders(FxController.class); Map<String,String> serviceMap = ServiceFactory.getServiceFinder().createNameMap(Configurator.class.getName()); for (Map.Entry<String,String> entry: serviceMap.entrySet()) { try { for (Class<ImageProvider> clazz: ServiceFactory.getServiceFinder().findServiceProviders(ImageProvider.class)) { ImageProviderService svc = clazz.getAnnotation(ImageProviderService.class); if (svc != null) { for (Class<ValueTranslator> clazz: ServiceFactory.getServiceFinder().findServiceProviders(ValueTranslator.class)) { ValueTranslatorService svc = clazz.getAnnotation(ValueTranslatorService.class); if (svc != null) { new DefaultClassMapper( ReflectionHelper.getClassBaseName(viewClass) + "-translator", ServiceFactory.getClassLoader(Constants.DEFAULT_SERVICE_PATH, ValueTranslator.class.getName()), new HashMap<>(), null)); translators.getNameMap().put(entry.getKey().getModelClass().getName(), entry.getValue().getName());
/** * C'tor for this class. * This class is supposed to have only one single instance. * @see #getInstance() */ public FormUtilities() { if (INSTANCE_COUNT.incrementAndGet() > 1) { throw new RuntimeException("Singleton! Only one single instance allowed."); } // create the window-Set windowListeners = new EventListenerList(); modalDialogs = new ArrayList<>(); try { // with fallback to DefaultFBF because of tools like Netbeans Matisse where META-INF/services does not work bindingFactory = ServiceFactory.createService(FormBindingFactory.class, DefaultFormBindingFactory.class); } catch (Exception ex) { throw new GUIRuntimeException("cannot create form binding factory", ex); } }
/** * Creates a list of providers. * * @return the non-empty list of providers, null if no providers found */ protected List<ResourceBundleControlProvider> createProviders() { List<ResourceBundleControlProvider> providerList = null; try { for (Class<ResourceBundleControlProvider> clazz: ServiceFactory.getServiceFinder().findServiceProviders(ResourceBundleControlProvider.class)) { if (providerList == null) { providerList = new ArrayList<>(); } providerList.add(clazz.getDeclaredConstructor().newInstance()); } } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { throw new TentackleRuntimeException("cannot load bundle control providers", ex); } return providerList; }
/** * Creates the factory. */ public FxBuilderFactory() { defaultFactory = new JavaFXBuilderFactory(); // pickup class mapping for builders Map<String,String> serviceMap = ServiceFactory.getServiceFinder().createNameMap(Builder.class.getName()); builderMap = new HashMap<>(); for (Map.Entry<String,String> entry: serviceMap.entrySet()) { try { Class<?> nodeClass = Class.forName(entry.getKey()); Class<?> builderClass = Class.forName(entry.getValue()); builderMap.put(nodeClass, builderClass); } catch (ClassNotFoundException ex) { throw new FxRuntimeException(ex); } } }
/** * Creates a map of classnames to mapped services. * <p> * They key of the map is the service name. * The value is the name providing the service which comes first in the classpath. * * @param serviceName the service class * @return the map */ @Override public synchronized Map<String, String> createNameMap(String serviceName) { Map<String, String> nameMap = new HashMap<>(); for (Map.Entry<String, URL> entry : ServiceFactory.getServiceFinder(Constants.MAPPED_SERVICE_PATH).findServiceConfigurations(serviceName).entrySet()) { String configuration = entry.getKey(); List<String> parts = StringHelper.split(configuration, ":= "); if (parts.size() != 2) { throw new TentackleRuntimeException("invalid service configuration '" + configuration + "' in " + entry.getValue()); } nameMap.putIfAbsent(parts.get(1), parts.get(0)); } return nameMap; }
/** * Creates a map of names to all mapped services.<p> * They key of the map is the service name. * The value is a list of names providing the service ordered by discovery along the classpath. * * @param serviceName the service class * @return the map */ @Override public synchronized Map<String, Set<String>> createNameMapToAll(String serviceName) { Map<String, Set<String>> nameMap = new HashMap<>(); for (Map.Entry<String, URL> entry : ServiceFactory.getServiceFinder(Constants.MAPPED_SERVICE_PATH).findServiceConfigurations(serviceName).entrySet()) { String configuration = entry.getKey(); List<String> parts = StringHelper.split(configuration, ":= "); if (parts.size() != 2) { throw new TentackleRuntimeException("invalid service configuration '" + configuration + "' in " + entry.getValue()); } String key = parts.get(1); Set<String> serviceList = nameMap.computeIfAbsent(key, k -> new HashSet<>()); serviceList.add(parts.get(0)); } return nameMap; }
@Override public synchronized Collection<ScriptingLanguage> getLanguages() { if (languages == null) { // load all languages via @Service // the abbreviations override previous ones in classpath // dto. for the default language // so it's easy to change the default or the meaning of an abbreviation languages = new HashMap<>(); try { for (Class<ScriptingLanguage> clazz: ServiceFactory.getServiceFinder().findServiceProviders(ScriptingLanguage.class)) { ScriptingLanguage language = clazz.getDeclaredConstructor().newInstance(); for (String abbrv: language.getAbbreviations()) { String abbrvLC = abbrv.toLowerCase(); languages.put(abbrvLC, language); LOGGER.info("added scripting language {0} as {1}", language, abbrvLC); } } } catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException | SecurityException | InvocationTargetException nfe) { throw new ScriptRuntimeException("supported scripting languages could not be determined", nfe); } } return languages.values(); }
ServiceFactory.getServiceFinder(classLoader, Constants.DEFAULT_SERVICE_PATH). findServiceConfigurations(BundleSupport.BUNDLE_SERVICE).entrySet()) { ServiceFactory.getServiceFinder(classLoader, Constants.BUNDLE_SERVICE_PATH). findServiceConfigurations(serviceName).entrySet()) { ModuleInfo info = ModuleSorter.INSTANCE.getModuleInfo(entry.getValue());
Map.Entry<String,URL> entry = ServiceFactory.getServiceFinder(). findFirstServiceConfiguration(interception.implementedByService()); implClass = (Class<? extends Interceptor>) Class.forName(entry.getKey()); Map.Entry<String,URL> entry = ServiceFactory.getServiceFinder(). findFirstServiceConfiguration(annotation.annotationType().getName()); implClass = (Class<? extends Interceptor>) Class.forName(entry.getKey());
scopesByIface = new HashMap<>(); try { Map<String,String> scopeMap = ServiceFactory.getServiceFinder().createNameMap(ValidationScopeService.class.getName()); for (Map.Entry<String,String> entry: scopeMap.entrySet()) { @SuppressWarnings("unchecked")