Automatic log4j configuration and bundleresource - eclipse

Two issues / questions:
1) In many of the log4j threads / forums / etc., I am seeing the results of defining the log4j.debug containing log4j configuration file references prefixed with "file:" as shown below:
log4j: Using URL [file:/data/app/conf/log4j.properties] for automatic log4j configuration.
In the code I am updating (authored by another developer now working someplace else), I am seeing:
log4j: Using URL [bundleresource://23/log4j.properties] for automatic log4j configuration
What/where is the "bundleresource" as shown above? I am thinking this is a dedicated resource Eclipse plug-in that I have been unable to identify.
2) This question stems from a larger issue of not being able to load a log4j configuration file without explicitly inserting it into the command line arguments of the JVM (e.g., -Dlog4j.configuration). I am replacing an Eclipse plugin-based application on a test server that does not have this command line argument specified in a start up script and is able to correctly fire up with log4j being initialized correctly. When I go to replace this application with the latest build from our source repository, the application fails to locate the log4j configuration file. Any thoughts on why the first application can start up and locate the log4j configuration file while the second (newer) application cannot?
Any help would be greatly appreciated.
Thanks!
Update / Edit
Output from the application with log4j.debug defined:
log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader#9fe666.
log4j: Trying to find [log4j.xml] using sun.misc.Launcher$AppClassLoader#9fe666 class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader sun.misc.Launcher$AppClassLoader#9fe666.
log4j: Trying to find [log4j.properties] using sun.misc.Launcher$AppClassLoader#9fe666 class loader.
log4j: Trying to find [log4j.properties] using ClassLoader.getSystemResource().
log4j: Could not find resource: [null].

You can load a properties file for Log4j by first loading the properties, then tossing it into log4j's property configurator:
Properties props = new Properties();
FileInputStream fis = new FileInputStream("mylog4j.properties");
props.load(fis);
fis.close();
PropertyConfigurator.configure(props);
The bundleresource seems like a library specific thing. I recommend using grep on the sourcecode:
grep -irn ./ -e "bundleresource"

Related

Debugging OfBiz in Eclipse gives MissingResourceException

I downloaded the OfBiz Java application and the following line throws an MissingResourceException:
ResourceBundle res = ResourceBundle.getBundle(settingsResourceName);
The value of settingsResourceName is "cache", but I cannot find any file called cache.properties or cache_en.properties.
Where should I be looking? I'm new to Java. All my research on SO says there should be such a file.
I imported OfBiz in Eclipse using the Import menu option and selecting Existing Project from File System (I'm not in front of my dev machine so I don't remember the exact wording). But I chose the root folder of the downloaded OfBiz.
I then added the appropriate VM Arguments in the Run Configuration to get it to run properly at least. And that's it, on the first Run I got the above error. I think it has to do with a missing class path but I don't know what to add to class path.
Here is the stack trace:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.apache.ofbiz.base.util.Debug.<clinit>(Debug.java:68)
at org.apache.ofbiz.base.container.ContainerLoader.load(ContainerLoader.java:61)
at org.apache.ofbiz.base.start.StartupControlPanel.loadStartupLoaders(StartupControlPanel.java:202)
at org.apache.ofbiz.base.start.StartupControlPanel.start(StartupControlPanel.java:69)
at org.apache.ofbiz.base.start.Start.main(Start.java:84)
Caused by: java.util.MissingResourceException: Can't find bundle for base name cache, locale en
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1564)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1387)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:773)
at org.apache.ofbiz.base.util.cache.UtilCache.setPropertiesParams(UtilCache.java:174)
at org.apache.ofbiz.base.util.cache.UtilCache.setPropertiesParams(UtilCache.java:170)
at org.apache.ofbiz.base.util.cache.UtilCache.setPropertiesParams(UtilCache.java:166)
at org.apache.ofbiz.base.util.cache.UtilCache.<init>(UtilCache.java:124)
at org.apache.ofbiz.base.util.cache.UtilCache.createUtilCache(UtilCache.java:769)
at org.apache.ofbiz.base.util.UtilProperties.<clinit>(UtilProperties.java:75)
... 5 more
UPDATE:
My mistake, I found two files both called cache.properties in the following folders:
ofbiz-trunk/build/resources/main
ofbiz-trunk/framework/base/config
But these are folders, not packages. I tried putting them in the .classpath but that did not work, I still kept getting the same error.
As suspected, I knew it was because of a missing reference to a class path. After looking at a section on this page: http://www.opensourcestrategies.com/ofbiz/ofbiz_eclipse.php, I learned that I was supposed to go to the Java Build Path and in the Libraries tab, click on Add Class Folder, then point that to ofbiz-trunk/framework/base/config. Which is where I have one of the cache.properties files.

Cannot initialize module TreeWalker during the build in Eclipse

I'm performing Project-Clean... in Eclipse Mars and getting the following error
Errors occurred during the build.
Errors running builder 'Checkstyle Builder' on project 'MyProject'.
cannot initialize module TreeWalker - Unable to instantiate
'RedundantThrows' class, it is also not possible to instantiate it as
com.puppycrawl.tools.checkstyle.checks.annotation.RedundantThrows,
com.puppycrawl.tools.checkstyle.checks.blocks.RedundantThrows,
com.puppycrawl.tools.checkstyle.checks.coding.RedundantThrows,
com.puppycrawl.tools.checkstyle.checks.design.RedundantThrows,
com.puppycrawl.tools.checkstyle.checks.header.RedundantThrows,
com.puppycrawl.tools.checkstyle.checks.imports.RedundantThrows,
com.puppycrawl.tools.checkstyle.checks.indentation.RedundantThrows,
com.puppycrawl.tools.checkstyle.checks.javadoc.RedundantThrows,
com.puppycrawl.tools.checkstyle.checks.metrics.RedundantThrows,
com.puppycrawl.tools.checkstyle.checks.modifier.RedundantThrows,
com.puppycrawl.tools.checkstyle.checks.naming.RedundantThrows,
com.puppycrawl.tools.checkstyle.checks.regexp.RedundantThrows,
com.puppycrawl.tools.checkstyle.checks.sizes.RedundantThrows,
com.puppycrawl.tools.checkstyle.checks.whitespace.RedundantThrows,
com.puppycrawl.tools.checkstyle.checks.RedundantThrows,
com.puppycrawl.tools.checkstyle.filters.RedundantThrows,
com.puppycrawl.tools.checkstyle.RedundantThrows. Please recheck that class name
is specified as canonical name or read how to configure short name usage
http://checkstyle.sourceforge.net/config.html#Packages. Please also recheck that
provided ClassLoader to Checker is configured correctly.
The CheckStyle plugin version is 6.11.
Could you please help me figuring out what may cause this error. Thanks.
The RedundantThrows check has been removed from Checkstyle. Just remove the <module> entry for it from your checkstyle.xml.

Strange cast exception in Groovy script

When I try to run my groovy script in Eclipse, I get a cast exception like:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'package.Config#6babd36b' with class 'package.Config' to class 'package.Config'
The exception occurs when I instantiate another object using the Config as parameter.
clazz.newInstance(config: config)
To me, this seems very odd. But perhaps someone out there can bring me an explanation?
I just want to add some information for this topic. Maybe somebody will find it useful.
I received same error then was trying to compile groovy script for using in multithread environment:
GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
Class<? extends Script> clazz = groovyClassLoader.parseClass(groovyCode);
return clazz.newInstance();
But by default GroovyClassLoader are using ClassLoader of current thread:
Thread.currentThread().getContextClassLoader())
So, you can pickup one class loader and use it for all threads. Or avoid shared groovy stuff :)
Solved! :)
In the Eclipse run configuration, I looked at the argument tab and noticed that both source files and compiled class files was part of the classpath parameter:
--classpath "${workspace_loc:/groovyscripts}/src/main/groovy:...
...:${workspace_loc:/groovyscripts}/classes"
the above setting gives different classloaders for my config object (loaded from src/) and the target field (loaded from classes/)
groovy.lang.GroovyClassLoader$InnerLoader#7a06cf15
org.codehaus.groovy.tools.RootLoader#32728d
Removing the first reference to the source files resulted in a successful run. Classloaders after removal is ONLY RootLoader.
org.codehaus.groovy.tools.RootLoader#32728d
The answer is to remove the source from the classpath in arguments tab in Eclipse.

Log4j email error "Cannot find java.home ??"

I'm using log4j under J2SE. I've configured it to use a mailer for a certain type of log event.. When the logger.error triggers I get the following exception:
java.lang.Error: Can't find java.home ??
at sun.net.NetProperties.loadDefaultProperties(NetProperties.java:45)
...
at org.apache.log4j.net.SMTPAppender.sendBuffer(SMTPAppender.java:416)
Anyone run into this before? I have the required mail.jar library included as well.
Any help is appreciated.
Thanks,
Chris
I looked at the source code of NetProperties and the error basically means what it says. The System property called "java.home" is unset. According to the javadoc for System.getProperties(), that property is set automatically by the JVM to the java installation directory.
The only explanation I can think of is that something in your application or some third-party library you are using has explicitly unset that property. That is obviously a bad thing to do ...

ATG taglibs on OSGI problems

We're currently creating an app that needs ATG taglibs on SLING/OSGI, we have created a bundle with these taglibs and uploaded it, of course these taglibs call ATG classes, so we are including them in the bootdelegation, using sling.properties file.
sling.bootdelegation.simple=atg.nucleus
sling.bootdelegation.class.atg.nucleus.Nucleus=atg.appassembly, \
atg.appassembly.ant, \
atg.appassembly.progress, \
atg.appassembly.util, \
...ETC...
First we got this error:
org.apache.sling.api.scripting.ScriptEvaluationException: atg/taglib/dspjsp/ImportBeanTag
at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:163)
at org.apache.sling.scripting.core.impl.DefaultSlingScript.eval(DefaultSlingScript.java:107)
at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:226)
at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:465)
....
....
Caused by: java.lang.NoClassDefFoundError: atg/taglib/dspjsp/ImportBeanTag
at org.apache.jsp.apps.mygeometrixx.components.contenpage.center_jsp._jspx_meth_dsp_005fimportbean_005f0(center_jsp.java:177)
at org.apache.jsp.apps.mygeometrixx.components.contenpage.center_jsp._jspService(center_jsp.java:154)
at org.apache.sling.scripting.jsp.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at
So we added atg.taglib.dspjsp to the packages to be added in bootdelegation sling.properties file.
Then we got this error:
org.apache.sling.api.scripting.ScriptEvaluationException: atg.taglib.dspjsp.ImportBeanTag
at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:163)
at org.apache.sling.scripting.core.impl.DefaultSlingScript.eval(DefaultSlingScript.java:107)
at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:226)
at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:465)
...
Caused by: java.lang.ClassCastException: atg.taglib.dspjsp.ImportBeanTag
at org.apache.sling.scripting.jsp.jasper.runtime.TagHandlerPool.get(TagHandlerPool.java:125)
at org.apache.jsp.apps.mygeometrixx.components.contenpage.center_jsp._jspx_meth_dsp_005fimportbean_005f0(center_jsp.java:177)
at org.apache.jsp.apps.mygeometrixx.components.contenpage.center_jsp._jspService(center_jsp.java:154)
at org.apache.sling.scripting.jsp.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
All this is running on JBOSS.
Is there a way to avoid this class conflict that is causing the cast exception?
The class cast exception is usually a sign that that class is being made available in two different places (in your case probably via bootdelegation and maybe via a bundle that exports this class). That's what I would investigate first.
Also, to make things more explicit, I would in general advise you to not use boot delegation but instead export these packages explicitly through the system bundle. That way at least you can better debug where classes come from and how things are "wired" by the OSGi resolver.
When loading the ATG tag libraries from outside of the OSGi framework you also have to make sure to provide the JSP API from outside of the framework. By default Sling embeds the JSP API (in the JSP Scripting Bundle).
There are various ways to expose the JSP API into the framework. One is to add them to the system packages in the sling.properties file:
sling.system.packages.atg_jsp = javax.servlet.jsp;javax.servlet.jsp.el; \
javax.servlet.jsp.resources;javax.servlet.jsp.tagext;version=2.1.0