1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
39
40
41 public abstract class AbstractAvalonStore
42 extends AbstractLogEnabled
43 implements Serviceable, Configurable, Initializable {
44
45 private HashMap objects;
46
47
48
49
50 protected Configuration configuration;
51
52
53
54
55 protected ServiceManager manager;
56
57
58
59
60 public void service( final ServiceManager manager )
61 throws ServiceException {
62 this.manager = manager;
63 }
64
65
66
67
68 public void configure( final Configuration configuration )
69 throws ConfigurationException {
70 this.configuration = configuration;
71 }
72
73
74
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
123
124
125
126
127
128
129 protected Object getObject(String name) {
130 return objects.get(name);
131 }
132
133
134
135
136
137
138
139
140 protected Iterator getObjectNames() {
141 return this.objects.keySet().iterator();
142 }
143
144
145
146
147
148
149
150
151
152 public abstract Object getClassInstance(ClassLoader loader, String className) throws Exception;
153
154
155
156
157
158
159
160 public abstract Configuration[] getConfigurations(Configuration config);
161
162
163
164
165
166
167 public abstract String getStoreName();
168
169 }