I am using log4j with Java and wanted to configure my XML-Config-File (I need to use XML for the ErrorHandler), so that some Properties in the XML (like the Backup-Value for the RollingFileAppender) could be changed within the DOMConfigurator of the log4j-API. This class also got the subst()-method, which should substitute the chosen values, but I really donĀ“t know how to handle it.
If their is no way changing the config with the DOMConfigurator, which else possibilities did I got to easily correct values in an ambiguous XML-File (So to say, because the XML-Tags are not unique or only their Tag-names, which are values itself)? My XML is kind of static or hand-written.
Why can't you fix the original XML file on the disk?
If for some reason that's not possible, I would rather try altering the configuration directly via Logger, after the config file has been loaded.
Related
I have a spring boot application with /resources/application.properties, when deployed there is a application.properties next to the jar (and the one inside it).
Somewhere in the code I use ResourceBundle.getBundle("application").getString("x") but that always return the value defined in the properties inside the jar.
My objective is to get the value from the external file if it exists, if not then I need to get the value from inside the jar. Just like the default springs behavior but I'm not able to achieve this.
edit:
You can either use the solution in the correct answer below or you can rely on springs Environment by autowiring it and using getProperty()
ResourceBundle.getBundle(String baseName) relies on the classloader and not directly the file system to find the resource.
This is equivalent to invoke the overloaded one :
getBundle(baseName, Locale.getDefault(), this.getClass().getClassLoader()),
So your result is expected.
What you look for is PropertyResourceBundle.
To create an instance of you need either a Reader or an InputStream.
You could load both external and internal files.
ResourceBundle internalProps = ResourceBundle.getBundle("application");
ResourceBundle externalProps = new PropertyResourceBundle(Files.newInputStream(Paths.get("application.properties"));
You could so use the external ResourceBundle in first intention and the second one as fallback.
I'm using Hibernate and java.util.Logger class for logs in my project. I have a separate config file for both. I am able to switch between showing and not showing SQL queries by setting org.hibernate.SQL.level property to ALL in the log configuration file, but I can't figure out how to do it programatically (I want to handle this through run parameters but without having to use two seperate log configuration files).
So far I have tried setting this parameter in hibernate Configuration class, to no avail (properties are getting set, I double checked, but no queries show up).
Then I figured it must be handled by Logger class itself, but LogManager does not have any methods for setting a property. Browsing through the web guided me towards FileHandler class but I am able to set only the 'usual' log properties (like pattern, level, etc).
Does it mean I'm wrong in thinking I have to change the Logger class and it should in fact be set in hibernate's Configuration? If that's the case, why did it not work?
...I can't figure out how to do it programmatically (I want to handle this through run parameters but without having to use two separate log configuration files).
I would have assumed that the current situation solved the problem as log configurations are set using a run-time parameter. That said, the general approach is to get and store a strong reference to the logger and change the properties of that logger.
private static final Logger hardRef = Logger.getLogger("org.hibernate.SQL");
static {
if (traceSql()) {
hardRef.setLevel(Level.ALL);
}
}
private static boolean traceSql() {
return true; //#todo Add code.
}
I have a configuration (config.properties) something like
app.rootDir=
app.reportDir=${app.rootDir}/report
The app.rootDir is not a fixed parameter and it must be initialized by external module. I need keep the ${app.reportDir} keep dynamic reference to ${app.rootDir}.
Use pseudo code to illustrate the problem:
// Init the root dir as '/usr/app'
config.setValue('app.rootDir','/usr/app');
// I need the reportDir to be '/usr/app/report'
String reportDir = config.getValue('app.reportDir');
I can write some codes to get this feature but I'd like to know if there is any existing library do this?
I can use properties, yaml, or json as configuration file type, according to the library availability.
I would like to use an XML config file for my application, something like this:
<defaults>
<timeout>3000</timeout>
<delay>200</delay>
..... etc
I would like to use these properties in multiple classes. The way I am doing it at the moment is reading the xml file in a static class and store them in static final properties like:
public static final int TIMEOUT;
and then in another class I read it like:
int timeout = Properties.TIMEOUT;
where Properties is the class where I store those static variables.
Having never worked with config files before, I do realize this is not the correct way to handle them. So my question is, what is the good way using a XML config file throughout a java application, and/or is there a design pattern for this to be used?
Thank you for any help!
Like you wrote, usually a properties-file is used for config. It comes in the format of:
key1=value1
key2=value2
...
It's really not complicated to use - you can practically pick it up in less than 5 mins. Here's a nice example for using a properties-file.
UPDATE
In case you have to use an XML file - I would use any XML parser (JAXB/XStream...) and create a hashMap that'll hold the pairs of key/value - same like it's done with properties-file.
I am attempting to transform some Docbook XSL to HTML using Java / Xalan and a mixture of the official Docbook XSL files from https://sourceforge.net/projects/docbook/files/docbook-xsl/1.76.1/ with some local xsl files that provide some customizations and overrides.
I want to prevent my application from having to download external resources or access local files. So I have implemented a class that extends the URIResolver interface.
The problem is that the resolve(final String href, final String base) function is not providing enough information to identify the particular file that is being requested.
For example, one of the local override files is imported from the xsl file using <xsl:import href="../../../xsl/html.xsl"/>. In this case the href parameter for my resolver class is set to ../../../xsl/html.xsl, which is fine. The html.xsl file then imports a file called defaults.xsl. The href parameter is set to only defaults.xsl, and the base parameter is set to null.
This might be followed by an import of http://docbook.sourceforge.net/release/xsl/current/xhtml/docbook.xsl, in which case the href parameter is set to http://docbook.sourceforge.net/release/xsl/current/xhtml/docbook.xsl. However, if the docbook.xsl imports a file called defaults.xsl, the href parameter is also set to defaults.xsl and base is set to null.
The issue is that the href and base parameters don't uniquely identify the resource, and you can't guess which file is being requested by watching the order of the previous hrefs either. Is there some trick to finding out exactly what context a file is being requested in?
Does the Source you are creating the transform from have a system ID? If not, this might be a reason why your base is always null in your URI resolver.
If you are creating transforms from input streams, you can manually assign a system ID to a source. You can generate an artificial one if necessary and use that artificial URI in your URI resolver to map back to a base URI. Also make sure the sources you create in your URI resolver also have system IDs or the same problem will occur with resources imported from those files.