We help IT Professionals succeed at work.

JBoss: Configuration inside an ear file

stephan_zehnder
on
Medium Priority
826 Views
Last Modified: 2012-05-05
I'm using jakarta configuration (XML) as a configuration framework together with JBoss. At the moment the configuration files are in a separate directory under my server directory. To load them I prepend the path of the configuration to the jboss classpath.

This is the code to initialize the jakarta configuration:

      ConfigurationFactory factory = new ConfigurationFactory();
      URL configURL = null;
      configURL = ClassLoader.getSystemResource(configDefinition);
      if (configURL == null) {
        throw new ConfigurationException("Could not resolve configuration ressource named : " + configDefinition);
      }
      factory.setConfigurationURL(configURL);
      rootConfiguration = factory.getConfiguration();

The configuration consists of several xml files. The main one references all the others.

Now I would like to change this and add the configuration to my ear file. I do this by jaring them up and put it in the root of the ear file.

The problem is, that this doesn't work, because the classloader doesn't find the main config file. So what I did I took the main one and added it to the configuration folder which is in the jboss classpath, the other configuration files are in the configuration.jar only.

But I want them all in the jar. Any idea?

Thanks a lot!!
Comment
Watch Question

Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> the other configuration files are in the configuration.jar only

Configuration-files should not be in JARs but they should be outside because they are meant to be changed (so that the application's behaviour and adaptability changes). If you keep them in a JAR, you cannot expect a user to extract it, change it and then JAR it again for the change to be effective.

Author

Commented:
Those configuration files are not meant to be changed. That's actually also why I want them inside the jar file.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
A configuration-file is meant to be changed :) e.g., the driver for the data-base to use, the connection URL (the IP-address or the machine you use as the data-base server might change, for example), authentication credentials, etc - what info are you storing in the configuration file that you don't want it to change??

Author

Commented:
Please stop telling me what my configuration files are for.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
I didn't tell you - I gave you an example and asked you what your files are for, that you don't want to change them. If you are not interested in being helped, then I'm off this question. EE is a collaborative forum and it is not possible to arrive at a solution without asking a few details from the questioner.

Author

Commented:
What you are asking hasn't got anything do to with my question. To load a configuration file I guess it doesn't matter what is in.
CERTIFIED EXPERT
Top Expert 2004

Commented:
What is "configDefinition" set to?
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Sometimes the requirement for a problem has to be understood one level higher. One solution which will work is that the configuration-file should be present in directory. Then comes the next question - why should it not be in a directory? And I was trying to solve that because generally in all production deployments that we've done, I don't think we kept configuration-files inside a JAR file as they were likely to be changed (since they contain configur*able* parameters for tweaking the application like turning logging on or off, IP-address, port, etc - they define the configuration for your application's run and that is why they are called as 'configuration'-files).

Author

Commented:
configDefinition is the name of the main file. In my case the name is ConfigDefinition.xml.

Together with a friend we discovered, that if I do this:
configURL = Thread.currentThread().getContextClassLoader().getResource(configDefinition);
it works.

But we are not sure if it is a good idea to use currentThread() in a J2EE environment.
CERTIFIED EXPERT
Top Expert 2004

Commented:
Does it work if you do:

  YourClass.class.getResource( configDefinition ) ;

?
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Does the file-name have a "/" preceding it or not?

Author

Commented:
@mayankeagle

Sure I agree with you. But the kind of configuration I have is really not to be mentioned to change by a customer. It is e.g. definition of services. They are fixed, but might change for each release. If the customer changes anything there, the product won't work.

Author

Commented:
IUt hasn't a / in front.
CERTIFIED EXPERT
Top Expert 2004
Commented:
Try doing:

    YourClass.class.getResource( configDefinition ) ;

and putting a slash infront of the configDefinition name (assuming it doesn't work without)

Tim

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Try with "/ConfigDefinition.xml"

>> configURL = Thread.currentThread().getContextClassLoader().getResource(configDefinition);

You might run into problems debugging that in an IDE :)
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Where is "ConfigDefinition.xml" specified? Is it hard-coded?

Author

Commented:
hehe. No it is an environment variable.

Author

Commented:
@Tim

It works with this.getClass().getResource() and a / in front. Accesing it the staic way doesn't work.

I'm gonna try it with a sticky class inside the configuration jar file. This seems to be the best solution according to Mario sitting next to me.

Thx guys.
Mayank SPrincipal Technologist
CERTIFIED EXPERT
Commented:
When you specify it as a resource, I guess "/" is required, which is why I asked: >> Does the file-name have a "/" preceding it or not?

Author

Commented:
ok
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.