001package org.apache.fulcrum.yaafi.service.servicemanager;
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
022import org.apache.avalon.framework.activity.Disposable;
023import org.apache.avalon.framework.context.Context;
024import org.apache.avalon.framework.context.ContextException;
025import org.apache.avalon.framework.context.Contextualizable;
026import org.apache.avalon.framework.context.DefaultContext;
027import org.apache.avalon.framework.logger.AbstractLogEnabled;
028import org.apache.avalon.framework.logger.Logger;
029import org.apache.avalon.framework.parameters.ParameterException;
030import org.apache.avalon.framework.parameters.Parameterizable;
031import org.apache.avalon.framework.parameters.Parameters;
032import org.apache.avalon.framework.service.ServiceException;
033import org.apache.avalon.framework.service.ServiceManager;
034import org.apache.avalon.framework.service.Serviceable;
035
036
037/**
038 * This is a sort of "edelhack" to solve the problem of accessing
039 * the Avalon infrastructure without having an instance of the
040 * container. The implementation stores the very first instance
041 * of itself in a static variable which can be accessed using
042 * getInstance().
043 *
044 * This allows access to the various Avalon artifacts.
045 *
046 *  @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
047 */
048
049public class ServiceManagerServiceImpl
050    extends AbstractLogEnabled
051    implements ServiceManagerService, Contextualizable, Parameterizable, Serviceable, Disposable
052{
053    /** The one and only instance */
054    private static ServiceManagerServiceImpl instance;
055
056    /** Store the ServiceContainer on a per instance base */
057    private ServiceManager serviceManager;
058
059    /** Store the passed parameters on a per instance base */
060    private Parameters parameters;
061
062    /** Store the passed parameters on a per instance base */
063    private Context context;
064
065    /**
066     * Constructor
067     */
068    public ServiceManagerServiceImpl()
069    {
070        setInstance(this);
071    }
072
073    /**
074     * @return the one and only instance of this class
075     */
076    public static synchronized ServiceManagerService getInstance()
077    {
078        return instance;
079    }
080
081    /**
082     * Create the one and only instance
083     * @param instance the instance
084     */
085    protected static synchronized void setInstance( ServiceManagerServiceImpl instance )
086    {
087        if( ServiceManagerServiceImpl.instance == null )
088        {
089            ServiceManagerServiceImpl.instance = instance;
090        }
091    }
092
093    /////////////////////////////////////////////////////////////////////////
094    // Avalon Lifecycle Implementation
095    /////////////////////////////////////////////////////////////////////////
096
097    /**
098     * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
099     */
100    public void service(ServiceManager serviceManager) throws ServiceException
101    {
102        this.serviceManager = serviceManager;
103    }
104
105    /**
106     * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
107     */
108    public void contextualize(Context context) throws ContextException
109    {
110        this.context = context;
111    }
112
113    /**
114     * @see org.apache.avalon.framework.parameters.Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters)
115     */
116    public void parameterize(Parameters parameters) throws ParameterException
117    {
118        this.parameters = parameters;
119    }
120
121    /**
122     * @see org.apache.avalon.framework.activity.Disposable#dispose()
123     */
124    public void dispose()
125    {
126        this.serviceManager = null;
127        this.parameters = new Parameters();
128        this.context = new DefaultContext();
129        ServiceManagerServiceImpl.instance = null;
130    }
131
132    /////////////////////////////////////////////////////////////////////////
133    // ServiceContainer Implementation
134    /////////////////////////////////////////////////////////////////////////
135
136    /**
137     * @see org.apache.avalon.framework.service.ServiceManager#hasService(java.lang.String)
138     */
139    public boolean hasService(String name)
140    {
141        return this.serviceManager.hasService(name);
142    }
143
144    /**
145     * @see org.apache.avalon.framework.service.ServiceManager#lookup(java.lang.String)
146     */
147    public Object lookup(String name) throws ServiceException
148    {
149        return this.serviceManager.lookup(name);
150    }
151
152    /**
153     * @see org.apache.avalon.framework.service.ServiceManager#release(java.lang.Object)
154     */
155    public void release(Object object)
156    {
157        this.serviceManager.release(object);
158    }
159
160    /**
161     * @return the ServiceManager for the container
162     */
163    public ServiceManager getServiceManager()
164    {
165        return this.serviceManager;
166    }
167
168    /**
169     * @return the Parameters for the container
170     */
171    public Parameters getParameters()
172    {
173        return this.parameters;
174    }
175
176    /**
177     * @return the Context for the container
178     */
179    public Context getContext()
180    {
181        return this.context;
182    }
183
184    /**
185     * @see org.apache.fulcrum.yaafi.service.servicemanager.ServiceManagerService#getAvalonLogger()
186     */
187    public Logger getAvalonLogger()
188    {
189        return this.getLogger();
190    }
191}