1 | /* |
2 | * jDTAUS Banking RI CurrencyDirectory |
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.currencydir; |
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 | import org.jdtaus.core.container.PropertyException; |
30 | |
31 | /** |
32 | * Default {@code JaxpCurrenciesProvider} implementation. |
33 | * <p>This implementation provides resources by searching the classpath. Property {@code resourceName} holds the name of |
34 | * the resources to search and defaults to {@code META-INF/jdtaus/currencies.xml}.</p> |
35 | * |
36 | * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> |
37 | * @version $JDTAUS: DefaultJaxpCurrenciesProvider.java 8661 2012-09-27 11:29:58Z schulte $ |
38 | * |
39 | * @see JaxpCurrencyDirectory |
40 | */ |
41 | public class DefaultJaxpCurrenciesProvider implements JaxpCurrenciesProvider |
42 | { |
43 | |
44 | /** Class loader searched for resources. */ |
45 | private ClassLoader classLoader; |
46 | |
47 | /** Name of the classpath resource to search. */ |
48 | private String resourceName; |
49 | |
50 | /** |
51 | * Creates a new {@code ClasspathCurrenciesProvider} instance taking the name of the classpath resources to search. |
52 | * |
53 | * @param resourceName Name of the classpath resources to search. |
54 | */ |
55 | public DefaultJaxpCurrenciesProvider( final String resourceName ) |
56 | { |
57 | this( resourceName, null ); |
58 | } |
59 | |
60 | /** |
61 | * Creates a new {@code ClasspathCurrenciesProvider} instance taking the class loader to search for resources. |
62 | * |
63 | * @param classLoader the class loader to search for resources. |
64 | */ |
65 | public DefaultJaxpCurrenciesProvider( final ClassLoader classLoader ) |
66 | { |
67 | this( null, classLoader ); |
68 | } |
69 | |
70 | /** |
71 | * Creates a new {@code ClasspathCurrenciesProvider} instance taking the name of the classpath resources to search |
72 | * and the class loader to search for resources. |
73 | * |
74 | * @param resourceName Name of the classpath resources to search. |
75 | * @param classLoader The class loader to search for resources. |
76 | */ |
77 | public DefaultJaxpCurrenciesProvider( final String resourceName, final ClassLoader classLoader ) |
78 | { |
79 | this.resourceName = resourceName; |
80 | this.classLoader = classLoader; |
81 | } |
82 | |
83 | /** |
84 | * Gets the name of the classpath resource to search. |
85 | * |
86 | * @return The name of the classpath resource to search. |
87 | */ |
88 | public String getResourceName() |
89 | { |
90 | if ( this.resourceName == null ) |
91 | { |
92 | this.resourceName = this.getDefaultResourceName(); |
93 | } |
94 | |
95 | return this.resourceName; |
96 | } |
97 | |
98 | /** |
99 | * Gets the class loader searched for resources. |
100 | * <p>This method returns either the current thread's context class loader or this classes class loader, if the |
101 | * current thread has no context class loader set. A custom class loader can be specified by using one of |
102 | * the constructors taking a class loader.</p> |
103 | * |
104 | * @return The class loader to search for resources. |
105 | */ |
106 | public ClassLoader getClassLoader() |
107 | { |
108 | if ( this.classLoader == null ) |
109 | { |
110 | if ( Thread.currentThread().getContextClassLoader() != null ) |
111 | { |
112 | return Thread.currentThread().getContextClassLoader(); |
113 | } |
114 | |
115 | this.classLoader = this.getClass().getClassLoader(); |
116 | if ( this.classLoader == null ) |
117 | { |
118 | this.classLoader = ClassLoader.getSystemClassLoader(); |
119 | } |
120 | } |
121 | |
122 | return this.classLoader; |
123 | } |
124 | |
125 | public URL[] getResources() throws IOException |
126 | { |
127 | this.assertValidProperties(); |
128 | final Collection col = new LinkedList(); |
129 | final Enumeration en = this.getClassLoader().getResources( this.getResourceName() ); |
130 | |
131 | while ( en.hasMoreElements() ) |
132 | { |
133 | col.add( en.nextElement() ); |
134 | } |
135 | |
136 | return (URL[]) col.toArray( new URL[ col.size() ] ); |
137 | } |
138 | |
139 | /** |
140 | * Checks configured properties. |
141 | * |
142 | * @throws PropertyException if properties hold invalid values. |
143 | */ |
144 | private void assertValidProperties() |
145 | { |
146 | if ( this.getResourceName() == null || this.getResourceName().length() <= 0 ) |
147 | { |
148 | throw new PropertyException( "resourceName", this.getResourceName() ); |
149 | } |
150 | } |
151 | |
152 | //--Constructors------------------------------------------------------------ |
153 | |
154 | // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausConstructors |
155 | // This section is managed by jdtaus-container-mojo. |
156 | |
157 | /** Standard implementation constructor <code>org.jdtaus.banking.ri.currencydir.DefaultJaxpCurrenciesProvider</code>. */ |
158 | public DefaultJaxpCurrenciesProvider() |
159 | { |
160 | super(); |
161 | } |
162 | |
163 | // </editor-fold>//GEN-END:jdtausConstructors |
164 | |
165 | //------------------------------------------------------------Constructors-- |
166 | //--Properties-------------------------------------------------------------- |
167 | |
168 | // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausProperties |
169 | // This section is managed by jdtaus-container-mojo. |
170 | |
171 | /** |
172 | * Gets the value of property <code>defaultResourceName</code>. |
173 | * |
174 | * @return Default name of the resources to provide. |
175 | */ |
176 | private java.lang.String getDefaultResourceName() |
177 | { |
178 | return (java.lang.String) ContainerFactory.getContainer(). |
179 | getProperty( this, "defaultResourceName" ); |
180 | |
181 | } |
182 | |
183 | // </editor-fold>//GEN-END:jdtausProperties |
184 | |
185 | //--------------------------------------------------------------Properties-- |
186 | } |