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
21
22 package org.apache.james.core;
23
24 import org.apache.avalon.framework.activity.Initializable;
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.container.ContainerUtil;
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
34 import java.util.HashMap;
35 import java.util.Iterator;
36
37 /**
38 * Provides a registry of objects
39 *
40 */
41 public abstract class AbstractAvalonStore
42 extends AbstractLogEnabled
43 implements Serviceable, Configurable, Initializable {
44
45 private HashMap objects;
46
47 /**
48 * The Avalon configuration used by the instance
49 */
50 protected Configuration configuration;
51
52 /**
53 * The Avalon component manager used by the instance
54 */
55 protected ServiceManager manager;
56
57 /**
58 * @see org.apache.avalon.framework.service.Serviceable#service(ServiceManager)
59 */
60 public void service( final ServiceManager manager )
61 throws ServiceException {
62 this.manager = manager;
63 }
64
65 /**
66 * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
67 */
68 public void configure( final Configuration configuration )
69 throws ConfigurationException {
70 this.configuration = configuration;
71 }
72
73 /**
74 * @see org.apache.avalon.framework.activity.Initializable#initialize()
75 */
76 public void initialize()
77 throws Exception {
78
79 getLogger().info(getStoreName() + " init...");
80 objects = new HashMap();
81
82 Configuration[] repConfs = getConfigurations(configuration);
83 ClassLoader theClassLoader = null;
84 for ( int i = 0; i < repConfs.length; i++ )
85 {
86 Configuration repConf = repConfs[i];
87 String repName = repConf.getAttribute("name");
88 String repClass = repConf.getAttribute("class");
89
90 if (getLogger().isDebugEnabled()) {
91 getLogger().debug("Starting " + repClass);
92 }
93
94 if (theClassLoader == null) {
95 theClassLoader = Thread.currentThread().getContextClassLoader();
96 }
97
98 Object object = getClassInstance(theClassLoader,repClass);
99
100 setupLogger(object);
101
102 ContainerUtil.service(object,manager);
103
104 ContainerUtil.configure(object,repConf);
105 ContainerUtil.initialize(object);
106
107
108 objects.put(repName, object);
109 if (getLogger().isInfoEnabled()) {
110 StringBuffer logBuffer =
111 new StringBuffer(64)
112 .append("Store ")
113 .append(repName)
114 .append(" started.");
115 getLogger().info(logBuffer.toString());
116 }
117 }
118 }
119
120
121 /**
122 * Get the object, if any, whose name corresponds to
123 * the argument parameter
124 *
125 * @param name the name of the desired object
126 *
127 * @return the Object corresponding to the name parameter
128 */
129 protected Object getObject(String name) {
130 return objects.get(name);
131 }
132
133 /**
134 * Yield an <code>Iterator</code> over the set of object
135 * names managed internally by this store.
136 *
137 * @return an Iterator over the set of repository names
138 * for this store
139 */
140 protected Iterator getObjectNames() {
141 return this.objects.keySet().iterator();
142 }
143
144 /**
145 * Return new Object of the loader classname
146 *
147 * @param loader the ClassLoader
148 * @param className the classname
149 * @return the loaded Objected
150 * @throws Exception
151 */
152 public abstract Object getClassInstance(ClassLoader loader, String className) throws Exception;
153
154 /**
155 * Return the Store configurations
156 *
157 * @param config the main config
158 * @return configurations
159 */
160 public abstract Configuration[] getConfigurations(Configuration config);
161
162 /**
163 * Return the Store name which should be used for logging
164 *
165 * @return the name
166 */
167 public abstract String getStoreName();
168
169 }