2

I have an application deployed in wildfly packaged in a war file. I need to load some properties to be used in my java code from a xml file placed outside the war file under deployment directory.

deployments
 -- myapplication.war
 -- myproperties.xml

How can my java code access this property ? Please advise.

Thanks..

Nova Guy
  • 505
  • 2
  • 9
  • 16
  • A full example using just CDI is given here: http://stackoverflow.com/questions/27953261/wildfly-reading-properties-from-configuration-directory/28996863#28996863 – Chris Ritchie Mar 11 '15 at 20:49

2 Answers2

1

In combination with CDI OWNER does the job really well.

Update

Inject Java Properties in Java EE Using CDI provides an introduction together with some sample code on github. As a next step you may delegate the properties part to OWNER.

Create an annotation type definition Config.java:

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
public @interface Config {
}

Get the ConfigProducer.java in place:

@Produces
@Config
public Configuration produce(InjectionPoint ip) {
    if (config == null) {
        config = ConfigFactory.create(Configuration.class);        
    }
    return config;
}

The producer must be instantiated by itself or another bean with @Startup @Singleton annotations.

Setup your OWNER backed Configuration.java

@HotReload(type = HotReloadType.SYNC)
@LoadPolicy(LoadType.MERGE)
@Sources("file:${config.filepath}/config.properties")
public interface Configuration extends Config, Reloadable, Mutable, Accessible {

    @Key("server.http.port")
    int port();

    @Key("server.host.name")
    String hostname();

}  

Inject and use the configuration in some bean:

@Stateless
public class SomeBean {

    @Inject
    @Config
    private Configuration config;

}

Not to forget the path configuration in wildfly's standalone.xml:

<system-properties>
        <property name="config.filepath" value="/your/path/"/>
</system-properties>
zellus
  • 9,617
  • 5
  • 39
  • 56
0

If you are ok accessing all this property at System level, ie , System.getProperty("propertyName") , then best option is to add a custom module for this property file.

Below is example from my custom module I create to keep oracle driver jar :

I created on directory under wildfly modules as system\layers\base\com\oracle\ojdbc14\main

Now put all your jars in this directory. Add one module.xml file in same directory with entries of your jar files :

<module xmlns="urn:jboss:module:1.1" name="com.oracle.ojdbc14">

<resources>

<resource-root path="ojdbc14.jar"/>

<resource-root path="second jar"/>

<resource-root path="third jar "/>



</resources>

<dependencies>

</dependencies>

</module>

 

pay attention , I keep my directory structure consistent to my Name property in module. You can add as many jars as you need to this directory and add entry for resource-root path

Now to use this module , you can add entry in jboss-deployment-structure.xml in your applications META-INF , like :

<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.1">

    <deployment>

        <dependencies>

              ..

..

..

 

                <module name = "com.oracle.ojdbc14"/>

        </dependencies>

..

..

..

</jboss-deployment-structure>

 

ref @ https://developer.jboss.org/thread/257516

Hope this helps.