1 /********************************************************************** 2 * AdapterFactory.java 3 * created on 15.03.2005 by netseeker 4 * $Source: /cvsroot/ejoe/EJOE/src/de/netseeker/ejoe/adapter/AdapterFactory.java,v $ 5 * $Date: 2007/11/17 10:56:30 $ 6 * $Revision: 1.17 $ 7 * 8 * ==================================================================== 9 * 10 * Copyright 2005-2006 netseeker aka Michael Manske 11 * 12 * Licensed under the Apache License, Version 2.0 (the "License"); 13 * you may not use this file except in compliance with the License. 14 * You may obtain a copy of the License at 15 * 16 * http://www.apache.org/licenses/LICENSE-2.0 17 * 18 * Unless required by applicable law or agreed to in writing, software 19 * distributed under the License is distributed on an "AS IS" BASIS, 20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 * See the License for the specific language governing permissions and 22 * limitations under the License. 23 * ==================================================================== 24 * 25 * This file is part of the ejoe framework. 26 * For more information on the author, please see 27 * <http://www.manskes.de/>. 28 * 29 *********************************************************************/ 30 package de.netseeker.ejoe.adapter; 31 32 import java.io.InputStream; 33 import java.util.Hashtable; 34 import java.util.Iterator; 35 import java.util.Map; 36 import java.util.Properties; 37 import java.util.logging.Level; 38 import java.util.logging.Logger; 39 40 import de.netseeker.ejoe.io.IOUtil; 41 42 /*** 43 * The AdapterFactory manages creation and caching of all known (already during runtime requested) SerializeAdapters. 44 * This factory is mostly used internally within EJOE with one big exception: If one is using a customized adapter, eg. 45 * a configurable adapter like the CastorAdapter, which should be initialized with a valid mapping file, then that 46 * adapter instance must get (re-)registered with this factory. To overwrite the default adapter instances use: 47 * 48 * <pre> 49 * CastorAdapter myCastorAdapterInstance = new CastorAdapter( "PathToTheMappingFile" ); 50 * AdapterFactory.registerAdapter( myCastorAdapterInstance ); 51 * </pre> 52 * 53 * AdapterFactory will then return myCastorAdapterInstance to all requests for CastorAdapter. To register multiple 54 * instances of a special adapter use: 55 * 56 * <pre> 57 * CastorAdapter myCastorAdapterInstance1 = new CastorAdapter( "PathToTheMappingFile1" ); 58 * CastorAdapter myCastorAdapterInstance2 = new CastorAdapter( "PathToTheMappingFile2" ); 59 * AdapterFactory.registerAdapter( "CastorAdapter1", myCastorAdapterInstance1 ); 60 * AdapterFactory.registerAdapter( "CastorAdapter2", myCastorAdapterInstance2 ); 61 * </pre> 62 * 63 * @author netseeker 64 * @since 0.3.1 65 */ 66 public final class AdapterFactory 67 { 68 /*** 69 * Logger for this class 70 */ 71 private static final Logger logger = Logger.getLogger( AdapterFactory.class.getName() ); 72 73 /*** 74 * cache for already created adapter instances, use a Hashtable because it's synchronized 75 */ 76 private static final Map _adapters = new Hashtable(); 77 78 private static Properties _adapterConfig; 79 80 static 81 { 82 _adapterConfig = new Properties(); 83 InputStream in = null; 84 try 85 { 86 in = AdapterFactory.class.getResourceAsStream( "/ejoe-adapter-conf.properties" ); 87 _adapterConfig.load( in ); 88 } 89 catch ( Exception e ) 90 { 91 logger 92 .log( 93 Level.INFO, 94 "No customized ejoe-adapter-conf.properties found on classpath! If you don't want to use" 95 + " the default adapter settings (META-INF/ejoe-adapter-conf.properties) make sure you have placed" 96 + " a valid ejoe-adapter-conf.properties file in your classpath." ); 97 } 98 finally 99 { 100 IOUtil.closeQuiet( in ); 101 } 102 103 try 104 { 105 in = AdapterFactory.class.getResourceAsStream( "/META-INF/ejoe-adapter-conf.properties" ); 106 Properties propsDefault = new Properties(); 107 propsDefault.load( in ); 108 Map.Entry entry = null; 109 for ( Iterator it = propsDefault.entrySet().iterator(); it.hasNext(); ) 110 { 111 entry = (Map.Entry)it.next(); 112 if ( !_adapterConfig.containsKey( entry.getKey() ) ) 113 { 114 _adapterConfig.put( entry.getKey(), entry.getValue() ); 115 } 116 } 117 } 118 catch ( Exception ioe ) 119 { 120 logger 121 .log( 122 Level.SEVERE, 123 "Adapter settings could no be read from /META-INF/ejoe-adapter-conf.properties!!! No adapters will be available in EJServer!" ); 124 } 125 finally 126 { 127 IOUtil.closeQuiet( in ); 128 } 129 } 130 131 /*** 132 * Returns either a new instance of the requested adapter or returns a already cached one. 133 * 134 * @param name class name or synonym for the SerializeAdapter. 135 * @return the requested SerializeAdapter 136 */ 137 public static SerializeAdapter createAdapter( String name ) throws IllegalAdapterException 138 { 139 SerializeAdapter adapter = (SerializeAdapter) _adapters.get( name ); 140 141 if ( adapter == null ) 142 { 143 boolean allowed = Boolean.valueOf( _adapterConfig.getProperty( name, Boolean.FALSE.toString() ) ) 144 .booleanValue(); 145 146 if ( allowed ) 147 { 148 try 149 { 150 adapter = (SerializeAdapter) Class.forName( name ).newInstance(); 151 _adapters.put( name, adapter ); 152 } 153 catch ( InstantiationException e ) 154 { 155 logger.log( Level.SEVERE, "Error while trying to instaniate SerializeAdapter: " + name, e ); 156 throw new IllegalAdapterException( e.getMessage() ); 157 } 158 catch ( IllegalAccessException e ) 159 { 160 logger.log( Level.SEVERE, "Error while trying to access SerializeAdapter: " + name, e ); 161 throw new IllegalAdapterException( e.getMessage() ); 162 } 163 catch ( ClassNotFoundException e ) 164 { 165 logger.log( Level.SEVERE, "Class for SerializeAdapter: " + name + " not found.", e ); 166 throw new IllegalAdapterException( e.getMessage() ); 167 } 168 } 169 else 170 { 171 throw new IllegalAdapterException( "No custom registration found for the adapter \"" + name 172 + "\" and ejoe-adapter-conf.xml doesn't permit loading of \"" + name + "\"!" ); 173 } 174 } 175 176 return adapter; 177 } 178 179 /*** 180 * Registers a new adapter to the registry of known adapters while using the class name of the adapter as synonym 181 * within the registry 182 * 183 * @param adapter 184 */ 185 public static void registerAdapter( SerializeAdapter adapter ) 186 { 187 _adapters.put( adapter.getClass().getName(), adapter ); 188 } 189 190 /*** 191 * Registers a new adapter to the registry of known adapters while using the given name as synonym within the 192 * registry 193 * 194 * @param name 195 * @param adapter 196 */ 197 public static void registerAdapter( String name, SerializeAdapter adapter ) 198 { 199 _adapters.put( name, adapter ); 200 } 201 202 /*** 203 * Removes all adapter registrations 204 */ 205 public static void clear() 206 { 207 _adapters.clear(); 208 } 209 210 /*** 211 * Returns the supported content type for the given adapter synonym 212 * 213 * @param name the synonym for the adapter 214 * @return the supported content type for the adapter 215 */ 216 public static String getSupportedContentType( String name ) throws IllegalAdapterException 217 { 218 SerializeAdapter adapter = createAdapter( name ); 219 return adapter.getContentType(); 220 } 221 }