View Javadoc
1   package org.apache.fulcrum.yaafi.service.servicemanager;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.avalon.framework.activity.Disposable;
23  import org.apache.avalon.framework.context.Context;
24  import org.apache.avalon.framework.context.ContextException;
25  import org.apache.avalon.framework.context.Contextualizable;
26  import org.apache.avalon.framework.context.DefaultContext;
27  import org.apache.avalon.framework.logger.AbstractLogEnabled;
28  import org.apache.avalon.framework.logger.Logger;
29  import org.apache.avalon.framework.parameters.ParameterException;
30  import org.apache.avalon.framework.parameters.Parameterizable;
31  import org.apache.avalon.framework.parameters.Parameters;
32  import org.apache.avalon.framework.service.ServiceException;
33  import org.apache.avalon.framework.service.ServiceManager;
34  import org.apache.avalon.framework.service.Serviceable;
35  
36  
37  /**
38   * This is a sort of "edelhack" to solve the problem of accessing
39   * the Avalon infrastructure without having an instance of the
40   * container. The implementation stores the very first instance
41   * of itself in a static variable which can be accessed using
42   * getInstance().
43   *
44   * This allows access to the various Avalon artifacts.
45   *
46   *  @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
47   */
48  
49  public class ServiceManagerServiceImpl
50      extends AbstractLogEnabled
51      implements ServiceManagerService, Contextualizable, Parameterizable, Serviceable, Disposable
52  {
53      /** The one and only instance */
54      private static ServiceManagerServiceImpl instance;
55  
56      /** Store the ServiceContainer on a per instance base */
57      private ServiceManager serviceManager;
58  
59      /** Store the passed parameters on a per instance base */
60      private Parameters parameters;
61  
62      /** Store the passed parameters on a per instance base */
63      private Context context;
64  
65      /**
66       * Constructor
67       */
68      public ServiceManagerServiceImpl()
69      {
70          setInstance(this);
71      }
72  
73      /**
74       * @return the one and only instance of this class
75       */
76      public static synchronized ServiceManagerService getInstance()
77      {
78          return instance;
79      }
80  
81      /**
82       * Create the one and only instance
83       * @param instance the instance
84       */
85      protected static synchronized void setInstance( ServiceManagerServiceImpl instance )
86      {
87          if( ServiceManagerServiceImpl.instance == null )
88          {
89              ServiceManagerServiceImpl.instance = instance;
90          }
91      }
92  
93      /////////////////////////////////////////////////////////////////////////
94      // Avalon Lifecycle Implementation
95      /////////////////////////////////////////////////////////////////////////
96  
97      /**
98       * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
99       */
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 }