View Javadoc

1   /************************************************************************
2    * Copyright (c) 2000-2006 The Apache Software Foundation.             *
3    * All rights reserved.                                                *
4    * ------------------------------------------------------------------- *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you *
6    * may not use this file except in compliance with the License. You    *
7    * may obtain a copy of the License at:                                *
8    *                                                                     *
9    *     http://www.apache.org/licenses/LICENSE-2.0                      *
10   *                                                                     *
11   * Unless required by applicable law or agreed to in writing, software *
12   * distributed under the License is distributed on an "AS IS" BASIS,   *
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
14   * implied.  See the License for the specific language governing       *
15   * permissions and limitations under the License.                      *
16   ***********************************************************************/
17  
18  package org.apache.james.core;
19  
20  import org.apache.avalon.framework.activity.Initializable;
21  import org.apache.avalon.framework.component.Composable;
22  import org.apache.avalon.framework.configuration.Configurable;
23  import org.apache.avalon.framework.configuration.Configuration;
24  import org.apache.avalon.framework.configuration.ConfigurationException;
25  import org.apache.avalon.framework.container.ContainerUtil;
26  import org.apache.avalon.framework.context.Context;
27  import org.apache.avalon.framework.context.ContextException;
28  import org.apache.avalon.framework.context.Contextualizable;
29  import org.apache.avalon.framework.logger.AbstractLogEnabled;
30  import org.apache.avalon.framework.service.ServiceException;
31  import org.apache.avalon.framework.service.ServiceManager;
32  import org.apache.avalon.framework.service.Serviceable;
33  import org.apache.james.services.UsersRepository;
34  import org.apache.james.services.UsersStore;
35  
36  import java.util.HashMap;
37  import java.util.Iterator;
38  
39  /***
40   * Provides a registry of user repositories.
41   *
42   */
43  public class AvalonUsersStore
44      extends AbstractLogEnabled
45      implements Contextualizable, Serviceable, Configurable, Initializable, UsersStore {
46  
47      /***
48       * A mapping of respository identifiers to actual repositories
49       * This mapping is obtained from the component configuration
50       */
51      private HashMap repositories;
52  
53      /***
54       * The Avalon context used by the instance
55       */
56      protected Context                context;
57  
58      /***
59       * The Avalon configuration used by the instance
60       */
61      protected Configuration          configuration;
62  
63      /***
64       * The Avalon component manager used by the instance
65       */
66      protected ServiceManager       manager;
67  
68      /***
69       * @see org.apache.avalon.framework.context.Contextualizable#contextualize(Context)
70       */
71      public void contextualize(final Context context)
72              throws ContextException {
73          this.context = context;
74      }
75  
76      /***
77       * @see org.apache.avalon.framework.service.Serviceable#compose(ServiceManager)
78       */
79      public void service( final ServiceManager manager )
80          throws ServiceException {
81          this.manager = manager;
82      }
83  
84      /***
85       * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
86       */
87      public void configure( final Configuration configuration )
88          throws ConfigurationException {
89          this.configuration = configuration;
90      }
91  
92      /***
93       * @see org.apache.avalon.framework.activity.Initializable#initialize()
94       */
95      public void initialize()
96          throws Exception {
97  
98          getLogger().info("AvalonUsersStore init...");
99          repositories = new HashMap();
100 
101         Configuration[] repConfs = configuration.getChildren("repository");
102         ClassLoader theClassLoader = null;
103         for ( int i = 0; i < repConfs.length; i++ )
104         {
105             Configuration repConf = repConfs[i];
106             String repName = repConf.getAttribute("name");
107             String repClass = repConf.getAttribute("class");
108 
109             if (getLogger().isDebugEnabled()) {
110                 getLogger().debug("Starting " + repClass);
111             }
112 
113             if (theClassLoader == null) {
114                 theClassLoader = this.getClass().getClassLoader();
115             }
116 
117             UsersRepository rep = (UsersRepository) theClassLoader.loadClass(repClass).newInstance();
118 
119             setupLogger(rep);
120 
121             ContainerUtil.contextualize(rep,context);
122             ContainerUtil.service(rep,manager);
123             if (rep instanceof Composable) {
124                 final String error = "no implementation in place to support Composable";
125                 getLogger().error( error );
126                 throw new IllegalArgumentException( error );
127             }
128             ContainerUtil.configure(rep,repConf);
129             ContainerUtil.initialize(rep);
130             
131             repositories.put(repName, rep);
132             if (getLogger().isInfoEnabled()) {
133                 StringBuffer logBuffer = 
134                     new StringBuffer(64)
135                             .append("UsersRepository ")
136                             .append(repName)
137                             .append(" started.");
138                 getLogger().info(logBuffer.toString());
139             }
140         }
141         getLogger().info("AvalonUsersStore ...init");
142     }
143 
144 
145     /*** 
146      * Get the repository, if any, whose name corresponds to
147      * the argument parameter
148      *
149      * @param name the name of the desired repository
150      *
151      * @return the UsersRepository corresponding to the name parameter
152      */
153     public UsersRepository getRepository(String name) {
154         UsersRepository response = (UsersRepository) repositories.get(name);
155         if ((response == null) && (getLogger().isWarnEnabled())) {
156             getLogger().warn("No users repository called: " + name);
157         }
158         return response;
159     }
160 
161     /*** 
162      * Yield an <code>Iterator</code> over the set of repository
163      * names managed internally by this store.
164      *
165      * @return an Iterator over the set of repository names
166      *         for this store
167      */
168     public Iterator getRepositoryNames() {
169         return this.repositories.keySet().iterator();
170     }
171 }