KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > petite > config > AutomagicPetiteConfig


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

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 JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16
17 /**
18  * Automagically configures Petite container by analyzing the classpath.
19  * <p>
20  * Scans all classes on classpath and in jar files, and scans for {@link jodd.petite.meta.PetiteBean}
21  * annotation (not by loading the class!). If annotation is founded, class will be loaded and
22  * registered as Petite bean.
23  */

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     /**
37      * Return elapsed number of milliseconds for configuration.
38      */

39     public long getElapsed() {
40         return elapsed;
41     }
42
43     /**
44      * Configures {@link jodd.petite.PetiteContainer} with specified class path.
45      * @see AutomagicPetiteConfig#configure(jodd.petite.PetiteContainer)
46      */

47     public void configure(PetiteContainer petiteContainer, URL JavaDoc[] classpath) {
48         this.container = petiteContainer;
49         elapsed = System.currentTimeMillis();
50         for (URL JavaDoc path : classpath) {
51             try {
52                 scanUrl(path);
53             } catch (IOException JavaDoc 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     /**
62      * Configures {@link jodd.petite.PetiteContainer} with default class path.
63      * @see AutomagicPetiteConfig#configure(jodd.petite.PetiteContainer, java.net.URL[])
64      */

65     public void configure(PetiteContainer petiteContainer) {
66         configure(petiteContainer, ClassLoaderUtil.getFullClassPath(AutomagicPetiteConfig.class));
67     }
68
69     /**
70      * Scans all classes and registers only those annotated with {@link jodd.petite.meta.PetiteBean}.
71      * Because of performance purposes, classes are not dynamically loaded; instead, their
72      * file content is examined.
73      */

74     @SuppressWarnings JavaDoc({"unchecked"})
75     protected void onClassName(String JavaDoc className, InputStream JavaDoc inputStream) {
76         byte[] data;
77         try {
78             data = StreamUtil.readBytes(inputStream);
79         } catch (IOException JavaDoc 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 JavaDoc beanClass;
87         try {
88             beanClass = ClassLoaderUtil.loadClass(className, this.getClass());
89         } catch (ClassNotFoundException JavaDoc 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