View Javadoc
1   /*
2    *  jDTAUS Banking RI Textschluesselverzeichnis
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.txtdirectory;
22  
23  import java.io.IOException;
24  import java.net.URL;
25  import java.util.Collection;
26  import java.util.Enumeration;
27  import java.util.LinkedList;
28  import org.jdtaus.core.container.ContainerFactory;
29  
30  /**
31   * Default {@code JaxpTextschluesselProvider} implementation.
32   * <p>This implementation provides resources by searching the classpath. Property {@code resourceName} holds the name of
33   * the resources to search and defaults to {@code META-INF/jdtaus/textschluessel.xml}.</p>
34   *
35   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
36   * @version $JDTAUS: DefaultJaxpTextschluesselProvider.java 8661 2012-09-27 11:29:58Z schulte $
37   *
38   * @see JaxpTextschluesselVerzeichnis
39   */
40  public class DefaultJaxpTextschluesselProvider implements JaxpTextschluesselProvider
41  {
42  
43      /** Class loader searched for resources. */
44      private ClassLoader classLoader;
45  
46      /** Name of the classpath resource to search. */
47      private String resourceName;
48  
49      /**
50       * Creates a new {@code DefaultTextschluesselProvider} instance taking the name of the classpath resource search.
51       *
52       * @param resourceName Name of the classpath resource to search.
53       */
54      public DefaultJaxpTextschluesselProvider( final String resourceName )
55      {
56          this( resourceName, null );
57      }
58  
59      /**
60       * Creates a new {@code DefaultTextschluesselProvider} instance taking the class loader to search for resources.
61       *
62       * @param classLoader The class loader to search for resources.
63       */
64      public DefaultJaxpTextschluesselProvider( final ClassLoader classLoader )
65      {
66          this( null, classLoader );
67      }
68  
69      /**
70       * Creates a new {@code DefaultTextschluesselProvider} instance taking the name of the classpath resource to search
71       * and the class loader to search for resources.
72       *
73       * @param resourceName Name of the classpath resource to search.
74       * @param classLoader The class loader to search for resources.
75       */
76      public DefaultJaxpTextschluesselProvider( final String resourceName, final ClassLoader classLoader )
77      {
78          this.resourceName = resourceName;
79          this.classLoader = classLoader;
80      }
81  
82      /**
83       * Gets the name of the classpath resource to search.
84       *
85       * @return The name of the classpath resource to search.
86       */
87      public String getResourceName()
88      {
89          if ( this.resourceName == null )
90          {
91              this.resourceName = this.getDefaultResourceName();
92          }
93  
94          return this.resourceName;
95      }
96  
97      /**
98       * Gets the class loader searched for resources.
99       * <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 }