1 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 99 class BlockEntry 100 { 101 private static final Class BLOCK_CLASS = getBlockClass(); 102 103 private Object 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 getName() 124 { 125 return m_componentProfile.getTemplate().getName(); 126 } 127 128 public synchronized Object getObject() 129 { 130 return m_object; 131 } 132 133 public synchronized void setObject( final Object 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 [] 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 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 [] getServiceClasses( final Object block, 179 final ServiceDescriptor[] services ) 180 { 181 final Class [] classes = new Class [ services.length + 1 ]; 182 final ClassLoader 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 throwable ) 192 { 193 } 195 } 196 197 classes[ services.length ] = BLOCK_CLASS; 205 return classes; 206 } 207 208 private static Class getBlockClass() 209 { 210 try 211 { 212 return Class.forName( "org.apache.avalon.phoenix.Block" ); 213 } 214 catch( ClassNotFoundException e ) 215 { 216 throw new IllegalStateException ( "Can not find block class" ); 217 } 218 } 219 } 220 | Popular Tags |