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 /** 040 * Builds a Registry for a file stored in the src/descriptor/META-INF directory. 041 * 042 * @param fileName -- 043 * the name of the module descriptor file. 044 */ 045 public static Registry buildRegistry(String fileName) 046 { 047 // The examples package is structured oddly (so that it doesn't interfere with 048 // the main HiveMind framework tests), so we have to go through some gyrations 049 // here that aren't necessary in an ordinary HiveMind application. 050 051 String projectRoot = System.getProperty("PROJECT_ROOT", "."); 052 String path = projectRoot + "/examples/src/descriptor/META-INF/" + fileName; 053 054 ClassResolver resolver = new DefaultClassResolver(); 055 056 // Register the examples.xml file, which (given its non-standard name) 057 // is not visible. 058 ModuleDescriptorProvider provider = new XmlModuleDescriptorProvider(resolver, 059 new FileResource(path)); 060 return buildRegistry(provider); 061 } 062 063 /** 064 * Convenience method for invoking {@link #buildClasspathRegistry(String[])} with only a single 065 * file. 066 */ 067 public static Registry buildClasspathRegistry(String file) throws Exception 068 { 069 return buildClasspathRegistry(new String[] { file }); 070 } 071 072 /** 073 * Builds a registry for files in the classpath. 074 */ 075 public static Registry buildClasspathRegistry(String[] files) throws Exception 076 { 077 ClassResolver resolver = new DefaultClassResolver(); 078 079 List descriptorResources = new ArrayList(); 080 for (int i = 0; i < files.length; i++) 081 { 082 Resource resource = getResource(files[i]); 083 descriptorResources.add(resource); 084 } 085 086 ModuleDescriptorProvider provider = new XmlModuleDescriptorProvider(resolver, 087 descriptorResources); 088 089 return buildRegistry(provider); 090 } 091 092 protected static Registry buildRegistry(ModuleDescriptorProvider customProvider) 093 { 094 ClassResolver resolver = new DefaultClassResolver(); 095 096 RegistryBuilder builder = new RegistryBuilder(); 097 098 builder.addModuleDescriptorProvider(new XmlModuleDescriptorProvider(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 { 110 URL url = ExampleUtils.class.getResource(file); 111 112 if (url == null) 113 throw new NullPointerException("No resource named '" + file + "'."); 114 115 return new URLResource(url); 116 } 117 118 }