View Javadoc

1   /*
2    * Copyright 2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.jjar;
18  
19  import java.util.Properties;
20  import java.util.StringTokenizer;
21  import java.util.ArrayList;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import java.io.InputStream;
26  
27  import java.net.URL;
28  import java.net.URLConnection;
29  
30  public class RepositoryProps extends Properties implements Repository
31  {
32      private List packageList = null;
33      private DependencyEngine de = new DependencyEngine();
34  
35      public void load( URL url )
36      {
37          try
38          {
39              URLConnection conn = url.openConnection();
40              InputStream is = conn.getInputStream();
41              
42              load(is);
43              
44              is.close();
45          }
46          catch(Exception e )
47              {
48              }
49  
50          /*
51           *  get the list of packages
52           */
53  
54          String packages = getProperty("packages");
55          
56          packages.trim();
57  
58          packageList = makeList( packages );
59  
60          /*
61           * setup the dependency engine
62           */
63  
64          Iterator i = packageList.iterator();
65  
66          while( i.hasNext() )
67          {
68              /*
69               * for each package
70               */
71  
72              String p = (String) i.next();
73  
74              //            System.out.println(" processing " + p );
75  
76              /*
77               *  get the dependency list
78               */
79  
80              String depstr = getProperty( p + ".dependencies");
81  
82              depstr.trim();
83  
84              List dep = makeList( depstr );
85              
86              /*
87               * now, remove an <none> marks
88               */
89  
90              Iterator ii = dep.iterator();
91  
92              List deplist = new ArrayList();
93  
94              while( ii.hasNext() )
95              {
96                  String sdep = (String) ii.next();
97  
98                  if ( !sdep.equals("<none>"))
99                      deplist.add( sdep );
100             }
101 
102             try
103             {
104                 de.addProject( p, deplist, p );
105             }
106             catch( Exception e )
107                 {
108                     System.out.println("Repository.Repository() : " + p + " : " + e );
109                 }
110         }
111     }
112 
113     public List getDependencyList( String pkg, String version )
114     {
115         if( isPackage( pkg ))
116             return de.getDependencies( pkg );
117 
118         return new ArrayList();
119     }
120 
121     public Iterator getPackageListIter()
122     {
123         return packageList.iterator();
124     }
125 
126     public int getPackageCount()
127     {
128         return packageList.size();
129     }
130 
131     public String getPackageDefaultVersion( String pkg )
132     {
133         if (isPackage( pkg ) )
134         {
135             return getProperty( pkg + ".default" );
136         }
137 
138         return null;
139     }
140 
141     public String getPackageDescription( String pkg )
142     {
143         if (isPackage( pkg ) )
144         {
145             return getProperty( pkg + ".desc" );
146         }
147 
148         return null;
149     }
150 
151     public String getFetchTarget( String pkg, String version )
152     {
153         if (!isPackage( pkg ))
154             return null;
155 
156         String ft = getProperty( pkg + "." + version + ".jar");
157 
158         return ft;
159     }
160 
161     public boolean isPackage( String pkg )
162     {
163         Iterator i = packageList.iterator();
164 
165         while( i.hasNext() )
166         {
167             String p = (String) i.next();
168 
169             if (p.equals( pkg ) )
170                 return true;
171         }
172     
173         return false;
174     }
175 
176     public List getPackageVersionList( String pkg )
177     {
178         if(!isPackage( pkg ))
179            return null;
180      
181         return makeList( getProperty( pkg + ".version" ) );
182     }
183 
184     private List makeList( String thing )
185     {
186         StringTokenizer st = new StringTokenizer( thing, ", ");
187 
188         List list = new ArrayList();
189 
190         while(st.hasMoreTokens() )
191         {
192             list.add( st.nextToken().trim() );
193         }
194 
195         return list;
196     }
197 }
198 
199 
200