1 package org.apache.maven.archiva.indexer.bytecode;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.maven.archiva.model.ArchivaArtifact;
23 import org.apache.maven.archiva.model.ArchivaArtifactJavaDetails;
24 import org.apache.maven.archiva.model.platform.JavaArtifactHelper;
25 import org.codehaus.plexus.util.IOUtil;
27 import java.io.BufferedReader;
29 import java.io.FileReader;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.HashMap;
35 import junit.framework.AssertionFailedError;
38 * BytecodeRecordLoader - Utility method for loading dump files into BytecordRecords.
40 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
43 public class BytecodeRecordLoader
45 private static Map cache = new HashMap();
47 public static BytecodeRecord loadRecord( File dumpFile, ArchivaArtifact artifact )
49 BytecodeRecord record = (BytecodeRecord) cache.get( artifact );
55 record = new BytecodeRecord();
56 record.setArtifact( artifact );
58 record.setClasses( new ArrayList() );
59 record.setMethods( new ArrayList() );
60 record.setFiles( new ArrayList() );
62 FileReader freader = null;
63 BufferedReader reader = null;
67 freader = new FileReader( dumpFile );
68 reader = new BufferedReader( freader );
70 String line = reader.readLine();
71 while ( line != null )
73 if ( line.startsWith( "FILENAME|" ) )
75 String filename = line.substring( "FILENAME|".length() );
76 record.setFilename( filename );
78 else if ( line.startsWith( "SIZE|" ) )
80 String size = line.substring( "SIZE|".length() );
81 record.getArtifact().getModel().setSize( Long.parseLong( size ) );
83 else if ( line.startsWith( "HASH_MD5|" ) )
85 String md5 = line.substring( "HASH_MD5|".length() );
86 record.getArtifact().getModel().setChecksumMD5( md5 );
88 else if ( line.startsWith( "HASH_SHA1|" ) )
90 String sha1 = line.substring( "HASH_SHA1|".length() );
91 record.getArtifact().getModel().setChecksumSHA1( sha1 );
93 else if ( line.startsWith( "HASH_BYTECODE|" ) )
95 String hash = line.substring( "HASH_BYTECODE|".length() );
96 ArchivaArtifactJavaDetails javaDetails = JavaArtifactHelper.getJavaDetails( record.getArtifact() );
97 javaDetails.setChecksumBytecode( hash );
99 else if ( line.startsWith( "JDK|" ) )
101 String jdk = line.substring( "JDK|".length() );
102 ArchivaArtifactJavaDetails javaDetails = JavaArtifactHelper.getJavaDetails( record.getArtifact() );
103 javaDetails.setJdk( jdk );
105 else if ( line.startsWith( "CLASS|" ) )
107 String classname = line.substring( "CLASS|".length() );
108 record.getClasses().add( classname );
110 else if ( line.startsWith( "METHOD|" ) )
112 String methodName = line.substring( "METHOD|".length() );
113 record.getMethods().add( methodName );
115 else if ( line.startsWith( "FILE|" ) )
117 String fileentry = line.substring( "FILE|".length() );
118 record.getFiles().add( fileentry );
121 line = reader.readLine();
124 catch ( IOException e )
126 throw new AssertionFailedError( "Unable to load record " + dumpFile + " from disk: " + e.getMessage() );
130 IOUtil.close( reader );
131 IOUtil.close( freader );
134 cache.put( artifact, record );