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.Locale;
019    
020    import org.apache.hivemind.ClassResolver;
021    import org.apache.hivemind.Registry;
022    import org.apache.hivemind.Resource;
023    import org.apache.hivemind.impl.DefaultClassResolver;
024    import org.apache.hivemind.impl.RegistryBuilder;
025    import org.apache.hivemind.impl.XmlModuleReader;
026    import org.apache.hivemind.util.URLResource;
027    
028    /**
029     * Utilities needed by the examples.
030     * 
031     * @author Howard Lewis Ship
032     */
033    public final class ExampleUtils
034    {
035    
036        /** Prevent instantiation. */
037        private ExampleUtils()
038        {
039        }
040    
041        /**
042         * Convenience method for invoking {@link #buildClasspathRegistry(String[])}
043         * with only a single file.
044         */
045        public static Registry buildClasspathRegistry(String file)
046            throws Exception
047        {
048            return buildClasspathRegistry(new String[] { file });
049        }
050    
051        /**
052         * Builds a registry for files in the classpath.
053         */
054        public static Registry buildClasspathRegistry(String[] files)
055            throws Exception
056        {
057            ClassResolver resolver = new DefaultClassResolver();
058            
059            RegistryBuilder builder = new RegistryBuilder();
060            builder.autoDetectModules();
061    
062            for (int i = 0; i < files.length; i++)
063            {
064                Resource resource = getResource(files[i]);
065    
066                XmlModuleReader reader = new XmlModuleReader(builder.getRegistryDefinition(),
067                        resolver, builder.getErrorHandler());
068                reader.readModule(resource);
069            }
070    
071            return builder.constructRegistry(Locale.getDefault());
072        }
073    
074    
075        /**
076         * Returns the given file as a {@link Resource} from the classpath.
077         * Typically, this is to find files in the same folder as the invoking
078         * class.
079         */
080        protected static Resource getResource(String file)
081        {
082            URL url = ExampleUtils.class.getResource(file);
083    
084            if (url == null) throw new NullPointerException("No resource named '" + file + "'.");
085    
086            return new URLResource(url);
087        }
088    
089    }