| 1 | |
package org.apache.turbine.services.avaloncomponent; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
import java.util.ArrayList; |
| 25 | |
import java.util.Iterator; |
| 26 | |
import java.util.List; |
| 27 | |
|
| 28 | |
import org.apache.avalon.excalibur.component.DefaultRoleManager; |
| 29 | |
import org.apache.avalon.excalibur.component.ExcaliburComponentManager; |
| 30 | |
import org.apache.avalon.excalibur.logger.Log4JLoggerManager; |
| 31 | |
import org.apache.avalon.excalibur.logger.LoggerManager; |
| 32 | |
import org.apache.avalon.framework.activity.Disposable; |
| 33 | |
import org.apache.avalon.framework.activity.Initializable; |
| 34 | |
import org.apache.avalon.framework.component.Component; |
| 35 | |
import org.apache.avalon.framework.component.ComponentException; |
| 36 | |
import org.apache.avalon.framework.configuration.Configuration; |
| 37 | |
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; |
| 38 | |
import org.apache.avalon.framework.context.DefaultContext; |
| 39 | |
import org.apache.avalon.framework.logger.Logger; |
| 40 | |
import org.apache.avalon.framework.service.ServiceException; |
| 41 | |
import org.apache.commons.logging.Log; |
| 42 | |
import org.apache.commons.logging.LogFactory; |
| 43 | |
import org.apache.turbine.Turbine; |
| 44 | |
import org.apache.turbine.services.InitializationException; |
| 45 | |
import org.apache.turbine.services.InstantiationException; |
| 46 | |
import org.apache.turbine.services.TurbineBaseService; |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | 0 | public class TurbineAvalonComponentService |
| 76 | |
extends TurbineBaseService |
| 77 | |
implements AvalonComponentService, Initializable, Disposable |
| 78 | |
{ |
| 79 | |
|
| 80 | 0 | private static Log log = LogFactory.getLog( |
| 81 | |
TurbineAvalonComponentService.class); |
| 82 | |
|
| 83 | |
|
| 84 | 0 | private ExcaliburComponentManager manager = null; |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
public void init() |
| 99 | |
throws InitializationException |
| 100 | |
{ |
| 101 | |
try |
| 102 | |
{ |
| 103 | 0 | initialize(); |
| 104 | |
|
| 105 | 0 | setInit(true); |
| 106 | |
} |
| 107 | 0 | catch (Exception e) |
| 108 | |
{ |
| 109 | 0 | throw new InitializationException("init failed", e); |
| 110 | 0 | } |
| 111 | 0 | } |
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
public void shutdown() |
| 119 | |
{ |
| 120 | 0 | dispose(); |
| 121 | 0 | setInit(false); |
| 122 | 0 | } |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
public void initialize() throws Exception |
| 134 | |
{ |
| 135 | 0 | org.apache.commons.configuration.Configuration conf |
| 136 | |
= getConfiguration(); |
| 137 | |
|
| 138 | |
|
| 139 | 0 | String sysConfigFilename = Turbine.getRealPath( |
| 140 | |
conf.getString(COMPONENT_CONFIG_KEY, COMPONENT_CONFIG_VALUE)); |
| 141 | 0 | String roleConfigFilename = Turbine.getRealPath( |
| 142 | |
conf.getString(COMPONENT_ROLE_KEY, COMPONENT_ROLE_VALUE)); |
| 143 | |
|
| 144 | 0 | log.debug("Config File: " + sysConfigFilename); |
| 145 | 0 | log.debug("Role File: " + roleConfigFilename); |
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | 0 | DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); |
| 150 | 0 | Configuration sysConfig = builder.buildFromFile(sysConfigFilename); |
| 151 | 0 | Configuration roleConfig = builder.buildFromFile(roleConfigFilename); |
| 152 | |
|
| 153 | |
|
| 154 | 0 | LoggerManager lm = new Log4JLoggerManager(); |
| 155 | |
|
| 156 | |
|
| 157 | 0 | DefaultRoleManager roles = new DefaultRoleManager(); |
| 158 | |
|
| 159 | 0 | Logger logger = lm.getLoggerForCategory(AVALON_LOG_CATEGORY); |
| 160 | |
|
| 161 | 0 | roles.enableLogging(logger); |
| 162 | 0 | roles.configure(roleConfig); |
| 163 | |
|
| 164 | |
|
| 165 | 0 | manager = new ExcaliburComponentManager(); |
| 166 | |
|
| 167 | 0 | manager.setLoggerManager(lm); |
| 168 | 0 | manager.enableLogging(logger); |
| 169 | |
|
| 170 | 0 | DefaultContext context = new DefaultContext(); |
| 171 | 0 | String realPath = Turbine.getRealPath("/"); |
| 172 | |
|
| 173 | 0 | context.put(AvalonComponentService.COMPONENT_APP_ROOT, realPath); |
| 174 | |
|
| 175 | |
|
| 176 | 0 | context.put("urn:avalon:home", realPath); |
| 177 | 0 | System.setProperty("applicationRoot", realPath); |
| 178 | 0 | System.setProperty("urn:avalon:home", realPath); |
| 179 | |
|
| 180 | 0 | log.debug("Application Root is " + realPath); |
| 181 | |
|
| 182 | 0 | manager.contextualize(context); |
| 183 | 0 | manager.setRoleManager(roles); |
| 184 | 0 | manager.configure(sysConfig); |
| 185 | |
|
| 186 | |
|
| 187 | 0 | manager.initialize(); |
| 188 | |
|
| 189 | 0 | List lookupComponents = conf.getList(COMPONENT_LOOKUP_KEY, |
| 190 | |
new ArrayList()); |
| 191 | |
|
| 192 | 0 | for (Iterator it = lookupComponents.iterator(); it.hasNext();) |
| 193 | |
{ |
| 194 | 0 | String component = (String) it.next(); |
| 195 | |
try |
| 196 | |
{ |
| 197 | 0 | Component c = manager.lookup(component); |
| 198 | 0 | log.info("Lookup for Component " + component + " successful"); |
| 199 | 0 | manager.release(c); |
| 200 | |
} |
| 201 | 0 | catch (Exception e) |
| 202 | |
{ |
| 203 | 0 | log.error("Lookup for Component " + component + " failed!"); |
| 204 | 0 | } |
| 205 | 0 | } |
| 206 | 0 | } |
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
public void dispose() |
| 212 | |
{ |
| 213 | 0 | manager.dispose(); |
| 214 | 0 | } |
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
public Object lookup(String roleName) |
| 224 | |
throws ServiceException |
| 225 | |
{ |
| 226 | |
try |
| 227 | |
{ |
| 228 | 0 | return manager.lookup(roleName); |
| 229 | |
} |
| 230 | 0 | catch (ComponentException e) |
| 231 | |
{ |
| 232 | 0 | throw new ServiceException(name, e.getMessage()); |
| 233 | |
} |
| 234 | |
} |
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
public void release(Object component) |
| 242 | |
{ |
| 243 | 0 | if( component instanceof Component ) |
| 244 | |
{ |
| 245 | 0 | manager.release((Component)component); |
| 246 | |
} |
| 247 | 0 | } |
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
public boolean hasService(String roleName) |
| 253 | |
{ |
| 254 | 0 | return manager.hasComponent(roleName); |
| 255 | |
} |
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
public boolean exists(String roleName) |
| 265 | |
{ |
| 266 | 0 | return this.hasService(roleName); |
| 267 | |
} |
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
public Object get(String roleName) throws InstantiationException |
| 273 | |
{ |
| 274 | |
try |
| 275 | |
{ |
| 276 | 0 | return this.lookup(roleName); |
| 277 | |
} |
| 278 | 0 | catch (ServiceException e) |
| 279 | |
{ |
| 280 | 0 | String msg = "Unable to get the following service : " + roleName; |
| 281 | 0 | log.error(msg); |
| 282 | 0 | throw new InstantiationException(msg); |
| 283 | |
} |
| 284 | 0 | catch (Throwable t) |
| 285 | |
{ |
| 286 | 0 | String msg = "Unable to get the following service : " + roleName; |
| 287 | 0 | log.error(msg,t); |
| 288 | 0 | throw new InstantiationException(msg,t); |
| 289 | |
} |
| 290 | |
} |
| 291 | |
} |