1 /* 2 * ModeShape (http://www.modeshape.org) 3 * See the COPYRIGHT.txt file distributed with this work for information 4 * regarding copyright ownership. Some portions may be licensed 5 * to Red Hat, Inc. under one or more contributor license agreements. 6 * See the AUTHORS.txt file in the distribution for a full listing of 7 * individual contributors. 8 * 9 * ModeShape is free software. Unless otherwise indicated, all code in ModeShape 10 * is licensed to you under the terms of the GNU Lesser General Public License as 11 * published by the Free Software Foundation; either version 2.1 of 12 * the License, or (at your option) any later version. 13 * 14 * ModeShape is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 * Lesser General Public License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public 20 * License along with this software; if not, write to the Free 21 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 22 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 23 */ 24 package org.modeshape.sequencer.classfile; 25 26 import java.io.InputStream; 27 import net.jcip.annotations.ThreadSafe; 28 import org.modeshape.graph.sequencer.SequencerOutput; 29 import org.modeshape.graph.sequencer.StreamSequencer; 30 import org.modeshape.graph.sequencer.StreamSequencerContext; 31 import org.modeshape.sequencer.classfile.metadata.ClassFileMetadataReader; 32 import org.modeshape.sequencer.classfile.metadata.ClassMetadata; 33 34 @ThreadSafe 35 public class ClassFileSequencer implements StreamSequencer { 36 37 private static final ClassFileRecorder DEFAULT_CLASS_FILE_RECORDER = new DefaultClassFileRecorder(); 38 39 private ClassFileRecorder classFileRecorder = DEFAULT_CLASS_FILE_RECORDER; 40 41 public void sequence( InputStream stream, 42 SequencerOutput output, 43 StreamSequencerContext context ) { 44 45 try { 46 ClassMetadata classMetadata = ClassFileMetadataReader.instance(stream); 47 48 classFileRecorder.recordClass(context, output, classMetadata); 49 } catch (Exception ex) { 50 context.getLogger(getClass()).error(ex, ClassFileSequencerI18n.errorSequencingClass, context.getInputPath()); 51 } 52 } 53 54 /** 55 * Sets the custom {@link ClassFileRecorder} by specifying a class name. This method attempts to instantiate an instance of 56 * the custom {@link ClassFileRecorder} class prior to ensure that the new value represents a valid implementation. 57 * 58 * @param classFileRecorderClassName the fully-qualified class name of the new custom class file recorder implementation; null 59 * indicates that {@link DefaultClassFileRecorder the default class file recorder} should be used. 60 * @throws ClassNotFoundException if the the class for the {@code ClassFileRecorder} implementation cannot be located 61 * @throws IllegalAccessException if the row factory class or its nullary constructor is not accessible. 62 * @throws InstantiationException if the row factory represents an abstract class, an interface, an array class, a primitive 63 * type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason. 64 * @throws ClassCastException if the instantiated class file recorder does not implement the {@link ClassFileRecorder} 65 * interface 66 */ 67 public void setClassFileRecorderClassName( String classFileRecorderClassName ) 68 throws ClassNotFoundException, IllegalAccessException, InstantiationException { 69 70 if (classFileRecorderClassName == null) { 71 this.classFileRecorder = DEFAULT_CLASS_FILE_RECORDER; 72 return; 73 } 74 75 Class<?> classFileRecorderClass = Class.forName(classFileRecorderClassName); 76 this.classFileRecorder = (ClassFileRecorder)classFileRecorderClass.newInstance(); 77 } 78 79 /** 80 * Sets a custom {@link ClassFileRecorder}. If {@code classFileRecorder} is null, then the {@link DefaultClassFileRecorder 81 * default class file recorder} will be used. 82 * 83 * @param classFileRecorder the new custom class file recorder implementation; null indicates that 84 * {@link DefaultClassFileRecorder the default class file recorder} should be used. 85 */ 86 public void setClassFileRecorder( ClassFileRecorder classFileRecorder ) { 87 this.classFileRecorder = classFileRecorder == null ? DEFAULT_CLASS_FILE_RECORDER : classFileRecorder; 88 } 89 }