001 // Copyright 2004, 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.hivemind; 016 017 import java.util.Collection; 018 019 import org.apache.hivemind.impl.LocationImpl; 020 import org.apache.hivemind.util.ClasspathResource; 021 022 /** 023 * Static utility class for HiveMind. 024 * 025 * @author Howard Lewis Ship 026 */ 027 public final class HiveMind 028 { 029 /** 030 * The full id of the {@link org.apache.hivemind.service.ThreadEventNotifier} service. 031 */ 032 public static final String THREAD_EVENT_NOTIFIER_SERVICE = "hivemind.ThreadEventNotifier"; 033 034 /** 035 * The full id of the {@link org.apache.hivemind.service.ThreadLocale} service. 036 * 037 * @since 1.1 038 */ 039 040 public static final String THREAD_LOCALE_SERVICE = "hivemind.ThreadLocale"; 041 042 /** 043 * The full id of the {@link org.apache.hivemind.service.InterfaceSynthesizer} service. 044 * 045 * @since 1.1 046 */ 047 048 public static final String INTERFACE_SYNTHESIZER_SERVICE = "hivemind.InterfaceSynthesizer"; 049 050 /** 051 * The full id of the {@link org.apache.hivemind.service.Autowiring} service. 052 * 053 * @since 2.0 054 */ 055 056 public static final String AUTOWIRING_SERVICE = "hivemind.Autowiring"; 057 058 /** 059 * An object used to synchronize access to {@link java.beans.Introspector} (which is not fully 060 * threadsafe). 061 * 062 * @since 1.1 063 */ 064 065 public static final Object INTROSPECTOR_MUTEX = new Object(); 066 067 private HiveMind() 068 { 069 // Prevent instantiation 070 } 071 072 public static ApplicationRuntimeException createRegistryShutdownException() 073 { 074 return new ApplicationRuntimeException(HiveMindMessages.registryShutdown()); 075 } 076 077 /** 078 * Selects the first {@link Location} in an array of objects. Skips over nulls. The objects may 079 * be instances of Location or {@link Locatable}. May return null if no Location can be found. 080 */ 081 082 public static Location findLocation(Object[] locations) 083 { 084 for (int i = 0; i < locations.length; i++) 085 { 086 Object location = locations[i]; 087 088 Location result = getLocation(location); 089 090 if (result != null) 091 return result; 092 093 } 094 095 return null; 096 } 097 098 /** 099 * Extracts a location from an object, checking to see if it implement {@link Location} or 100 * {@link Locatable}. 101 * 102 * @return the Location, or null if it can't be found 103 */ 104 public static Location getLocation(Object object) 105 { 106 if (object == null) 107 return null; 108 109 if (object instanceof Location) 110 return (Location) object; 111 112 if (object instanceof Locatable) 113 { 114 Locatable locatable = (Locatable) object; 115 116 return locatable.getLocation(); 117 } 118 119 return null; 120 } 121 122 /** 123 * Invokes {@link #getLocation(Object)}, then translate the result to a string value, or 124 * "unknown location" if null. 125 */ 126 public static String getLocationString(Object object) 127 { 128 Location l = getLocation(object); 129 130 if (l != null) 131 return l.toString(); 132 133 return HiveMindMessages.unknownLocation(); 134 } 135 136 public static Location getClassLocation(Class theClass, ClassResolver classResolver) 137 { 138 String path = "/" + theClass.getName().replace('.', '/'); 139 140 Resource r = new ClasspathResource(classResolver, path); 141 142 return new LocationImpl(r); 143 } 144 145 /** 146 * Returns true if the string is null, empty, or contains only whitespace. 147 * <p> 148 * The commons-lang library provides a version of this, but the naming and behavior changed 149 * between 1.0 and 2.0, which causes some dependency issues. 150 */ 151 public static boolean isBlank(String string) 152 { 153 if (string == null || string.length() == 0) 154 return true; 155 156 if (string.trim().length() == 0) 157 return true; 158 159 return false; 160 } 161 162 /** 163 * As with {@link #isBlank(String)}, but inverts the response. 164 */ 165 public static boolean isNonBlank(String string) 166 { 167 return !isBlank(string); 168 } 169 170 /** 171 * Updates the location of an object, if the object implements {@link LocationHolder}. 172 * 173 * @param holder 174 * the object to be updated 175 * @param location 176 * the location to assign to the holder object 177 */ 178 public static void setLocation(Object holder, Location location) 179 { 180 if (holder != null && holder instanceof LocationHolder) 181 { 182 LocationHolder lh = (LocationHolder) holder; 183 184 lh.setLocation(location); 185 } 186 } 187 188 /** 189 * Returns true if the Collection is null or empty. 190 */ 191 public static boolean isEmpty(Collection c) 192 { 193 return c == null || c.isEmpty(); 194 } 195 }