001/*
002 *  jDTAUS Banking RI Textschluesselverzeichnis
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.txtdirectory;
022
023import java.io.IOException;
024import java.net.URL;
025import java.util.Collection;
026import java.util.Enumeration;
027import java.util.LinkedList;
028import org.jdtaus.core.container.ContainerFactory;
029
030/**
031 * Default {@code JaxpTextschluesselProvider} implementation.
032 * <p>This implementation provides resources by searching the classpath. Property {@code resourceName} holds the name of
033 * the resources to search and defaults to {@code META-INF/jdtaus/textschluessel.xml}.</p>
034 *
035 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
036 * @version $JDTAUS: DefaultJaxpTextschluesselProvider.java 8661 2012-09-27 11:29:58Z schulte $
037 *
038 * @see JaxpTextschluesselVerzeichnis
039 */
040public class DefaultJaxpTextschluesselProvider implements JaxpTextschluesselProvider
041{
042
043    /** Class loader searched for resources. */
044    private ClassLoader classLoader;
045
046    /** Name of the classpath resource to search. */
047    private String resourceName;
048
049    /**
050     * Creates a new {@code DefaultTextschluesselProvider} instance taking the name of the classpath resource search.
051     *
052     * @param resourceName Name of the classpath resource to search.
053     */
054    public DefaultJaxpTextschluesselProvider( final String resourceName )
055    {
056        this( resourceName, null );
057    }
058
059    /**
060     * Creates a new {@code DefaultTextschluesselProvider} instance taking the class loader to search for resources.
061     *
062     * @param classLoader The class loader to search for resources.
063     */
064    public DefaultJaxpTextschluesselProvider( final ClassLoader classLoader )
065    {
066        this( null, classLoader );
067    }
068
069    /**
070     * Creates a new {@code DefaultTextschluesselProvider} instance taking the name of the classpath resource to search
071     * and the class loader to search for resources.
072     *
073     * @param resourceName Name of the classpath resource to search.
074     * @param classLoader The class loader to search for resources.
075     */
076    public DefaultJaxpTextschluesselProvider( final String resourceName, final ClassLoader classLoader )
077    {
078        this.resourceName = resourceName;
079        this.classLoader = classLoader;
080    }
081
082    /**
083     * Gets the name of the classpath resource to search.
084     *
085     * @return The name of the classpath resource to search.
086     */
087    public String getResourceName()
088    {
089        if ( this.resourceName == null )
090        {
091            this.resourceName = this.getDefaultResourceName();
092        }
093
094        return this.resourceName;
095    }
096
097    /**
098     * Gets the class loader searched for resources.
099     * <p>This method returns either the current thread's context class loader or this classes class loader, if the
100     * current thread has no context class loader set. A custom class loader can be specified by using one of the
101     * constructors taking a class loader.</p>
102     *
103     * @return The class loader to search for resources.
104     */
105    public ClassLoader getClassLoader()
106    {
107        if ( this.classLoader == null )
108        {
109            if ( Thread.currentThread().getContextClassLoader() != null )
110            {
111                return Thread.currentThread().getContextClassLoader();
112            }
113
114            this.classLoader = this.getClass().getClassLoader();
115            if ( this.classLoader == null )
116            {
117                this.classLoader = ClassLoader.getSystemClassLoader();
118            }
119        }
120
121        return this.classLoader;
122    }
123
124    public URL[] getResources() throws IOException
125    {
126        final Collection col = new LinkedList();
127        final Enumeration en = this.getClassLoader().getResources( this.getResourceName() );
128
129        while ( en.hasMoreElements() )
130        {
131            col.add( en.nextElement() );
132        }
133
134        return (URL[]) col.toArray( new URL[ col.size() ] );
135    }
136
137    //--Constructors------------------------------------------------------------
138
139// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausConstructors
140    // This section is managed by jdtaus-container-mojo.
141
142    /** Standard implementation constructor <code>org.jdtaus.banking.ri.txtdirectory.DefaultJaxpTextschluesselProvider</code>. */
143    public DefaultJaxpTextschluesselProvider()
144    {
145        super();
146    }
147
148// </editor-fold>//GEN-END:jdtausConstructors
149
150    //------------------------------------------------------------Constructors--
151    //--Properties--------------------------------------------------------------
152
153// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausProperties
154    // This section is managed by jdtaus-container-mojo.
155
156    /**
157     * Gets the value of property <code>defaultResourceName</code>.
158     *
159     * @return Default name of the classpath resource to search.
160     */
161    private java.lang.String getDefaultResourceName()
162    {
163        return (java.lang.String) ContainerFactory.getContainer().
164            getProperty( this, "defaultResourceName" );
165
166    }
167
168// </editor-fold>//GEN-END:jdtausProperties
169
170    //--------------------------------------------------------------Properties--
171}