001    /*
002     *  Licensed under the Apache License, Version 2.0 (the "License");
003     *  you may not use this file except in compliance with the License.
004     *  You may obtain a copy of the License at
005     *
006     *      http://www.apache.org/licenses/LICENSE-2.0
007     *
008     *  Unless required by applicable law or agreed to in writing, software
009     *  distributed under the License is distributed on an "AS IS" BASIS,
010     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011     *  See the License for the specific language governing permissions and
012     *  limitations under the License.
013     */
014    /*
015     * This file has been modified by Chris Pheby in accordance with Section 4.2
016     * of the Apache Software License
017     */
018    package org.jadira.dependencynavigator.implementations.local;
019    
020    import org.apache.maven.project.interpolation.ModelInterpolationException;
021    import org.codehaus.plexus.context.Context;
022    import org.codehaus.plexus.context.ContextException;
023    import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
024    import org.jadira.dependencynavigator.controller.DependencyResolutionException;
025    import org.jadira.dependencynavigator.implementations.Workspace;
026    import org.jadira.dependencynavigator.model.ArtifactInitialisationException;
027    import org.jadira.dependencynavigator.model.PomFile;
028    import org.jadira.dependencynavigator.model.RootPom;
029    
030    import java.io.BufferedReader;
031    import java.io.File;
032    import java.io.FileNotFoundException;
033    import java.io.FileReader;
034    
035    public class LocalDiskWorkspace extends Workspace implements Contextualizable {
036    
037        public static final String KEY_WORKSPACE_PATH = "local.workspace";
038    
039        private File workspaceDirectory;
040    
041        private File rootFile;
042    
043        public File getWorkspaceDirectory() {
044            return this.workspaceDirectory;
045        }
046    
047        public void loadRootPom(File file) throws ArtifactInitialisationException {
048            this.rootFile = file;
049            reloadPomFile();
050        }
051    
052        @Override
053        public void reloadPomFile() throws ArtifactInitialisationException {
054            try {
055                BufferedReader fileReader = new BufferedReader(new FileReader(rootFile));
056                root = new RootPom(loadPom(fileReader), this);
057            } catch (FileNotFoundException e) {
058                throw new DependencyResolutionException(e);
059            } catch (ModelInterpolationException e) {
060                throw new DependencyResolutionException(e);
061            }
062        }
063    
064        @Override
065        public String getPath(String moduleName) {
066            return rootFile.getParent() + "/" + moduleName + "/pom.xml";
067        }
068    
069        @Override
070        public PomFile loadModulePom(String module) {
071            try {
072                File file2 = new File(getPath(module));
073                BufferedReader fileReader = new BufferedReader(new FileReader(file2));
074                return loadPom(fileReader);
075            } catch (FileNotFoundException e) {
076                throw new DependencyResolutionException(e);
077            }
078        }
079    
080        @Override
081        public boolean canCreateSnapshot() {
082            return true;
083        }
084    
085        public void contextualize(Context context) throws ContextException {
086            String workspacePath = (String) context.get(KEY_WORKSPACE_PATH);
087            if (workspacePath == null || workspacePath.trim().length() == 0) {
088                throw new ContextException("Missing configuration parameter: " + KEY_WORKSPACE_PATH);
089            }
090            workspaceDirectory = new File(workspacePath);
091        }
092    }