aspose file tools*
The moose likes IDEs, Version Control and other tools and the fly likes log4j setting in enterprise project structure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "log4j setting in enterprise project structure" Watch "log4j setting in enterprise project structure" New topic
Author

log4j setting in enterprise project structure

mark I thomas
Ranch Hand

Joined: Apr 07, 2008
Posts: 86
I am sure some people have used MyEclipse or RAD's j2ee enterprise project struture. What it does is it creates template and create a top level "myapp" which depends on several other projects like EJB, Web pj, etc. So we end up with few EJB pj, one web pj, and few other java project, all related to the top level "myapp" enterprise application project. The "application.xml" is in the "myapp" pj.

Now, i want to apply log4j to each EJB and web pj, etc. Do I need to create "log4j.properties" in each project ? If yes then it is lot of redundancy and whenever I need to change one log4j property file I need to change others in other projects as well. For me, I can use a "cross-project" log4j.properties because I want to make the log4j settings for each project be the same. So how to accomplish this ?

Thanks.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
Please don't use unknown abbreviations, like pj as it makes it hard to understand what you're saying. I assume by pj you mean project - please correct me if I'm wrong.

As for the issue, you need to consider what the class loader will look like for your application. You can do a search on Class Loaders for your type of application and get lots of results. For example, in a JEE (Java Enterprise) application, you package your application components in a .ear file (Enterprise ARchive). If you place a Jar file in the 'lib' directory of the .ear file, it will be placed on the classpath for the entire application, including the application components, like EJB jar files, web application archives, etc.

So, just place the .properties file in a Jar file, and place that Jar file in the 'lib' directory of your .ear file, and it should just work.

To understand why, you really need to read about class loading, like mentioned above. You won't get all you need from my small example. Here's a link to get you started: http://en.wikipedia.org/wiki/Java_Classloader

mark I thomas
Ranch Hand

Joined: Apr 07, 2008
Posts: 86
Mark Hansen:

1) if you unpack a .ear there is such a "lib" directory for you. You will get

ejb-1.jar, ejb-2.jar, web-app.war.

that's it.

So I don't understand what "lib" you are talking about.

2) Don't you think it is funny to put a single log4j.properties file into a .jar file ??


3) My question is specific about using an Eclipse (MYEclipse or IBM RAD) IDE to develop such an EAR. As I said you have several projects (like ejb, web) there. Where should I put my log4j.properties file ? if I put it in one project (I mean a project in IDE), it can't be accessed by others. the only "possible" project to put is the top level "myapp" project. Do you mean to customize the "project dependency" of this "myapp" to make it depends on that little log4j.property "jar" ?? Sounds really weird I have to say because this "myapp" is dependent on the ejb and web projects only.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
You will understand better if you read up on class loaders in JEE applications.

You said that when you unpack your Ear file, you already have a lib directory with your jars/wars in it? That doesn't sound right.

It doesn't matter which IDE you use to create the Ear file.

mark I thomas
Ranch Hand

Joined: Apr 07, 2008
Posts: 86
Mark E Hansen wrote:You will understand better if you read up on class loaders in JEE applications.

You said that when you unpack your Ear file, you already have a lib directory with your jars/wars in it? That doesn't sound right.

It doesn't matter which IDE you use to create the Ear file.



sorry I had a typo in my last post. When I unpacked my .ear, there is NOT such a "lib" folder immediately available there. It only includes few ejb jar files and one web .war file. This is consistent with the application.xml

*************
<application..
<module>
<ejb> xyz-ejb.jar </ejb>
</module>
...
<module>
<web>
<web-uri> abc.war </web-uri>
<context-root> app-web </context-root>
</web>
</module>

</application>
*************

note: my EAR application runs fine so there is no problem with this application.xml

Which "lib" directory did you refer to when you said adding the "property file jar" to "lib" ??

please be specific to the implmentation detail instead of the class loading concept.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
It will become clearer if you read the link I provided.

In the EAR file, you can create a top-level directory, named 'lib'. Any Jar files you place in that directory will be on the class loader for the entire EAR (the EAR class loader). This means they will also be on the classpath for any EJB Jar files as well as web application WAR files.

If you place the log4j.properties file in a Jar file, and place that Jar file in the 'lib' directory of the ear file, then it will be on the classpath for all application components contained within the ear.

You can place any resources in such a Jar file (class files included) that you want on the classpath for the entire application. Note however, that classes placed in such a Jar file will not see classes placed within an EJB Jar file or a web application War file. This is because the class loader is hierarchical. The web application class loader is built on top of the ear class loader. So the web application can see all resources known to the ear class loader, but the ear classes cannot see the resources contained within the web application. The same is true of the EJB Jar files.

I can't really say more without duplicating everything that's already been written in that page I referenced (as well as other similar pages). Read up on Java Enterprise Application class loaders. It will help you a lot in understanding how to put your application components together for deployment.


mark I thomas
Ranch Hand

Joined: Apr 07, 2008
Posts: 86
Thanks Mark. I have read those class loader stuff you suggested. Talking about the class loader I do encounter a problem and want to see if you can explain ---

1. First I add log4j.jar and log4j.properties to my web application project (add log4j.jar to WEB-INF/lib and the property to web-inf/classes) then it worked for any logging that I added in this web project java code.

2. Then I moved to my ejb project and tried to add logging there. I added log4j.jar to the "library" of this ejb project (Ecliipse) and log4j.properties to its "src". I got "ClassDefNotFound" exception for "Logger" when the code executed to the point where it started logging in ejb project java code. It doesn't matter where inside the EJB project I put the log4j.properties, it just can't load the Logger class !

Question --- I understand that a log4j.jar added to web application project can not be seen by an EJB project, but that's why I added log4j.jar to the EJB project's "library" ! Why it still turned out to be that exception ??

I think I need to understand this first.

Thanks.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
When building an application, you need to consider two things with regard to resources like 3rd-party Jars/classes: Are they available during the build process (and to the IDE for intellisense-type operations) and whether they're available to the application once it's been deployed.

Adding things to Eclipse doesn't mean it's correctly in the application. Placing Jar files in the web application's WEB-INF/lib directory will result in those Jar files being placed in the WEB-INF/lib directory of the web application deployment archive (the .war) file, so this covers both build time and run time. However, I'm not sure what directory you're talking about in your EJB project. Create your Ear file, then take that off somewhere and extract it. Then you can see exactly what is in there.

Note that, like I said before, if you want the log4j Jar file to be on the class path for all application components, place it in the 'lib' directory of the .ear file. That will place it on the Ear class loader, which will result in it being placed on the EJB class loader and the web application class loader. If you don't know how to get the IDE to build your application archive with this Jar file in the 'lib' directory, someone may be able to help you with that.

Are you sure that the log4j Jar file is not available on the class path of your application server already? I use JBoss 5.1.0.GA and the Jar is provided in the app server, so I don't need to explicitly include it in my application archive.
mark I thomas
Ranch Hand

Joined: Apr 07, 2008
Posts: 86
Getting to add .jar to the "lib" of EAR. First I don't know how to make a "lib" in the ear. As I said there is no "lib" immediately when I unpack the .ear. Don't tell me manually create a "lib" folder and put things there. I am saying if you use Eclipse or MyEclipse or RAD, how do you do it ? Why don't you just tell me how you did it using your IDE like eclipse ?
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
I haven't told you how I did it, because I haven't done it.

I've created a utility Jar in my enterprise application project, which resulted in the Jar being placed in the 'lib' directory of the Ear, but I don't know (off hand) how to get a 3rd-party Jar into the same directory. However, I expect there is a way. Have you looked at the Eclipse documentation? Otherwise, perhaps someone else here can tell you.

mark I thomas
Ranch Hand

Joined: Apr 07, 2008
Posts: 86
1)what IDE do you use ?

2) What do you put in your utility jar ?

3) is it just a utility.jar or is it actually a java project and you let your existing enterprise project depend on this utility project ? please be specific.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
mark I thomas wrote:1)what IDE do you use ?
I use Eclipse Galileo (3.5.1) for JEE application developers
mark I thomas wrote:2) What do you put in your utility jar ?
Just some classes that are used by both my EJB and War application components. These are utility classes that I created, not from a 3rd party.
mark I thomas wrote:3) is it just a utility.jar or is it actually a java project and you let your existing enterprise project depend on this utility project ? please be specific.

I created a project in Eclipse which results in a utility Jar file in my EAR file. To do so, I did the following:
In Eclipse, click on File -> New -> Other, then navigated to Java EE Utility Project;
I named the project 'utility';
I selected 'none' as the targeted runtime;
I selected 'Add project to EAR', then selected my Ear project;
I left the 'source folder on build path' and 'default output folder' settings alone.

On the Project Properties for the Enterprise Application, I navigated to Java EE Module Dependencies. I then located the utility.jar file, and selected the 'In Lib Dir' check box.

Like I said, I didn't include a 3rd-party Jar.
mark I thomas
Ranch Hand

Joined: Apr 07, 2008
Posts: 86
Mark, if you want to create some common classes that are available to both EJB and web project, I think what you did makes sense to me.

In my case, I need both EJB project and Web Project be able to use same "log4j.properties" (not any common java code) and they need to have "log4j.jar" be available to them. have you tried if it is still doable using your approach ? (how ? just put "log4j.properties" in src of Utility project ?)

how do you handle the logging in both EJB and web project ?
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
I think what you're not seeing here is that the log4j.properties file is a resource, just like Java .class files are. It is located at runtime by searching the classpath. If you place this file in a Jar, and place that Jar in the 'lib' directory of the ear, then at runtime both the EJB and Web applications will find it - because the Jar file will be on the classpath for both.

You can place the file in a Jar using the Utility Jar project I mentioned earlier.

As far as including the log4j.jar file, I haven't had to do that, as that Jar file is supplied by my application server. Are you sure your application server does not?

If you're sure it does not, then you just need to figure out how to place it in your ear's 'lib' directory as well. I haven't done this, so I can't tell you how. Plus, I don't think we're using the same IDE. I would think your IDE would provide help for performing such a task. As an alternative, you can place the Jar in the application server's 'lib' directory (if you don't mind doing such a thing) - you just have to remember to do this for each server you deploy to.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: log4j setting in enterprise project structure