001/* 002 * jDTAUS Banking RI CurrencyDirectory 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.currencydir; 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; 029import org.jdtaus.core.container.PropertyException; 030 031/** 032 * Default {@code JaxpCurrenciesProvider} implementation. 033 * <p>This implementation provides resources by searching the classpath. Property {@code resourceName} holds the name of 034 * the resources to search and defaults to {@code META-INF/jdtaus/currencies.xml}.</p> 035 * 036 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 037 * @version $JDTAUS: DefaultJaxpCurrenciesProvider.java 8661 2012-09-27 11:29:58Z schulte $ 038 * 039 * @see JaxpCurrencyDirectory 040 */ 041public class DefaultJaxpCurrenciesProvider implements JaxpCurrenciesProvider 042{ 043 044 /** Class loader searched for resources. */ 045 private ClassLoader classLoader; 046 047 /** Name of the classpath resource to search. */ 048 private String resourceName; 049 050 /** 051 * Creates a new {@code ClasspathCurrenciesProvider} instance taking the name of the classpath resources to search. 052 * 053 * @param resourceName Name of the classpath resources to search. 054 */ 055 public DefaultJaxpCurrenciesProvider( final String resourceName ) 056 { 057 this( resourceName, null ); 058 } 059 060 /** 061 * Creates a new {@code ClasspathCurrenciesProvider} instance taking the class loader to search for resources. 062 * 063 * @param classLoader the class loader to search for resources. 064 */ 065 public DefaultJaxpCurrenciesProvider( final ClassLoader classLoader ) 066 { 067 this( null, classLoader ); 068 } 069 070 /** 071 * Creates a new {@code ClasspathCurrenciesProvider} instance taking the name of the classpath resources to search 072 * and the class loader to search for resources. 073 * 074 * @param resourceName Name of the classpath resources to search. 075 * @param classLoader The class loader to search for resources. 076 */ 077 public DefaultJaxpCurrenciesProvider( final String resourceName, final ClassLoader classLoader ) 078 { 079 this.resourceName = resourceName; 080 this.classLoader = classLoader; 081 } 082 083 /** 084 * Gets the name of the classpath resource to search. 085 * 086 * @return The name of the classpath resource to search. 087 */ 088 public String getResourceName() 089 { 090 if ( this.resourceName == null ) 091 { 092 this.resourceName = this.getDefaultResourceName(); 093 } 094 095 return this.resourceName; 096 } 097 098 /** 099 * 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}