1 3 package jodd.petite.config; 4 5 import jodd.petite.PetiteContainer; 6 import jodd.petite.PetiteException; 7 import jodd.petite.meta.PetiteBean; 8 import jodd.io.findfile.FindClass; 9 import jodd.io.StreamUtil; 10 import jodd.util.ClassLoaderUtil; 11 import jodd.util.ArraysUtil; 12 13 import java.net.URL ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 17 24 public class AutomagicPetiteConfig extends FindClass implements PetiteConfig { 25 26 protected byte[] petiteBeanAnnotationBytes = "Ljodd/petite/meta/PetiteBean;".getBytes(); 27 28 public AutomagicPetiteConfig() { 29 this.createInputStream = true; 30 } 31 32 protected PetiteContainer container; 33 34 protected long elapsed; 35 36 39 public long getElapsed() { 40 return elapsed; 41 } 42 43 47 public void configure(PetiteContainer petiteContainer, URL [] classpath) { 48 this.container = petiteContainer; 49 elapsed = System.currentTimeMillis(); 50 for (URL path : classpath) { 51 try { 52 scanUrl(path); 53 } catch (IOException ioex) { 54 throw new PetiteException("Unable to scan classpath: '" + path + "'.", ioex); 55 } 56 } 57 elapsed = System.currentTimeMillis() - elapsed; 58 System.out.println("Petite configured in " + elapsed + "ms"); 59 } 60 61 65 public void configure(PetiteContainer petiteContainer) { 66 configure(petiteContainer, ClassLoaderUtil.getFullClassPath(AutomagicPetiteConfig.class)); 67 } 68 69 74 @SuppressWarnings ({"unchecked"}) 75 protected void onClassName(String className, InputStream inputStream) { 76 byte[] data; 77 try { 78 data = StreamUtil.readBytes(inputStream); 79 } catch (IOException ioex) { 80 throw new PetiteException("Unable to read class file '" + className + "'.", ioex); 81 } 82 int index = ArraysUtil.indexOf(data, petiteBeanAnnotationBytes); 83 if (index == -1) { 84 return; 85 } 86 Class beanClass; 87 try { 88 beanClass = ClassLoaderUtil.loadClass(className, this.getClass()); 89 } catch (ClassNotFoundException cnfex) { 90 throw new PetiteException("Unable to load class '" + className + "'.", cnfex); 91 } 92 93 PetiteBean petiteBean = (PetiteBean) beanClass.getAnnotation(PetiteBean.class); 94 if (petiteBean == null) { 95 return; 96 } 97 container.register(beanClass); 98 } 99 } 100 | Popular Tags |