How to load properties file in JBOSS 6 from other place - java

I want to change location of all properties file but I do not know where to change in JBOSS 6 for new path, I am asking it because I am new in JBOSS.
it uses some default path, for example for "com/test/configuration/main" path is modules/com/test/configuration/main
but I want to load all property from say c:\prop_files\
for it i have tried to modified module.xml
<module xmlns="urn:jboss:module:1.1" name="com.test.configuration">
<resources>
<resource-root path="c:\props_files\"/>
</resources>
</module>
but it is giving me exception
org.jboss.modules.xml.XmlPullParserException: Failed to add resource root 'C:\props_files' at path 'C:\props_files'
please suggest.

Firstly, you need to add the path in your configuration file ( standalone.xml/domain.xml).
For example in standalone.xml (it goes after </extensions> and before <management> tag):
<paths>
<path name="my.home.dir" path="C:\Users\elvis\Downloads"/>
</paths>
You can do that via CLI as well:
./bin/jboss-cli.sh --connect --controller=localhost:9999
/path=my.home.dir:add(path=C:\\Users\\elvis\\Downloads)
Then you reference the path in your application, for example:
String path = System.getProperty("my.home.dir") + propertiesFileName;
Properties props = new Properties();
URL url = new URL(path);
props.load(url.openStream());

Related

Context.xml not recognized and not executed by Tomcat

after setting $CATALINA_HOME in this path /usr/local/cellar/tomcat/9.0.68/libexec, i put the file Context.xml with the same name as my war "warName.xml" in this path : /usr/local/Cellar/tomcat/9.0.68/libexec/work/Catalina/localhost.
here is the content of my file :
<Context>
<Resources className="org.apache.catalina.webresources.StandardRoot">
<PreResources className="org.apache.catalina.webresources.DirResourceSet"
base="/Users/user.name/Desktop/workspace/projectName/dev-resources/config" webAppMount="/WEB-INF/classes/"/>
</Resources>
</Context>
and when i deploy and launch the server i have not my dev-resources/config folder inside /WEB-INF/classes/ in my unzipped project.
the unzipped project has the following permissions :
drwxr-x---
i'am wondering if the process can not inject the configuration because of permissions problems
thanks

How to separate application config from AP in container on Kuberetes?

I want to deploy a application which use an extenal config on Kubernetes. I built a spring boot application to a war file and put it into websphere liberty. Then put application.properties in /config/config ( /config is shortcut of /opt/ibm/wlp/usr/servers/defaultServer) which I want my application to use.
I write a Dockerfile as below:
FROM websphere-liberty:19.0.0.6-javaee8
USER root
RUN mkdir -p /ibank-pv && chown -R 1001:0 /ibank-pv
RUN mkdir -p /config/config/ && chown -R 1001:0 /config/config
COPY --chown=1001:0 ibank.war /config/apps/
COPY --chown=1001:0 server.xml /config
COPY --chown=1001:0 application.properties /config/config/
USER 1001
CMD ["/opt/ibm/wlp/bin/server","run","defaultServer"]
Here is my server.xml. I set ${server.config.dir}/config as a config resource folder.
<?xml version="1.0" encoding="UTF-8"?>
<server description="new server">
<featureManager>
<feature>javaee-8.0</feature>
</featureManager>
<basicRegistry id="basic" realm="BasicRealm">
<!-- <user name="yourUserName" password="" /> -->
</basicRegistry>
<httpEndpoint id="defaultHttpEndpoint"
httpPort="9080"
httpsPort="9443" />
<applicationManager autoExpand="true"/>
<library id="configResources">
<folder dir="${server.config.dir}/config" />
</library>
<application location="ibank.war">
<classloader privateLibraryRef="configResources" />
</application>
</server>
And I add these code to read the application.properties in ${server.config.dir}/config.
public class IbankServlet extends HttpServlet {
private static final long serialVersionUID = 7526471155622776147L;
private final Properties config;
public IbankServlet() throws Exception {
InputStream is = getClass().getResourceAsStream("application.properties");
config = new Properties();
config.load(is);
}
}
However, when I start the server my application, it still read the application.properties which is in war file. What I have to do let my application use application.properties in ${server.config.dir}/config.
Is the issue that getClass().getResourceAsStream("relative-path") looks in the Java package of that specified class? That is, whatever subdirectory corresponds to the Java package of IbankServlet. In other words, is your application.properties within the WAR file buried down in a package subdirectory?
If so, you'll need to either change to getClass().getResourceAsStream("/application.properties") (with the leading slash), and move it to the classpath root in your WAR file, or have your Docker file copy it down into the correct subdirectory.
However, if your IbankServlet is actually not in a Java package at all, this probably isn't the issue.)
(But also, I admit, I'm not certain of the precedence if a file exists in both your WAR and in a "shared library".)

Wildfly: How to change the module name of WAR deployment? (without changing the WAR filename)

I have a maven project and the name of generated .war file is my-project-1.0.0-SNAPSHOT.war.
And I try to get the name and slot of the war module with this code:
Module module = Module.forClass(clazz);
ModuleIdentifier identifier = module.getIdentifier();
String name = identifier.getName();
String slot = identifier.getSlot();
The value of variable name is deployment.my-project-1.0.0-SNAPSHOT.war and slot is main.
How can I change the module name without changing the WAR filename?
I have this jboss-deployment-structure.xml (located in WEB-INF folder, I also tried in META-INF folder) file:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<module-alias name="deployment.my-project" slot="main"/>
<dependencies>
<module name="org.infinispan" services="import"/>
<module name="org.infinispan.commons" />
</dependencies>
</deployment>
</jboss-deployment-structure>
but the module name remains with war filename (my-project-1.0.0-SNAPSHOT.war).
Actually the main reason I want to change the module name is because I use the jboss-marshalling to serialize some java objects and the ModularClassResolver includes these modules informations (name and slot), making the serializated data not compatible when we change the project version and the .war file has another filename.
Is there other way to change the module name of my project?
Thanks

JBoss datasource creation

JBoss creates datasource from *ds.xml files and I want to do this without such an xml file and still make JBoss provide datasources through jndi.
The code that I have written registers 4 mbeans :
RARDeployment at jboss.jca:service=ManagedConnectionFactory,name=" + dataSourceJndiUrl
JBossManagedConnectionPool at "jca:service=ManagedConnectionPool,name=" + dataSourceJndiUrl
TxConnectionManager at "jboss.jca:service=XATxCM,name=" + dataSourceJndiUrl
WrapperDataSourceService at "jboss.jca:service=DataSourceBinding,name=" + dataSourceJndiUrl
I would like to be able to retrieve a DataSource objects using the following code:
InitialContext ctx = null;
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup(dataSourceJndiUrl);
Can someone tell me if the above is a correct approach and if yes what else needs to be done in order for it to work?
yes, is possible and is the best way to do it.
In my case, I needed to use an standalone-full configuration (EAP 6.2).
The first you need is the correct JDBC for your DB, for example I used the ojdbc6.jar. Copy this Jar into ${JBOSS_HOME}/modules/com/oracle/main/ . The JDBC must be with an module.xml, to make it valid for JBoss. To me, this was a correct configuration of the module.xml:
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="com.oracle">
<resources>
<resource-root path="ojdbc6.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>
Then, on the standalone-full.xml file you will find a tag with the urn of datasources, inside of that tag a datasources tag, and inside a drivers tag. You need to add this new JDBC, to me this was a succesfull configuration:
<drivers>
<driver name="Oracle" module="com.oracle">
<xa-datasource-class>com.oracle.jdbc.xa.client.OracleXADataSource</xa-datasource-class>
</driver>
</drivers>
Start the JBoss with that configuration and go to the Profile > Connector > Datasources. Add a new one with your own configuration (JNDI, User, Password, etc...), dont forget to add on properties the URL to your DB. Then, you will be able to use the JNDI for the DataSource.

set a data source in jboss standalone mode

I want to set a mysql data source in jboss standalone mode. I have already deploy the mysql-connector-java-5.1.15-bin.jar and set the below data source configuration in standalone.xml under datasources
<datasource jndi-name="java:jboss/datasources/MySqlDS" pool-name="MySqlDS" enabled="true" use-java-context="true">
<connection-url>jdbc:mysql://localhost:3306/testdb</connection-url>
<driver>mysql</driver>
<security>
<user-name>root</user-name>
</security>
</datasource>
And when i click on the configured data source name in the web console im getting below error,
Internal server error{
"outcome" => "failed",
"failure-description" => "JBAS014739: No handler for read resource at address [
(\"subsystem\"=>"\datasource\"),
(\"data-source\"=>"\MySqlDS\"),
(\"statstics\"=>"\pool\"),
"],
"roleback" => "true"
}
I didnt add any thing to the drivers section since it not nessaccary,
Below one is set to the sample data source set in jboss
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class
</driver>
</drivers>
What i am missing here please?
You will also need to specify driver class
<driver-class>com.mysql.jdbc.Driver</driver-class>
Checkout this link How do I migrate my application from AS5 or AS6 to AS7
I've experienced the same issue under the same circumstances. The problem was that my AS didn't have the required module for PostgreSQL. Check in jboss/modules/org if you have a folder named postgresql. If not then create it. Then create a directory in it named main.
You then have to have two files present in there:
PostgreSQL JDBC JAR
module.xml configuration file
Download the JAR file according to the database you're using and copy it here. As for the module.xml just create a new file and set up the configuration. Mine looks like this, customize it for your case:
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="org.postgresql">
<resources>
<resource-root path="postgresql-9.3-1100.jdbc4.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>
Basically you just have to change the resource path to where your JAR file is.
This is an issue with JBOSS, it doesn't warn you even though the JDBC driver is missing. I've wasted a ton of time finding this hidden bug :D

Categories

Resources