KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > core > AvalonUsersStore


1 /***********************************************************************
2  * Copyright (c) 2000-2004 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.Component;
22 import org.apache.avalon.framework.component.ComponentException;
23 import org.apache.avalon.framework.component.ComponentManager;
24 import org.apache.avalon.framework.component.Composable;
25 import org.apache.avalon.framework.configuration.Configurable;
26 import org.apache.avalon.framework.configuration.Configuration;
27 import org.apache.avalon.framework.configuration.ConfigurationException;
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.james.services.UsersRepository;
33 import org.apache.james.services.UsersStore;
34
35 import java.util.HashMap JavaDoc;
36 import java.util.Iterator JavaDoc;
37
38 /**
39  * Provides a registry of user repositories.
40  *
41  */

42 public class AvalonUsersStore
43     extends AbstractLogEnabled
44     implements Component, Contextualizable, Composable, Configurable, Initializable, UsersStore {
45
46     /**
47      * A mapping of respository identifiers to actual repositories
48      * This mapping is obtained from the component configuration
49      */

50     private HashMap JavaDoc repositories;
51
52     /**
53      * The Avalon context used by the instance
54      */

55     protected Context context;
56
57     /**
58      * The Avalon configuration used by the instance
59      */

60     protected Configuration configuration;
61
62     /**
63      * The Avalon component manager used by the instance
64      */

65     protected ComponentManager componentManager;
66
67     /**
68      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(Context)
69      */

70     public void contextualize(final Context context)
71             throws ContextException {
72         this.context = context;
73     }
74
75     /**
76      * @see org.apache.avalon.framework.component.Composable#compose(ComponentManager)
77      */

78     public void compose( final ComponentManager componentManager )
79         throws ComponentException {
80         this.componentManager = componentManager;
81     }
82
83     /**
84      * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
85      */

86     public void configure( final Configuration configuration )
87         throws ConfigurationException {
88         this.configuration = configuration;
89     }
90
91     /**
92      * @see org.apache.avalon.framework.activity.Initializable#initialize()
93      */

94     public void initialize()
95         throws Exception JavaDoc {
96
97         getLogger().info("AvalonUsersStore init...");
98         repositories = new HashMap JavaDoc();
99
100         Configuration[] repConfs = configuration.getChildren("repository");
101         ClassLoader JavaDoc theClassLoader = null;
102         for ( int i = 0; i < repConfs.length; i++ )
103         {
104             Configuration repConf = repConfs[i];
105             String JavaDoc repName = repConf.getAttribute("name");
106             String JavaDoc repClass = repConf.getAttribute("class");
107
108             if (getLogger().isDebugEnabled()) {
109                 getLogger().debug("Starting " + repClass);
110             }
111
112             if (theClassLoader == null) {
113                 theClassLoader = this.getClass().getClassLoader();
114             }
115
116             UsersRepository rep = (UsersRepository) theClassLoader.loadClass(repClass).newInstance();
117
118             setupLogger((Component)rep);
119
120             if (rep instanceof Contextualizable) {
121                 ((Contextualizable) rep).contextualize(context);
122             }
123             if (rep instanceof Composable) {
124                 ((Composable) rep).compose( componentManager );
125             }
126             if (rep instanceof Configurable) {
127                 ((Configurable) rep).configure(repConf);
128             }
129             if (rep instanceof Initializable) {
130                 ((Initializable) rep).initialize();
131             }
132             repositories.put(repName, rep);
133             if (getLogger().isInfoEnabled()) {
134                 StringBuffer JavaDoc logBuffer =
135                     new StringBuffer JavaDoc(64)
136                             .append("UsersRepository ")
137                             .append(repName)
138                             .append(" started.");
139                 getLogger().info(logBuffer.toString());
140             }
141         }
142         getLogger().info("AvalonUsersStore ...init");
143     }
144
145
146     /**
147      * Get the repository, if any, whose name corresponds to
148      * the argument parameter
149      *
150      * @param name the name of the desired repository
151      *
152      * @return the UsersRepository corresponding to the name parameter
153      */

154     public UsersRepository getRepository(String JavaDoc name) {
155         UsersRepository response = (UsersRepository) repositories.get(name);
156         if ((response == null) && (getLogger().isWarnEnabled())) {
157             getLogger().warn("No users repository called: " + name);
158         }
159         return response;
160     }
161
162     /**
163      * Yield an <code>Iterator</code> over the set of repository
164      * names managed internally by this store.
165      *
166      * @return an Iterator over the set of repository names
167      * for this store
168      */

169     public Iterator JavaDoc getRepositoryNames() {
170         return this.repositories.keySet().iterator();
171     }
172 }
173
Popular Tags