001    package org.apache.fulcrum.yaafi.framework.util;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.File;
023    import java.io.FileInputStream;
024    import java.io.IOException;
025    import java.io.InputStream;
026    
027    import org.apache.avalon.framework.logger.Logger;
028    import org.apache.avalon.framework.logger.NullLogger;
029    
030    /**
031     * Helper for locating a file name and returning an input stream.
032     *
033     * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
034     */
035    
036    public class InputStreamLocator
037    {
038        /** the root directory of our search */
039        private File rootDir;
040    
041        /** the logger to be used */
042        private Logger logger;
043    
044        /**
045         * Constructor
046         */
047        public InputStreamLocator()
048        {
049            this.rootDir = new File( new File("").getAbsolutePath() );
050            this.logger = new NullLogger();
051        }
052    
053        /**
054         * Constructor
055         *
056         * @param rootDir the root directory to start the search     */
057        public InputStreamLocator( File rootDir )
058        {
059            this( rootDir, new NullLogger() );
060        }
061    
062        /**
063         * Constructor
064         *
065         * @param rootDir the root directory to start the search
066         * @param logger the logger to be used
067         */
068        public InputStreamLocator( File rootDir, Logger logger )
069        {
070            this.rootDir    = rootDir;
071            this.logger     = logger;
072        }
073    
074        /**
075         * Locate the file with the given position using the following steps
076         *
077         * @param location the location of the source to be loaded
078         */
079        public InputStream locate( String location ) throws IOException
080        {
081            if( ( location == null ) || ( location.length() == 0 ) )
082            {
083                return null;
084            }
085    
086            String baseName = null;
087            File file = null;
088            InputStream is = null;
089    
090            // try to load a relative location with the given root dir
091            // e.g. "componentRoles.xml" located in the current working directory
092    
093            if( is == null )
094            {
095                file = new File( this.rootDir, location );
096    
097                this.getLogger().debug("Looking for " + location + " in the root directory");
098    
099                if( file.exists() )
100                {
101                    is = new FileInputStream( file );
102                    this.getLogger().debug("Found " + location + " as " + file.getAbsolutePath() );
103                }
104            }
105    
106            // try to load an absolute location as file
107            // e.g. "/foo/componentRoles.xml" from the root of the file system
108    
109            if( is == null )
110            {
111                file = new File( location );
112    
113                this.getLogger().debug("Looking for " + location + " as absolute file location");
114    
115                if( file.isAbsolute() && file.exists() )
116                {
117                    is = new FileInputStream( file );
118                    this.getLogger().debug("Found " + location + " as " + file.getAbsolutePath() );
119                }
120            }
121    
122            // try to load an absolute location through the classpath
123            // e.g. "/componentRoles.xml" located in the classpath
124    
125            if( ( is == null ) && ( location.startsWith( "/" ) == true ) )
126            {
127                this.getLogger().debug("Looking for " + location + " using the class loader");
128                is =  getClass().getResourceAsStream( location );
129    
130                if( is != null )
131                {
132                    this.getLogger().debug("Successfully located " + location);
133                }
134            }
135    
136            // try to load the last part of the file name using the classloader
137            // e.g. "conf/componentRoles.xml" as "/componentRoles.xml" located in
138            // the classpath.
139    
140            if( ( is == null ) && ( location.startsWith( "/" ) == false ) )
141            {
142                baseName = '/' + new File(location).getName();
143                this.getLogger().debug("Looking for " + baseName + " using the class loader");
144                is =  getClass().getResourceAsStream( baseName );
145    
146                if( is != null )
147                {
148                    this.getLogger().debug("Successfully located " + baseName);
149                }
150            }
151    
152            if( is == null )
153            {
154                this.getLogger().warn("Unable to find any resource with the name '" + location + "'");
155            }
156    
157            return is;
158        }
159    
160        /**
161         * @return Returns the logger.
162         */
163        protected Logger getLogger()
164        {
165            return logger;
166        }
167    
168        /**
169         * @return Returns the rootDir.
170         */
171        protected File getRootDir()
172        {
173            return rootDir;
174        }
175    
176    
177    }