Changeset 15681 for lang/java

Show
Ignore:
Timestamp:
07/12/08 00:57:32 (4 months ago)
Author:
nobeans
Message:

POMファイルの内容を保持し続けるようにPomクラスを導入した。Pomの取得、解析をそれぞれ別クラスに分けた。
Metadataについても同様の構成に変更した。
これによりRepositoryの肥満がかなり解消された。
特定の目的に束縛されない処理はユーティリティクラスに抽出した。

Location:
lang/java/mvnbeans/trunk/src
Files:
9 added
4 modified

Legend:

Unmodified
Added
Removed
  • lang/java/mvnbeans/trunk/src/main/java/jp/javelindev/mvnbeans/Artifact.java

    r14434 r15681  
    1818package jp.javelindev.mvnbeans; 
    1919 
    20 import java.net.MalformedURLException; 
    2120import java.net.URL; 
    2221import java.util.ArrayList; 
    2322import java.util.List; 
    2423 
    25 import jp.javelindev.mvnbeans.exception.MalformedURLRuntimeException; 
     24import jp.javelindev.mvnbeans.utils.UrlUtils; 
    2625 
    2726import org.apache.commons.lang.StringUtils; 
     
    4039    private String version; 
    4140    private DependencyScope scope; 
    42     private URL pomUrl; 
     41    private Pom pom; 
    4342    private List<Artifact> dependencies; 
    4443 
     
    5352    public String getGroupId() { 
    5453        return groupId; 
    55     } 
    56  
    57     public String getGroupIdAsPath() { 
    58         return groupId.replace('.', '/'); 
    5954    } 
    6055 
     
    7570    } 
    7671 
    77     public URL getPomUrl() { 
    78         return pomUrl; 
     72    public Pom getPom() { 
     73        return pom; 
    7974    } 
    8075 
    81     public void setPomUrl(URL pomUrl) { 
    82         this.pomUrl = pomUrl; 
    83     } 
    84  
    85     /** 
    86      * 指定されたリポジトリに対するPOMファイルのURLを解決します。 
    87      * <p> 
    88      * 接続可能かどうかはここでは保証されません。 
    89      * 接続可能、つまり、アーティファクトの存在するリポジトリとそれに対するURLが 
    90      * 確定した際には、本インスタンスのpomUrlにそのURLが格納されます。 
    91      * </p> 
    92      *  
    93      * @param repository 
    94      * @return 
    95      */ 
    96     public URL resolvePomUrl(Repository repository) { 
    97         StringBuilder sb = new StringBuilder(repository.getRepositoryUrl()); 
    98         sb.append(getGroupIdAsPath()).append("/"); 
    99         sb.append(getArtifactId()).append("/"); 
    100         sb.append(getVersion()).append("/"); 
    101         sb.append(getFileName(ResourceType.POM)); 
    102         return createUrl(sb.toString()); 
     76    public void setPom(Pom pom) { 
     77        this.pom = pom; 
    10378    } 
    10479 
     
    11287 
    11388    public URL resolveUrl(String fileName) { 
    114         assert (pomUrl != null); 
    115         String urlBase = pomUrl.toString().replaceFirst("/[^/]*$", "/"); 
    116         return createUrl(urlBase + fileName); 
    117     } 
    118  
    119     private static URL createUrl(String url) throws MalformedURLRuntimeException { 
    120         try { 
    121             return new URL(url); 
    122         } catch (MalformedURLException e) { 
    123             throw new MalformedURLRuntimeException(url, e); 
    124         } 
     89        assert (pom != null); 
     90        String urlBase = pom.getUrl().toString().replaceFirst("/[^/]*$", "/"); 
     91        return UrlUtils.createUrl(urlBase + fileName); 
    12592    } 
    12693 
     
    184151        sb.append(version).append(" "); 
    185152        sb.append("[").append(scope).append("] "); 
    186         sb.append("(").append(pomUrl).append(")"); 
     153        sb.append("(").append(pom.getUrl()).append(")"); 
    187154        return sb.toString(); 
    188155    } 
  • lang/java/mvnbeans/trunk/src/main/java/jp/javelindev/mvnbeans/Repository.java

    r15207 r15681  
    22 
    33import java.io.File; 
    4 import java.io.FileNotFoundException; 
    54import java.io.IOException; 
    6 import java.io.InputStream; 
    7 import java.net.MalformedURLException; 
    8 import java.net.URL; 
    9 import java.util.ArrayList; 
    105import java.util.List; 
    116 
    12 import java.util.logging.Level; 
    13 import java.util.logging.Logger; 
    147import jp.javelindev.mvnbeans.exception.ArtifactNotFoundException; 
     8import jp.javelindev.mvnbeans.exception.MalformedURLRuntimeException; 
    159import jp.javelindev.mvnbeans.exception.RepositoryIOException; 
    16 import jp.javelindev.mvnbeans.utils.CastUtils; 
     10import jp.javelindev.mvnbeans.exception.XmlParseException; 
    1711 
    18 import org.apache.commons.io.IOUtils; 
    19 import org.jdom.Element; 
    20 import org.jdom.JDOMException; 
    21 import org.jdom.Namespace; 
    22 import org.jdom.input.SAXBuilder; 
    23 import org.jdom.xpath.XPath; 
     12import org.apache.commons.lang.StringUtils; 
    2413 
    2514/** 
     
    2817 * @author nobeans 
    2918 */ 
    30 public class Repository {  
     19public class Repository { 
    3120 
    32     private Namespace pomNameSpace; 
    3321    private String repositoryUrl; 
    3422 
     
    4331     * 指定された条件に一致するアーティファクトを返します。 
    4432     *  
    45      * @param groupId 
    46      * @param artifactId 
    47      * @param version 
     33     * @param groupId グループID(必須) 
     34     * @param artifactId アーティファクトID(必須) 
     35     * @param version バージョン(省略可/null可) 
    4836     * @return 
    4937     * @throws ArtifactNotFoundException 
     
    5139     */ 
    5240    public Artifact findArtifact(String groupId, String artifactId, String version) throws ArtifactNotFoundException, RepositoryIOException { 
    53                 //バージョンが空の場合、maven-metadata.xmlから最新バージョンを探し出してきてそれを使います。 
    54                 String completedVersion = completeVersion(groupId, artifactId, version); 
    55         Artifact artifact = new Artifact(groupId, artifactId, completedVersion); 
    56         complete(artifact); 
     41        assert (groupId != null) && (artifactId != null) : "省略不可"; 
     42        String resolvedVersion = resolveVersion(groupId, artifactId, version); 
     43        Artifact artifact = new Artifact(groupId, artifactId, resolvedVersion); 
     44        setupPom(artifact); // この2つ、 
     45        setupDependencies(artifact); // 順番重要。 
    5746        return artifact; 
    5847    } 
    5948 
    60     private void complete(Artifact artifact) throws RepositoryIOException, ArtifactNotFoundException { 
    61         List<Artifact> dependencies = getDependencies(artifact); 
     49    private void setupPom(Artifact artifact) throws ArtifactNotFoundException, RepositoryIOException { 
     50        Pom pom = new PomRetriever(this).retrieve(artifact); 
     51        artifact.setPom(pom); 
     52    } 
     53 
     54    private void setupDependencies(Artifact artifact) throws RepositoryIOException { 
     55        List<Artifact> dependencies = new PomParser(artifact.getPom()).getDependencies(); 
    6256        artifact.setDependencies(dependencies); 
    6357    } 
    6458 
    65     private List<Artifact> getDependencies(Artifact artifact) throws RepositoryIOException, ArtifactNotFoundException { 
    66         artifact.setPomUrl(artifact.resolvePomUrl(this)); 
    67         List<Element> dependencies = retrieveDependencyElements(artifact); 
    68         return convertElementsToArtifacts(dependencies); 
    69     } 
    70  
    71     private List<Element> retrieveDependencyElements(Artifact artifact) throws RepositoryIOException, ArtifactNotFoundException { 
    72         InputStream is = null; 
     59    /** 
     60     * バージョンが空の場合、maven-metadata.xmlから最新バージョンを探し出してきてそれを使います。 
     61     *  
     62     * @param groupId 
     63     * @param artifactId 
     64     * @param version 
     65     * @return 
     66     * @throws RepositoryIOException  
     67     */ 
     68    private String resolveVersion(String groupId, String artifactId, String version) throws RepositoryIOException { 
    7369        try { 
    74             is = SingletonInputStreamOpener.getInstance().openStream(artifact.getPomUrl()); 
    75             Element root = new SAXBuilder().build(is).getRootElement(); 
    76  
    77             // XPathで依存性をパースする。 
    78             resolveNameSpace(root); 
    79             XPath xpath = XPath.newInstance("//p:dependencies/p:dependency"); 
    80             xpath.addNamespace(pomNameSpace); 
    81             return CastUtils.cast(xpath.selectNodes(root)); 
    82  
    83         } catch (FileNotFoundException e) { 
    84             throw new ArtifactNotFoundException(String.format("Artifact '%s:%s:%s' is not found in repository '%s'.", artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), repositoryUrl), e); 
     70            if (!StringUtils.isEmpty(version)) return version; 
     71            Metadata metadata = new MetadataRetriever(this).retrieve(groupId, artifactId); 
     72            return new MetadataParser(metadata).getLatestVersion(); 
     73        } catch (XmlParseException e) { 
     74            throw new RepositoryIOException(e); 
     75        } catch (MalformedURLRuntimeException e) { 
     76            throw new RepositoryIOException(e); 
    8577        } catch (IOException e) { 
    86             throw new RepositoryIOException("Cannot access to: " + artifact.getPomUrl(), e); 
    87         } catch (JDOMException e) { 
    88             throw new RepositoryIOException("Failed to analyze pom file: " + artifact, e); 
    89         } finally { 
    90             IOUtils.closeQuietly(is); 
     78            throw new RepositoryIOException(e); 
    9179        } 
    9280    } 
    9381 
    94     private void resolveNameSpace(Object root) throws JDOMException { 
    95         String namespace = (String) XPath.selectSingleNode(root, "namespace-uri()"); 
    96         if (namespace != null) { 
    97             pomNameSpace = Namespace.getNamespace("p", namespace); 
    98         } else { 
    99             pomNameSpace = Namespace.NO_NAMESPACE; 
    100         } 
    101     } 
    102  
    103     private List<Artifact> convertElementsToArtifacts(List<Element> dependencies) { 
    104         List<Artifact> artifactList = new ArrayList<Artifact>(); 
    105         for (Element element : dependencies) { 
    106             String depGroupId = element.getChildText("groupId", pomNameSpace); 
    107             String depArtifactId = element.getChildText("artifactId", pomNameSpace); 
    108             String depVersion = element.getChildText("version", pomNameSpace); 
    109  
    110                         //からの<dependencies>などの場合にgroupIdがnullになるケースがある。その場合は次のループにまわす。おそらく 
    111                         //ループは直後に終わるはず。 
    112                         if(depGroupId == null) continue; 
    113                          
    114                         depVersion = completeVersion(depGroupId, depArtifactId, depVersion); 
    115             String depScope = element.getChildText("scope", pomNameSpace); 
    116             if (!isTargetForScope(depScope)) continue; 
    117  
    118             String optional = element.getChildText("optional", pomNameSpace); 
    119             if (!isTargetForOptional(optional)) continue; 
    120  
    121             Artifact depArtifact = new Artifact(depGroupId, depArtifactId, depVersion); 
    122             depArtifact.setScope(DependencyScope.get(depScope)); 
    123             artifactList.add(depArtifact); 
    124         } 
    125         return artifactList; 
    126     } 
    127  
    128         private String completeVersion(String groupId, String artifactId, String version) { 
    129                 if(version != null && !version.isEmpty()) { 
    130                         return version; 
    131                 } 
    132                 try { 
    133                         return getLatestVersion(groupId, artifactId); 
    134                 } catch (RepositoryIOException ex) { 
    135                         Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, ex); 
    136                         throw new RuntimeException(ex); 
    137                 } 
    138         } 
    139  
    140     private String groupIdAsPath(String groupId) { 
    141                 if(groupId == null) return null; 
    142         return groupId.replace('.', '/'); 
    143     }    
    144          
    145         private String getLatestVersion(String groupId, String artifactId) throws RepositoryIOException, RepositoryIOException { 
    146                 StringBuilder sb = new StringBuilder(getRepositoryUrl()); 
    147                 sb.append(groupIdAsPath(groupId)).append("/"); 
    148                 sb.append(artifactId).append("/"); 
    149                 sb.append("maven-metadata.xml"); 
    150                  
    151                 String metadataPath = sb.toString(); 
    152                  
    153                 Logger.getLogger(this.getClass().getName()).fine("maven-metadata.xml path: " + metadataPath); 
    154                  
    155                 InputStream is = null; 
    156                 try { 
    157                         URL metadataUrl = new URL(metadataPath); 
    158                         is = SingletonInputStreamOpener.getInstance().openStream(metadataUrl); 
    159                         Element root = new SAXBuilder().build(is).getRootElement(); 
    160                         resolveNameSpace(root); 
    161             XPath xpath = XPath.newInstance("//p:versioning/p:versions/p:version"); 
    162             xpath.addNamespace(pomNameSpace); 
    163                          
    164                          
    165                         List<Element> elems = CastUtils.cast(xpath.selectNodes(root)); 
    166                         if(elems == null || elems.isEmpty()) { 
    167                                 return null; 
    168                         } else { 
    169                                 Element element = elems.get(elems.size() - 1); 
    170                                 return element.getText(); 
    171                         } 
    172                 } catch (FileNotFoundException ex) { 
    173                         //metadata.xmlが見つからない。最新バージョンも変わらないのでnullを返す。 
    174                         Logger.getLogger(this.getClass().getName()).log(Level.FINE, "File not found.", ex); 
    175                         return null; 
    176                 } catch (IOException ex) { 
    177                         throw new RepositoryIOException("Cannot access to: " + metadataPath, ex); 
    178                 } catch (JDOMException ex) { 
    179                         throw new RepositoryIOException("Failed to analyze maven-metadata.xml file: " + metadataPath, ex); 
    180                 } finally { 
    181                         IOUtils.closeQuietly(is); 
    182                 }  
    183  
    184         } 
    185  
    186     private static boolean isTargetForScope(String scope) { 
    187         return scope == null || !(scope.equalsIgnoreCase("test") || scope.equalsIgnoreCase("provided")); // FIXME 固定でいいかなぁ? 
    188     } 
    189  
    190     private static boolean isTargetForOptional(String optional) { 
    191         return optional == null || !optional.equalsIgnoreCase("true"); // FIXME 固定でいいかなぁ? 
    192     } 
    193  
     82    /** 
     83     * このリポジトリのベースURLを返します。 
     84     *  
     85     * @return 
     86     */ 
    19487    public String getRepositoryUrl() { 
    19588        return repositoryUrl; 
    19689    } 
    19790 
     91    /** 
     92     * ローカルリポジトリかどうかを返します。 
     93     *  
     94     * @return 
     95     */ 
    19896    public boolean isLocal() { 
    19997        return RepositoryScope.LOCAL_ONLY.match(repositoryUrl); 
  • lang/java/mvnbeans/trunk/src/test/java/jp/javelindev/mvnbeans/DummyArtifactFactory.java

    r14393 r15681  
    88        Artifact artifact = new Artifact("mygroup", "myartifact1", "1.0"); 
    99        artifact.setScope(DependencyScope.ROOT); 
    10         artifact.setPomUrl(new URL("http://hogehoge/foo/myartifact1-1.0.pom")); 
     10        artifact.setPom(new Pom(new URL("http://hogehoge/foo/myartifact1-1.0.pom"), "DUMMY")); 
    1111        return artifact; 
    1212    } 
     
    2121        Artifact artifact2 = new Artifact("mygroup2", "myartifact2", "2." + revision); 
    2222        artifact2.setScope(DependencyScope.COMPILE); 
    23         artifact2.setPomUrl(new URL("http://hogehoge/foo/myartifact2-2." + revision + ".pom")); 
     23        artifact2.setPom(new Pom(new URL("http://hogehoge/foo/myartifact2-2." + revision + ".pom"), "DUMMY")); 
    2424        artifact.addDependency(artifact2); 
    2525 
    2626        Artifact artifact3 = new Artifact("mygroup3", "myartifact3", "3." + revision); 
    2727        artifact3.setScope(DependencyScope.PROVIDED); 
    28         artifact3.setPomUrl(new URL("http://hogehoge/bar/myartifact3-3." + revision + ".pom")); 
     28        artifact3.setPom(new Pom(new URL("http://hogehoge/bar/myartifact3-3." + revision + ".pom"), "DUMMY")); 
    2929        artifact.addDependency(artifact3); 
    3030 
    3131        Artifact artifact4 = new Artifact("mygroup4", "myartifact4", "4." + revision); 
    3232        artifact4.setScope(DependencyScope.TEST); 
    33         artifact4.setPomUrl(new URL("http://hogehoge/bar/myartifact4-4." + revision + ".pom")); 
     33        artifact4.setPom(new Pom(new URL("http://hogehoge/bar/myartifact4-4." + revision + ".pom"), "DUMMY")); 
    3434        artifact2.addDependency(artifact4); 
    3535 
  • lang/java/mvnbeans/trunk/src/test/java/jp/javelindev/mvnbeans/ui/LibTest.java

    r14542 r15681  
    6262 
    6363        // 各種URLを確認する。 
    64         URL pomUrl = artifact.getPomUrl(); 
     64        URL pomUrl = artifact.getPom().getUrl(); 
    6565        System.out.println(pomUrl); 
    6666        assertTrue(pomUrl.toString().endsWith("/src/test/resources/localRepository/mygroup/myartifact/1.0/myartifact-1.0.pom")); 
     
    105105 
    106106        // 各種URLを確認する。 
    107         URL pomUrl = artifact.getPomUrl(); 
     107        URL pomUrl = artifact.getPom().getUrl(); 
    108108        System.out.println(pomUrl); 
    109109        assertThat(pomUrl.toString(), is("http://maven2.javelindev.jp/repository/commons-pool/commons-pool/1.2.2/commons-pool-1.2.2.pom")); 
     
    228228    @Test 
    229229    public void プロキシの設定() throws Exception { 
    230         // プロキシは直接どうぞ:これで効くのであれば・・・ 
    231         // ★未確認 
    232         System.setProperty("http.proxyHost", "localhost"); 
    233         System.setProperty("http.proxyPort", "8080"); 
     230        // http://java.sun.com/javase/ja/6/docs/ja/technotes/guides/net/proxies.html 
     231 
     232        //        { 
     233        //            InputStream is = new URL("http://java.sun.com/").openStream(); 
     234        //            List lines = UrlUtils.readLines(is); 
     235        //            System.out.println(lines); 
     236        //        } 
     237        //        System.out.println(System.getProperty("http.proxyHost")); 
     238 
     239        // プロキシサーバとポート 
     240        System.setProperty("http.proxyHost", "hoge.locaasdfasfsad234;lj;lkajsdfl;sakjfl;safkj;salkflhost"); 
     241        System.setProperty("http.proxyPort", "1234"); 
     242 
     243        // プロキシ対象外 
     244        System.setProperty("http.nonProxyHosts", "localhost|*.my.local.network"); 
     245 
     246        // この後のじゃまにならないように元に戻す 
     247        System.getProperties().remove("http.proxyHost"); 
     248        System.getProperties().remove("http.proxyPort"); 
     249 
     250        //        { 
     251        //            InputStream is = new URL("http://java.sun.com/").openStream(); 
     252        //            List lines = UrlUtils.readLines(is); 
     253        //            System.out.println(lines); 
     254        //        } 
    234255    } 
    235256}