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.lang.reflect.Method;
020    import java.util.List;
021    import java.util.Iterator;
022    import java.net.URLConnection;
023    import java.net.URL;
024    
025    /**
026     *  JJAR command line program
027     *
028     *  @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
029     *  @version $Id: JJAR.java 155454 2005-02-26 13:23:34Z dirkv $
030     */
031    public class JJAR
032    {
033        public static String title = "JJAR : Jakarta Jar Archive Respository v@version@";
034        public static String defaultRepository = ClasspathUtil.getDefaultRepository();
035    
036        public static void main( String args[])
037        {
038            /*
039             *  first thing is the verb
040             */
041    
042            if (args.length < 1 )
043            {
044                System.out.println(title);
045                return;
046            }
047    
048            String verb = args[0];
049            Class [] ca = {args.getClass() };
050            Object [] arg = { args };
051    
052            try 
053            {
054                Method m = Class.forName("org.apache.commons.jjar.JJAR").getMethod(verb, ca );
055    
056                m.invoke( null, arg );
057            }
058            catch( NoSuchMethodException nsme )
059            {
060                System.out.println("JJAR : invalid action " + verb );
061                JJAR.help(  args );
062            }
063            catch( Exception e )
064            {
065                System.out.println("JJAR : exception : " + e );
066            }
067        }
068    
069        public static void help( String args[] )
070        {
071            header();
072            
073            System.out.println("list : list the JJAR packages in the repository");
074            System.out.println("fetch : fetch a JJAR package from the repository");
075            System.out.println("See the README.txt for more info.");
076        }
077    
078        public static void fetch( String args[] )
079        {
080            boolean classpath = false;
081            String jarname = null;
082            String packagename = null;
083            String version = null;
084            boolean verifyignore = false;
085            boolean getdeps = true;
086            boolean onlydeps = false;
087    
088            String dir = ".";
089    
090            if ( args.length < 3 )
091            {
092                System.out.println("Usage :  fetch [-v] [-od] -j jarname -p package -v version ");
093                return;
094            }
095    
096            for( int i = 1; i < args.length; i++)
097            {
098                String arg = args[i];
099    
100                if (arg.startsWith("-"))
101                {
102                    if (arg.equals("-j"))
103                    {
104                        jarname = args[i+1];
105                        i++;
106                    }
107                    else if (arg.equals("-p"))
108                    {
109                        packagename = args[i+1];
110                        i++;
111                    }
112                    else if (arg.equals("-v"))
113                    {
114                        version = args[i+1];
115                        i++;
116                    }
117                    else if (arg.equals("-c"))
118                    {
119                        classpath = true;
120                    }
121                    else if (arg.equals("-vi"))
122                    {
123                        verifyignore = true;
124                    }
125                    else if( arg.equals("-d"))
126                    {
127                        dir = args[i+1];
128                        i++;
129                    }
130                    else if( arg.equals("-nd"))
131                    {
132                        getdeps = false;
133                    }
134                    else if( arg.equals("-od"))
135                    {
136                        onlydeps = true;
137                    }
138                    
139                }
140            }
141    
142            if ( packagename == null)
143            {
144                System.out.println("Usage :  fetch -j jarname -p package -v version ");
145                return;
146            }
147    
148            /*
149             * now go get
150             */
151            
152            try
153            {
154                /*
155                 *  get the repository info
156                 */
157    
158                Repository rep = new RepositoryXML();
159                rep.load( new URL( defaultRepository + "repository.xml") );
160            
161                /*
162                 *  get the fetch target
163                 */
164    
165                if( version == null)
166                    version = rep.getPackageDefaultVersion( packagename );
167    
168                System.out.println("Fetching " + packagename + " v" + version + " into file " + (jarname == null? "<default name>" : jarname ) );
169     
170                String fetchTarget = rep.getFetchTarget( packagename, version );
171    
172                if (fetchTarget == null)
173                {
174                    System.out.println("JJAR.fetch : target not found for " + packagename + " v" + version );
175                    return;
176                }
177    
178                /*
179                 *  are there any dependencies for this fetch target?
180                 */
181    
182                String deptarget = "";
183    
184                if (getdeps)
185                {
186                    List deplist = rep.getDependencyList( packagename, version );
187                    
188                    Iterator ii = deplist.iterator();
189                    
190                    while(ii.hasNext())
191                    {
192                        /*
193                         *  for now, use the fetchtarget names
194                         */
195                        
196                        String depnameuni = (String) ii.next();
197                        
198                        JJARPackage jjarp = new JJARPackage( depnameuni );
199                        
200                        String depname = jjarp.getName();
201                        String depver = jjarp.getVersionString();
202                        
203                        deptarget = rep.getFetchTarget( depname, depver );
204                        
205                        if( deptarget == null)
206                        {
207                            System.out.println("  Repository error : returned null for dependency " + depname + " v" + depver 
208                                               + " Skipping.");
209                            continue;
210                        }
211                        
212                        /*
213                         *  check to see if exists if verifyignore
214                         */
215    
216      
217                        if (verifyignore )
218                        {
219                            JJARPackage jpak = null;
220                            
221                            try
222                            {
223                                jpak = ClasspathUtil.getPackage( dir + "/" + deptarget );
224                            }
225                            catch(Exception e)
226                            {
227                                // ignore 
228                            }
229    
230                            if (jpak != null)
231                            {
232                                if( jpak.equals( depname, depver ))
233                                {
234                                    System.out.println(" Dependency '" + depname + "' exists.  Skipping.");
235                                    continue;
236                                }
237                            }
238                        }
239                         
240                        /*
241                         *  now get it
242                         */
243    
244                        URL url = new URL( defaultRepository + deptarget );
245                        URLConnection uconn  = (URLConnection) url.openConnection(); 
246                        
247                        System.out.println(" Fetching dependency : " + deptarget + " to " + dir + "/" + deptarget  );
248    
249                        Transport.fetchJar( uconn, dir + "/" + deptarget );                
250                    }
251                }
252    
253                /*
254                 *  now the actual jar.  If we are told to ignore the deps, so be it
255                 */
256    
257                if ( onlydeps )
258                {
259                    return;
260                }
261    
262                /*
263                 *  if verifyignore then check the jar to see if it satisfies the request
264                 */
265                
266                if (jarname == null)
267                    jarname = fetchTarget;
268    
269                if( verifyignore)
270                {
271                    try
272                    {
273                        JJARPackage packageinfo = ClasspathUtil.getPackage( dir + "/" + jarname );
274    
275                        if( packageinfo.equals( packagename, version ))
276                        {
277                            System.out.println("Target exists.  skipping fetch");
278                            return;
279                        }
280                    }
281                    catch(Exception e)
282                    {
283                        // ignore and continue
284                    }
285                }
286    
287                URL url = new URL( defaultRepository + fetchTarget );
288                URLConnection uconn  = (URLConnection) url.openConnection(); 
289    
290                String destjar = dir + "/" + jarname;
291    
292                System.out.println(" Fetching target : " + fetchTarget + " to " + destjar );
293    
294                Transport.fetchJar( uconn, destjar);
295            }
296            catch( Exception e )
297            {
298                System.out.println("JJAR.fetch : exception " + e );
299                e.printStackTrace();
300            } 
301         }
302    
303        public static void verify( String args[] )
304        {
305            header();
306    
307            /*
308             *  do we do it to classpath, or was a jar specified
309             */
310    
311            String flag = null;
312            String jarname = null;
313    
314            if (args.length > 1 )
315            {
316                flag = args[1];
317    
318                if (flag.equals("-j"))
319                {
320                    if (args.length >= 3)
321                    {
322                        jarname = args[2];
323        
324                        try 
325                        {
326                            JJARPackage jjarp = ClasspathUtil.getPackage( jarname );
327                            
328                            if (jjarp != null)
329                                System.out.println("    " + jjarp.getName() + " v" + jjarp.getVersionString() );
330                            else
331                                System.out.println("    " + jarname + "not a jjar compatable jar");
332                        }
333                        catch( Exception e )
334                        {
335                        }
336                    }
337                }
338                else
339                {
340                    /*
341                     * do the classpath
342                     */
343                    List jars = ClasspathUtil.getJarList( null );
344                    
345                    Iterator i = jars.iterator();
346                    
347                    while( i.hasNext() )
348                    {
349                        String jar =  (String) i.next();
350                        
351                        try 
352                        {
353                            JJARPackage jjarp = ClasspathUtil.getPackage( jar );
354                            
355                            if (jjarp != null)
356                                System.out.println("    " + jjarp.getName() + " v" + jjarp.getVersionString() );
357                            else
358                                System.out.println("    " + jar + "not a jjar compatable jar");
359                        }
360                        catch( Exception e )
361                        {
362                        }
363                    }
364                }
365            }
366            else
367            {
368                System.out.println(" usage : verify -c | -j jarname");
369            }
370            return;
371        }
372    
373        public static void list( String args[] ) 
374        {
375            header();
376    
377            try
378            {
379                URL url = new URL(defaultRepository + "repository.xml");
380                
381                Repository rep = new RepositoryXML();
382                rep.load(url);
383    
384                if (args.length >= 3 )
385                {
386                    String flag = args[1];
387    
388                    if (flag.equals("-p"))
389                    {
390                        String pkg = args[2];
391                    
392                        dumpInfo( rep, pkg );
393                    }
394                }
395                else
396                {
397                    System.out.println("Repository contains " + rep.getPackageCount() + " packages : " );
398    
399                    Iterator i = rep.getPackageListIter();
400               
401                    while( i.hasNext() )
402                    {
403                        String p = (String) i.next();
404                        dumpInfo( rep, p );
405                        System.out.println("");
406                    }
407                }
408            }
409            catch( Exception e )
410            {
411                System.out.println("JJAR.list : exception " + e );
412                e.printStackTrace();
413            } 
414        }
415    
416        private static void dumpInfo(Repository rep, String p )
417        {
418            System.out.println(" " + p );
419            System.out.println("    desc     : " + rep.getPackageDescription( p ) );
420            System.out.println("    default  : " + rep.getPackageDefaultVersion( p ));
421            System.out.println("    versions : ");
422            
423            List l = rep.getPackageVersionList( p );
424            
425            Iterator ii = l.iterator();
426            
427            while( ii.hasNext() )
428            {
429                String version = (String) ii.next() ;
430    
431                System.out.print(  "      " + version + " deps : "  );
432                
433                List depl = rep.getDependencyList( p, version );
434                
435                Iterator iii = depl.iterator();
436                
437                while( iii.hasNext() )
438                {
439                    System.out.print( iii.next() + ", " );
440                }   
441                
442                System.out.println("");
443            }
444                 
445           System.out.println("--");
446          
447        }
448    
449        public static void mark( String args[] )
450        {
451            //   mark -j jarname -p package -v version
452    
453            String packagename = null;
454            String jarname = null;
455            String version = null;
456    
457            if ( args.length < 7 )
458            {
459                System.out.println("Usage : mark  -j jarname -p package -v version ");
460                return;
461            }
462    
463            for( int i = 1; i < args.length; i++)
464            {
465                String arg = args[i];
466    
467                if (arg.startsWith("-"))
468                {
469                    if (arg.equals("-j"))
470                    {
471                        jarname = args[i+1];
472                        i++;
473                    }
474                    else if (arg.equals("-p"))
475                    {
476                        packagename = args[i+1];
477                        i++;
478                    }
479                    else if (arg.equals("-v"))
480                    {
481                        version = args[i+1];
482                        i++;
483                    }
484                    
485                }
486            }
487    
488            if ( packagename == null || jarname == null || version == null)
489            {
490                System.out.println("Usage :  mark -j jarname -p package -v version ");
491                return;
492            }
493            
494            try
495            {
496                if (ClasspathUtil.markJar( jarname, packagename, version ))
497                    System.out.println("successful");
498    
499            }
500            catch( Exception e)
501            {
502                System.out.println("Exception e : " + e );
503                }
504            
505        }
506    
507        private static void header()
508        {
509            System.out.println(title);
510            System.out.println(" - using repository " + defaultRepository);
511           System.out.println("---------------------------------------------");
512            System.out.println("");
513        }
514    }