/** * Set the properties of an object passed as a parameter in one go. The <code>properties</code> * are parsed relative to a <code>prefix</code>. * <p> * @param obj The object to configure. * @param properties A java.util.Properties containing keys and values. * @param prefix Only keys having the specified prefix will be set. */ public static void setProperties( Object obj, Properties properties, String prefix ) { new PropertySetter( obj ).setProperties( properties, prefix ); }
PropertyDescriptor prop = getPropertyDescriptor( name ); setProperty( prop, name, value );
/** * Gets the propertyDescriptor attribute of the PropertySetter object * @param name * @return The propertyDescriptor value */ protected PropertyDescriptor getPropertyDescriptor( String name ) { if ( props == null ) { introspect(); } for ( int i = 0; i < props.length; i++ ) { if ( name.equals( props[i].getName() ) ) { return props[i]; } } return null; } }
@Test public void testConvertArg() { PropertySetter ps = new PropertySetter(this); Object s = ps.convertArg("test", String.class); assertEquals("Should be a string", "test", s); Object i = ps.convertArg("1", Integer.TYPE); assertEquals("Should be an integer", Integer.valueOf(1), i); Object l = ps.convertArg("1", Long.TYPE); assertEquals("Should be a long", Long.valueOf(1), l); Object b = ps.convertArg("true", Boolean.TYPE); assertEquals("Should be a boolean", Boolean.TRUE, b); Object e = ps.convertArg("TWO", EnumTest.class); assertEquals("Should be an enum", EnumTest.TWO, e); Object f = ps.convertArg("test.conf", File.class); assertTrue("Should be a file", f instanceof File); }
/** * Configure. * <p> * jcs.remotehttpcache.serverattributes.ATTRIBUTENAME=ATTRIBUTEVALUE * <p> * @param prop * @return RemoteCacheServerAttributesconfigureRemoteCacheServerAttributes */ protected RemoteHttpCacheServerAttributes configureRemoteHttpCacheServerAttributes( Properties prop ) { RemoteHttpCacheServerAttributes rcsa = new RemoteHttpCacheServerAttributes(); // configure automatically PropertySetter.setProperties( rcsa, prop, IRemoteHttpCacheConstants.HTTP_CACHE_SERVER_ATTRIBUTES_PROPERTY_PREFIX + "." ); return rcsa; }
try arg = convertArg( value, paramTypes[0] );
/** * Set the properties for the object that match the <code>prefix</code> passed as parameter. * <p> * @param properties The new properties value * @param prefix The new properties value */ public void setProperties( Properties properties, String prefix ) { int len = prefix.length(); for ( Enumeration<?> e = properties.propertyNames(); e.hasMoreElements(); ) { String key = (String) e.nextElement(); // handle only properties that start with the desired prefix. if ( key.startsWith( prefix ) ) { // ignore key if it contains dots after the prefix if ( key.indexOf( '.', len + 1 ) > 0 ) { //System.err.println("----------Ignoring---["+key // +"], prefix=["+prefix+"]."); continue; } String value = OptionConverter.findAndSubst( key, properties ); key = key.substring( len ); setProperty( key, value ); } } }
@Test public void testConvertArg() { PropertySetter ps = new PropertySetter(this); Object s = ps.convertArg("test", String.class); assertEquals("Should be a string", "test", s); Object i = ps.convertArg("1", Integer.TYPE); assertEquals("Should be an integer", Integer.valueOf(1), i); Object l = ps.convertArg("1", Long.TYPE); assertEquals("Should be a long", Long.valueOf(1), l); Object b = ps.convertArg("true", Boolean.TYPE); assertEquals("Should be a boolean", Boolean.TRUE, b); Object e = ps.convertArg("TWO", EnumTest.class); assertEquals("Should be an enum", EnumTest.TWO, e); Object f = ps.convertArg("test.conf", File.class); assertTrue("Should be a file", f instanceof File); }
/** * Configure. * <p> * jcs.remotehttpcache.serverattributes.ATTRIBUTENAME=ATTRIBUTEVALUE * <p> * @param prop * @return RemoteCacheServerAttributesconfigureRemoteCacheServerAttributes */ protected static RemoteHttpCacheServerAttributes configureRemoteHttpCacheServerAttributes( Properties prop ) { RemoteHttpCacheServerAttributes rcsa = new RemoteHttpCacheServerAttributes(); // configure automatically PropertySetter.setProperties( rcsa, prop, IRemoteHttpCacheConstants.HTTP_CACHE_SERVER_ATTRIBUTES_PROPERTY_PREFIX + "." ); return rcsa; } }
try arg = convertArg( value, paramTypes[0] );
/** * Set the properties for the object that match the <code>prefix</code> passed as parameter. * <p> * @param properties The new properties value * @param prefix The new properties value */ public void setProperties( Properties properties, String prefix ) { int len = prefix.length(); for ( Enumeration<?> e = properties.propertyNames(); e.hasMoreElements(); ) { String key = (String) e.nextElement(); // handle only properties that start with the desired prefix. if ( key.startsWith( prefix ) ) { // ignore key if it contains dots after the prefix if ( key.indexOf( '.', len + 1 ) > 0 ) { //System.err.println("----------Ignoring---["+key // +"], prefix=["+prefix+"]."); continue; } String value = OptionConverter.findAndSubst( key, properties ); key = key.substring( len ); setProperty( key, value ); } } }
/** * This configures an object specific custom factory. This will be configured for just this * object in the registry. This can be null. * <p> * @param props * @return RMISocketFactory */ protected static RMISocketFactory configureObjectSpecificCustomFactory( Properties props ) { RMISocketFactory customRMISocketFactory = OptionConverter.instantiateByKey( props, CUSTOM_RMI_SOCKET_FACTORY_PROPERTY_PREFIX, null ); if ( customRMISocketFactory != null ) { PropertySetter.setProperties( customRMISocketFactory, props, CUSTOM_RMI_SOCKET_FACTORY_PROPERTY_PREFIX + "." ); if ( log.isInfoEnabled() ) { log.info( "Will use server specific custom socket factory. " + customRMISocketFactory ); } } else { if ( log.isInfoEnabled() ) { log.info( "No server specific custom socket factory defined." ); } } return customRMISocketFactory; }
/** * Set the properties of an object passed as a parameter in one go. The <code>properties</code> * are parsed relative to a <code>prefix</code>. * <p> * @param obj The object to configure. * @param properties A java.util.Properties containing keys and values. * @param prefix Only keys having the specified prefix will be set. */ public static void setProperties( Object obj, Properties properties, String prefix ) { new PropertySetter( obj ).setProperties( properties, prefix ); }
PropertyDescriptor prop = getPropertyDescriptor( name ); setProperty( prop, name, value );
/** * Gets the propertyDescriptor attribute of the PropertySetter object * @param name * @return The propertyDescriptor value */ protected PropertyDescriptor getPropertyDescriptor( String name ) { if ( props == null ) { introspect(); } for ( int i = 0; i < props.length; i++ ) { if ( name.equals( props[i].getName() ) ) { return props[i]; } } return null; } }
PropertySetter.setProperties( cacheEventLogger, props, cacheEventLoggerAttributePrefix + "." ); if ( log.isInfoEnabled() )
/** * This configures an object specific custom factory. This will be configured for just this * object in the registry. This can be null. * <p> * @param props * @return RMISocketFactory */ protected static RMISocketFactory configureObjectSpecificCustomFactory( Properties props ) { RMISocketFactory customRMISocketFactory = OptionConverter.instantiateByKey( props, CUSTOM_RMI_SOCKET_FACTORY_PROPERTY_PREFIX, null ); if ( customRMISocketFactory != null ) { PropertySetter.setProperties( customRMISocketFactory, props, CUSTOM_RMI_SOCKET_FACTORY_PROPERTY_PREFIX + "." ); if ( log.isInfoEnabled() ) { log.info( "Will use server specific custom socket factory. " + customRMISocketFactory ); } } else { if ( log.isInfoEnabled() ) { log.info( "No server specific custom socket factory defined." ); } } return customRMISocketFactory; }
PropertySetter.setProperties( cacheEventLogger, props, cacheEventLoggerAttributePrefix + "." ); if ( log.isInfoEnabled() )
/** * Configures the attributes using the properties. * <p> * @param poolName the name of the pool * @param props the configuration properties for the pool * @return JDBCDiskCachePoolAccessAttributes */ protected JDBCDiskCachePoolAccessAttributes configurePoolAccessAttributes( String poolName, Properties props ) { JDBCDiskCachePoolAccessAttributes poolAttributes = new JDBCDiskCachePoolAccessAttributes(); String poolAccessAttributePrefix = POOL_CONFIGURATION_PREFIX + poolName + ATTRIBUTE_PREFIX; PropertySetter.setProperties( poolAttributes, props, poolAccessAttributePrefix + "." ); poolAttributes.setPoolName( poolName ); if ( log.isInfoEnabled() ) { log.info( "Configured attributes " + poolAttributes ); } return poolAttributes; }
PropertySetter.setProperties( eAttr, props, attrName + "." );