View Javadoc
1 /* ==================================================================== 2 * License: 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in 13 * the documentation and/or other materials provided with the 14 * distribution. 15 * 16 * 3. The end-user documentation included with the redistribution, 17 * if any, must include the following acknowledgment: 18 * "This product includes software developed by 19 * Robert Half International (http://www.rhi.com/)." 20 * Alternately, this acknowledgment may appear in the software itself, 21 * if and wherever such third-party acknowledgments normally appear. 22 * 23 * 4. The names "Parc", "RHI", and "Robert Half International" must 24 * not be used to endorse or promote products derived from this 25 * software without prior written permission. For written 26 * permission, please contact pete.mckinstry@rhi.com. 27 * 28 * 5. Products derived from this software may not be called "PARC", 29 * nor may "PARC" appear in their name, without prior written 30 * permission of Robert Half International. 31 * 32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 35 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 36 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 39 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 40 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 41 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 42 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 43 * SUCH DAMAGE. 44 * ==================================================================== 45 * 46 */ 47 package com.rhi.architecture.resource; 48 49 import com.rhi.architecture.config.ConfigurationException; 50 import com.rhi.architecture.logging.LogFactory; 51 import com.rhi.architecture.logging.LogUtil; 52 import com.rhi.architecture.logging.Logger; 53 54 import java.util.Collection; 55 import java.util.HashMap; 56 import java.util.Iterator; 57 import java.util.List; 58 import java.util.ListIterator; 59 import java.util.Properties; 60 61 /*** 62 * The ResourceContext class is a pseudo InitialContext. It is 63 * responsible for servicing lookups from the application classes 64 * to a set of the ResourceFactories. It creates & initializes 65 * the Factories during application startup, and call the Factory 66 * cleanup methods during application shutdown. 67 * 68 * @author Pete McKinstry 69 * @copyright 2002, Robert Half Int'l., Inc. All rights reserved. 70 * 71 * @since 1.0 72 */ 73 public class ResourceContext { 74 75 private static Logger log = null; 76 77 private static HashMap resourceMap = new HashMap(); 78 79 private static final ResourceParser parser = new ResourceParserImpl(); 80 81 /*** 82 * Default Constructor 83 * 84 * @since 1.0 85 */ 86 public ResourceContext() { 87 // no op 88 } 89 90 /*** 91 * Load the configured ResourceFactories. 92 * @param p 93 * @throws InitializationException 94 * @since 1.0 95 */ 96 public void init(Properties p) throws InitializationException { 97 98 List resources = parser.getResources(p); 99 100 // do this first so that we can use the logger right away. 101 initLogger(resources, p); 102 103 // Resources are sorted by the Parser w/in the List, so the iterator 104 // will return them in priority order. 105 ListIterator iter = resources.listIterator(); 106 while (iter.hasNext()) { 107 loadResource((ResourceEntry) iter.next(), p); 108 } 109 } 110 111 /*** 112 * Initialize Logger 113 * 114 * @deprecated - Use prioritized resources instead. Will be removed 115 * in version 1.1 116 * @param resources 117 * @param p 118 * @throws InitializationException 119 */ 120 private void initLogger(List resources, Properties p) 121 throws InitializationException { 122 123 // Find the Logger resource entry & load it first. 124 boolean found = false; 125 ListIterator iter = resources.listIterator(); 126 while (iter.hasNext()) { 127 ResourceEntry resource = (ResourceEntry) iter.next(); 128 if (resource.getKey().equals(LogFactory.KEY)) { 129 loadResource(resource, p); 130 found = true; 131 } 132 else if (resource.getKey().equals(Logger.KEY)) { 133 loadResource(resource, p); 134 found = true; 135 } 136 } 137 138 if (!found) { 139 throw new InitializationException("Logger configuration missing from properties"); 140 } 141 142 try { 143 log = LogUtil.getLogger(); 144 } 145 catch (ConfigurationException e) { 146 log = LogUtil.getDefaultLogger(); 147 log.error("ConfigurationException loading logger.", e); 148 throw new InitializationException(e); 149 } 150 } 151 152 /*** 153 * Load & initialize a given Resouce. 154 * 155 * @param entry 156 * @param p 157 * @throws InitializationException 158 */ 159 private void loadResource(ResourceEntry entry, Properties p) 160 throws InitializationException { 161 try { 162 String resourceClassName = entry.getType(); 163 //Class resourceClass = Thread.currentThread() 164 // .getContextClassLoader().loadClass(resourceClassName); 165 Class resourceClass = Class.forName(resourceClassName); 166 Resource resource = (Resource) resourceClass.newInstance(); 167 resource.init(p); 168 register(entry.getKey(), resource); 169 } 170 catch (ClassNotFoundException e) { 171 throw new InitializationException( 172 "ClassNotFoundException: " + "Class not found for Resource.", 173 e); 174 } 175 catch (InstantiationException e) { 176 throw new InitializationException( 177 "InstantiationException: " 178 + "No default constructor found for for Resource. ", 179 e); 180 } 181 catch (IllegalAccessException e) { 182 throw new InitializationException( 183 "IllegalAccessException: " 184 + "Check that the Resource has a public default " 185 + "constructor.", 186 e); 187 } 188 catch (ClassCastException e) { 189 throw new InitializationException( 190 "ClassCastException: " 191 + "Class identified as Resource does not implement " 192 + "Resource interface.", 193 e); 194 } 195 } 196 197 /*** 198 * Manually register a resource w/ the ResourceContext object. 199 * 200 * @param name name by which this resource will be known. Similar 201 * to the JNDI name for an object in J2EE land. 202 * @param resource Resource to be managed by the Context object. 203 * 204 * @since 1.0 205 */ 206 public void register(Object name, Resource resource) { 207 resourceMap.put(name, resource); 208 } 209 210 /*** 211 * Lookup a resource by name. If found, return to caller. Else 212 * return null. 213 * @param name 214 * @return Object 215 * @since 1.0 216 */ 217 public Object lookup(Object name) { 218 return resourceMap.get(name); 219 } 220 221 /*** 222 * Perform whatever cleanup is required of the 223 * underlying object.. 224 * 225 * @since 1.1 226 */ 227 public void cleanup() { 228 Collection resources = resourceMap.values(); 229 Iterator iter = resources.iterator(); 230 while (iter.hasNext()) { 231 Resource resource = (Resource) iter.next(); 232 resource.close(); 233 } 234 resourceMap.clear(); 235 } 236 237 }

This page was automatically generated by Maven