Amazon

Friday, July 25, 2014

AXIS 2 - Catching Errors

Axis2 - This utility will never throw errors if your log4j is not enabled in the application. This will make you to struggle to find the issue. You will never understand the issue is in Client Code or Server Code. And it will consume your 2-3-4 days to one Week, So, then what is the solution: Enable the log4j of your WebService Server Application.

Step 1 - Create a log4j.properties file.
Step 2 - Create a ServletContextListener
Step 3 - Load the log4j.properties file in PropertyConfigurator, its a log4j class.

Step 1 - log4j.properties

#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE
log4j.rootCategory=DEBUG, LOGFILE

# Set the enterprise logger priority to FATAL
log4j.logger.org.apache.axis2.enterprise=DEBUG
log4j.logger.de.hunsicker.jalopy.io=DEBUG
log4j.logger.httpclient.wire.header=DEBUG
log4j.logger.org.apache.commons.httpclient=DEBUG

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%p] %m%n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=D:\\AXIS2_DUMP\\axis2-1.6.2\\logs\\axis2.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n

Step 2 - Create a ServletContextListener and Step 3
MyClass.java

package com.init;

import java.util.Properties;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.PropertyConfigurator;

public class MyClass implements ServletContextListener {

public void contextInitialized(ServletContextEvent e) {
init();
System.out.println("\n\ncontextInitialized(ServletContextEvent e)");
}

public void contextDestroyed(ServletContextEvent e) {

System.out.println("\n\ncontextDestroyed(ServletContextEvent e)");
}

private static void init() {
Properties prop = new Properties();
// code added by sanjeev for NON_CLUSTER_CR -START
try {
String logpath = System.getProperty("log4j.configuration");
System.out.println("logpath::"+logpath);

PropertyLoader.loadProperties(logpath);
prop = PropertyLoader.getProperties();

System.out.println("IPHLogger | init | prop="+prop );

if (prop != null) {
                                        //loading log4j.properties in log4j memory
PropertyConfigurator.configure(prop);
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
}

PropertyLoader.java 
package com.init;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import java.util.ResourceBundle;


public abstract class PropertyLoader {

private PropertyLoader() {
}

private static final boolean THROW_ON_LOAD_FAILURE = true;

private static final boolean LOAD_AS_RESOURCE_BUNDLE = true;

private static final String SUFFIX = ".properties";

private static volatile Properties result = null;

// public: ................................................................

/**
* Looks up a resource named 'name' in the classpath. The resource must map
* to a file with .properties extention. The name is assumed to be absolute
* and can use either "/" or "." for package segment separation with an
* optional leading "/" and optional ".properties" suffix. Thus, the
* following names refer to the same resource:
*


* some.pkg.Resource
* some.pkg.Resource.properties
* some/pkg/Resource
* some/pkg/Resource.properties
* /some/pkg/Resource
* /some/pkg/Resource.properties
*
*
* @param argStrname classpath resource name [may not be null]
* @param argClassloader classloader through which to load the resource [null
* is equivalent to the application loader]
*
* @return resource converted to java.util.Properties [may be null if the
* resource was not found and THROW_ON_LOAD_FAILURE is false]
* @throws IllegalArgumentException if the resource was not found and
* THROW_ON_LOAD_FAILURE is true
*/
public static Properties loadProperties(String argStrname,
ClassLoader argClassloader) {
if (argStrname == null)
throw new IllegalArgumentException("null input: name");

if (argStrname.startsWith("/"))
argStrname = argStrname.substring(1);

if (argStrname.endsWith(SUFFIX))
argStrname = argStrname.substring(0, argStrname.length()
- SUFFIX.length());

InputStream objInputStream = null;
try {
if (argClassloader == null)
argClassloader = ClassLoader.getSystemClassLoader();

if (LOAD_AS_RESOURCE_BUNDLE) {
argStrname = argStrname.replace('/', '.');

System.out.println("0->"+argStrname);
// throws MissingResourceException on lookup failures:
final ResourceBundle objResourceBundle = ResourceBundle .getBundle(argStrname);

result = new Properties();
for (Enumeration keys = objResourceBundle.getKeys(); keys.hasMoreElements();) {
final String key = (String) keys.nextElement();
final String value = objResourceBundle.getString(key);
result.put(key, value);
}
} else {
argStrname = argStrname.replace('.', '/');
System.out.println("1->"+argStrname);

if (!argStrname.endsWith(SUFFIX))
argStrname = argStrname.concat(SUFFIX);

// returns null on lookup failures:
objInputStream = argClassloader.getResourceAsStream(argStrname);
if (objInputStream != null) {
result = new Properties();
result.load(objInputStream); // can throw IOException
}
}
} catch (Exception e) {
result = null;
} finally {
if (objInputStream != null)
try {
objInputStream.close();
} catch (Throwable ignore) {
}
}

if (THROW_ON_LOAD_FAILURE && (result == null)) {
throw new IllegalArgumentException("could not load ["
+ argStrname
+ "]"
+ " as "
+ (LOAD_AS_RESOURCE_BUNDLE ? "a resource bundle"
: "a classloader resource"));
}

return result;
}

/**
* A convenience overload of loadProperties(String, ClassLoader)
* that uses the current thread's context classloader.
*/
public static Properties loadProperties(final String argStrName) {
return loadProperties(argStrName, Thread.currentThread().getContextClassLoader());
}

/**
* A method to retrieve the properties value based on the key
* passed as an argument
*
*/
public static String getValue(String argStrKeyVal) {
return (result == null) ? (null): ((result.containsKey(argStrKeyVal)) ? ((String) result
.get(argStrKeyVal)) : (null));

}
/**
* A method to retrieve the properties object associated with
* the file.
*/
public static synchronized Properties getProperties() {
return (result == null) ? (null) : result;
}

} // end of class

Adding listener in web.xml
 
<?xml version="1.0" encoding="UTF-8"?>
<web-app >
<listener>
<listener-class>com.init.MyClass</listener-class>
</listener>  
</web-app>
   

No comments:

Post a Comment

Amazon Best Sellors

Weblogic - Server Creation - Issues

Datasource creation using python script.  Python Script was failing after creating Datasource. From UI Admin Console, if I try to activat...