View Javadoc

1   /*
2    *  Copyright (c) 2005, 2006 Imola Informatica.
3    *  All rights reserved. This program and the accompanying materials
4    *  are made available under the terms of the LGPL License v2.1
5    *  which accompanies this distribution, and is available at
6    *  http://www.gnu.org/licenses/lgpl.html
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   * Base class for components bootstrap. Due to classloading mechanism in JBI,
24   * Shared Libraries are not available at bootstrap time, so this class should be
25   * copied in your own component and modified directly, instead of inheriting it.
26   *
27   * @author  Guillaume Nodet
28   * @author  <a href="mailto:mcimatti@imolinfo.it">Marco Cimatti</a>
29   * @version $Revision: 1.4 $
30   * @since   3.0
31   */
32  public class BaseBootstrap implements Bootstrap {
33  
34      /**
35       * The logger for this class and its instances.
36       */
37      private static final Logger LOG
38              = LoggerFactory.getLogger(BaseBootstrap.class);
39  
40      /**
41       * The responsible to translate localized messages.
42       */
43      private static final Messages MESSAGES
44              = Messages.getMessages(BaseBootstrap.class);
45  
46      /**
47       * The context containing information from the install command and from the
48       * component installation ZIP file.
49       *
50       * @see  #init(InstallationContext)
51       */
52      private InstallationContext context;
53  
54      /**
55       * The <code>ObjectName</code> of the installer configuration MBean.
56       *
57       * @see  #getExtensionMBeanName()
58       */
59      private ObjectName mbeanName;
60  
61      /**
62       * Creates a new instance of this class. This constructor is required by JBI
63       * specifications.
64       */
65      public BaseBootstrap() {
66      }
67  
68      /**
69       * Obtains the <code>ObjectName</code> of the optional installer
70       * configuration MBean. If none is provided by this component, this method
71       * must return <code>null</code>.
72       * <p>
73       * This method must be called before <code>onInstall()</code> (or
74       * <code>onUninstall()</code>) is called by the JBI implementation.
75       *
76       * @return  the <code>ObjectName</code> of the optional installer
77       *          configuration MBean; returns <code>null</code> if there is no
78       *          such MBean.
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       * Initializes the installation environment for a component. This method is
96       * expected to save any information from the installation context that may
97       * be needed by other methods.
98       * <p>
99       * If the component needs to register an optional installer configuration
100      * MBean, it MUST do so during execution of this method, or the
101      * <code>getExtensionMBean()</code> method.
102      * <p>
103      * This method must be called after the installation root (available through
104      * the <code>installContext</code> parameter) is prepared.
105      *
106      * @param   installContext  the context containing information from the
107      *                          install command and from the component
108      *                          installation ZIP file; this must be
109      *                          non-<code>null</code>.
110      * @throws  JBIException    when there is an error requiring that the
111      *                          installation be terminated.
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      * Cleans up any resources allocated by the bootstrap implementation,
153      * including performing deregistration of the extension MBean, if
154      * applicable.
155      * <p>
156      * This method must be called after the <code>onInstall()</code> or
157      * <code>onUninstall()</code> method is called, whether it succeeds or
158      * fails. It must be called after <code>init()</code> is called, if
159      * <code>init()</code> fails by throwing an exception.
160      *
161      * @throws  JBIException  if the bootstrap cannot clean up allocated
162      *                        resources.
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      * Called at the beginning of installation of a component to perform any
197      * special installation tasks required by the component.
198      * <p>
199      * This method must not be called if the <code>init()</code> method failed
200      * with an exception.
201      *
202      * @throws  JBIException  when there is an error requiring that the
203      *                        installation be terminated.
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      * Called at the beginning of uninstallation of a component to perform any
227      * special uninstallation tasks required by the component.
228      * <p>
229      * This method must not be called if the <code>init()</code> method failed
230      * with an exception.
231      *
232      * @throws  JBIException  when there is an error requiring that the
233      *                        uninstallation be terminated.
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 }