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: /**
020: * Static utility class for HiveMind.
021: *
022: * @author Howard Lewis Ship
023: */
024: public final class HiveMind {
025: /**
026: * The full id of the {@link org.apache.hivemind.service.ThreadEventNotifier} service.
027: */
028: public static final String THREAD_EVENT_NOTIFIER_SERVICE = "hivemind.ThreadEventNotifier";
029:
030: /**
031: * The full id of the {@link org.apache.hivemind.service.ThreadLocale} service.
032: *
033: * @since 1.1
034: */
035:
036: public static final String THREAD_LOCALE_SERVICE = "hivemind.ThreadLocale";
037:
038: /**
039: * The full id of the {@link org.apache.hivemind.service.InterfaceSynthesizer} service.
040: *
041: * @since 1.1
042: */
043:
044: public static final String INTERFACE_SYNTHESIZER_SERVICE = "hivemind.InterfaceSynthesizer";
045:
046: /**
047: * An object used to synchronize access to {@link java.beans.Introspector} (which is not fully
048: * threadsafe).
049: *
050: * @since 1.1
051: */
052:
053: public static final Object INTROSPECTOR_MUTEX = new Object();
054:
055: private HiveMind() {
056: // Prevent instantiation
057: }
058:
059: public static ApplicationRuntimeException createRegistryShutdownException() {
060: return new ApplicationRuntimeException(HiveMindMessages
061: .registryShutdown());
062: }
063:
064: /**
065: * Selects the first {@link Location} in an array of objects. Skips over nulls. The objects may
066: * be instances of Location or {@link Locatable}. May return null if no Location can be found.
067: */
068:
069: public static Location findLocation(Object[] locations) {
070: for (int i = 0; i < locations.length; i++) {
071: Object location = locations[i];
072:
073: Location result = getLocation(location);
074:
075: if (result != null)
076: return result;
077:
078: }
079:
080: return null;
081: }
082:
083: /**
084: * Extracts a location from an object, checking to see if it implement {@link Location} or
085: * {@link Locatable}.
086: *
087: * @return the Location, or null if it can't be found
088: */
089: public static Location getLocation(Object object) {
090: if (object == null)
091: return null;
092:
093: if (object instanceof Location)
094: return (Location) object;
095:
096: if (object instanceof Locatable) {
097: Locatable locatable = (Locatable) object;
098:
099: return locatable.getLocation();
100: }
101:
102: return null;
103: }
104:
105: /**
106: * Invokes {@link #getLocation(Object)}, then translate the result to a string value, or
107: * "unknown location" if null.
108: */
109: public static String getLocationString(Object object) {
110: Location l = getLocation(object);
111:
112: if (l != null)
113: return l.toString();
114:
115: return HiveMindMessages.unknownLocation();
116: }
117:
118: /**
119: * Returns true if the string is null, empty, or contains only whitespace.
120: * <p>
121: * The commons-lang library provides a version of this, but the naming and behavior changed
122: * between 1.0 and 2.0, which causes some dependency issues.
123: */
124: public static boolean isBlank(String string) {
125: if (string == null || string.length() == 0)
126: return true;
127:
128: if (string.trim().length() == 0)
129: return true;
130:
131: return false;
132: }
133:
134: /**
135: * As with {@link #isBlank(String)}, but inverts the response.
136: */
137: public static boolean isNonBlank(String string) {
138: return !isBlank(string);
139: }
140:
141: /**
142: * Updates the location of an object, if the object implements {@link LocationHolder}.
143: *
144: * @param holder
145: * the object to be updated
146: * @param location
147: * the location to assign to the holder object
148: */
149: public static void setLocation(Object holder, Location location) {
150: if (holder != null && holder instanceof LocationHolder) {
151: LocationHolder lh = (LocationHolder) holder;
152:
153: lh.setLocation(location);
154: }
155: }
156:
157: /**
158: * Returns true if the Collection is null or empty.
159: */
160: public static boolean isEmpty(Collection c) {
161: return c == null || c.isEmpty();
162: }
163: }
|