KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > HiveMind


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind;
16
17 import java.util.Collection JavaDoc;
18
19 import org.apache.hivemind.impl.LocationImpl;
20 import org.apache.hivemind.util.ClasspathResource;
21
22 /**
23  * Static utility class for HiveMind.
24  *
25  * @author Howard Lewis Ship
26  */

27 public final class HiveMind
28 {
29     /**
30      * The full id of the {@link org.apache.hivemind.service.ThreadEventNotifier} service.
31      */

32     public static final String JavaDoc THREAD_EVENT_NOTIFIER_SERVICE = "hivemind.ThreadEventNotifier";
33
34     /**
35      * The full id of the {@link org.apache.hivemind.service.ThreadLocale} service.
36      *
37      * @since 1.1
38      */

39
40     public static final String JavaDoc THREAD_LOCALE_SERVICE = "hivemind.ThreadLocale";
41
42     /**
43      * The full id of the {@link org.apache.hivemind.service.InterfaceSynthesizer} service.
44      *
45      * @since 1.1
46      */

47
48     public static final String JavaDoc INTERFACE_SYNTHESIZER_SERVICE = "hivemind.InterfaceSynthesizer";
49     
50     /**
51      * The full id of the {@link org.apache.hivemind.service.Autowiring} service.
52      *
53      * @since 2.0
54      */

55
56     public static final String JavaDoc AUTOWIRING_SERVICE = "hivemind.Autowiring";
57
58     /**
59      * An object used to synchronize access to {@link java.beans.Introspector} (which is not fully
60      * threadsafe).
61      *
62      * @since 1.1
63      */

64
65     public static final Object JavaDoc INTROSPECTOR_MUTEX = new Object JavaDoc();
66
67     private HiveMind()
68     {
69         // Prevent instantiation
70
}
71
72     public static ApplicationRuntimeException createRegistryShutdownException()
73     {
74         return new ApplicationRuntimeException(HiveMindMessages.registryShutdown());
75     }
76
77     /**
78      * Selects the first {@link Location} in an array of objects. Skips over nulls. The objects may
79      * be instances of Location or {@link Locatable}. May return null if no Location can be found.
80      */

81
82     public static Location findLocation(Object JavaDoc[] locations)
83     {
84         for (int i = 0; i < locations.length; i++)
85         {
86             Object JavaDoc location = locations[i];
87
88             Location result = getLocation(location);
89
90             if (result != null)
91                 return result;
92
93         }
94
95         return null;
96     }
97
98     /**
99      * 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 JavaDoc 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 JavaDoc getLocationString(Object JavaDoc 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 JavaDoc theClass, ClassResolver classResolver)
137     {
138         String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc c)
192     {
193         return c == null || c.isEmpty();
194     }
195 }
Popular Tags