/** * Creates a new class instance with the given loader. Logs errors along the way. * * @param loader the classloader to load the class * @param className a fully qualified class name * @return the newly created instance * @throws CacheException if instance cannot be created due to a missing class or exception */ public static Object createNewInstance(ClassLoader loader, String className) throws CacheException { return createNewInstance(loader, className, new Class[0], new Object[0]); }
/** * Load resource. * * @param configurationResourceName * the configuration resource name * @return the uRL */ private URL loadResource(String configurationResourceName) { ClassLoader standardClassloader = ClassLoaderUtil.getStandardClassLoader(); URL url = null; if (standardClassloader != null) { url = standardClassloader.getResource(configurationResourceName); } if (url == null) { url = this.getClass().getResource(configurationResourceName); } log.info("Creating EhCacheFactory from a specified resource: " + configurationResourceName + " Resolved to URL: " + url); if (url == null) { log.warn("A configurationResourceName was set to {} but the resource could not be loaded from the classpath.Ehcache will configure itself using defaults." ,configurationResourceName); } return url; }
/** * Returns the factory class for this mode or null if class is not present in classpath * * @return the factory class for this mode or null if class is not present in classpath */ public Class getFactoryClassOrNull() { try { return ClassLoaderUtil.loadClass(factoryClassName); } catch (ClassNotFoundException e) { return null; } } }
/** * Load the given class by name * * @param className a fully qualified class name * @return Class the loaded class * @throws ClassNotFoundException if the class cannot be loaded * @since 1.7 */ public static Class loadClass(String className) throws ClassNotFoundException { Class clazz; try { clazz = Class.forName(className, true, getStandardClassLoader()); } catch (ClassNotFoundException e) { //try fallback clazz = Class.forName(className, true, getFallbackClassLoader()); } return clazz; }
/** * Get (and potentially) instantiate the instance * * @param cacheConfiguration the cache configuration * @param loader classloader to use to create the instance * @return the instance */ public ElementValueComparator createElementComparatorInstance(CacheConfiguration cacheConfiguration, ClassLoader loader) { try { if (DEFAULT_IMPL.equals(className)) { loader = getClass().getClassLoader(); } return (ElementValueComparator) ClassLoaderUtil.createNewInstance( loader, className, new Class[] {CacheConfiguration.class}, new Object[] {cacheConfiguration} ); } catch (ClassCastException cce) { throw new CacheException(className + " must implement " + ElementValueComparator.class.getName(), cce); } }
/** * Load a resource from the classpath. */ protected static URL loadResource(String configurationResourceName) { ClassLoader standardClassloader = ClassLoaderUtil.getStandardClassLoader(); URL url = null; if (standardClassloader != null) { url = standardClassloader.getResource(configurationResourceName); } if (url == null) { url = AbstractEhcacheRegionFactory.class.getResource(configurationResourceName); } LOG.debug("Creating EhCacheRegionFactory from a specified resource: {}. Resolved to URL: {}", configurationResourceName, url); if (url == null) { LOG.warn("A configurationResourceName was set to {} but the resource could not be loaded from the classpath." + "Ehcache will configure itself using defaults.", configurationResourceName); } return url; }
@Override protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException { return ClassLoaderUtil.loadClass(desc.getName()); }
/** * Construct the extractor for this attribute configuration */ public AttributeExtractor constructExtractor(ClassLoader loader) { if (name == null) { throw new InvalidConfigurationException("search attribute has no name"); } if (expression != null) { return new ReflectionAttributeExtractor(expression); } else if (className != null) { if (properties != null) { return (AttributeExtractor) ClassLoaderUtil.createNewInstance(loader, className, new Class[] {Properties.class}, new Object[] {PropertyUtil.parseProperties(properties, propertySeparator)}); } else { return (AttributeExtractor) ClassLoaderUtil.createNewInstance(loader, className); } } else { return new JavaBeanAttributeExtractor(name); } }
/** * Load the supplied resource from the classpath. */ protected URL loadResource(String configurationResourceName) { ClassLoader standardClassloader = ClassLoaderUtil.getStandardClassLoader(); URL url = null; if (standardClassloader != null) { url = standardClassloader.getResource(configurationResourceName); } if (url == null) { url = this.getClass().getResource(configurationResourceName); } LOG.debug("Creating EhCacheProvider from a specified resource: {}. Resolved to URL: ", configurationResourceName, url); if (url == null) { LOG.warn("A configurationResourceName was set to {} but the resource could not be loaded from the classpath." + "Ehcache will configure itself using defaults.", configurationResourceName); } return url; } }
/** * {@inheritDoc} */ @Override protected TransactionManager doLookup() { TransactionManager transactionManager = null; try { Class txManagerClass = ClassLoaderUtil.loadClass(classname); transactionManager = (TransactionManager) txManagerClass.newInstance(); } catch (ClassNotFoundException e) { LOG.debug("FactorySelector failed lookup", e); } catch (InstantiationException e) { LOG.debug("FactorySelector failed lookup", e); } catch (IllegalAccessException e) { LOG.debug("FactorySelector failed lookup", e); } return transactionManager; } }
/** * Tries to load the class specified. * * @return If there is none returns null. * @param cacheManager */ public final CacheManagerEventListener createCacheManagerEventListener(CacheManager cacheManager) throws CacheException { String className = null; FactoryConfiguration cacheManagerEventListenerFactoryConfiguration = configuration.getCacheManagerEventListenerFactoryConfiguration(); if (cacheManagerEventListenerFactoryConfiguration != null) { className = cacheManagerEventListenerFactoryConfiguration.getFullyQualifiedClassPath(); } if (className == null || className.length() == 0) { LOG.debug("No CacheManagerEventListenerFactory class specified. Skipping..."); return null; } else { CacheManagerEventListenerFactory factory = (CacheManagerEventListenerFactory) ClassLoaderUtil.createNewInstance(loader, className); Properties properties = PropertyUtil.parseProperties(cacheManagerEventListenerFactoryConfiguration.properties, cacheManagerEventListenerFactoryConfiguration.getPropertySeparator()); return factory.createCacheManagerEventListener(cacheManager, properties); } }
private static SizeOfFilter getUserFilter() { String userFilterProperty = System.getProperty(USER_FILTER_RESOURCE); if (userFilterProperty != null) { List<URL> filterUrls = new ArrayList<URL>(); try { filterUrls.add(new URL(userFilterProperty)); } catch (MalformedURLException e) { LOG.debug("MalformedURLException using {} as a URL", userFilterProperty); } try { filterUrls.add(new File(userFilterProperty).toURI().toURL()); } catch (MalformedURLException e) { LOG.debug("MalformedURLException using {} as a file URL", userFilterProperty); } filterUrls.add(ClassLoaderUtil.getStandardClassLoader().getResource(userFilterProperty)); for (URL filterUrl : filterUrls) { SizeOfFilter filter; try { filter = new ResourceSizeOfFilter(filterUrl); LOG.info("Using user supplied filter @ {}", filterUrl); return filter; } catch (IOException e) { LOG.debug("IOException while loading user size-of filter resource", e); } } } return null; }
/** * {@inheritDoc} */ @Override public void unregisterResource(EhcacheXAResource ehcacheXAResource, boolean forRecovery) { String uniqueName = ehcacheXAResource.getCacheName(); try { Class producerClass = ClassLoaderUtil.loadClass("bitronix.tm.resource.ehcache.EhCacheXAResourceProducer"); Class[] signature = new Class[] {String.class, XAResource.class}; Object[] args = new Object[] {uniqueName, ehcacheXAResource}; Method method = producerClass.getMethod("unregisterXAResource", signature); method.invoke(null, args); } catch (Exception e) { LOG.error("unable to unregister resource of cache " + uniqueName + " with BTM", e); } } }
/** * Tries to load a BootstrapCacheLoader from the class specified. * @param loader * * @return If there is none returns null. */ private static final BootstrapCacheLoader createBootstrapCacheLoader( CacheConfiguration.BootstrapCacheLoaderFactoryConfiguration factoryConfiguration, ClassLoader loader) throws CacheException { String className = null; BootstrapCacheLoader bootstrapCacheLoader = null; if (factoryConfiguration != null) { className = factoryConfiguration.getFullyQualifiedClassPath(); } if (className == null || className.length() == 0) { LOG.debug("No BootstrapCacheLoaderFactory class specified. Skipping..."); } else { BootstrapCacheLoaderFactory factory = (BootstrapCacheLoaderFactory) ClassLoaderUtil.createNewInstance(loader, className); Properties properties = PropertyUtil.parseProperties(factoryConfiguration.getProperties(), factoryConfiguration.getPropertySeparator()); return factory.createBootstrapCacheLoader(properties); } return bootstrapCacheLoader; }
/** * Configures a bean from an XML file in the classpath. */ public static Configuration parseConfiguration() throws CacheException { ClassLoader standardClassloader = ClassLoaderUtil.getStandardClassLoader(); URL url = null; if (standardClassloader != null) { url = standardClassloader.getResource(DEFAULT_CLASSPATH_CONFIGURATION_FILE); } if (url == null) { url = ConfigurationFactory.class.getResource(DEFAULT_CLASSPATH_CONFIGURATION_FILE); } if (url != null) { LOG.debug("Configuring ehcache from ehcache.xml found in the classpath: " + url); } else { url = ConfigurationFactory.class.getResource(FAILSAFE_CLASSPATH_CONFIGURATION_FILE); LOG.warn("No configuration found. Configuring ehcache from ehcache-failsafe.xml " + " found in the classpath: {}", url); } Configuration configuration = parseConfiguration(url); configuration.setSource(ConfigurationSource.getConfigurationSource()); return configuration; }
/** * {@inheritDoc} */ @Override public void registerResource(EhcacheXAResource ehcacheXAResource, boolean forRecovery) { String uniqueName = ehcacheXAResource.getCacheName(); try { Class producerClass = ClassLoaderUtil.loadClass("bitronix.tm.resource.ehcache.EhCacheXAResourceProducer"); Class[] signature = new Class[] {String.class, XAResource.class}; Object[] args = new Object[] {uniqueName, ehcacheXAResource}; Method method = producerClass.getMethod("registerXAResource", signature); method.invoke(null, args); } catch (Exception e) { LOG.error("unable to register resource of cache " + uniqueName + " with BTM", e); } }
/** * Tries to create a CacheLoader from the configuration using the factory * specified. * * @return The CacheExceptionHandler, or null if it could not be found. */ public static CacheExceptionHandler createCacheExceptionHandler( CacheConfiguration.CacheExceptionHandlerFactoryConfiguration factoryConfiguration, ClassLoader loader) throws CacheException { String className = null; CacheExceptionHandler cacheExceptionHandler = null; if (factoryConfiguration != null) { className = factoryConfiguration.getFullyQualifiedClassPath(); } if (className == null || className.length() == 0) { LOG.debug("No CacheExceptionHandlerFactory class specified. Skipping..."); } else { CacheExceptionHandlerFactory factory = (CacheExceptionHandlerFactory) ClassLoaderUtil.createNewInstance(loader, className); Properties properties = PropertyUtil.parseProperties(factoryConfiguration.getProperties(), factoryConfiguration.getPropertySeparator()); return factory.createExceptionHandler(properties); } return cacheExceptionHandler; }
final ClassLoader contextClassLoader = ClassLoaderUtil.getStandardClassLoader(); final URL configUrl = contextClassLoader.getResource(file);
/** * {@inheritDoc} */ @Override protected TransactionManager doLookup() { TransactionManager transactionManager = null; try { Class factoryClass = ClassLoaderUtil.loadClass(factoryClassName); Class[] signature = null; Object[] args = null; Method method = factoryClass.getMethod(factoryMethodName, signature); transactionManager = (TransactionManager) method.invoke(null, args); } catch (ClassNotFoundException e) { LOG.debug("FactorySelector failed lookup", e); } catch (NoSuchMethodException e) { LOG.debug("FactorySelector failed lookup", e); } catch (InvocationTargetException e) { LOG.debug("FactorySelector failed lookup", e); } catch (IllegalAccessException e) { LOG.debug("FactorySelector failed lookup", e); } return transactionManager; } }
/** * Tries to load the class specified otherwise defaults to null. * * @param factoryConfiguration * @param loader */ private static CacheLoader createCacheLoader( CacheConfiguration.CacheLoaderFactoryConfiguration factoryConfiguration, Ehcache cache, ClassLoader loader) { String className = null; CacheLoader cacheLoader = null; if (factoryConfiguration != null) { className = factoryConfiguration.getFullyQualifiedClassPath(); } if (className == null) { LOG.debug("CacheLoader factory not configured. Skipping..."); } else { CacheLoaderFactory factory = (CacheLoaderFactory) ClassLoaderUtil.createNewInstance(loader, className); Properties properties = PropertyUtil.parseProperties(factoryConfiguration.getProperties(), factoryConfiguration.getPropertySeparator()); cacheLoader = factory.createCacheLoader(cache, properties); } return cacheLoader; }