KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > util > factory > DefaultComponentFactory


1 /*
2  * Copyright (C) The Loom Group. All rights reserved.
3  *
4  * This software is published under the terms of the Loom
5  * Software License version 1.1, a copy of which has been included
6  * with this distribution in the LICENSE.txt file.
7  */

8 package org.codehaus.loom.components.util.factory;
9
10 import java.util.Map JavaDoc;
11 import java.util.WeakHashMap JavaDoc;
12
13 import org.codehaus.dna.Logger;
14 import org.codehaus.loom.components.util.info.ComponentInfo;
15 import org.codehaus.loom.components.util.infobuilder.BlockInfoReader;
16 import org.codehaus.loom.components.util.infobuilder.CascadingBlockInfoReader;
17
18 /**
19  * The default implementation of ComponentFactory that simply creates components from a ClassLoader.
20  *
21  * @author Peter Donald
22  * @version $Revision: 1.2 $ $Date: 2004/05/01 12:48:33 $
23  */

24 public class DefaultComponentFactory
25     implements ComponentFactory
26 {
27     /**
28      * Cache of ComponentInfo objects.
29      */

30     private final Map JavaDoc m_infos = new WeakHashMap JavaDoc();
31
32     /**
33      * The utility class that is used when building info objects for Components.
34      */

35     private final BlockInfoReader m_blockInfoReader;
36
37     /**
38      * The classloader from which all resources are loaded.
39      */

40     private final ClassLoader JavaDoc m_classLoader;
41
42     /**
43      * Create a Factory that loads from specified ClassLoader.
44      *
45      * @param classLoader the classLoader to use in factory, must not be null
46      * @param logger the logger to use for the BlockInfoReader
47      */

48     public DefaultComponentFactory( final ClassLoader JavaDoc classLoader, final Logger logger )
49     {
50         if( null == classLoader )
51         {
52             throw new NullPointerException JavaDoc( "classLoader" );
53         }
54         else if( null == logger )
55         {
56             throw new NullPointerException JavaDoc( "logger" );
57         }
58
59         m_classLoader = classLoader;
60         m_blockInfoReader = new CascadingBlockInfoReader( classLoader, logger );
61     }
62
63     /**
64      * Create a component by creating info for class with specified name and loaded from factorys ClassLoader.
65      *
66      * @see ComponentFactory#createInfo
67      */

68     public ComponentInfo createInfo( final String JavaDoc implementationKey )
69         throws Exception JavaDoc
70     {
71         ComponentInfo bundle = (ComponentInfo)m_infos.get( implementationKey );
72         if( null == bundle )
73         {
74             bundle = createComponentInfo( implementationKey );
75             m_infos.put( implementationKey, bundle );
76         }
77
78         return bundle;
79     }
80
81     /**
82      * Create a component by creating instance of class with specified name and loaded from factorys ClassLoader.
83      *
84      * @see ComponentFactory#createComponent
85      */

86     public Object JavaDoc createComponent( final String JavaDoc implementationKey )
87         throws Exception JavaDoc
88     {
89         final Class JavaDoc clazz = getClassLoader().loadClass( implementationKey );
90         return clazz.newInstance();
91     }
92
93     /**
94      * Create a ComponentInfo for component with specified implementationKey.
95      *
96      * @param implementationKey the implementationKey
97      *
98      * @return the created ComponentInfo
99      *
100      * @throws Exception if unabel to create componentInfo
101      */

102     protected ComponentInfo createComponentInfo( final String JavaDoc implementationKey )
103         throws Exception JavaDoc
104     {
105         final Class JavaDoc type = getClassLoader().loadClass( implementationKey );
106         return m_blockInfoReader.buildComponentInfo( type );
107     }
108
109     /**
110      * Return the ClassLoader associated with the ComponentFactory.
111      *
112      * @return the ClassLoader associated with the ComponentFactory.
113      */

114     protected ClassLoader JavaDoc getClassLoader()
115     {
116         return m_classLoader;
117     }
118 }
119
Popular Tags