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.examples;
016:
017: import java.net.URL;
018: import java.util.ArrayList;
019: import java.util.List;
020: import java.util.Locale;
021:
022: import org.apache.hivemind.ClassResolver;
023: import org.apache.hivemind.ModuleDescriptorProvider;
024: import org.apache.hivemind.Registry;
025: import org.apache.hivemind.Resource;
026: import org.apache.hivemind.impl.DefaultClassResolver;
027: import org.apache.hivemind.impl.RegistryBuilder;
028: import org.apache.hivemind.impl.XmlModuleDescriptorProvider;
029: import org.apache.hivemind.util.FileResource;
030: import org.apache.hivemind.util.URLResource;
031:
032: /**
033: * Utilities needed by the examples.
034: *
035: * @author Howard Lewis Ship
036: */
037: public class ExampleUtils {
038: /**
039: * Builds a Registry for a file stored in the src/descriptor/META-INF directory.
040: *
041: * @param fileName --
042: * the name of the module descriptor file.
043: */
044: public static Registry buildRegistry(String fileName) {
045: // The examples package is structured oddly (so that it doesn't interfere with
046: // the main HiveMind framework tests), so we have to go through some gyrations
047: // here that aren't necessary in an ordinary HiveMind application.
048:
049: String projectRoot = System.getProperty("PROJECT_ROOT", ".");
050: String path = projectRoot
051: + "/examples/src/descriptor/META-INF/" + fileName;
052:
053: ClassResolver resolver = new DefaultClassResolver();
054:
055: // Register the examples.xml file, which (given its non-standard name)
056: // is not visible.
057: ModuleDescriptorProvider provider = new XmlModuleDescriptorProvider(
058: resolver, new FileResource(path));
059: return buildRegistry(provider);
060: }
061:
062: /**
063: * Convenience method for invoking {@link #buildClasspathRegistry(String[])} with only a single
064: * file.
065: */
066: public static Registry buildClasspathRegistry(String file)
067: throws Exception {
068: return buildClasspathRegistry(new String[] { file });
069: }
070:
071: /**
072: * Builds a registry for files in the classpath.
073: */
074: public static Registry buildClasspathRegistry(String[] files)
075: throws Exception {
076: ClassResolver resolver = new DefaultClassResolver();
077:
078: List descriptorResources = new ArrayList();
079: for (int i = 0; i < files.length; i++) {
080: Resource resource = getResource(files[i]);
081: descriptorResources.add(resource);
082: }
083:
084: ModuleDescriptorProvider provider = new XmlModuleDescriptorProvider(
085: resolver, descriptorResources);
086:
087: return buildRegistry(provider);
088: }
089:
090: protected static Registry buildRegistry(
091: ModuleDescriptorProvider customProvider) {
092: ClassResolver resolver = new DefaultClassResolver();
093:
094: RegistryBuilder builder = new RegistryBuilder();
095:
096: builder
097: .addModuleDescriptorProvider(new XmlModuleDescriptorProvider(
098: resolver));
099: builder.addModuleDescriptorProvider(customProvider);
100:
101: return builder.constructRegistry(Locale.getDefault());
102: }
103:
104: /**
105: * Returns the given file as a {@link Resource} from the classpath. Typically, this is to find
106: * files in the same folder as the invoking class.
107: */
108: protected static Resource getResource(String file) {
109: URL url = ExampleUtils.class.getResource(file);
110:
111: if (url == null)
112: throw new NullPointerException("No resource named '" + file
113: + "'.");
114:
115: return new URLResource(url);
116: }
117:
118: }
|