All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tsc9526.monalisa.tools.maven.Repository Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
/*******************************************************************************************
 *	Copyright (c) 2016, zzg.zhou([email protected])
 * 
 *  Monalisa is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU Lesser General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.

 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU Lesser General Public License for more details.

 *	You should have received a copy of the GNU Lesser General Public License
 *	along with this program.  If not, see .
 *******************************************************************************************/
package com.tsc9526.monalisa.tools.maven;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.tsc9526.monalisa.tools.datatable.CaseInsensitiveMap;
import com.tsc9526.monalisa.tools.logger.Logger;
import com.tsc9526.monalisa.tools.xml.XMLDocument;

/**
 * 
 * @author zzg.zhou([email protected])
 */
public class Repository {
	static Logger logger=Logger.getLogger(Repository.class);
	
	private List activeProfiles=new ArrayList();
	
	private String localRepository;
	
	private List remoteRepositoryUrls=new ArrayList();
	
	private CaseInsensitiveMap mirrors = new CaseInsensitiveMap();
	
	private CaseInsensitiveMap profiles = new CaseInsensitiveMap();
	
	private XMLDocument parser;
	private Document document;
	
	public Repository(){
		localRepository      = System.getProperty("user.home") + "/.m2/repository";
		
		remoteRepositoryUrls.add("https://repo1.maven.org/maven2");
		
		loadFromSettings();
	}
	
	
	public String getLocalRepository(){
		return localRepository;
	}
	
	public List getRemoteRepositoryUrls(){
		return remoteRepositoryUrls;
	}
	
	protected void loadFromSettings(){
		String settings = System.getProperty("user.home") + "/.m2/settings.xml";
		if (!new File(settings).exists()) {
			String home = System.getProperty("M2_HOME", System.getenv("M2_HOME"));
			if(home==null){
				home = System.getProperty("MAVEN_HOME", System.getenv("MAVEN_HOME"));
			}
			
			if (home != null) {
				settings = home + "/conf/settings.xml";
			}
		}
		
		File fs=new File(settings);
		if (fs.exists()){
			try{
				parser = new XMLDocument();
				document = parser.parseDocument(fs);
				
				parseSettings(); 
			}catch(Exception e){
				logger.error("Exception parse maven settings: "+fs.getAbsolutePath(),e);
			}
		}
	}

	
	protected void parseSettings()throws Exception {
		parseLocalRespository();

		parseMirrors();
		
		parseProfiles();
		
		parseActiveProfiles();
		
		parseRemoteRepositoryUrls();
	}
	
	protected void parseLocalRespository()throws Exception {
		localRepository = parser.getNodeStringValue(document, "/settings/localRepository");
	}

	protected void parseMirrors()throws Exception {
		NodeList ns=parser.selectNodes(document, "/settings/mirrors/mirror");
		if(ns!=null && ns.getLength()>0){
			for(int i=0;i0){
			for(int i=0;i rs=new CaseInsensitiveMap();
					rs.put(id, url);
					
					profiles.put(profileId, new Profile(profileId,rs));
				}else{
					profile.repositories.put(id,url);
				}
				
			}
		}
	}

	protected void parseActiveProfiles()throws Exception {
		NodeList ns = parser.selectNodes(document, "/settings/activeProfiles/activeProfile");
		if(ns!=null && ns.getLength()>0){
			for(int i=0;i mirrorUrls=new LinkedHashSet();
		Set urls=new LinkedHashSet();
		
		for(String profileId:activeProfiles){
			Profile profile=profiles.get(profileId);
			if(profile!=null){
				for(String id:profile.repositories.keySet()){
					String url=mirrors.get(id);
					if(url!=null && url.trim().length()>1){
						mirrorUrls.add(url.trim());
					}
					
					urls.add(profile.repositories.get(id));
				}
			}
		}
		
		mirrorUrls.addAll(urls);
		
		remoteRepositoryUrls.clear();
		remoteRepositoryUrls.addAll(mirrorUrls);
		remoteRepositoryUrls.add("https://repo1.maven.org/maven2");
	}
  
	static class Profile {
		private String id;
		private CaseInsensitiveMap repositories;
		
		public Profile(String id,CaseInsensitiveMap repositories){
			this.id=id;
			this.repositories=repositories;
		}

		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		public CaseInsensitiveMap getRepositories() {
			return repositories;
		}

		public void setRepositories(CaseInsensitiveMap repositories) {
			this.repositories = repositories;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy