1
2
3
4
5
6
7
8
9
10 package it.imolinfo.jbi4cics.jbi;
11
12 import it.imolinfo.jbi4cics.Logger;
13 import it.imolinfo.jbi4cics.LoggerFactory;
14
15 import javax.jbi.JBIException;
16 import javax.jbi.component.Bootstrap;
17 import javax.jbi.component.InstallationContext;
18 import javax.jbi.management.MBeanNames;
19 import javax.management.MBeanServer;
20 import javax.management.ObjectName;
21
22
23
24
25
26
27
28
29
30
31
32 public class BaseBootstrap implements Bootstrap {
33
34
35
36
37 private static final Logger LOG
38 = LoggerFactory.getLogger(BaseBootstrap.class);
39
40
41
42
43 private static final Messages MESSAGES
44 = Messages.getMessages(BaseBootstrap.class);
45
46
47
48
49
50
51
52 private InstallationContext context;
53
54
55
56
57
58
59 private ObjectName mbeanName;
60
61
62
63
64
65 public BaseBootstrap() {
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80 public final ObjectName getExtensionMBeanName() {
81 return mbeanName;
82 }
83
84 protected Object getExtensionMBean() throws Exception {
85 return null;
86 }
87
88 protected ObjectName createExtensionMBeanName() throws Exception {
89 MBeanNames names = context.getContext().getMBeanNames();
90
91 return names.createCustomComponentMBeanName("bootstrap");
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 public final void init(final InstallationContext installContext)
114 throws JBIException {
115 try {
116 LOG.debug("Initializing bootstrap");
117 context = installContext;
118 doInit();
119 LOG.debug("Bootstrap initialized");
120 } catch (JBIException e) {
121 LOG.error(e.getLocalizedMessage(), e);
122 throw e;
123 } catch (Exception e) {
124 Object[] args = new Object[] { e.getLocalizedMessage() };
125
126 LOG.error("CIC001005_Init_error", args, e);
127 throw new JBIException(
128 MESSAGES.getString("CIC001005_Init_error", args), e);
129 }
130 }
131
132 protected void doInit() throws Exception {
133 Object mbean = getExtensionMBean();
134
135 if (mbean != null) {
136 MBeanServer server;
137
138 mbeanName = createExtensionMBeanName();
139 server = context.getContext().getMBeanServer();
140 if (server == null) {
141 throw new JBIException(
142 MESSAGES.getString("CIC001006_Null_mBeanServer"));
143 }
144 if (server.isRegistered(mbeanName)) {
145 server.unregisterMBean(mbeanName);
146 }
147 server.registerMBean(mbean, mbeanName);
148 }
149 }
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164 public final void cleanUp() throws JBIException {
165 try {
166 LOG.debug("Cleaning up bootstrap");
167 doCleanUp();
168 LOG.debug("Bootstrap cleaned up");
169 } catch (JBIException e) {
170 LOG.error(e.getLocalizedMessage(), e);
171 throw e;
172 } catch (Exception e) {
173 Object[] args = new Object[] { e.getLocalizedMessage() };
174
175 LOG.error("CIC001007_Clean_up_error", args, e);
176 throw new JBIException(
177 MESSAGES.getString("CIC001007_Clean_up_error", args), e);
178 }
179 }
180
181 protected void doCleanUp() throws Exception {
182 if (mbeanName != null) {
183 MBeanServer server = context.getContext().getMBeanServer();
184
185 if (server == null) {
186 throw new JBIException(
187 MESSAGES.getString("CIC001006_Null_mBeanServer"));
188 }
189 if (server.isRegistered(mbeanName)) {
190 server.unregisterMBean(mbeanName);
191 }
192 }
193 }
194
195
196
197
198
199
200
201
202
203
204
205 public final void onInstall() throws JBIException {
206 try {
207 LOG.debug("Bootstrap onInstall");
208 doOnInstall();
209 LOG.debug("Bootstrap onInstall done");
210 } catch (JBIException e) {
211 LOG.error(e.getLocalizedMessage(), e);
212 throw e;
213 } catch (Exception e) {
214 Object[] args = new Object[] { e.getLocalizedMessage() };
215
216 LOG.error("CIC001008_On_install_error", args, e);
217 throw new JBIException(
218 MESSAGES.getString("CIC001008_On_install_error", args), e);
219 }
220 }
221
222 protected void doOnInstall() throws Exception {
223 }
224
225
226
227
228
229
230
231
232
233
234
235 public final void onUninstall() throws JBIException {
236 try {
237 LOG.debug("Bootstrap onUninstall");
238 doOnUninstall();
239 LOG.debug("Bootstrap onUninstall done");
240 } catch (JBIException e) {
241 LOG.error(e.getLocalizedMessage(), e);
242 throw e;
243 } catch (Exception e) {
244 Object[] args = new Object[] { e.getLocalizedMessage() };
245
246 LOG.error("CIC001009_On_uninstall_error", args, e);
247 throw new JBIException(MESSAGES.getString(
248 "CIC001009_On_uninstall_error", args), e);
249 }
250 }
251
252 protected void doOnUninstall() throws Exception {
253 }
254 }