/** * Extract a Boolean out of a Property * * @param properties the properties * @param propertyName the name of the property * @param defaultValue the deulat value id none is found * @return the extracted property */ protected boolean extractBooleanProperty( Properties properties, String propertyName, boolean defaultValue) { boolean ret; String pString = PropertyUtil.extractAndLogProperty(propertyName, properties); if (pString != null) { ret = PropertyUtil.parseBoolean(pString); } else { ret = defaultValue; } return ret; }
/** Tries to load the class specified otherwise defaults to null */ public Map<String, CacheManagerPeerListener> createCachePeerListeners() { String className = null; Map<String, CacheManagerPeerListener> cacheManagerPeerListeners = new HashMap<String, CacheManagerPeerListener>(); List<FactoryConfiguration> cacheManagerPeerListenerFactoryConfigurations = configuration.getCacheManagerPeerListenerFactoryConfigurations(); boolean first = true; for (FactoryConfiguration factoryConfiguration : cacheManagerPeerListenerFactoryConfigurations) { if (factoryConfiguration != null) { className = factoryConfiguration.getFullyQualifiedClassPath(); } if (className == null) { LOG.fine( "No CachePeerListenerFactoryConfiguration specified. Not configuring a CacheManagerPeerListener."); return null; } else { CacheManagerPeerListenerFactory cacheManagerPeerListenerFactory = (CacheManagerPeerListenerFactory) ClassLoaderUtil.createNewInstance(className); Properties properties = PropertyUtil.parseProperties( factoryConfiguration.getProperties(), factoryConfiguration.getPropertySeparator()); CacheManagerPeerListener cacheManagerPeerListener = cacheManagerPeerListenerFactory.createCachePeerListener(cacheManager, properties); cacheManagerPeerListeners.put( cacheManagerPeerListener.getScheme(), cacheManagerPeerListener); } } return cacheManagerPeerListeners; }
private static float getSpeedAdjustmentFactor() { final String speedAdjustmentFactorString = PropertyUtil.extractAndLogProperty( "net.sf.ehcache.speedAdjustmentFactor", System.getProperties()); if (speedAdjustmentFactorString != null) { return Float.parseFloat(speedAdjustmentFactorString); } else { return 1; } }
/** * peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446, * multicastPacketTimeToLive=255 */ protected CacheManagerPeerProvider createAutomaticallyConfiguredCachePeerProvider( CacheManager cacheManager, Properties properties) throws IOException { String hostName = PropertyUtil.extractAndLogProperty(HOST_NAME, properties); InetAddress hostAddress = null; if (hostName != null && hostName.length() != 0) { hostAddress = InetAddress.getByName(hostName); if (hostName.equals("localhost")) { LOG.log( Level.WARNING, "Explicitly setting the multicast hostname to 'localhost' is not recommended. " + "It will only work if all CacheManager peers are on the same machine."); } } String groupAddressString = PropertyUtil.extractAndLogProperty(MULTICAST_GROUP_ADDRESS, properties); InetAddress groupAddress = InetAddress.getByName(groupAddressString); String multicastPortString = PropertyUtil.extractAndLogProperty(MULTICAST_GROUP_PORT, properties); Integer multicastPort = new Integer(multicastPortString); String packetTimeToLiveString = PropertyUtil.extractAndLogProperty(MULTICAST_PACKET_TTL, properties); Integer timeToLive; if (packetTimeToLiveString == null) { timeToLive = new Integer(1); LOG.log( Level.FINE, "No TTL set. Setting it to the default of 1, which means packets are limited to the same subnet."); } else { timeToLive = new Integer(packetTimeToLiveString); if (timeToLive.intValue() < 0 || timeToLive.intValue() > MAXIMUM_TTL) { throw new CacheException("The TTL must be set to a value between 0 and 255"); } } return new MulticastRMICacheManagerPeerProvider( cacheManager, groupAddress, multicastPort, timeToLive, hostAddress); }
/** * @param properties implementation specific properties. These are configured as comma separated * name value pairs in ehcache.xml */ public CacheManagerPeerProvider createCachePeerProvider( CacheManager cacheManager, Properties properties) throws CacheException { String peerDiscovery = PropertyUtil.extractAndLogProperty(PEER_DISCOVERY, properties); if (peerDiscovery == null || peerDiscovery.equalsIgnoreCase(AUTOMATIC_PEER_DISCOVERY)) { try { return createAutomaticallyConfiguredCachePeerProvider(cacheManager, properties); } catch (IOException e) { throw new CacheException( "Could not create CacheManagerPeerProvider. Initial cause was " + e.getMessage(), e); } } else if (peerDiscovery.equalsIgnoreCase(MANUALLY_CONFIGURED_PEER_DISCOVERY)) { return createManuallyConfiguredCachePeerProvider(properties); } else { return null; } }
/** * Extract a long out of a string. * * @param properties the property * @param propertyName the name of the property * @param defaultValue the default value if none is found * @return the extracted value */ protected long extractAsynchronousReplicationIntervalMillis( Properties properties, String propertyName, long defaultValue) { String parsedString = PropertyUtil.extractAndLogProperty(propertyName, properties); if (parsedString != null) { try { Long longValue = new Long(parsedString); return longValue.longValue(); } catch (NumberFormatException e) { LOG.warn( "Number format exception trying to set asynchronousReplicationIntervalMillis. " + "Using the default instead. String value was: '" + parsedString + "'"); } } return defaultValue; }
/** * Tries to load the class specified otherwise defaults to null. * * @param factoryConfiguration */ private static CacheLoader createCacheLoader( CacheConfiguration.CacheLoaderFactoryConfiguration factoryConfiguration, Ehcache cache) { String className = null; CacheLoader cacheLoader = null; if (factoryConfiguration != null) { className = factoryConfiguration.getFullyQualifiedClassPath(); } if (className == null) { LOG.fine("CacheLoader factory not configured. Skipping..."); } else { CacheLoaderFactory factory = (CacheLoaderFactory) ClassLoaderUtil.createNewInstance(className); Properties properties = PropertyUtil.parseProperties( factoryConfiguration.getProperties(), factoryConfiguration.getPropertySeparator()); cacheLoader = factory.createCacheLoader(cache, properties); } return cacheLoader; }
/** * Tries to load the class specified. * * @return If there is none returns null. */ public final CacheManagerEventListener createCacheManagerEventListener() throws CacheException { String className = null; FactoryConfiguration cacheManagerEventListenerFactoryConfiguration = configuration.getCacheManagerEventListenerFactoryConfiguration(); if (cacheManagerEventListenerFactoryConfiguration != null) { className = cacheManagerEventListenerFactoryConfiguration.getFullyQualifiedClassPath(); } if (className == null || className.length() == 0) { LOG.fine("No CacheManagerEventListenerFactory class specified. Skipping..."); return null; } else { CacheManagerEventListenerFactory factory = (CacheManagerEventListenerFactory) ClassLoaderUtil.createNewInstance(className); Properties properties = PropertyUtil.parseProperties( cacheManagerEventListenerFactoryConfiguration.properties, cacheManagerEventListenerFactoryConfiguration.getPropertySeparator()); return factory.createCacheManagerEventListener(properties); } }
/** * Tries to create a CacheLoader from the configuration using the factory specified. * * @return The CacheExceptionHandler, or null if it could not be found. */ public final CacheExceptionHandler createCacheExceptionHandler( CacheConfiguration.CacheExceptionHandlerFactoryConfiguration factoryConfiguration) throws CacheException { String className = null; CacheExceptionHandler cacheExceptionHandler = null; if (factoryConfiguration != null) { className = factoryConfiguration.getFullyQualifiedClassPath(); } if (className == null || className.length() == 0) { LOG.fine("No CacheExceptionHandlerFactory class specified. Skipping..."); } else { CacheExceptionHandlerFactory factory = (CacheExceptionHandlerFactory) ClassLoaderUtil.createNewInstance(className); Properties properties = PropertyUtil.parseProperties( factoryConfiguration.getProperties(), factoryConfiguration.getPropertySeparator()); return factory.createExceptionHandler(properties); } return cacheExceptionHandler; }
/** * peerDiscovery=manual, rmiUrls=//hostname:port/cacheName //hostname:port/cacheName * //hostname:port/cacheName */ protected CacheManagerPeerProvider createManuallyConfiguredCachePeerProvider( Properties properties) { String rmiUrls = PropertyUtil.extractAndLogProperty(RMI_URLS, properties); if (rmiUrls == null || rmiUrls.length() == 0) { LOG.log( Level.INFO, "Starting manual peer provider with empty list of peers. " + "No replication will occur unless peers are added."); rmiUrls = new String(); } rmiUrls = rmiUrls.trim(); StringTokenizer stringTokenizer = new StringTokenizer(rmiUrls, PayloadUtil.URL_DELIMITER); RMICacheManagerPeerProvider rmiPeerProvider = new ManualRMICacheManagerPeerProvider(); while (stringTokenizer.hasMoreTokens()) { String rmiUrl = stringTokenizer.nextToken(); rmiUrl = rmiUrl.trim(); rmiPeerProvider.registerPeer(rmiUrl); if (LOG.isLoggable(Level.FINE)) { LOG.log(Level.FINE, "Registering peer " + rmiUrl); } } return rmiPeerProvider; }