001/*
002 *  jDTAUS Banking RI Bankleitzahlenverzeichnis
003 *  Copyright (C) 2005 Christian Schulte
004 *  <cs@schulte.it>
005 *
006 *  This library is free software; you can redistribute it and/or
007 *  modify it under the terms of the GNU Lesser General Public
008 *  License as published by the Free Software Foundation; either
009 *  version 2.1 of the License, or any later version.
010 *
011 *  This library is distributed in the hope that it will be useful,
012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 *  Lesser General Public License for more details.
015 *
016 *  You should have received a copy of the GNU Lesser General Public
017 *  License along with this library; if not, write to the Free Software
018 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
019 *
020 */
021package org.jdtaus.banking.ri.blzdirectory;
022
023import java.io.IOException;
024import java.net.URL;
025import java.util.Locale;
026import java.util.Properties;
027import org.jdtaus.core.container.ContainerFactory;
028import org.jdtaus.core.container.PropertyException;
029import org.jdtaus.core.logging.spi.Logger;
030
031/**
032 * Default {@code BankfileProvider} implementation backed by a property file.
033 *
034 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
035 * @version $JDTAUS: DefaultBankfileProvider.java 8865 2014-01-10 17:13:42Z schulte $
036 *
037 * @see BankfileBankleitzahlenVerzeichnis
038 */
039public final class DefaultBankfileProvider extends AbstractPropertiesBankfileProvider
040{
041
042    /** Class loader searched for resources. */
043    private ClassLoader classLoader;
044
045    /** Location searched for resources. */
046    private String classpathLocation;
047
048    /** Name of the properties resource. */
049    private String propertiesResourceName;
050
051    /** Properties backing the instance. */
052    private Properties properties;
053
054    /**
055     * Creates a new {@code ClasspathBankfileProvider} instance taking a class loader to search for resources,
056     * a location resources are searched at and the name of the properties resource backing the instance.
057     *
058     * @param classLoader Class loader to search for resources or {@code null}.
059     * @param classpathLocation Location to search resources at or {@code null}.
060     * @param propertiesResourceName Name of the properties resource backing the instance or {@code null}.
061     */
062    public DefaultBankfileProvider( final ClassLoader classLoader, final String classpathLocation,
063                                    final String propertiesResourceName )
064    {
065        super();
066        this.classLoader = classLoader;
067        this.classpathLocation = classpathLocation;
068        this.propertiesResourceName = propertiesResourceName;
069    }
070
071    public URL getBankfile( final int index ) throws IOException
072    {
073        if ( index < 0 || index >= this.getBankfileCount() )
074        {
075            throw new IndexOutOfBoundsException( Integer.toString( index ) );
076        }
077
078        final String resourceName = this.getClasspathLocation() + "/" + this.getBankfileLocation( index );
079        final URL resource = this.getClassLoader().getResource( resourceName );
080        assert resource != null : "Resource '" + resourceName + "' not found.";
081        return resource;
082    }
083
084    /**
085     * Gets the class loader searched for resources.
086     * <p>This method returns either the current thread's context class loader or this classes class loader, if the
087     * current thread has no context class loader set. A custom class loader can be specified by using one of the
088     * constructors taking a class loader.</p>
089     *
090     * @return The class loader searched for resources.
091     */
092    public ClassLoader getClassLoader()
093    {
094        if ( this.classLoader == null )
095        {
096            if ( Thread.currentThread().getContextClassLoader() != null )
097            {
098                return Thread.currentThread().getContextClassLoader();
099            }
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}