View Javadoc

1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.domain.util;
17  
18  import java.io.Serializable;
19  import java.lang.reflect.Field;
20  import java.text.DateFormat;
21  import java.util.Calendar;
22  import java.util.Collection;
23  
24  import org.acegisecurity.domain.PersistableEntity;
25  
26  import org.apache.commons.lang.builder.ToStringStyle;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  
31  /***
32   * Customized Commons Lang <code>ReflectionToStringBuilder</code>
33   * that ignores collections and inaccessible (ie lazy-loaded) fields.
34   *
35   * @author Carlos Sanchez
36   * @author Ben Alex
37   * @version $Id: ReflectionToStringBuilder.java,v 1.3 2005/11/17 00:55:50 benalex Exp $
38   */
39  public class ReflectionToStringBuilder
40      extends org.apache.commons.lang.builder.ReflectionToStringBuilder {
41      //~ Static fields/initializers =============================================
42  
43      protected final transient Log logger = LogFactory.getLog(getClass());
44  
45  	private static DateFormat formatter = DateFormat.getDateTimeInstance();
46  
47      //~ Constructors ===========================================================
48  
49      public ReflectionToStringBuilder(Object object) {
50          super(object, ToStringStyle.MULTI_LINE_STYLE);
51      }
52  
53      //~ Methods ================================================================
54  
55      /***
56       * Calendar fields are formatted with DateFormat.getDateTimeInstance()
57       * instead of using Calendar.toString().
58       *
59       * @see org.apache.commons.lang.builder.ReflectionToStringBuilder#getValue(java.lang.reflect.Field)
60       */
61      protected Object getValue(Field f)
62          throws IllegalArgumentException, IllegalAccessException {
63          Object value = super.getValue(f);
64  
65          if (Calendar.class.isInstance(value)) {
66              Calendar c = (Calendar) value;
67  
68              return formatter.format(c.getTime());
69          } else {
70              return value;
71          }
72      }
73  	
74      protected boolean accept(Field field) {
75  		// Ignore if field inaccessible or collection
76  		try {
77  			Object o = getValue(field);
78  			if (o != null) {
79  				if (o instanceof PersistableEntity) {
80  					Serializable id = ((PersistableEntity)o).getInternalId();
81  					if (logger.isDebugEnabled()) {
82  						logger.debug(field + " id: " + id);
83  					}
84  				}
85  				if (o instanceof Collection) {
86  					int size = ((Collection)o).size();
87  					this.append(field.getName(), "<collection with " + size + " elements>");
88  					if (logger.isDebugEnabled()) {
89  						logger.debug(field + " size: " + size);
90  					}
91  				}
92  			}
93  		} catch (Exception fieldInaccessible) {
94  			this.append(field.getName(), "<inaccessible>");
95  			if (logger.isDebugEnabled()) {
96  				logger.debug("Inaccessible: " + field);
97  			}
98  			return false;
99  		}
100 
101 		if (logger.isDebugEnabled()) {
102 			logger.debug("Accessible: " + field);
103 		}
104 		
105         return true;
106     }	
107 }