Skip to main content
summaryrefslogtreecommitdiffstats
blob: c8a224b01b9121666d371572391415b988237c0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*******************************************************************************
 * Copyright (c) 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.wsdl.validation.internal.ui.text;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.eclipse.wst.wsdl.validation.internal.logging.ILogger;

/**
 * A custom WSDL validator logger that passes logging events to log4j.
 */
public class Log4jLogger implements ILogger 
{
  // This class is implemented using reflection to avoid a comilation dependency
  // on log4j.
  protected Object logger = null;
  protected Method error1 = null;
  protected Method warn1 = null;
  protected Method info1 = null;
  protected Method debug1 = null;
  protected Method error2 = null;
  protected Method warn2 = null;
  protected Method info2 = null;
  protected Method debug2 = null;
  
  public Log4jLogger()
  {
	try
    {
	  Class loggerClass = getClass().getClassLoader().loadClass("org.apache.log4j.Logger");
	  Class categoryClass = getClass().getClassLoader().loadClass("org.apache.log4j.Category");
	  Method getLogger = loggerClass.getDeclaredMethod("getLogger" , new Class[]{Class.class});
	  logger = getLogger.invoke(loggerClass, new Object[]{WSDLValidate.class});
	  error1 = categoryClass.getDeclaredMethod("error" , new Class[]{Object.class});
	  warn1 = categoryClass.getDeclaredMethod("warn" , new Class[]{Object.class});
	  info1 = categoryClass.getDeclaredMethod("info" , new Class[]{Object.class});
	  debug1 = categoryClass.getDeclaredMethod("debug" , new Class[]{Object.class});
	  error2 = categoryClass.getDeclaredMethod("error" , new Class[]{Object.class, Throwable.class});
	  warn2 = categoryClass.getDeclaredMethod("warn" , new Class[]{Object.class, Throwable.class});
	  info2 = categoryClass.getDeclaredMethod("info" , new Class[]{Object.class, Throwable.class});
	  debug2 = categoryClass.getDeclaredMethod("debug" , new Class[]{Object.class, Throwable.class});
	}
	catch(ClassNotFoundException e)
	{
	  System.err.println("Unable to create Log4j Logger. Ensure Log4J is on the classpath."); 
	}
	catch(NoSuchMethodException e)
	{
		
	}
	catch(IllegalAccessException e)
	{
		
	}
	catch(InvocationTargetException e)
	{
		
	}
  }
  
  /* (non-Javadoc)
   * @see org.eclipse.wst.wsdl.validation.internal.logging.ILogger#log(java.lang.String, int)
   */
  public void log(String message, int severity) 
  {
	if(logger != null)
    {
      try
      {
        if(severity == ILogger.SEV_ERROR)
        {
          error1.invoke(logger, new Object[]{message});
		}
        else if(severity == ILogger.SEV_WARNING)
        {
          warn1.invoke(logger, new Object[]{message});
        }
        else if(severity == ILogger.SEV_INFO)
        {
          info1.invoke(logger, new Object[]{message});
        }
        else if(severity == ILogger.SEV_VERBOSE)
        {
          debug1.invoke(logger, new Object[]{message});
        }
      }
      catch(InvocationTargetException e)
      {
        // Do nothing.
      }
      catch(IllegalAccessException e)
      {
        // Do nothing.  
      }
    }
  }

  /* (non-Javadoc)
   * @see org.eclipse.wst.wsdl.validation.internal.logging.ILogger#log(java.lang.String, int, java.lang.Throwable)
   */
  public void log(String message, int severity, Throwable throwable) 
  {
	if(logger != null)
	{
	  try
	  {
	    if(severity == ILogger.SEV_ERROR)
	    {
		  error2.invoke(logger, new Object[]{message, throwable});
	    }
	    else if(severity == ILogger.SEV_WARNING)
	    {
		  warn2.invoke(logger, new Object[]{message, throwable});
	    }
	    else if(severity == ILogger.SEV_INFO)
	    {
		  info2.invoke(logger, new Object[]{message, throwable});
	    }
	    else if(severity == ILogger.SEV_VERBOSE)
	    {
	      debug2.invoke(logger, new Object[]{message, throwable});
	    }
	  }
	  catch(InvocationTargetException e)
	  {
		// Do nothing.
	  }
	  catch(IllegalAccessException e)
	  {
		// Do nothing.  
	  }
	}
  }
}

Back to the top