Where to put a properties file in Struts 2? - properties

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"/>

Related

Use Koin fileProperties with an absolute path

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

How to externalize rest-messages.properties in Spring Data REST?

Is it possible to externalize rest-messages.properties in SDR?
I tried to move it to ./config folder, for example, but it didn't work.
The rest-messages.properties (used to add custom details to your ALPS descriptions) goes to the same location where you would put custom application-*.properties or messages-*.properties file.
For instance, for a Spring Boot project the .properties file are located in the src/main/resources/ directory.
You can also set custom config locations for your .properties file.
Edit after the OP's comment: However, I tried using a custom rest-messages.properties outside of the project .jar (as I usually do with my application.properties), and it does not seem to work.

How to get environment information in Mule

I need to put some production specific behavior in mule-config.xml. Is there a way to get environment info in mule?
You can access all system properties via MEL. But I would rather suggest you modularize your configuration in several files (say: common-config.xml, test-config.xml, prod-config.xml) and load the right files at Mule startup based on the environment.
And also you can create properties file for each environment and access it inside mule config file.
You can create xml file with all the configurations and import/load the file into mule project
In your Mule configuration file (src/main/app/app-config-file.xml), you can add the following line:
<context:property-placeholder location="${environment.name}.properties" />
${environment.name} must be a system environment variable. You need to make sure the system where you are deploying your app contains that variable. I think you can also specify it in the Maven build, if you are using Maven (clean install -Denvironment.name=test). You can try.
To test locally, right click on your project, select run as, select run configurations, click on Environment tab, add your variable "environment.name" and the value "local" (without quotation marks) and make sure your local.properties file exists in a location added to the build path. It can be src/main/app for example or in the resources folder.
MEL has some context objects like server, mule ,application using these you can get the envinformation from #[server.env] for more info refer
https://docs.mulesoft.com/mule-user-guide/v/3.6/mule-expression-language-basic-syntax
You can use different mule properties file like Mule-dev.properties , mule-test.properties.
In global elements add a property placeholder :
In mule-app.properties define env= test or dev depending on the env value which you want to use.
Clean the project and deploy. Sometimes it is not able to read the env specific properties. Cleaning the project solves this issue.
You can add a properties file in you project and refer it from property place holder. it is better to add environment value in mule-app.properties file ehich is in /src/main/app
you can have one variable like mule.env in this mule-app.properties and send value to this Property as DEV,QA,UAT,PROD from cloudhub properties and create different properties file in mule/src/app main resources then use Properties placeholder to configure this property file and send the value of mule-env dynamically from cloudhub
Typically the way this is done is to delcare a property bean in which you set the environment specific value like PROD,QA,DEV or local. This environment specific value is set in mule-app.properties like mule.env=local, This needs to be set to the environment we are trying to deploy it and the API picks up the right properties file.

How can I write into a file within an Eclipse bundle?

I have an xml configuration file located into my plugin resources. I want to update this file whenever in the plugin happens some event. I found some methods to find and read the contents of a file located my plugin classpath, but I'm looking for a way to write into such a file.
Is there any way?
Many thanks.
That location (the install directory) is intended to be read-only since it may be shared in a network install scenario. I suggest you instead write the XML file to your plugin's state location which is intended for just this purpose:
String path = Activator.getDefault().getStateLocation().toString();
I should add that this gives you a fully qualified path to the directory created by Eclipse for any files your plugin wants to store. This directory is unique to your plugin.

External properties file with Weblogic

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.