KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > repository > cli > RepositoryVerifier


1 /*
2  * Copyright 2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.repository.cli;
19
20 import java.io.File JavaDoc;
21 import java.io.FileFilter JavaDoc;
22
23 import org.apache.avalon.repository.provider.InitialContext;
24
25 import org.apache.avalon.util.i18n.ResourceManager;
26 import org.apache.avalon.util.i18n.Resources;
27
28
29 /**
30  * Merlin command line handler.
31  *
32  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
33  * @version $Revision: 1.5 $
34  */

35 public class RepositoryVerifier
36 {
37     //----------------------------------------------------------
38
// static
39
//----------------------------------------------------------
40

41     private static Resources REZ =
42         ResourceManager.getPackageResources( RepositoryVerifier.class );
43
44     private static final String JavaDoc PADDING =
45        " ";
46
47     //----------------------------------------------------------
48
// state
49
//----------------------------------------------------------
50

51     private final InitialContext m_context;
52
53     private final String JavaDoc m_root;
54
55     //----------------------------------------------------------
56
// constructor
57
//----------------------------------------------------------
58

59    /**
60     * Creation of a new repository verifier.
61     * @param context the repository inital context
62     * @exception Exception if an error occurs
63     */

64     public RepositoryVerifier(
65       InitialContext context ) throws Exception JavaDoc
66     {
67         m_context = context;
68         m_root = m_context.getInitialCacheDirectory().toString();
69     }
70
71     void verify()
72     {
73         StringBuffer JavaDoc buffer =
74           new StringBuffer JavaDoc( InitialContext.LINE );
75         buffer.append( "\nAvalon Repository" );
76         buffer.append( InitialContext.LINE );
77
78         prepareInfoListing( buffer );
79         buffer.append( InitialContext.LINE );
80
81         prepareContentListing( buffer );
82         buffer.append( InitialContext.LINE );
83
84         System.out.println( buffer.toString() );
85     }
86
87     private void prepareInfoListing( StringBuffer JavaDoc buffer )
88     {
89         buffer.append( "\n${avalon.repository.cache} = " );
90         buffer.append( m_context.getInitialCacheDirectory() );
91         buffer.append( "\n${avalon.dir} = " );
92         buffer.append( m_context.getInitialWorkingDirectory() );
93         String JavaDoc[] hosts = m_context.getInitialHosts();
94         buffer.append( "\n${avalon.repository.hosts} = (" );
95         buffer.append( hosts.length );
96         buffer.append( ")" );
97         for( int i=0; i<hosts.length; i++ )
98         {
99             buffer.append( "\n " + hosts[i] );
100         }
101     }
102
103     private void prepareContentListing( StringBuffer JavaDoc buffer )
104     {
105         File JavaDoc cache = m_context.getInitialCacheDirectory();
106         File JavaDoc[] groups = getGroups( cache );
107         int n = getGroupsWidth( groups );
108         for( int i=0; i<groups.length; i++ )
109         {
110             prepareGroupListing( buffer, groups[i], n );
111         }
112     }
113
114     private int getGroupsWidth( File JavaDoc[] groups )
115     {
116         int n = 0;
117         for( int i=0; i<groups.length; i++ )
118         {
119             File JavaDoc group = groups[i];
120             int j = group.toString().length();
121             if( j > n ) n = j;
122         }
123         return n;
124     }
125
126     private void prepareGroupListing( StringBuffer JavaDoc buffer, File JavaDoc file, int n )
127     {
128
129         int rootLength = m_root.length() + 1;
130         String JavaDoc path = file.toString();
131         String JavaDoc group = path.substring( rootLength );
132
133         int offset = n - rootLength + 3;
134         int padding = offset - group.length();
135         String JavaDoc pad = PADDING.substring( 0, padding );
136
137         buffer.append( "\n" );
138         buffer.append( " " + group + pad );
139         prepareTypeSummary( buffer, file );
140     }
141
142    /**
143     * List the types within the group.
144     * @param buffer the string buffer
145     * @param file the group directory
146     */

147     private void prepareTypeSummary( StringBuffer JavaDoc buffer, File JavaDoc file )
148     {
149         File JavaDoc[] types = file.listFiles( new TypesFilter() );
150         for( int i=0; i<types.length; i++ )
151         {
152             File JavaDoc type = types[i];
153             String JavaDoc key = type.getName();
154             File JavaDoc[] versions = type.listFiles( new VersionedArtifactFilter( key ) );
155             if( i > 0 )
156             {
157                 buffer.append( ", " );
158             }
159             else
160             {
161                 buffer.append( " " );
162             }
163             buffer.append(
164               type.getName()
165               + ":"
166               + versions.length );
167         }
168         /*
169         for( int i=0; i<types.length; i++ )
170         {
171             File type = types[i];
172             String key = type.getName();
173             File[] versions = type.listFiles( new VersionedArtifactFilter( key ) );
174             for( int j=0; j<versions.length; j++ )
175             {
176                 File artifact = versions[j];
177                 String name = artifact.getName();
178                 buffer.append( "\n " + name );
179             }
180         }
181         */

182     }
183
184    /**
185     * Return the parent of the last directory.
186     * @return the groups
187     */

188     private File JavaDoc[] getGroups( File JavaDoc root )
189     {
190         return root.listFiles( new DirectoryFilter() );
191     }
192
193     private class TypesFilter implements FileFilter JavaDoc
194     {
195         public boolean accept( File JavaDoc file )
196         {
197             if( !file.isDirectory() ) return false;
198             final String JavaDoc type = file.getName();
199             File JavaDoc[] artifacts = file.listFiles( new ArtifactFilter( type ) );
200             return artifacts.length > 0;
201         }
202     }
203
204     private class DirectoryFilter implements FileFilter JavaDoc
205     {
206         public boolean accept( File JavaDoc file )
207         {
208             return file.isDirectory();
209         }
210     }
211
212     private class VersionedArtifactFilter extends ArtifactFilter
213     {
214         public VersionedArtifactFilter( String JavaDoc type )
215         {
216             super( type );
217         }
218         public boolean accept( File JavaDoc file )
219         {
220             return super.accept( file );
221         }
222     }
223
224     private class ArtifactFilter implements FileFilter JavaDoc
225     {
226         private String JavaDoc m_type;
227         public ArtifactFilter( String JavaDoc type )
228         {
229             int n = type.length();
230             m_type = type.substring( 0, n-1 );
231         }
232         public boolean accept( File JavaDoc file )
233         {
234             if( file.isDirectory() ) return false;
235            
236             return file.getName().endsWith( m_type );
237         }
238     }
239 }
240
Popular Tags