001    /*
002     * Copyright 2001,2004 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.apache.commons.jjar;
018    
019    import java.util.Properties;
020    import java.util.StringTokenizer;
021    import java.util.ArrayList;
022    import java.util.Iterator;
023    import java.util.List;
024    
025    import java.io.InputStream;
026    
027    import java.net.URL;
028    import java.net.URLConnection;
029    
030    public class RepositoryProps extends Properties implements Repository
031    {
032        private List packageList = null;
033        private DependencyEngine de = new DependencyEngine();
034    
035        public void load( URL url )
036        {
037            try
038            {
039                URLConnection conn = url.openConnection();
040                InputStream is = conn.getInputStream();
041                
042                load(is);
043                
044                is.close();
045            }
046            catch(Exception e )
047                {
048                }
049    
050            /*
051             *  get the list of packages
052             */
053    
054            String packages = getProperty("packages");
055            
056            packages.trim();
057    
058            packageList = makeList( packages );
059    
060            /*
061             * setup the dependency engine
062             */
063    
064            Iterator i = packageList.iterator();
065    
066            while( i.hasNext() )
067            {
068                /*
069                 * for each package
070                 */
071    
072                String p = (String) i.next();
073    
074                //            System.out.println(" processing " + p );
075    
076                /*
077                 *  get the dependency list
078                 */
079    
080                String depstr = getProperty( p + ".dependencies");
081    
082                depstr.trim();
083    
084                List dep = makeList( depstr );
085                
086                /*
087                 * now, remove an <none> marks
088                 */
089    
090                Iterator ii = dep.iterator();
091    
092                List deplist = new ArrayList();
093    
094                while( ii.hasNext() )
095                {
096                    String sdep = (String) ii.next();
097    
098                    if ( !sdep.equals("<none>"))
099                        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