View Javadoc

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