I'm looking for the best way to use an external properties file with an application that is going to be deployed on Weblogic 10.3 server. I read a number of articles on the site but I don't want to hard-code the path to the properties file or put the file in the domains/mydomain folder.
Is there a dynamic way of doing this so when the application is deployed the properties file is also installed for example under the deployments folder and read from there?
Many thanks
Another alternative that does not require putting the file in a place other applications will read it is to use the Generic File Loading Overrides:
http://download.oracle.com/docs/cd/E21764_01/web.1111/e13702/config.htm#i1066493
This involves creating a directory that will be the root directory of your deployment, let's call it FooApplication that has FooApplication.ear and FooWeb.war. This is called the Application Installation Directory. Your application goes in the FooApplication/app sub-directory whether it is an archive (like .ear, .war, jar) or whether it is an exploded version of one of those archives. Your optional deployment plan (you must have one to use this feature, it could be a plan that does not do much beyond specifying a config-root element and values as described in the documentation) goes in the FooApplication/plan. You can put your properties that you want to override ones in the application in FooApplication/plan/AppFileOverrides directory structure.
http://download.oracle.com/docs/cd/E21764_01/web.1111/e13702/deployunits.htm#sthref9
Once that style of deployment is done, you write code like this from your application and the contents of myApp.properties get read from the FooApplication/plan/AppFileOverrides/FooWeb.war/myApp.properties will be the actual file that is read in.
Properties myAppProps = new Properties();
InputStream iostream =
Thread.currentThread().getContextClassLoader().getResourceAsStream("myCfg/myApp.properties");
myAppProps.load(iostream);
This is accomplished by adding a classloader to your application as explained in the docs. It might seem tedious to initially configure, but it is a feature that directly satisfies the original question and only for that particular application.
You can set a directory on the classpath and Place your custom properties file in that folder/directory. So that, the entire directory along with property file will be on classpath.
To set the directory on the classpath in weblogic 10.3.x
Create a folder in %DOMAIN_HOME%\config\ folder. example appConfig.
Place your custom property file (Let's say config.properties) in appConfig directory/folder.
Modify the setDomainEnv.cmd (Windows) to include appConfig in the classpath by setting %DOMAIN_HOME%\config\appConfig as value to EXT_POST_CLASSPATH(this variable is already defined in the setDomainEnv.cmd file) variable as below:
set EXT_POST_CLASSPATH=%EXT_POST_CLASSPATH%;%DOMAIN_HOME%\config\appConfig
You can access that file in you java code as below:
InputStream inputStream = Thread.currentThread ().getContextClassLoader().getResourceAsStream ("config.properties");
Properties prop = new Properties();
prop.load(inputStream);
String value = prop.getProperty("key");
Hope this helps.
Approach #2
Use Weblogic shared library
http://download.oracle.com/docs/cd/E12840_01/wls/docs103/programming/libraries.html
Follow below steps
Package all your configuration as separate JAR during the build process
Deploy configuration JAR as shared library
Reference above shared library from your EAR/WAR
Deploy EAR/WAR (Configurations will be available in classpath)
When you say " I read a number of articles on the site but I don't want to hard-code the path to the properties file" I assume you are saying you don't want to hard code it in your Java code. If that is so, then please see below
Answered here:
There are ways to read properties file in Java from weblogic classpath
One (Properties file located in the weblogic domain): Drop the properties file inside the Domain directory. This way the properties file is added to the weblogic classpath automatically and we can read from Java using resourceAsStream.
Two (Properties file from a User defined location):The advantage with this approach is that the property file can reside outside the JAR or EAR file and can be modified conveniently.
package com.test;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertyFileExample {
private static Properties prop;
public static void myMethod() {
InputStream is = null;
try {
prop = new Properties();
String propFilePath = System.getProperty(“propFileLocation“);
InputStream iStream = PropertyFileExample.class.getClassLoader().getResourceAsStream(propFilePath);
prop.load(iStream);
prop.getProperty(“dbuser”);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the weblogic setDomainEnv(under bin) we need to pass the location of the property file as a -D argument to JAVA_OPTIONS
set JAVA_OPTIONS=%JAVA_OPTIONS% -DpropFileLocation =/dev/file/properties/some.properties
Hope it helps!
Approach #1
Update your server startup script to pass below system variable to JVM (below is example on Windows OS)
call "%DOMAIN_HOME%\bin\startWebLogic.cmd" "-Dcom.mycompany.myapp.EXTERNAL_CONFIG_PATH=/mycompany/myapp/config" %*
Using this variable which points to your configuration directories, read configurations from there. You will need to make this setting on each server where you want to deploy your application.
Related
I am trying to deploy a koin application. So far I am using fileProperties pointing to a location in src/main/resources called local.properties.
Now I am deploying a jar and would like to tell Koin to look in a particular folder for the server properties, eg /usr/share/my-app/config/
Unfortunately every attempt to run results in this exception
org.koin.core.error.NoPropertyFileFoundException: No properties found for file
I have tried including the file specifically on the class path of the jar command but without any luck.
Is this by design? That the file properties should be within the jar? Or am I doing something totally wrong...
snippets below
install(Koin) {
fileProperties(propertiesFileName)
modules(properties, monitoring, etc)
}
with various run configs
val propertiesFileName = "/usr/share/my-app/config/app.properties"
val propertiesFileName = "file:///usr/share/my-app/config/app.properties"
val propertiesFileName = "/app.properties" //with -cp including the file specifically
I'm developing an application in Dropwizard 0.9, using IntelliJ Idea as my IDE. I have a run configuration configured, and it passes the command line arguments "server ./path/to/config.yml" to specify the configuration file to run with.
When I try to run my unit tests, however, I cannot determine how to pass this config file path to the server. The Run/Debug Configuration dialog has a "Program Arguments" field, but it is permanently disabled (and the edit button adjacent to it does nothing).
How can I enable program arguments, or, alternatively, is there another way to specify the config file for tests?
if your config.yml is in the project's root folder, just set server config.yml in the IDE. Take a look at this video.
If you would like to access the config.yml from the project's root folder, just pass the name of the file to the rule.
#ClassRule
public static final DropwizardAppRule<DWGettingStartedConfiguration> RULE
= new DropwizardAppRule<>(DWGettingStartedApplication.class,
"config.yml");
Also, it is possible to store the configuration file for integration tests in the src/test/resources folder. In this case, the file can be accessed using the following code:
/**
* A path to test configuration file.
*/
private static final String CONFIG_PATH
= ResourceHelpers.resourceFilePath("test-config.yml");
/**
* Start the application before all test methods.
*/
#ClassRule
public static final DropwizardAppRule<DropBookmarksConfiguration> RULE
= new DropwizardAppRule<>(
DropBookmarksApplication.class,
CONFIG_PATH);
In addition, please take a look at my example projects here, here and here.
I have a property file placed in the root of the web project in Java. I am using Struts 2.
My code is unable to read properties file. Where should I keep my properties file?
I have checked default path , it is where my Eclipse in installed. But I want that system should read file from project folder itself.
Usually you should put the properties file to the src folder so your application is able to read the properties file when you do run your application the properties file is copied from the src folder to the classes folder. As far as you know the classes folder should be the project output folder, so it will be used as a classpath folder and application is able to load the properties file if it is on the classpath.
An example to get properties from the classpath:
Properties prop = new Properties();
try {
//load properties from the class path
prop.load(this.getClass().getClassLoader().getResourceAsStream("myproperties.properties"));
//get the property
System.out.println(prop.getProperty("mykey"));
} catch (IOException ex) {
ex.printStackTrace();
throw ex;
}
However, you can load properties if you know the path to the file on the filesystem, in this case use
prop.load(new FileInputStream("/path/to/myproperties.properties"));
If you are talking about struts.properties
The framework uses a number of properties that can be changed to fit
your needs. To change any of these properties, specify the property
key and value in an struts.properties file. The properties file can be
locate anywhere on the classpath, but it is typically found under
/WEB-INF/classes.
If you are looking for Message Resource properties it could be configured in the struts.properties or struts.xml the later is proffered.
<constant name="struts.custom.i18n.resources" value="path/to/resources/MessageResources"/>
the value is a filepath src/path/to/resources/MessageResources.properties
If you are looking for the proper way to configure your application consider the choice to use EasyConf.
Property files will generally either go:
on the the classpath, e.g., for opening as a resource, or
at a location inaccessible to clients, e.g., under /WEB-INF
Which is more appropriate depends on your needs. Classpath-based files allow bundled default property files without configuration. For example, Log4J will look for log4j.properties at the root of the classpath as its default configuration file.
This can occasionally lead to issues, however, depending on class loading order: sometimes the system can pick up a "stray" config file. Configuring it yourself may still be preferable; I still tend towards the classpath, but config files are also commonly found in WEB-INF. Either method works, and both styles can be configured using JNDI, init params, environment variables, or system variables (e.g., -D).
Keep your myPropertyFile.properties file in src folder (after build project you will find it in WEB-INF/classes) and access them using this code:
Properties prop = new Properties();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
prop.load(classLoader.getResourceAsStream("/myPropertyFile.properties"));
Ideally, you can save properties files in:
/src/com/cft/web/bundle/.
like,
/src/com/cft/web/bundle/LabelResources.properties
OR
/src/com/cft/web/bundle/Applicationresources.properties.
in fact its up to you to give whatever the path you like.
Just remember to add the correct complete path in web.xml/struts-config.xml
for ex=g.
1. in web.xml :
<description>Application Resources</description>
<env-entry-name>ApplicationResources</env-entry-name>
<env-entry-value>com.cft.web.bundle.ApplicationResources</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
In Struts-config.xml
<message-resources parameter="com.cft.web.bundle.LabelResources" key="yourPropertiesFileName"/>
I have created an eclipse plugin and I wanted to deploy during eclipse runtime. I have below package structure.
com.myplugin
|
---resources
|
---server.bat
As part of the plugin job, "server.bat" file should be executed.
I packaged the plugin as .jar file including resouces folder in the binary and placed in to the eclipse "plugins" folder.
Plugin took effect and it does work fine, but I have a problem while executing the "server.bat" file, which is inside the jar that I generated. The error message says:
"Windows cannot find "resources\server.bat" make sure you typed name
correctly and try again"
I tried with relative paths and absolute paths, but it didnt work.
Here is the code doing that work:
URL url = Activator.getDefault().getBundle().getEntry("/resources/server.bat");
String fileURL = FileLocator.toFileURL(url).toString();
String commandLine = "cmd.exe /c start " +fileURL;
Process process= Runtime.getRuntime().exec(commandLine);
I got the "fileURL" output:
file:/D:/Program
Files/IBM/SDP/configuration/org.eclipse.osgi/bundles/2392/1/.cp/resources/server.bat
I am not sure this is correct.
Hope this is clear enough to answer the question.
Alternatively, please suggest some other way, such as creating features to deploy the plugin with folder structure. I haven't tried this option yet.
I have had a similar problem when I export my plugin. I had to refer an exe file stored in my plugin jar file. When the plugin was exported I can not access the zipped file, while it is accessible when I develop the plugin cause eclipse looks for the file in my "development folder".
To solve the problem I created a plugin feature and in the "Included Plug-ins" tab of feature.xml I checked the option
Unpack the plug-in archive after the installation.
for the plugin which contains the exe file.
Using this option you will find your plugin files in a folder under the eclipse plugin folder and you will be able to access them as regular files.
For example
Bundle bundle = <get a bundle of your plugin>;
URL url = FileLocator.find(bundle, new Path(<relative path from plugin root to your file>), null);
try {
url = FileLocator.resolve(url);
} catch (IOException e) {
e.printStackTrace();
return false;
}
Otherwise I think you should decompress your jar.
Hope this help.
EDIT
As I said you can create a feature project(for an example look here) and when you add your plugins, check the option "Unpack the plug-in archive after the installation." Eclipse will do the work for you and you will find your plugin unzipped in the eclipse plugin folder. This solved the problem for me.
Your code seems ok. Note that:
You need to convert your fileURL to file path (i.e. remove "file:/" prefix). This is hugely platform-dependent - try to rely on org.eclipse.core.runtime.IPath and java.io.Path (I don't remember proper code, sorry)
You need to escape the path as it contains space character.
I made it worked using ProcessBuilder. Below is the code snippet..
URL url = Platform.getBundle(Activator.PLUGIN_ID).getEntry("resources/server.bat");
String fileURL = FileLocator.toFileURL(url).toString();
ProcessBuilder pb = new ProcessBuilder("cmd.exe","/c",fileURL);
pb.redirectErrorStream(true);
Process p = pb.start();
p.destroy();
Ofcourse, i extracted the jar and put that directory to plugins folder of eclipse.
Use the second solution of http://www.vogella.com/blog/2010/07/06/reading-resources-from-plugin/
And don't forget to export the files you want to access in the build configuration of your plugin.
I found this worked well with Eclipse RCP 4, This code took the given gif image from a jar file that was in the given plugin
public ImageDescriptor getImgDesc(final String bundleId, final String fullPath)
throws IOException {
final URL url = new URL("platform:/plugin/" + bundleId + "/" + fullPath);
final ImageDescriptor imgDesc = ImageDescriptor.createFromURL(url);
return imgDesc;
}
....
final ImageDescriptor imgDesc = getImgDesc("com.awe.test","images/Applet24.gif");
final Image applet24 = imgDesc.createImage();
Hopefully should be a simple question...
I have an integration test module which contains the default directory structure:
src
|-main
|-test
|-java
|-resources
Then within my resources directory I have an xxxx.xml and xxxx.xsd file, and I need to load these files in as part of my test:
#Test
public void should_do_some_stuff_with_xml_and_xsd() // not actual test name
{
File xmlFile = new File("xxxx.xml");
File xsdFile = new File("xxxx.xsd");
...
}
It keeps failing trying to load the file, now I presumed it was down to me needing to give it a relative path from the project root or something. I need this test to run externally to my IDE so I can run the tests on the build server when it gets there...
So my question is, how do I target these files?
The classpath mechanism does not work for files. Relative file paths are resolved from the current directory, not from the classpath elements.
So you could just do
File xmlFile = new File("target/test-classes/xxxx.xml");
File xsdFile = new File("target/test-classes/xxxx.xsd");
However, a much cleaner solution would be to work with InputStreams instead of files. Almost every library that supports File parameters also supports InputStream parameters. And that way you can use ClassLoader magic without manually specifying any paths:
ClassLoader cldr = Thread.currentThread().getContextClassLoader();
InputStream xmlStream = cldr.getResourceAsStream("xxxx.xml");
InputStream xsdStream = cldr.getResourceAsStream("xxxx.xsd");
Reference:
Byte Streams (Sun Java Tutorial)
ClassLoader (javadoc)
InputStream (javadoc)