001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.core;
019:
020: import org.apache.avalon.framework.activity.Initializable;
021: import org.apache.avalon.framework.component.Composable;
022: import org.apache.avalon.framework.configuration.Configurable;
023: import org.apache.avalon.framework.configuration.Configuration;
024: import org.apache.avalon.framework.configuration.ConfigurationException;
025: import org.apache.avalon.framework.container.ContainerUtil;
026: import org.apache.avalon.framework.context.Context;
027: import org.apache.avalon.framework.context.ContextException;
028: import org.apache.avalon.framework.context.Contextualizable;
029: import org.apache.avalon.framework.logger.AbstractLogEnabled;
030: import org.apache.avalon.framework.service.ServiceException;
031: import org.apache.avalon.framework.service.ServiceManager;
032: import org.apache.avalon.framework.service.Serviceable;
033: import org.apache.james.services.UsersRepository;
034: import org.apache.james.services.UsersStore;
035:
036: import java.util.HashMap;
037: import java.util.Iterator;
038:
039: /**
040: * Provides a registry of user repositories.
041: *
042: */
043: public class AvalonUsersStore extends AbstractLogEnabled implements
044: Contextualizable, Serviceable, Configurable, Initializable,
045: UsersStore {
046:
047: /**
048: * A mapping of respository identifiers to actual repositories
049: * This mapping is obtained from the component configuration
050: */
051: private HashMap repositories;
052:
053: /**
054: * The Avalon context used by the instance
055: */
056: protected Context context;
057:
058: /**
059: * The Avalon configuration used by the instance
060: */
061: protected Configuration configuration;
062:
063: /**
064: * The Avalon component manager used by the instance
065: */
066: protected ServiceManager manager;
067:
068: /**
069: * @see org.apache.avalon.framework.context.Contextualizable#contextualize(Context)
070: */
071: public void contextualize(final Context context)
072: throws ContextException {
073: this .context = context;
074: }
075:
076: /**
077: * @see org.apache.avalon.framework.service.Serviceable#compose(ServiceManager)
078: */
079: public void service(final ServiceManager manager)
080: throws ServiceException {
081: this .manager = manager;
082: }
083:
084: /**
085: * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
086: */
087: public void configure(final Configuration configuration)
088: throws ConfigurationException {
089: this .configuration = configuration;
090: }
091:
092: /**
093: * @see org.apache.avalon.framework.activity.Initializable#initialize()
094: */
095: public void initialize() throws Exception {
096:
097: getLogger().info("AvalonUsersStore init...");
098: repositories = new HashMap();
099:
100: Configuration[] repConfs = configuration
101: .getChildren("repository");
102: ClassLoader theClassLoader = null;
103: for (int i = 0; i < repConfs.length; i++) {
104: Configuration repConf = repConfs[i];
105: String repName = repConf.getAttribute("name");
106: String 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
117: .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 = new StringBuffer(64).append(
134: "UsersRepository ").append(repName).append(
135: " started.");
136: getLogger().info(logBuffer.toString());
137: }
138: }
139: getLogger().info("AvalonUsersStore ...init");
140: }
141:
142: /**
143: * Get the repository, if any, whose name corresponds to
144: * the argument parameter
145: *
146: * @param name the name of the desired repository
147: *
148: * @return the UsersRepository corresponding to the name parameter
149: */
150: public UsersRepository getRepository(String name) {
151: UsersRepository response = (UsersRepository) repositories
152: .get(name);
153: if ((response == null) && (getLogger().isWarnEnabled())) {
154: getLogger().warn("No users repository called: " + name);
155: }
156: return response;
157: }
158:
159: /**
160: * Yield an <code>Iterator</code> over the set of repository
161: * names managed internally by this store.
162: *
163: * @return an Iterator over the set of repository names
164: * for this store
165: */
166: public Iterator getRepositoryNames() {
167: return this.repositories.keySet().iterator();
168: }
169: }
|