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