1 17 18 package org.apache.avalon.repository.cli; 19 20 import java.io.File ; 21 import java.io.FileFilter ; 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 35 public class RepositoryVerifier 36 { 37 41 private static Resources REZ = 42 ResourceManager.getPackageResources( RepositoryVerifier.class ); 43 44 private static final String PADDING = 45 " "; 46 47 51 private final InitialContext m_context; 52 53 private final String m_root; 54 55 59 64 public RepositoryVerifier( 65 InitialContext context ) throws Exception 66 { 67 m_context = context; 68 m_root = m_context.getInitialCacheDirectory().toString(); 69 } 70 71 void verify() 72 { 73 StringBuffer buffer = 74 new StringBuffer ( 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 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 [] 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 buffer ) 104 { 105 File cache = m_context.getInitialCacheDirectory(); 106 File [] 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 [] groups ) 115 { 116 int n = 0; 117 for( int i=0; i<groups.length; i++ ) 118 { 119 File 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 buffer, File file, int n ) 127 { 128 129 int rootLength = m_root.length() + 1; 130 String path = file.toString(); 131 String group = path.substring( rootLength ); 132 133 int offset = n - rootLength + 3; 134 int padding = offset - group.length(); 135 String pad = PADDING.substring( 0, padding ); 136 137 buffer.append( "\n" ); 138 buffer.append( " " + group + pad ); 139 prepareTypeSummary( buffer, file ); 140 } 141 142 147 private void prepareTypeSummary( StringBuffer buffer, File file ) 148 { 149 File [] types = file.listFiles( new TypesFilter() ); 150 for( int i=0; i<types.length; i++ ) 151 { 152 File type = types[i]; 153 String key = type.getName(); 154 File [] 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 182 } 183 184 188 private File [] getGroups( File root ) 189 { 190 return root.listFiles( new DirectoryFilter() ); 191 } 192 193 private class TypesFilter implements FileFilter 194 { 195 public boolean accept( File file ) 196 { 197 if( !file.isDirectory() ) return false; 198 final String type = file.getName(); 199 File [] artifacts = file.listFiles( new ArtifactFilter( type ) ); 200 return artifacts.length > 0; 201 } 202 } 203 204 private class DirectoryFilter implements FileFilter 205 { 206 public boolean accept( File file ) 207 { 208 return file.isDirectory(); 209 } 210 } 211 212 private class VersionedArtifactFilter extends ArtifactFilter 213 { 214 public VersionedArtifactFilter( String type ) 215 { 216 super( type ); 217 } 218 public boolean accept( File file ) 219 { 220 return super.accept( file ); 221 } 222 } 223 224 private class ArtifactFilter implements FileFilter 225 { 226 private String m_type; 227 public ArtifactFilter( String type ) 228 { 229 int n = type.length(); 230 m_type = type.substring( 0, n-1 ); 231 } 232 public boolean accept( File file ) 233 { 234 if( file.isDirectory() ) return false; 235 236 return file.getName().endsWith( m_type ); 237 } 238 } 239 } 240 | Popular Tags |