View Javadoc

1   /*
2    * The SmartWeb Framework
3    * Copyright (C) 2004-2006
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   *
19   * For further informations on the SmartWeb Framework please visit
20   *
21   *                        http://smartweb.sourceforge.net
22   */
23  package net.smartlab.web;
24  
25  import java.io.IOException;
26  import java.io.ObjectInputStream;
27  import java.lang.reflect.Field;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /**
33   * TODO documentation
34   * 
35   * @author rlogiacco
36   * @uml.dependency supplier="net.smartlab.web.Enumeration"
37   * @uml.dependency supplier="net.smartlab.web.StringEnumeration"
38   */
39  public abstract class BusinessObject implements DataTransferObject {
40  
41  	private final static long serialVersionUID = 8960870409825593855L;
42  
43  	/**
44  	 * Allows for business operations logging.
45  	 */
46  	protected final transient Log logger = LogFactory.getLog(this.getClass());
47  
48  	/**
49  	 * This number stores the object version and is used for optimistical
50  	 * locking.
51  	 * 
52  	 * @uml.property name="version"
53  	 */
54  	private long version;
55  
56  
57  	/**
58  	 * Returns the object version. This property ensures optimistical locking
59  	 * support to every subclass.
60  	 * 
61  	 * @return returns the version.
62  	 * @hibernate.version
63  	 * @uml.property name="version"
64  	 */
65  	public long getVersion() {
66  		return version;
67  	}
68  
69  	/**
70  	 * Sets the object version. This property ensures optimistical locking
71  	 * support to every subclass.
72  	 * 
73  	 * @param version the version to set.
74  	 * @uml.property name="version"
75  	 */
76  	public void setVersion(long version) {
77  		this.version = version;
78  	}
79  
80  	/**
81  	 * Custom deserialization. We need to re-initialize a logger instance as loggers
82  	 * can't be serialized.
83  	 */
84  	private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
85  		try {
86  			Class type = BusinessObject.class;
87  			Field logger = type.getDeclaredField("logger");
88  			logger.setAccessible(true);
89  			logger.set(this, LogFactory.getLog(type));
90  			logger.setAccessible(false);
91  		} catch (Exception e) {
92  			LogFactory.getLog(this.getClass())
93  				.warn("unable to recover the logger after deserialization: logging statements will cause null pointer exceptions", e);
94  		}
95  		in.defaultReadObject();
96  	}
97  }