1 package com.tirsen.nanning.attribute; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.net.MalformedURLException ; 6 import java.util.Properties ; 7 8 public class PropertyFileAttributeLoader implements AttributesLoader { 9 public static final String ATTRIBUTE_FILE_SUFFIX = ".attributes"; 10 11 public void load(ClassAttributes classAttributes) { 12 ClassPropertiesHelper classPropertiesHelper = new ClassPropertiesHelper(classAttributes); 13 classPropertiesHelper.loadAttributes(getProperties(classAttributes.getAttributeClass())); 14 } 15 16 Properties getProperties(Class klass) { 17 Properties properties = new Properties (); 18 InputStream inputStream = null; 19 try { 20 String className = klass.getName(); 21 22 inputStream = Attributes.findFile(klass, ClassPropertiesHelper.className(klass) + ATTRIBUTE_FILE_SUFFIX); 24 if (inputStream == null) { 25 inputStream = Attributes.findFile(klass, className.replace('.', '/') + ATTRIBUTE_FILE_SUFFIX); 26 } 27 28 if (inputStream != null) { 29 properties.load(inputStream); 30 } 31 32 } catch (MalformedURLException e) { 33 throw new AttributeException("Error fetching properties for " + klass, e); 34 } catch (IOException e) { 35 throw new AttributeException("Error fetching properties for " + klass, e); 36 } catch (AttributeException e) { 37 throw e; 38 } catch (Exception e) { 39 throw new AttributeException("Error fetching properties for " + klass, e); 40 } finally { 41 if (inputStream != null) { 42 try { 43 inputStream.close(); 44 } catch (IOException e) { 45 throw new RuntimeException (e); 46 } 47 } 48 } 49 return properties; 50 } 51 52 } 53 | Popular Tags |