KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > phoenix > components > application > BlockEntry


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

8 package org.apache.avalon.phoenix.components.application;
9
10 import org.apache.avalon.phoenix.metadata.BlockMetaData;
11 import org.apache.avalon.phoenix.metainfo.BlockInfo;
12 import org.apache.avalon.phoenix.metainfo.ServiceDescriptor;
13
14 /**
15  * This is the structure describing each block before it is loaded.
16  *
17  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
18  */

19 class BlockEntry
20 {
21     private Object JavaDoc m_object;
22
23     private BlockMetaData m_blockMetaData;
24
25     private BlockInvocationHandler m_invocationHandler;
26
27     public BlockEntry( final BlockMetaData blockMetaData )
28     {
29         invalidate();
30         m_blockMetaData = blockMetaData;
31     }
32
33     public String JavaDoc getName()
34     {
35         return getMetaData().getName();
36     }
37
38     public BlockMetaData getMetaData()
39     {
40         return m_blockMetaData;
41     }
42
43     public synchronized Object JavaDoc getObject()
44     {
45         return m_object;
46     }
47
48     public synchronized void setObject( final Object JavaDoc object )
49     {
50         invalidate();
51
52         if( null != object && ! getMetaData().isDisableProxy() )
53         {
54             final BlockInfo blockInfo = getMetaData().getBlockInfo();
55             final Class JavaDoc[] interfaces = getServiceClasses( object, blockInfo.getServices() );
56             m_invocationHandler = new BlockInvocationHandler( object, interfaces );
57         }
58         m_object = object;
59     }
60
61     public synchronized Object JavaDoc getProxy()
62     {
63         if ( getMetaData().isDisableProxy() )
64         {
65             return m_object;
66         }
67         else
68         {
69             if( null != m_invocationHandler )
70             {
71                 return m_invocationHandler.getProxy();
72             }
73             else
74             {
75                 return null;
76             }
77         }
78     }
79
80     protected synchronized void invalidate()
81     {
82         if( null != m_invocationHandler )
83         {
84             m_invocationHandler.invalidate();
85             m_invocationHandler = null;
86         }
87         m_object = null;
88     }
89
90     private Class JavaDoc[] getServiceClasses( final Object JavaDoc block, final ServiceDescriptor[] services )
91     {
92         final Class JavaDoc[] classes = new Class JavaDoc[ services.length + 1 ];
93         final ClassLoader JavaDoc classLoader = block.getClass().getClassLoader();
94
95         for( int i = 0; i < services.length; i++ )
96         {
97             try
98             {
99                 classes[ i ] = classLoader.loadClass( services[ i ].getName() );
100             }
101             catch( final Throwable JavaDoc throwable )
102             {
103                 //Ignore
104
}
105         }
106
107         //Note that the proxy is still built using the
108
//Block interface so that ComponentManaers can
109
//still be used to provide blocks with services.
110
//Block extends Component and thus the proxy
111
//extends Component. The magic is that the Block
112
//interface has no methods and thus will never cause
113
//any issues for Proxy class.
114
classes[ services.length ] =
115             org.apache.avalon.phoenix.Block.class;
116         return classes;
117     }
118 }
119
Popular Tags