001 package org.apache.fulcrum.yaafi.service.baseservice; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022 import java.io.File; 023 024 import org.apache.avalon.framework.configuration.Configuration; 025 import org.apache.avalon.framework.configuration.ConfigurationException; 026 import org.apache.avalon.framework.context.Context; 027 import org.apache.avalon.framework.context.ContextException; 028 import org.apache.avalon.framework.logger.AbstractLogEnabled; 029 import org.apache.avalon.framework.parameters.ParameterException; 030 import org.apache.avalon.framework.parameters.Parameters; 031 import org.apache.avalon.framework.service.ServiceException; 032 import org.apache.avalon.framework.service.ServiceManager; 033 034 /** 035 * Base class for a service implementation capturing the Avalon 036 * serviceConfiguration artifacts. Take care that using this class 037 * introduces a dependency to the YAAFI library. 038 * 039 * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a> 040 */ 041 042 public abstract class BaseServiceImpl 043 extends AbstractLogEnabled 044 implements BaseService 045 { 046 /** The name of the service as defined in the role configuration file */ 047 private String serviceName; 048 049 /** The context supplied by the Avalon framework */ 050 private Context serviceContext; 051 052 /** The service manager supplied by the Avalon framework */ 053 private ServiceManager serviceManager; 054 055 /** The configuraton supplied by the Avalon framework */ 056 private Configuration serviceConfiguration; 057 058 /** The parameters supplied by the avalon framework */ 059 private Parameters serviceParameters; 060 061 /** the Avalon application directory */ 062 private File serviceApplicationDir; 063 064 /** the Avalon temp directory */ 065 private File serviceTempDir; 066 067 /** the Avalon partition name */ 068 private String servicePartitionName; 069 070 /** the class loader for this service */ 071 private ClassLoader serviceClassLoader; 072 073 ///////////////////////////////////////////////////////////////////////// 074 // Avalon Lifecycle Implementation 075 ///////////////////////////////////////////////////////////////////////// 076 077 /** 078 * Constructor 079 */ 080 public BaseServiceImpl() 081 { 082 // nothing to do 083 } 084 085 /** 086 * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context) 087 */ 088 public void contextualize(Context context) throws ContextException 089 { 090 this.serviceContext = context; 091 this.serviceName = (String) context.get("urn:avalon:name"); 092 this.serviceApplicationDir = (File) context.get("urn:avalon:home"); 093 this.serviceTempDir = (File) context.get("urn:avalon:temp"); 094 this.servicePartitionName = (String) context.get("urn:avalon:partition"); 095 this.serviceClassLoader = (ClassLoader) context.get("urn:avalon:classloader"); 096 } 097 098 /** 099 * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager) 100 */ 101 public void service(ServiceManager serviceManager) throws ServiceException 102 { 103 this.serviceManager = serviceManager; 104 } 105 106 /** 107 * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration) 108 */ 109 public void configure(Configuration configuration) throws ConfigurationException 110 { 111 this.serviceConfiguration = configuration; 112 } 113 114 /** 115 * @see org.apache.avalon.framework.parameters.Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters) 116 */ 117 public void parameterize(Parameters parameters) throws ParameterException 118 { 119 this.serviceParameters = parameters; 120 } 121 122 /** 123 * @see org.apache.avalon.framework.configuration.Reconfigurable#reconfigure(org.apache.avalon.framework.configuration.Configuration) 124 */ 125 public void reconfigure(Configuration configuration) throws ConfigurationException 126 { 127 this.serviceConfiguration = configuration; 128 } 129 130 /** 131 * @see org.apache.avalon.framework.activity.Disposable#dispose() 132 */ 133 public void dispose() 134 { 135 this.serviceApplicationDir = null; 136 this.serviceClassLoader = null; 137 this.serviceConfiguration = null; 138 this.serviceContext = null; 139 this.serviceManager = null; 140 this.serviceName = null; 141 this.serviceParameters = null; 142 this.servicePartitionName = null; 143 this.serviceTempDir = null; 144 } 145 146 ///////////////////////////////////////////////////////////////////////// 147 // Service Implementation 148 ///////////////////////////////////////////////////////////////////////// 149 150 /** 151 * @see java.lang.Object#toString() 152 */ 153 public String toString() 154 { 155 StringBuffer result = new StringBuffer(); 156 157 result.append( getClass().getName() + "@" + Integer.toHexString(hashCode())); 158 159 result.append("{"); 160 161 result.append("serviceName: "); 162 result.append(this.getServiceName()); 163 result.append(";"); 164 165 result.append(" servicePartitionName: "); 166 result.append(this.getServicePartitionName()); 167 result.append(";"); 168 169 result.append(" serviceApplicatonDir: "); 170 result.append(this.getServiceApplicationDir().getAbsolutePath()); 171 result.append(";"); 172 173 result.append(" serviceTempDir: "); 174 result.append(this.getServiceTempDir().getAbsolutePath()); 175 result.append(";"); 176 177 result.append(" serviceContext: "); 178 result.append(this.getServiceContext().toString()); 179 result.append(";"); 180 181 result.append(" serviceConfiguration: "); 182 result.append(this.getServiceConfiguration().toString()); 183 result.append(";"); 184 185 result.append(" serviceParameters: "); 186 result.append(Parameters.toProperties(this.getServiceParameters())); 187 result.append(";"); 188 189 result.append(" serviceClassLoader: "); 190 result.append(this.getServiceClassLoader()); 191 result.append(";"); 192 193 result.append(" serviceLogger: "); 194 result.append(this.getLogger()); 195 result.append(";"); 196 197 result.append(" serviceManager: "); 198 result.append(this.getServiceManager()); 199 200 result.append("}"); 201 202 return result.toString(); 203 } 204 205 /** 206 * @see org.apache.avalon.framework.service.ServiceManager#hasService(java.lang.String) 207 */ 208 protected boolean hasService(String key) 209 { 210 return this.getServiceManager().hasService(key); 211 } 212 213 /** 214 * @see org.apache.avalon.framework.service.ServiceManager#lookup(java.lang.String) 215 */ 216 protected Object lookup(String key) 217 { 218 try 219 { 220 return this.getServiceManager().lookup(key); 221 } 222 catch (ServiceException e) 223 { 224 String msg = "Unable to lookup the following service : " + key; 225 this.getLogger().error(msg,e); 226 throw new RuntimeException(msg); 227 } 228 } 229 230 /** 231 * @see org.apache.avalon.framework.service.ServiceManager#release(java.lang.Object) 232 */ 233 protected void release(Object object) 234 { 235 this.release(object); 236 } 237 238 /** 239 * Determines the absolute file based on the application directory 240 * @param fileName the filename 241 * @return the absolute file 242 */ 243 protected File createAbsoluteFile( String fileName ) 244 { 245 File result = new File(fileName); 246 247 if( result.isAbsolute() == false ) 248 { 249 result = new File( this.getServiceApplicationDir(), fileName ); 250 } 251 252 return result; 253 } 254 255 /** 256 * Determines the absolute path based on the application directory 257 * @param fileName the filename 258 * @return the absolute path 259 */ 260 protected String createAbsolutePath( String fileName ) 261 { 262 return this.createAbsoluteFile(fileName).getAbsolutePath(); 263 } 264 265 /** 266 * @return Returns the serviceApplicationDir. 267 */ 268 protected File getServiceApplicationDir() 269 { 270 return serviceApplicationDir; 271 } 272 273 /** 274 * @return Returns the serviceClassLoader. 275 */ 276 protected ClassLoader getServiceClassLoader() 277 { 278 return serviceClassLoader; 279 } 280 281 /** 282 * @return Returns the serviceConfiguration. 283 */ 284 protected Configuration getServiceConfiguration() 285 { 286 return serviceConfiguration; 287 } 288 289 /** 290 * @return Returns the serviceContext. 291 */ 292 protected Context getServiceContext() 293 { 294 return serviceContext; 295 } 296 297 /** 298 * @return Returns the serviceManager. 299 */ 300 protected ServiceManager getServiceManager() 301 { 302 return serviceManager; 303 } 304 305 /** 306 * @return Returns the serviceName. 307 */ 308 protected String getServiceName() 309 { 310 return serviceName; 311 } 312 313 /** 314 * @return Returns the serviceParameters. 315 */ 316 protected Parameters getServiceParameters() 317 { 318 return serviceParameters; 319 } 320 321 /** 322 * @return Returns the servicePartitionName. 323 */ 324 protected String getServicePartitionName() 325 { 326 return servicePartitionName; 327 } 328 329 /** 330 * @return Returns the serviceTempDir. 331 */ 332 protected File getServiceTempDir() 333 { 334 return serviceTempDir; 335 } 336 }