📄 runtimeinstance.java
字号:
log.error("Cannot get Velocity Runtime default properties!", ioe);
}
finally
{
try
{
if (inputStream != null)
{
inputStream.close();
}
}
catch (IOException ioe)
{
log.error("Cannot close Velocity Runtime default properties!", ioe);
}
}
}
/**
* Allows an external system to set a property in
* the Velocity Runtime.
*
* @param key property key
* @param value property value
*/
public void setProperty(String key, Object value)
{
if (overridingProperties == null)
{
overridingProperties = new ExtendedProperties();
}
overridingProperties.setProperty(key, value);
}
/**
* Allow an external system to set an ExtendedProperties
* object to use. This is useful where the external
* system also uses the ExtendedProperties class and
* the velocity configuration is a subset of
* parent application's configuration. This is
* the case with Turbine.
*
* @param configuration
*/
public void setConfiguration( ExtendedProperties configuration)
{
if (overridingProperties == null)
{
overridingProperties = configuration;
}
else
{
// Avoid possible ConcurrentModificationException
if (overridingProperties != configuration)
{
overridingProperties.combine(configuration);
}
}
}
/**
* Add a property to the configuration. If it already
* exists then the value stated here will be added
* to the configuration entry. For example, if
*
* resource.loader = file
*
* is already present in the configuration and you
*
* addProperty("resource.loader", "classpath")
*
* Then you will end up with a Vector like the
* following:
*
* ["file", "classpath"]
*
* @param key
* @param value
*/
public void addProperty(String key, Object value)
{
if (overridingProperties == null)
{
overridingProperties = new ExtendedProperties();
}
overridingProperties.addProperty(key, value);
}
/**
* Clear the values pertaining to a particular
* property.
*
* @param key of property to clear
*/
public void clearProperty(String key)
{
if (overridingProperties != null)
{
overridingProperties.clearProperty(key);
}
}
/**
* Allows an external caller to get a property. The calling
* routine is required to know the type, as this routine
* will return an Object, as that is what properties can be.
*
* @param key property to return
* @return Value of the property or null if it does not exist.
*/
public Object getProperty(String key)
{
Object o = null;
/**
* Before initialization, check the user-entered properties first.
*/
if (!initialized && !initializing && overridingProperties != null)
{
o = overridingProperties.get(key);
}
/**
* After initialization, configuration will hold all properties.
*/
if (o == null)
{
o = configuration.getProperty(key);
}
if (o instanceof String)
{
return StringUtils.nullTrim((String) o);
}
else
{
return o;
}
}
/**
* Initialize Velocity properties, if the default
* properties have not been laid down first then
* do so. Then proceed to process any overriding
* properties. Laying down the default properties
* gives a much greater chance of having a
* working system.
*/
private void initializeProperties()
{
/*
* Always lay down the default properties first as
* to provide a solid base.
*/
if (configuration.isInitialized() == false)
{
setDefaultProperties();
}
if( overridingProperties != null)
{
configuration.combine(overridingProperties);
}
}
/**
* Initialize the Velocity Runtime with a Properties
* object.
*
* @param p
* @throws Exception When an error occurs during initialization.
*/
public void init(Properties p) throws Exception
{
overridingProperties = ExtendedProperties.convertProperties(p);
init();
}
/**
* Initialize the Velocity Runtime with the name of
* ExtendedProperties object.
*
* @param configurationFile
* @throws Exception When an error occurs during initialization.
*/
public void init(String configurationFile)
throws Exception
{
overridingProperties = new ExtendedProperties(configurationFile);
init();
}
private void initializeResourceManager()
throws Exception
{
/*
* Which resource manager?
*/
String rm = getString(RuntimeConstants.RESOURCE_MANAGER_CLASS);
if (rm != null && rm.length() > 0)
{
/*
* if something was specified, then make one.
* if that isn't a ResourceManager, consider
* this a huge error and throw
*/
Object o = null;
try
{
o = ClassUtils.getNewInstance( rm );
}
catch (ClassNotFoundException cnfe )
{
String err = "The specified class for ResourceManager (" + rm
+ ") does not exist or is not accessible to the current classloader.";
log.error(err);
throw new Exception(err);
}
if (!(o instanceof ResourceManager))
{
String err = "The specified class for ResourceManager (" + rm
+ ") does not implement " + ResourceManager.class.getName()
+ "; Velocity is not initialized correctly.";
log.error(err);
throw new Exception(err);
}
resourceManager = (ResourceManager) o;
resourceManager.initialize(this);
}
else
{
/*
* someone screwed up. Lets not fool around...
*/
String err = "It appears that no class was specified as the"
+ " ResourceManager. Please ensure that all configuration"
+ " information is correct.";
log.error(err);
throw new Exception( err );
}
}
private void initializeEventHandlers()
throws Exception
{
eventCartridge = new EventCartridge();
/**
* For each type of event handler, get the class name, instantiate it, and store it.
*/
String[] referenceinsertion = configuration.getStringArray(RuntimeConstants.EVENTHANDLER_REFERENCEINSERTION);
if ( referenceinsertion != null )
{
for ( int i=0; i < referenceinsertion.length; i++ )
{
EventHandler ev = initializeSpecificEventHandler(referenceinsertion[i],RuntimeConstants.EVENTHANDLER_REFERENCEINSERTION,ReferenceInsertionEventHandler.class);
if (ev != null)
eventCartridge.addReferenceInsertionEventHandler((ReferenceInsertionEventHandler) ev);
}
}
String[] nullset = configuration.getStringArray(RuntimeConstants.EVENTHANDLER_NULLSET);
if ( nullset != null )
{
for ( int i=0; i < nullset.length; i++ )
{
EventHandler ev = initializeSpecificEventHandler(nullset[i],RuntimeConstants.EVENTHANDLER_NULLSET,NullSetEventHandler.class);
if (ev != null)
eventCartridge.addNullSetEventHandler((NullSetEventHandler) ev);
}
}
String[] methodexception = configuration.getStringArray(RuntimeConstants.EVENTHANDLER_METHODEXCEPTION);
if ( methodexception != null )
{
for ( int i=0; i < methodexception.length; i++ )
{
EventHandler ev = initializeSpecificEventHandler(methodexception[i],RuntimeConstants.EVENTHANDLER_METHODEXCEPTION,MethodExceptionEventHandler.class);
if (ev != null)
eventCartridge.addMethodExceptionHandler((MethodExceptionEventHandler) ev);
}
}
String[] includeHandler = configuration.getStringArray(RuntimeConstants.EVENTHANDLER_INCLUDE);
if ( includeHandler != null )
{
for ( int i=0; i < includeHandler.length; i++ )
{
EventHandler ev = initializeSpecificEventHandler(includeHandler[i],RuntimeConstants.EVENTHANDLER_INCLUDE,IncludeEventHandler.class);
if (ev != null)
eventCartridge.addIncludeEventHandler((IncludeEventHandler) ev);
}
}
String[] invalidReferenceSet = configuration.getStringArray(RuntimeConstants.EVENTHANDLER_INVALIDREFERENCES);
if ( invalidReferenceSet != null )
{
for ( int i=0; i < invalidReferenceSet.length; i++ )
{
EventHandler ev = initializeSpecificEventHandler(invalidReferenceSet[i],RuntimeConstants.EVENTHANDLER_INVALIDREFERENCES,InvalidReferenceEventHandler.class);
if (ev != null)
{
eventCartridge.addInvalidReferenceEventHandler((InvalidReferenceEventHandler) ev);
}
}
}
}
private EventHandler initializeSpecificEventHandler(String classname, String paramName, Class EventHandlerInterface)
throws Exception
{
if ( classname != null && classname.length() > 0)
{
Object o = null;
try {
o = ClassUtils.getNewInstance(classname);
}
catch (ClassNotFoundException cnfe )
{
String err = "The specified class for "
+ paramName + " (" + classname
+ ") does not exist or is not accessible to the current classloader.";
log.error(err);
throw new Exception(err);
}
if (!EventHandlerInterface.isAssignableFrom(EventHandlerInterface))
{
String err = "The specified class for " + paramName + " ("
+ classname + ") does not implement "
+ EventHandlerInterface.getName()
+ "; Velocity is not initialized correctly.";
log.error(err);
throw new Exception(err);
}
EventHandler ev = (EventHandler) o;
if ( ev instanceof RuntimeServicesAware )
((RuntimeServicesAware) ev).setRuntimeServices(this);
return ev;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -