View Javadoc

1   /*
2    * This file is part of AceLogger.
3    * 
4    * AceLogger is free software: you can redistribute it and/or modify it under the terms of the GNU
5    * Lesser General Public License as published by the Free Software Foundation, either version 3 of
6    * the License, or (at your option) any later version.
7    * 
8    * AceLogger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
9    * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10   * Lesser General Public License for more details.
11   * 
12   * You should have received a copy of the GNU Lesser General Public License along with AceLogger.
13   * If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
14   */
15  package net.sourceforge.acelogger.location.gatherer;
16  
17  import net.sourceforge.acelogger.location.LogEventLocation;
18  import net.sourceforge.acelogger.location.resolver.CodeFrameResolver;
19  import net.sourceforge.acelogger.location.resolver.UserCodeFrameResolver;
20  
21  /**
22   * Abstracts common tasks for LocationGatherer.
23   * 
24   * @author Zardi (https://sourceforge.net/users/daniel_zardi)
25   * @version 1.0.0
26   * @since 1.0.0
27   */
28  public abstract class BaseLocationGatherer implements LocationGatherer {
29  
30  	/**
31  	 * The identifier of this gatherer.
32  	 */
33  	private String identifier;
34  
35  	/**
36  	 * The CodeFrameResolver used to discover the caller location.
37  	 */
38  	private CodeFrameResolver codeFrameResolver;
39  
40  	/**
41  	 * Constructs a BaseLocationGatherer with the supplied identifier and a UserCodeFrameResolver as
42  	 * it's resolver.
43  	 * 
44  	 * @param identifier
45  	 *            A string that identifies this gatherer.
46  	 * @since 1.0.0
47  	 */
48  	public BaseLocationGatherer(String identifier) {
49  		this(identifier, new UserCodeFrameResolver());
50  	}
51  
52  	/**
53  	 * Constructs a BaseLocationGatherer with the supplied identifier and resolver.
54  	 * 
55  	 * @param identifier
56  	 *            A string that identifies this gatherer.
57  	 * @param codeFrameResolver
58  	 *            The resolver that should be used to generate caller information.
59  	 * @since 1.0.0
60  	 */
61  	public BaseLocationGatherer(String identifier, CodeFrameResolver codeFrameResolver) {
62  		setIdentifier(identifier);
63  		setCodeFrameResolver(codeFrameResolver);
64  	}
65  
66  	/** {@inheritDoc} */
67  	public final String getIdentifier() {
68  		return identifier;
69  	}
70  
71  	/**
72  	 * Sets the string that identifies this object.
73  	 * 
74  	 * @param identifier
75  	 *            The identifier of this object.
76  	 * @since 1.0.0
77  	 */
78  	private void setIdentifier(String identifier) {
79  		this.identifier = identifier;
80  	}
81  
82  	/**
83  	 * Returns the CodeFrameResolver used by this gatherer.
84  	 * 
85  	 * @return The resolver that is used to generate caller information.
86  	 * @since 1.0.0
87  	 */
88  	public final CodeFrameResolver getCodeFrameResolver() {
89  		return codeFrameResolver;
90  	}
91  
92  	/**
93  	 * Sets the CodeFrameResolver used by this gatherer.
94  	 * 
95  	 * @param codeFrameResolver
96  	 *            The resolver that should be used to generate caller information.
97  	 * @since 1.0.0
98  	 */
99  	private final void setCodeFrameResolver(CodeFrameResolver codeFrameResolver) {
100 		this.codeFrameResolver = codeFrameResolver;
101 	}
102 
103 	/** {@inheritDoc} */
104 	public LogEventLocation getCallLocation() {
105 		return getCallLocation(Thread.currentThread());
106 	}
107 
108 	/**
109 	 * A short-hand to get the caller location.
110 	 * 
111 	 * @param currentThread
112 	 *            The thread that originated the call.
113 	 * @return The frame that originated the call.
114 	 * @since 1.0.0
115 	 */
116 	protected StackTraceElement getUserCodeFrame(Thread currentThread) {
117 		return codeFrameResolver.getCodeFrame(currentThread);
118 	}
119 
120 }