You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@deltaspike.apache.org by Charlie Mordant <cm...@gmail.com> on 2014/12/01 13:23:08 UTC

Re: DeltaSpike configuration with absolute path of a properties file

Hi,

So you can always make your own ConfigSourceProvider.

Regards,

Charlie

2014-11-27 10:57 GMT+01:00 Pablo Pita <pa...@gmail.com>:

> I have been looking into DeltaSpike code for my problem. Ideally, I would
> like to load a properties file with an absolute path by implementing
> PropertyFileConfig, but it seems that is not feasible.
>
> The reason is that DeltaSpike will resolve the filename using
> PropertyFileUtils.resolvePropertyFiles. This method is :
>
>  public static Enumeration<URL> resolvePropertyFiles(String
> propertyFileName) throws IOException
>     {
>         ClassLoader cl = ClassUtils.getClassLoader(null);
>
>         Enumeration<URL> propertyFileUrls =
> cl.getResources(propertyFileName);
>
>         //fallback - see DELTASPIKE-98
>         if (!propertyFileUrls.hasMoreElements())
>         {
>             cl = PropertyFileUtils.class.getClassLoader();
>             propertyFileUrls = cl.getResources(propertyFileName);
>         }
>
>         return propertyFileUrls;
>     }
>
>
> After reading some Resource information [1] and [2], by just using
> getResources in this method to locate the propertyFileName, the case when
> it is an absolute file path in the system is not covered as getResources
> searches by taking  the classpath into account. If I extend the method this
> way, then it works:
>
>     public static Enumeration<URL> resolvePropertyFiles(String
> propertyFileName) throws IOException
>     {
>         ClassLoader cl = ClassUtils.getClassLoader(null);
>
>         Enumeration<URL> propertyFileUrls =
> cl.getResources(propertyFileName);
>
>         //fallback - see DELTASPIKE-98
>         if (!propertyFileUrls.hasMoreElements())
>         {
>             cl = PropertyFileUtils.class.getClassLoader();
>             propertyFileUrls = cl.getResources(propertyFileName);
>         }
>
>         if (!propertyFileUrls.hasMoreElements())
>         {
>             File f = new File(propertyFileName);
>             if (f.isFile()) {
>                 Vector<URL> v = new Vector<URL>();
>                 v.add(f.toURI().toURL());
>                 propertyFileUrls = v.elements();
>             }
>         }
>         return propertyFileUrls;
>     }
>
>
> Should then the resolvePropertyFiles try a bit more as done here? Any
> feedback on this?
>
>
> [1]
> https://docs.oracle.com/javase/jp/8/technotes/guides/lang/resources.html
> [2]
>
> http://www.thinkplexx.com/learn/howto/java/system/java-resource-loading-explained-absolute-and-relative-names-difference-between-classloader-and-class-resource-loading
>
> On Wed, Nov 26, 2014 at 3:56 PM, Pablo Pita <pa...@gmail.com> wrote:
>
> > Uhmm ... I have not succeeded with the gist, but I have just created a
> > small project that can be cloned:
> >
> > https://github.com/pleira/ds-config-file.git
> >
> > Move the file myconfig.properties to D:/EMOS.web/cfg/myconfig.properties
> > and launch the test. I get:
> >
> > 26.11.2014 15:41:51
> >
> org.apache.deltaspike.testcontrol.api.junit.CdiTestSuiteRunner$LogRunListener
> > testStarted
> > INFO: [run] com.pleira.MyConfigFileTest#testGetPropertyFileName
> > 26.11.2014 15:41:51
> >
> org.apache.deltaspike.testcontrol.api.junit.CdiTestSuiteRunner$LogRunListener
> > testFailure
> > INFO: [failed] com.pleira.MyConfigFileTest#testGetPropertyFileName
> > message: expected:<somevalue> but was:<null>
> > 26.11.2014 15:41:51
> >
> org.apache.deltaspike.testcontrol.api.junit.CdiTestSuiteRunner$LogRunListener
> > testFinished
> > INFO: [finished] com.pleira.MyConfigFileTest#testGetPropertyFileName
> >
> >
> >
> > On Wed, Nov 26, 2014 at 2:41 PM, John D. Ament <jo...@apache.org>
> > wrote:
> >
> >> For the second option, can you post a gist w/ log file output?
> >>
> >> On Wed Nov 26 2014 at 8:35:58 AM Pablo Pita <pa...@gmail.com>
> wrote:
> >>
> >> > As I am on Windows, and I have been using thiese kind of
> >> > PropertyFileConfig's without success:
> >> >
> >> > 1.-
> >> >
> >> > public class MyConfigFile implements PropertyFileConfig {
> >> >
> >> >     @Override
> >> >     public String getPropertyFileName() {
> >> >         return "D:\\web\\cfg\\my.properties";
> >> >     }
> >> >
> >> > }
> >> >
> >> > 2.- returning the file:// :
> >> >
> >> > public class MyConfigFile implements PropertyFileConfig {
> >> >
> >> >     @Override
> >> >     public String getPropertyFileName() {
> >> >         return "file:///D:/web/cfg/my.properties";
> >> >     }
> >> >
> >> > }
> >> >
> >> > Can you succeed loading the properties with a full file path?
> >> >
> >> > Pablo Pita
> >> >
> >> >
> >> >
> >> > On Wed, Nov 26, 2014 at 2:27 PM, John D. Ament <johndament@apache.org
> >
> >> > wrote:
> >> >
> >> > > What value did you provide?  All of the property file loading is
> done
> >> via
> >> > > URLs, so if I had to guess you need file://
> >> > >
> >> > >
> >> > > https://github.com/apache/deltaspike/blob/master/
> >> > deltaspike/core/api/src/main/java/org/apache/deltaspike/
> >> > core/util/PropertyFileUtils.java
> >> > >
> >> > > John
> >> > >
> >> > >
> >> > > On Wed Nov 26 2014 at 7:47:22 AM Pablo Pita <pa...@gmail.com>
> >> > wrote:
> >> > >
> >> > > > Hello,
> >> > > >
> >> > > > I would like to use DeltaSpike configuration mechanisms using a
> >> > > properties
> >> > > > file which is not in the classpath. I had no success by
> implementing
> >> > > > MyPropertyFileConfig and returning the absolute path of the file
> in
> >> the
> >> > > > String getPropertyFileName() method. Is there a simple way to
> load a
> >> > > > property file that is not in the classpath?
> >> > > >
> >> > > > Thanks for any hints,
> >> > > > --
> >> > > > Pablo Pita Leira
> >> > > >
> >> > >
> >> >
> >> >
> >> >
> >> > --
> >> > Pablo Pita Leira
> >> >
> >>
> >
> >
> >
> > --
> > Pablo Pita Leira
> >
>
>
>
> --
> Pablo Pita Leira
>



-- 
Charlie Mordant

Full OSGI/EE stack made with Karaf:
https://github.com/OsgiliathEnterprise/net.osgiliath.parent

Re: DeltaSpike configuration with absolute path of a properties file

Posted by Pablo Pita <pa...@gmail.com>.
The way I have proceeded is by registering my custom ConfigSource
extension.

I have created a custom PropertiesConfigSource which extends DeltaSpike's
BaseConfigSource.
My implementation basically uses PropertyFileUtils.loadProperties(url) to
load the properties file.
The URL parameter is the absolute file path with the "file:" prefix.
Otherwise, my custom implementation is very similar to DeltaSpike's
PropertiesConfigSource.



On Mon, Dec 1, 2014 at 1:23 PM, Charlie Mordant <cm...@gmail.com> wrote:

> Hi,
>
> So you can always make your own ConfigSourceProvider.
>
> Regards,
>
> Charlie
>
> 2014-11-27 10:57 GMT+01:00 Pablo Pita <pa...@gmail.com>:
>
> > I have been looking into DeltaSpike code for my problem. Ideally, I would
> > like to load a properties file with an absolute path by implementing
> > PropertyFileConfig, but it seems that is not feasible.
> >
> > The reason is that DeltaSpike will resolve the filename using
> > PropertyFileUtils.resolvePropertyFiles. This method is :
> >
> >  public static Enumeration<URL> resolvePropertyFiles(String
> > propertyFileName) throws IOException
> >     {
> >         ClassLoader cl = ClassUtils.getClassLoader(null);
> >
> >         Enumeration<URL> propertyFileUrls =
> > cl.getResources(propertyFileName);
> >
> >         //fallback - see DELTASPIKE-98
> >         if (!propertyFileUrls.hasMoreElements())
> >         {
> >             cl = PropertyFileUtils.class.getClassLoader();
> >             propertyFileUrls = cl.getResources(propertyFileName);
> >         }
> >
> >         return propertyFileUrls;
> >     }
> >
> >
> > After reading some Resource information [1] and [2], by just using
> > getResources in this method to locate the propertyFileName, the case when
> > it is an absolute file path in the system is not covered as getResources
> > searches by taking  the classpath into account. If I extend the method
> this
> > way, then it works:
> >
> >     public static Enumeration<URL> resolvePropertyFiles(String
> > propertyFileName) throws IOException
> >     {
> >         ClassLoader cl = ClassUtils.getClassLoader(null);
> >
> >         Enumeration<URL> propertyFileUrls =
> > cl.getResources(propertyFileName);
> >
> >         //fallback - see DELTASPIKE-98
> >         if (!propertyFileUrls.hasMoreElements())
> >         {
> >             cl = PropertyFileUtils.class.getClassLoader();
> >             propertyFileUrls = cl.getResources(propertyFileName);
> >         }
> >
> >         if (!propertyFileUrls.hasMoreElements())
> >         {
> >             File f = new File(propertyFileName);
> >             if (f.isFile()) {
> >                 Vector<URL> v = new Vector<URL>();
> >                 v.add(f.toURI().toURL());
> >                 propertyFileUrls = v.elements();
> >             }
> >         }
> >         return propertyFileUrls;
> >     }
> >
> >
> > Should then the resolvePropertyFiles try a bit more as done here? Any
> > feedback on this?
> >
> >
> > [1]
> > https://docs.oracle.com/javase/jp/8/technotes/guides/lang/resources.html
> > [2]
> >
> >
> http://www.thinkplexx.com/learn/howto/java/system/java-resource-loading-explained-absolute-and-relative-names-difference-between-classloader-and-class-resource-loading
> >
> > On Wed, Nov 26, 2014 at 3:56 PM, Pablo Pita <pa...@gmail.com>
> wrote:
> >
> > > Uhmm ... I have not succeeded with the gist, but I have just created a
> > > small project that can be cloned:
> > >
> > > https://github.com/pleira/ds-config-file.git
> > >
> > > Move the file myconfig.properties to
> D:/EMOS.web/cfg/myconfig.properties
> > > and launch the test. I get:
> > >
> > > 26.11.2014 15:41:51
> > >
> >
> org.apache.deltaspike.testcontrol.api.junit.CdiTestSuiteRunner$LogRunListener
> > > testStarted
> > > INFO: [run] com.pleira.MyConfigFileTest#testGetPropertyFileName
> > > 26.11.2014 15:41:51
> > >
> >
> org.apache.deltaspike.testcontrol.api.junit.CdiTestSuiteRunner$LogRunListener
> > > testFailure
> > > INFO: [failed] com.pleira.MyConfigFileTest#testGetPropertyFileName
> > > message: expected:<somevalue> but was:<null>
> > > 26.11.2014 15:41:51
> > >
> >
> org.apache.deltaspike.testcontrol.api.junit.CdiTestSuiteRunner$LogRunListener
> > > testFinished
> > > INFO: [finished] com.pleira.MyConfigFileTest#testGetPropertyFileName
> > >
> > >
> > >
> > > On Wed, Nov 26, 2014 at 2:41 PM, John D. Ament <jo...@apache.org>
> > > wrote:
> > >
> > >> For the second option, can you post a gist w/ log file output?
> > >>
> > >> On Wed Nov 26 2014 at 8:35:58 AM Pablo Pita <pa...@gmail.com>
> > wrote:
> > >>
> > >> > As I am on Windows, and I have been using thiese kind of
> > >> > PropertyFileConfig's without success:
> > >> >
> > >> > 1.-
> > >> >
> > >> > public class MyConfigFile implements PropertyFileConfig {
> > >> >
> > >> >     @Override
> > >> >     public String getPropertyFileName() {
> > >> >         return "D:\\web\\cfg\\my.properties";
> > >> >     }
> > >> >
> > >> > }
> > >> >
> > >> > 2.- returning the file:// :
> > >> >
> > >> > public class MyConfigFile implements PropertyFileConfig {
> > >> >
> > >> >     @Override
> > >> >     public String getPropertyFileName() {
> > >> >         return "file:///D:/web/cfg/my.properties";
> > >> >     }
> > >> >
> > >> > }
> > >> >
> > >> > Can you succeed loading the properties with a full file path?
> > >> >
> > >> > Pablo Pita
> > >> >
> > >> >
> > >> >
> > >> > On Wed, Nov 26, 2014 at 2:27 PM, John D. Ament <
> johndament@apache.org
> > >
> > >> > wrote:
> > >> >
> > >> > > What value did you provide?  All of the property file loading is
> > done
> > >> via
> > >> > > URLs, so if I had to guess you need file://
> > >> > >
> > >> > >
> > >> > > https://github.com/apache/deltaspike/blob/master/
> > >> > deltaspike/core/api/src/main/java/org/apache/deltaspike/
> > >> > core/util/PropertyFileUtils.java
> > >> > >
> > >> > > John
> > >> > >
> > >> > >
> > >> > > On Wed Nov 26 2014 at 7:47:22 AM Pablo Pita <pablo.pita@gmail.com
> >
> > >> > wrote:
> > >> > >
> > >> > > > Hello,
> > >> > > >
> > >> > > > I would like to use DeltaSpike configuration mechanisms using a
> > >> > > properties
> > >> > > > file which is not in the classpath. I had no success by
> > implementing
> > >> > > > MyPropertyFileConfig and returning the absolute path of the file
> > in
> > >> the
> > >> > > > String getPropertyFileName() method. Is there a simple way to
> > load a
> > >> > > > property file that is not in the classpath?
> > >> > > >
> > >> > > > Thanks for any hints,
> > >> > > > --
> > >> > > > Pablo Pita Leira
> > >> > > >
> > >> > >
> > >> >
> > >> >
> > >> >
> > >> > --
> > >> > Pablo Pita Leira
> > >> >
> > >>
> > >
> > >
> > >
> > > --
> > > Pablo Pita Leira
> > >
> >
> >
> >
> > --
> > Pablo Pita Leira
> >
>
>
>
> --
> Charlie Mordant
>
> Full OSGI/EE stack made with Karaf:
> https://github.com/OsgiliathEnterprise/net.osgiliath.parent
>



-- 
Pablo Pita Leira