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.java; 25 26 import java.io.InputStream; 27 import org.modeshape.graph.sequencer.SequencerOutput; 28 import org.modeshape.graph.sequencer.StreamSequencer; 29 import org.modeshape.graph.sequencer.StreamSequencerContext; 30 import org.modeshape.sequencer.java.metadata.JavaMetadata; 31 32 public class JavaMetadataSequencer implements StreamSequencer { 33 34 private static final SourceFileRecorder DEFAULT_SOURCE_FILE_RECORDER = new ClassSourceFileRecorder(); 35 36 private SourceFileRecorder sourceFileRecorder = DEFAULT_SOURCE_FILE_RECORDER; 37 38 /** 39 * {@inheritDoc} 40 * 41 * @see org.modeshape.graph.sequencer.StreamSequencer#sequence(java.io.InputStream, 42 * org.modeshape.graph.sequencer.SequencerOutput, org.modeshape.graph.sequencer.StreamSequencerContext) 43 */ 44 public void sequence( InputStream stream, 45 SequencerOutput output, 46 StreamSequencerContext context ) { 47 try { 48 JavaMetadata javaMetadata = JavaMetadata.instance(stream, JavaMetadataUtil.length(stream), null); 49 50 sourceFileRecorder.record(context, output, javaMetadata); 51 52 } catch (Exception ex) { 53 context.getLogger(getClass()).error(ex, JavaMetadataI18n.errorSequencingFile, context.getInputPath()); 54 } 55 } 56 57 /** 58 * Sets the custom {@link SourceFileRecorder} by specifying a class name. This method attempts to instantiate an instance of 59 * the custom {@link SourceFileRecorder} class prior to ensure that the new value represents a valid implementation. 60 * 61 * @param sourceFileRecorderClassName the fully-qualified class name of the new custom class file recorder implementation; 62 * null indicates that {@link ClassSourceFileRecorder the class file recorder} should be used. 63 * @throws ClassNotFoundException if the the class for the {@code SourceFileRecorder} implementation cannot be located 64 * @throws IllegalAccessException if the row factory class or its nullary constructor is not accessible. 65 * @throws InstantiationException if the row factory represents an abstract class, an interface, an array class, a primitive 66 * type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason. 67 * @throws ClassCastException if the instantiated class file recorder does not implement the {@link SourceFileRecorder} 68 * interface 69 */ 70 public void setSourceFileRecorderClassName( String sourceFileRecorderClassName ) 71 throws ClassNotFoundException, IllegalAccessException, InstantiationException { 72 73 if (sourceFileRecorderClassName == null) { 74 this.sourceFileRecorder = DEFAULT_SOURCE_FILE_RECORDER; 75 return; 76 } 77 78 Class<?> sourceFileRecorderClass = Class.forName(sourceFileRecorderClassName); 79 this.sourceFileRecorder = (SourceFileRecorder)sourceFileRecorderClass.newInstance(); 80 } 81 82 /** 83 * Sets a custom {@link SourceFileRecorder}. If {@code sourceFileRecorder} is null, then the {@link ClassSourceFileRecorder 84 * class file recorder} will be used. 85 * 86 * @param sourceFileRecorder the new custom class file recorder implementation; null indicates that 87 * {@link ClassSourceFileRecorder the class file recorder} should be used. 88 */ 89 public void setSourceFileRecorder( SourceFileRecorder sourceFileRecorder ) { 90 this.sourceFileRecorder = sourceFileRecorder == null ? DEFAULT_SOURCE_FILE_RECORDER : sourceFileRecorder; 91 } 92 }