1 /*
2 * jDTAUS Banking RI Bankleitzahlenverzeichnis
3 * Copyright (C) 2005 Christian Schulte
4 * <cs@schulte.it>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21 package org.jdtaus.banking.ri.blzdirectory;
22
23 import java.io.IOException;
24 import java.net.URL;
25 import java.util.Locale;
26 import java.util.Properties;
27 import org.jdtaus.core.container.ContainerFactory;
28 import org.jdtaus.core.container.PropertyException;
29 import org.jdtaus.core.logging.spi.Logger;
30
31 /**
32 * Default {@code BankfileProvider} implementation backed by a property file.
33 *
34 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
35 * @version $JDTAUS: DefaultBankfileProvider.java 8865 2014-01-10 17:13:42Z schulte $
36 *
37 * @see BankfileBankleitzahlenVerzeichnis
38 */
39 public final class DefaultBankfileProvider extends AbstractPropertiesBankfileProvider
40 {
41
42 /** Class loader searched for resources. */
43 private ClassLoader classLoader;
44
45 /** Location searched for resources. */
46 private String classpathLocation;
47
48 /** Name of the properties resource. */
49 private String propertiesResourceName;
50
51 /** Properties backing the instance. */
52 private Properties properties;
53
54 /**
55 * Creates a new {@code ClasspathBankfileProvider} instance taking a class loader to search for resources,
56 * a location resources are searched at and the name of the properties resource backing the instance.
57 *
58 * @param classLoader Class loader to search for resources or {@code null}.
59 * @param classpathLocation Location to search resources at or {@code null}.
60 * @param propertiesResourceName Name of the properties resource backing the instance or {@code null}.
61 */
62 public DefaultBankfileProvider( final ClassLoader classLoader, final String classpathLocation,
63 final String propertiesResourceName )
64 {
65 super();
66 this.classLoader = classLoader;
67 this.classpathLocation = classpathLocation;
68 this.propertiesResourceName = propertiesResourceName;
69 }
70
71 public URL getBankfile( final int index ) throws IOException
72 {
73 if ( index < 0 || index >= this.getBankfileCount() )
74 {
75 throw new IndexOutOfBoundsException( Integer.toString( index ) );
76 }
77
78 final String resourceName = this.getClasspathLocation() + "/" + this.getBankfileLocation( index );
79 final URL resource = this.getClassLoader().getResource( resourceName );
80 assert resource != null : "Resource '" + resourceName + "' not found.";
81 return resource;
82 }
83
84 /**
85 * Gets the class loader searched for resources.
86 * <p>This method returns either the current thread's context class loader or this classes class loader, if the
87 * current thread has no context class loader set. A custom class loader can be specified by using one of the
88 * constructors taking a class loader.</p>
89 *
90 * @return The class loader searched for resources.
91 */
92 public ClassLoader getClassLoader()
93 {
94 if ( this.classLoader == null )
95 {
96 if ( Thread.currentThread().getContextClassLoader() != null )
97 {
98 return Thread.currentThread().getContextClassLoader();
99 }
100
101 this.classLoader = this.getClass().getClassLoader();
102 if ( this.classLoader == null )
103 {
104 this.classLoader = ClassLoader.getSystemClassLoader();
105 }
106 }
107
108 return this.classLoader;
109 }
110
111 /**
112 * Gets the classpath location searched for resources.
113 *
114 * @return The classpath location searched for resources.
115 */
116 public String getClasspathLocation()
117 {
118 if ( this.classpathLocation == null )
119 {
120 this.classpathLocation = this.getDefaultClasspathLocation();
121 }
122
123 return this.classpathLocation;
124 }
125
126 /**
127 * Gets the name of the properties resource backing the instance.
128 *
129 * @return The name of the properties resource backing the instance.
130 */
131 public String getPropertiesResourceName()
132 {
133 if ( this.propertiesResourceName == null )
134 {
135 this.propertiesResourceName = this.getDefaultPropertiesResourceName();
136 }
137
138 return this.propertiesResourceName;
139 }
140
141 /**
142 * Gets the properties backing the instance.
143 *
144 * @return The properties backing the instance.
145 *
146 * @throws IOException if loading the properties fails.
147 */
148 public Properties getProperties() throws IOException
149 {
150 if ( this.properties == null )
151 {
152 this.assertValidProperties();
153 final String propertiesLocation = this.getClasspathLocation() + "/" + this.getPropertiesResourceName();
154 final URL rsrc = this.getClassLoader().getResource( propertiesLocation );
155 final Properties p = new Properties();
156
157 if ( rsrc != null )
158 {
159 p.load( rsrc.openStream() );
160 }
161 else
162 {
163 this.getLogger().info( this.getPropertiesNotFoundMessage( this.getLocale(), propertiesLocation ) );
164 }
165
166 this.properties = p;
167 }
168
169 return this.properties;
170 }
171
172 /**
173 * Checks configured properties.
174 *
175 * @throws PropertyException if properties hold invalid values.
176 */
177 private void assertValidProperties()
178 {
179 if ( this.getClasspathLocation() == null )
180 {
181 throw new PropertyException( "classpathLocation", this.getClasspathLocation() );
182 }
183 if ( this.getPropertiesResourceName() == null || this.getPropertiesResourceName().length() <= 0 )
184 {
185 throw new PropertyException( "propertiesResourceName", this.getPropertiesResourceName() );
186 }
187 }
188
189 //--Constructors------------------------------------------------------------
190
191 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausConstructors
192 // This section is managed by jdtaus-container-mojo.
193
194 /** Standard implementation constructor <code>org.jdtaus.banking.ri.blzdirectory.DefaultBankfileProvider</code>. */
195 public DefaultBankfileProvider()
196 {
197 super();
198 }
199
200 // </editor-fold>//GEN-END:jdtausConstructors
201
202 //------------------------------------------------------------Constructors--
203 //--Dependencies------------------------------------------------------------
204
205 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausDependencies
206 // This section is managed by jdtaus-container-mojo.
207
208 /**
209 * Gets the configured <code>Logger</code> implementation.
210 *
211 * @return The configured <code>Logger</code> implementation.
212 */
213 private Logger getLogger()
214 {
215 return (Logger) ContainerFactory.getContainer().
216 getDependency( this, "Logger" );
217
218 }
219
220 /**
221 * Gets the configured <code>Locale</code> implementation.
222 *
223 * @return The configured <code>Locale</code> implementation.
224 */
225 private Locale getLocale()
226 {
227 return (Locale) ContainerFactory.getContainer().
228 getDependency( this, "Locale" );
229
230 }
231
232 // </editor-fold>//GEN-END:jdtausDependencies
233
234 //------------------------------------------------------------Dependencies--
235 //--Properties--------------------------------------------------------------
236
237 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausProperties
238 // This section is managed by jdtaus-container-mojo.
239
240 /**
241 * Gets the value of property <code>defaultPropertiesResourceName</code>.
242 *
243 * @return Default resource name of the classpath properties resource backing the implementation.
244 */
245 private java.lang.String getDefaultPropertiesResourceName()
246 {
247 return (java.lang.String) ContainerFactory.getContainer().
248 getProperty( this, "defaultPropertiesResourceName" );
249
250 }
251
252 /**
253 * Gets the value of property <code>defaultClasspathLocation</code>.
254 *
255 * @return Default classpath location of the resources backing the implementation.
256 */
257 private java.lang.String getDefaultClasspathLocation()
258 {
259 return (java.lang.String) ContainerFactory.getContainer().
260 getProperty( this, "defaultClasspathLocation" );
261
262 }
263
264 // </editor-fold>//GEN-END:jdtausProperties
265
266 //--------------------------------------------------------------Properties--
267 //--Messages----------------------------------------------------------------
268
269 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausMessages
270 // This section is managed by jdtaus-container-mojo.
271
272 /**
273 * Gets the text of message <code>propertiesNotFound</code>.
274 * <blockquote><pre>Properties Ressource ''{0}'' nicht gefunden - keine Bereitstellung von Klassenpfad-Bankleitzahlen-Dateien.</pre></blockquote>
275 * <blockquote><pre>Properties resource ''{0}'' not found - not providing classpath bankcode files.</pre></blockquote>
276 *
277 * @param locale The locale of the message instance to return.
278 * @param location format parameter.
279 *
280 * @return the text of message <code>propertiesNotFound</code>.
281 */
282 private String getPropertiesNotFoundMessage( final Locale locale,
283 final java.lang.String location )
284 {
285 return ContainerFactory.getContainer().
286 getMessage( this, "propertiesNotFound", locale,
287 new Object[]
288 {
289 location
290 });
291
292 }
293
294 // </editor-fold>//GEN-END:jdtausMessages
295
296 //----------------------------------------------------------------Messages--
297 }