KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > application > BlockEntry


1 /* ====================================================================
2  * Loom Software License, version 1.1
3  *
4  * Copyright (c) 2003, Loom Group. All rights reserved.
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. Neither the name of the Loom Group nor the name "Loom" nor
18  * the names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior
20  * written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * ====================================================================
36  *
37  * Loom includes code from the Apache Software Foundation
38  *
39  * ====================================================================
40  * The Apache Software License, Version 1.1
41  *
42  * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
43  * reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  *
49  * 1. Redistributions of source code must retain the above copyright
50  * notice, this list of conditions and the following disclaimer.
51  *
52  * 2. Redistributions in binary form must reproduce the above copyright
53  * notice, this list of conditions and the following disclaimer in
54  * the documentation and/or other materials provided with the
55  * distribution.
56  *
57  * 3. The end-user documentation included with the redistribution,
58  * if any, must include the following acknowledgment:
59  * "This product includes software developed by the
60  * Apache Software Foundation (http://www.apache.org/)."
61  * Alternately, this acknowledgment may appear in the software
62  * itself, if and wherever such third-party acknowledgments
63  * normally appear.
64  *
65  * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
66  * must not be used to endorse or promote products derived from this
67  * software without prior written permission. For written
68  * permission, please contact apache@apache.org.
69  *
70  * 5. Products derived from this software may not be called "Apache",
71  * nor may "Apache" appear in their name, without prior written
72  * permission of the Apache Software Foundation.
73  *
74  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
75  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
76  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
77  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
78  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
79  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
80  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
81  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
82  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
83  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
84  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85  * SUCH DAMAGE.
86  */

87 package org.codehaus.loom.components.application;
88
89 import org.codehaus.loom.components.util.info.ComponentInfo;
90 import org.codehaus.loom.components.util.info.ServiceDescriptor;
91 import org.codehaus.loom.components.util.metadata.ComponentTemplate;
92 import org.codehaus.loom.components.util.profile.ComponentProfile;
93
94 /**
95  * This is the structure describing each block before it is loaded.
96  *
97  * @author Peter Donald
98  */

99 class BlockEntry
100 {
101     private static final Class JavaDoc BLOCK_CLASS = getBlockClass();
102
103     private Object JavaDoc m_object;
104     private final ComponentProfile m_componentProfile;
105     private BlockInvocationHandler m_invocationHandler;
106
107     public BlockEntry( final ComponentProfile componentProfile )
108     {
109         invalidate();
110         m_componentProfile = componentProfile;
111     }
112
113     public ComponentInfo getInfo()
114     {
115         return m_componentProfile.getInfo();
116     }
117
118     public ComponentTemplate getTemplate()
119     {
120         return m_componentProfile.getTemplate();
121     }
122
123     public String JavaDoc getName()
124     {
125         return m_componentProfile.getTemplate().getName();
126     }
127
128     public synchronized Object JavaDoc getObject()
129     {
130         return m_object;
131     }
132
133     public synchronized void setObject( final Object JavaDoc object )
134     {
135         invalidate();
136
137         if( null != object &&
138             !m_componentProfile.getTemplate().isDisableProxy() )
139         {
140             final ComponentInfo blockInfo = m_componentProfile.getInfo();
141             final Class JavaDoc[] interfaces = getServiceClasses( object,
142                                                           blockInfo.getServices() );
143             m_invocationHandler =
144             new BlockInvocationHandler( object, interfaces );
145         }
146         m_object = object;
147     }
148
149     public synchronized Object JavaDoc getProxy()
150     {
151         if( m_componentProfile.getTemplate().isDisableProxy() )
152         {
153             return m_object;
154         }
155         else
156         {
157             if( null != m_invocationHandler )
158             {
159                 return m_invocationHandler.getProxy();
160             }
161             else
162             {
163                 return null;
164             }
165         }
166     }
167
168     synchronized void invalidate()
169     {
170         if( null != m_invocationHandler )
171         {
172             m_invocationHandler.invalidate();
173             m_invocationHandler = null;
174         }
175         m_object = null;
176     }
177
178     private Class JavaDoc[] getServiceClasses( final Object JavaDoc block,
179                                        final ServiceDescriptor[] services )
180     {
181         final Class JavaDoc[] classes = new Class JavaDoc[ services.length + 1 ];
182         final ClassLoader JavaDoc classLoader = block.getClass().getClassLoader();
183
184         for( int i = 0; i < services.length; i++ )
185         {
186             try
187             {
188                 classes[ i ] =
189                 classLoader.loadClass( services[ i ].getType() );
190             }
191             catch( final Throwable JavaDoc throwable )
192             {
193                 //Ignore
194
}
195         }
196
197         //Note that the proxy is still built using the
198
//Block interface so that ComponentManagers can
199
//still be used to provide blocks with services.
200
//Block extends Component and thus the proxy
201
//extends Component. The magic is that the Block
202
//interface has no methods and thus will never cause
203
//any issues for Proxy class.
204
classes[ services.length ] = BLOCK_CLASS;
205         return classes;
206     }
207
208     private static Class JavaDoc getBlockClass()
209     {
210         try
211         {
212             return Class.forName( "org.apache.avalon.phoenix.Block" );
213         }
214         catch( ClassNotFoundException JavaDoc e )
215         {
216             throw new IllegalStateException JavaDoc( "Can not find block class" );
217         }
218     }
219 }
220
Popular Tags