File: JDBCAppender.java

package info (click to toggle)
apache-log4j1.2 1.2.17-7%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,880 kB
  • sloc: java: 35,228; xml: 4,813; cpp: 252; perl: 69; makefile: 30; sh: 14
file content (601 lines) | stat: -rw-r--r-- 18,135 bytes parent folder | download | duplicates (7)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/*
 * 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.
 */

package com.klopotek.utils.log;

import java.sql.*;
import java.util.*;
import org.apache.log4j.*;
import org.apache.log4j.helpers.*;
import org.apache.log4j.spi.*;

/**
The JDBCAppender, writes messages into a database

<p><b>The JDBCAppender is configurable at runtime by setting options in two alternatives : </b></p>
<dir>
	<p><b>1. Use a configuration-file</b></p>
	<p>Define the options in a file (<A HREF="configfile_example.txt">example</A>) and call a <code>PropertyConfigurator.configure(filename)</code> in your code.</p>
	<p><b>2. Use the methods of JDBCAppender to do it</b></p>
	<p>Call <code>JDBCAppender::setOption(JDBCAppender.xxx_OPTION, String value)</code> to do it analogically without a configuration-file (<A HREF="code_example2.java">example</A>)</p>
</dir>

<p>All available options are defined as static String-constants in JDBCAppender named xxx_OPTION.</p>

<p><b>Here is a description of all available options :</b></p>
<dir>
	<p><b>1. Database-options to connect to the database</b></p>
	<p>- <b>URL_OPTION</b>			: a database url of the form jdbc:subprotocol:subname</p>
	<p>- <b>USERNAME_OPTION</b>	: the database user on whose behalf the connection is being made</p>
	<p>- <b>PASSWORD_OPTION</b>	: the user's password</p>

	<p><b>2. Connector-option to specify your own JDBCConnectionHandler</b></p>
	<p>- <b>CONNECTOR_OPTION</b>	: a classname which is implementing the JDBCConnectionHandler-interface</p>
	<p>This interface is used to get a customized connection.</p>
	<p>If in addition the database-options are given, these options will be used as arguments for the JDBCConnectionHandler-interface to get a connection.</p>
	<p>Else if no database-options are given, the JDBCConnectionHandler-interface is called without them.</p>
	<p>Else if this option is not defined, the database-options are required to open a connection by the JDBCAppender.</p>

	<p><b>3. SQL-option to specify a static sql-statement which will be performed with every occuring message-event</b></p>
	<p>- <b>SQL_OPTION</b>			: a sql-statement which will be used to write to the database</p>
	<p>Use the variable <b>@MSG@</b> on a location in the statement, which has to be dynamically replaced by the message-text.</p>
	<p>If you give this option, the table-option and columns-option will be ignored !</p>

	<p><b>4. Table-option to specify a table contained by the database</b></p>
	<p>- <b>TABLE_OPTION</b>		: the table in which the logging will be done</p>

	<p><b>5. Columns-option to describe the important columns of the table (Not nullable columns are mandatory to describe!)</b></p>
	<p>- <b>COLUMNS_OPTION</b>		: a formatted list of column-descriptions</p>
	<p>Each column description consists of</p>
	<dir>
		<p>- the <b><i>name</i></b> of the column (required)</p>
		<p>- a <b><i>logtype</i></b> which is a static constant of class LogType (required)</p>
		<p>- and a <b><i>value</i></b> which depends by the LogType (optional/required, depending by logtype)</p>
	</dir>
	<p>Here is a description of the available logtypes of class <b>{@link LogType}</b> and how to handle the <b><i>value</i></b>:</p>
	<dir>
		<p>o <b>MSG</b>			= a value will be ignored, the column will get the message. (One columns need to be of this type!)</p>
		<p>o <b>STATIC</b>		= the value will be filled into the column with every logged message. (Ensure that the type of value can be casted into the sql-type of the column!)</p>
		<p>o <b>ID</b>			= value must be a classname, which implements the JDBCIDHandler-interface.</p>
		<p>o <b>TIMESTAMP</b>	= a value will be ignored, the column will be filled with a actually timestamp with every logged message.</p>
		<p>o <b>EMPTY</b>		= a value will be ignored, the column will be ignored when writing to the database (Ensure to fill not nullable columns by a database trigger!)</p>
	</dir>
	<p>If there are more than one column to describe, the columns must be separated by a Tabulator-delimiter (unicode0008) !</p>
	<p>The arguments of a column-description must be separated by the delimiter '~' !</p>
	<p><i>(Example :  name1~logtype1~value1   name2~logtype2~value2...)</i></p>

	<p><b>6. Layout-options to define the layout of the messages (optional)</b></p>
	<p>- <b>_</b> : the layout wont be set by a xxx_OPTION</p>
	<p>See the configuration-file and code examples below...</p>
	<p>The default is a layout of the class {@link org.apache.log4j.PatternLayout} with the pattern=%m which representate only the message.</p>

	<p><b>7. Buffer-option to define the size of the message-event-buffer (optional)</b></p>
	<p>- <b>BUFFER_OPTION</b>		: define how many messages will be buffered until they will be updated to the database.</p>
	<p>The default is buffer=1, which will do a update with every happened message-event.</p>

	<p><b>8. Commit-option to define a auto-commitment</b></p>
	<p>- <b>COMMIT_OPTION</b>		: define whether updated messages should be committed to the database (Y) or not (N).</p>
	<p>The default is commit=Y.</p>
</dir>

<p><b>The sequence of some options is important :</b></p>
<dir>
	<p><b>1. Connector-option OR/AND Database-options</b></p>
	<p>Any database connection is required !</p>
	<p><b>2. (Table-option AND Columns-option) OR SQL-option</b></p>
	<p>Anything of that is required ! Whether where to write something OR what to write somewhere...;-)</p>
	<p><b>3. All other options can be set at any time...</b></p>
	<p>The other options are optional and have a default initialization, which can be customized.</p>
</dir>

<p><b>Here is a <b>configuration-file example</b>, which can be used as argument for the <b>PropertyConfigurator</b> : </b><A HREF="configfile_example.txt"> configfile_example.txt</A></p>

<p><b>Here is a <b>code-example</b> to configure the JDBCAppender <b>with a configuration-file</b> : </b><A HREF="code_example1.java"> code_example1.java</A></p>

<p><b>Here is a <b>another code-example</b> to configure the JDBCAppender <b>without a configuration-file</b> : </b><A HREF="code_example2.java"> code_example2.java</A></p>



<p><b>Author : </b><A HREF="mailto:t.fenner@klopotek.de">Thomas Fenner</A></p>

@since 1.0
*/
public class JDBCAppender extends AppenderSkeleton
{
	/**
	A database-option to to set a database url of the form jdbc:subprotocol:subname.
	*/
	public static final String URL_OPTION			= "url";

	/**
	A database-option to set the database user on whose behalf the connection is being made.
	*/
	public static final String USERNAME_OPTION	= "username";

	/**
	A database-option to set the user's password.
	*/
	public static final String PASSWORD_OPTION	= "password";

	/**
	A table-option to specify a table contained by the database
	*/
	public static final String TABLE_OPTION		= "table";

	/**
	A connector-option to specify your own JDBCConnectionHandler
	*/
	public static final String CONNECTOR_OPTION	= "connector";

	/**
   A columns-option to describe the important columns of the table
	*/
	public static final String COLUMNS_OPTION		= "columns";

	/**
	A sql-option to specify a static sql-statement which will be performed with every occuring message-event
   */
	public static final String SQL_OPTION			= "sql";

	/**
   A buffer-option to define the size of the message-event-buffer
	*/
	public static final String BUFFER_OPTION		= "buffer";

	/**
   A commit-option to define a auto-commitment
	*/
	public static final String COMMIT_OPTION		= "commit";


	//Variables to store the options values setted by setOption() :
	private String url		= null;
	private String username	= null;
	private String password	= null;
	private String table		= null;
	private String connection_class = null;
	private String sql		= null;
	private boolean docommit = true;
	private int buffer_size	= 1;
   private JDBCConnectionHandler connectionHandler = null;

	//This buffer stores message-events.
   //When the buffer_size is reached, the buffer will be flushed and the messages will updated to the database.
	private ArrayList buffer = new ArrayList();

   //Database-connection
	private Connection con = null;

	//This class encapsulate the logic which is necessary to log into a table
	private JDBCLogger jlogger = new JDBCLogger();

   //Flags :
   //A flag to indicate a established database connection
	private boolean connected = false;
   //A flag to indicate configuration status
	private boolean configured = false;
   //A flag to indicate that everything is ready to get append()-commands.
	private boolean ready = false;

	/**
	If program terminates close the database-connection and flush the buffer
   */
	public void finalize()
	{
		close();
      super.finalize();
	}

	/**
	Internal method. Returns a array of strings containing the available options which can be set with method setOption()
	*/
	public String[] getOptionStrings()
   {
   	// The sequence of options in this string is important, because setOption() is called this way ...
		return new String[]{CONNECTOR_OPTION, URL_OPTION, USERNAME_OPTION, PASSWORD_OPTION, SQL_OPTION, TABLE_OPTION, COLUMNS_OPTION, BUFFER_OPTION, COMMIT_OPTION};
	}


	/**
     Sets all necessary options
	*/
	public void setOption(String _option, String _value)
	{
   	_option = _option.trim();
      _value = _value.trim();

		if(_option == null || _value == null) return;
		if(_option.length() == 0 || _value.length() == 0) return;

      _value = _value.trim();

		if(_option.equals(CONNECTOR_OPTION))
      {
      	if(!connected) connection_class = _value;
      }
		else if(_option.equals(URL_OPTION))
		{
			if(!connected) url = _value;
		}
		else if(_option.equals(USERNAME_OPTION))
		{
			if(!connected) username = _value;
		}
		else if(_option.equals(PASSWORD_OPTION))
		{
			if(!connected) password = _value;
		}
		else if(_option.equals(SQL_OPTION))
      {
			sql = _value;
      }
		else if(_option.equals(TABLE_OPTION))
      {
      	if(sql != null) return;
      	table = _value;
      }
		else if(_option.equals(COLUMNS_OPTION))
      {
      	if(sql != null) return;

			String name = null;
         int logtype = -1;
         String value = null;
         String column = null;
         String arg = null;
         int num_args = 0;
         int num_columns = 0;
			StringTokenizer st_col;
			StringTokenizer st_arg;

         //Columns are TAB-separated
			st_col = new StringTokenizer(_value,  "	");

			num_columns = st_col.countTokens();

         if(num_columns < 1)
  	      {
     	   	errorHandler.error("JDBCAppender::setOption(), Invalid COLUMN_OPTION value : " + _value + " !");
            return;
        	}

         for(int i=1; i<=num_columns; i++)
         {
				column = st_col.nextToken();

            //Arguments are ~-separated
				st_arg = new StringTokenizer(column, "~");

				num_args = st_arg.countTokens();

	         if(num_args < 2)
   	      {
      	   	errorHandler.error("JDBCAppender::setOption(), Invalid COLUMN_OPTION value : " + _value + " !");
               return;
         	}

	         for(int j=1; j<=num_args; j++)
   	      {
					arg = st_arg.nextToken();

					if(j == 1) name = arg;
					else if(j == 2)
      	      {
         	   	try
            	   {
							logtype = Integer.parseInt(arg);
	               }
   	            catch(Exception e)
      	         {
         	      	logtype = LogType.parseLogType(arg);
	               }

						if(!LogType.isLogType(logtype))
   	            {
	   	            errorHandler.error("JDBCAppender::setOption(), Invalid COLUMN_OPTION LogType : " + arg + " !");
                     return;
         	      }
            	}
					else if(j == 3) value = arg;
   	      }

	         if(!setLogType(name, logtype, value)) return;
         }
      }
		else if(_option.equals(BUFFER_OPTION))
      {
        	try
         {
				buffer_size = Integer.parseInt(_value);
         }
         catch(Exception e)
         {
	         errorHandler.error("JDBCAppender::setOption(), Invalid BUFFER_OPTION value : " + _value + " !");
				return;
         }
      }
		else if(_option.equals(COMMIT_OPTION))
      {
      	docommit = _value.equals("Y");
      }

      if(_option.equals(SQL_OPTION) || _option.equals(TABLE_OPTION))
      {
			if(!configured) configure();
      }
	}

	/**
	Internal method. Returns true, you may define your own layout...
	*/
	public boolean requiresLayout()
	{
		return true;
	}


	/**
	Internal method. Close the database connection & flush the buffer.
	*/
	public void close()
	{
	   flush_buffer();
      if(connection_class == null)
      {
			try{con.close();}catch(Exception e){errorHandler.error("JDBCAppender::close(), " + e);}
      }
		this.closed = true;
	}


	/**
	You have to call this function for all provided columns of your log-table !
   */
	public boolean setLogType(String _name, int _logtype, Object _value)
	{
   	if(sql != null) return true;

		if(!configured)
		{
			if(!configure()) return false;
		}

		try
		{
			jlogger.setLogType(_name, _logtype, _value);
		}
		catch(Exception e)
		{
			errorHandler.error("JDBCAppender::setLogType(), " + e);
			return false;
		}

		return true;
	}


	/**
	Internal method. Appends the message to the database table.
	*/
	public void append(LoggingEvent event)
	{
		if(!ready)
      {
      	if(!ready())
         {
				errorHandler.error("JDBCAppender::append(), Not ready to append !");
         	return;
			}
      }

		buffer.add(event);

		if(buffer.size() >= buffer_size) flush_buffer();
	}


	/**
	Internal method. Flushes the buffer.
	*/
   public void flush_buffer()
   {
   	try
      {
      	int size = buffer.size();

         if(size < 1) return;

        	for(int i=0; i<size; i++)
         {
				LoggingEvent event = (LoggingEvent)buffer.get(i);

				//Insert message into database
				jlogger.append(layout.format(event));
         }

         buffer.clear();

			if(docommit) con.commit();
      }
		catch(Exception e)
		{
			errorHandler.error("JDBCAppender::flush_buffer(), " + e + " : " + jlogger.getErrorMsg());
			try{con.rollback();} catch(Exception ex){}
			return;
		}
   }


	/**
	Internal method. Returns true, when the JDBCAppender is ready to append messages to the database, else false.
	*/
	public boolean ready()
	{
   	if(ready) return true;

		if(!configured) return false;

		ready = jlogger.ready();

      if(!ready){errorHandler.error(jlogger.getErrorMsg());}

      return ready;
	}


	/**
	Internal method. Connect to the database.
	*/
	protected void connect() throws Exception
	{
   	if(connected) return;

		try
		{
      	if(connection_class == null)
         {
				if(url == null)		throw new Exception("JDBCAppender::connect(), No URL defined.");

				if(username == null)	throw new Exception("JDBCAppender::connect(), No USERNAME defined.");

				if(password == null)	throw new Exception("JDBCAppender::connect(), No PASSWORD defined.");

				connectionHandler = new DefaultConnectionHandler();
			}
         else
         {
				connectionHandler = (JDBCConnectionHandler)(Class.forName(connection_class).newInstance());
         }

         if(url != null && username != null && password != null)
         {
				con = connectionHandler.getConnection(url, username, password);
         }
         else
         {
	     		con = connectionHandler.getConnection();
         }

         if(con.isClosed())
         {
         	throw new Exception("JDBCAppender::connect(), JDBCConnectionHandler returns no connected Connection !");
			}
		}
		catch(Exception e)
		{
			throw new Exception("JDBCAppender::connect(), " + e);
		}

      connected = true;
	}

	/**
	Internal method. Configures for appending...
	*/
	protected boolean configure()
	{
		if(configured) return true;

		if(!connected)
		{
      	if((connection_class == null) && (url == null || username == null || password == null))
			{
				errorHandler.error("JDBCAppender::configure(), Missing database-options or connector-option !");
				return false;
         }

         try
         {
				connect();
         }
         catch(Exception e)
         {
         	connection_class = null;
            url = null;
				errorHandler.error("JDBCAppender::configure(), " + e);
            return false;
         }
		}

		if(sql == null && table == null)
		{
			errorHandler.error("JDBCAppender::configure(), No SQL_OPTION or TABLE_OPTION given !");
			return false;
		}

		if(!jlogger.isConfigured())
		{
			try
         {
         	jlogger.setConnection(con);

         	if(sql == null)
            {
	         	jlogger.configureTable(table);
            }
            else jlogger.configureSQL(sql);
         }
         catch(Exception e)
         {
	         errorHandler.error("JDBCAppender::configure(), " + e);
         	return false;
         }
		}

      //Default Message-Layout
      if(layout == null)
      {
      	layout = new PatternLayout("%m");
      }

      configured = true;

		return true;
	}
}

/**
This is a default JDBCConnectionHandler used by JDBCAppender
*/
class DefaultConnectionHandler implements JDBCConnectionHandler
{
	Connection con = null;

   public Connection getConnection()
   {
   	return con;
   }

   public Connection getConnection(String _url, String _username, String _password)
   {
   	try
      {
   		if(con != null && !con.isClosed()) con.close();
			con = DriverManager.getConnection(_url, _username, _password);
			con.setAutoCommit(false);
      }
      catch(Exception e){}

   	return con;
   }
}