1 package org.codehaus.plexus.component.manager;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.Map;
23 import java.util.Map.Entry;
24
25 import org.codehaus.plexus.MutablePlexusContainer;
26 import org.codehaus.plexus.classworlds.realm.ClassRealm;
27 import org.codehaus.plexus.component.builder.AbstractComponentBuildListener;
28 import org.codehaus.plexus.component.builder.ComponentBuilder;
29 import org.codehaus.plexus.component.builder.XBeanComponentBuilder;
30 import org.codehaus.plexus.component.factory.ComponentInstantiationException;
31 import org.codehaus.plexus.component.repository.ComponentDescriptor;
32 import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
33 import org.codehaus.plexus.lifecycle.LifecycleHandler;
34 import org.codehaus.plexus.logging.Logger;
35 import org.codehaus.plexus.personality.plexus.lifecycle.phase.PhaseExecutionException;
36
37 public abstract class AbstractComponentManager<T> implements ComponentManager<T> {
38 protected final MutablePlexusContainer container;
39
40 private final ClassRealm realm;
41
42 protected final ComponentDescriptor<T> componentDescriptor;
43
44 private final Class<? extends T> type;
45
46 private final String role;
47
48 private final String roleHint;
49
50 protected final ComponentBuilder<T> builder = new XBeanComponentBuilder<T>(this);
51
52 private final LifecycleHandler lifecycleHandler;
53
54
55
56
57
58
59
60 protected final Map<Object, ClassRealm> componentContextRealms =
61 Collections.synchronizedMap(new HashMap<Object, ClassRealm>());
62
63 private int connections;
64
65 private long startId;
66
67 public AbstractComponentManager(
68 MutablePlexusContainer container,
69 LifecycleHandler lifecycleHandler,
70 ComponentDescriptor<T> componentDescriptor,
71 String role,
72 String roleHint) {
73 if (container == null) {
74 throw new NullPointerException("container is null");
75 }
76 this.container = container;
77
78 if (lifecycleHandler == null) {
79 throw new NullPointerException("lifecycleHandler is null");
80 }
81 this.lifecycleHandler = lifecycleHandler;
82
83 if (componentDescriptor == null) {
84 throw new NullPointerException("componentDescriptor is null");
85 }
86 this.componentDescriptor = componentDescriptor;
87
88 if (role == null) {
89 throw new NullPointerException("role is null");
90 }
91 this.role = role;
92
93 if (roleHint == null) {
94 throw new NullPointerException("roleHint is null");
95 }
96 this.roleHint = roleHint;
97
98 this.realm = componentDescriptor.getRealm();
99
100 this.type = componentDescriptor.getImplementationClass();
101 }
102
103 public ComponentDescriptor<T> getComponentDescriptor() {
104 return componentDescriptor;
105 }
106
107 public Class<? extends T> getType() {
108 return type;
109 }
110
111 public ClassRealm getRealm() {
112 return realm;
113 }
114
115 public String getRole() {
116 return role;
117 }
118
119 public String getRoleHint() {
120 return roleHint;
121 }
122
123 public LifecycleHandler getLifecycleHandler() {
124 return lifecycleHandler;
125 }
126
127 protected void incrementConnectionCount() {
128 connections++;
129 }
130
131 protected void decrementConnectionCount() {
132 connections--;
133 }
134
135 protected boolean connected() {
136 return connections > 0;
137 }
138
139 public int getConnections() {
140 return connections;
141 }
142
143
144
145
146
147 public void start(Object component) throws PhaseExecutionException {
148 startId = NEXT_START_ID.getAndIncrement();
149 getLifecycleHandler().start(component, this, componentDescriptor.getRealm());
150 }
151
152
153
154
155 public long getStartId() {
156 return startId;
157 }
158
159 protected T createComponentInstance() throws ComponentInstantiationException, ComponentLifecycleException {
160 return builder.build(componentDescriptor, realm, new AbstractComponentBuildListener() {
161 public void componentCreated(
162 ComponentDescriptor<?> componentDescriptor, Object component, ClassRealm realm) {
163 componentContextRealms.put(component, realm);
164 }
165 });
166 }
167
168 protected void endComponentLifecycle(Object component) throws ComponentLifecycleException {
169 ClassRealm contextRealm = componentContextRealms.remove(component);
170 if (contextRealm == null) {
171 contextRealm = container.getLookupRealm(component);
172 }
173
174 try {
175 getLifecycleHandler().end(component, this, contextRealm);
176 } catch (PhaseExecutionException e) {
177 throw new ComponentLifecycleException("Error ending component lifecycle", e);
178 }
179 }
180
181 public MutablePlexusContainer getContainer() {
182 return container;
183 }
184
185 public Logger getLogger() {
186 return container.getLogger();
187 }
188
189 public void dissociateComponentRealm(ClassRealm realm) throws ComponentLifecycleException {
190 synchronized (componentContextRealms) {
191 for (Iterator<Entry<Object, ClassRealm>> iterator =
192 componentContextRealms.entrySet().iterator();
193 iterator.hasNext(); ) {
194 Entry<Object, ClassRealm> entry = iterator.next();
195 ClassRealm componentRealm = entry.getValue();
196
197 if (componentRealm.getId().equals(realm.getId())) {
198 iterator.remove();
199 }
200 }
201 }
202 }
203 }