KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > server > StandAloneConnectorModulesManager


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.server;
25
26 import com.sun.enterprise.admin.event.AdminEventListenerException;
27 import com.sun.enterprise.admin.event.AdminEventListenerRegistry;
28 import com.sun.enterprise.admin.event.BaseDeployEvent;
29 import com.sun.enterprise.admin.event.DeployEventListenerHelper;
30 import com.sun.enterprise.admin.event.ModuleDeployEvent;
31 import com.sun.enterprise.admin.event.ModuleDeployEventListener;
32 import com.sun.enterprise.config.ConfigContext;
33 import com.sun.enterprise.config.ConfigException;
34 import com.sun.enterprise.deployment.backend.DeployableObjectType;
35 import com.sun.enterprise.instance.ConnectorModulesManager;
36 import com.sun.enterprise.management.StateManageable;
37 import com.sun.enterprise.util.i18n.StringManager;
38 import com.sun.logging.LogDomains;
39 import java.util.logging.Level JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41 import javax.management.MBeanException JavaDoc;
42
43
44 /**
45  * Connector Manager acts as an listener for the deployment events.
46  *
47  * @author Orit Flint
48  * @since JDK1.4
49  */

50 public class StandAloneConnectorModulesManager extends AbstractManager
51     implements ModuleDeployEventListener {
52     // ConnectorModulesManager configManager;
53
// Hashtable id2loader;
54

55     /**
56             Logger to log core messages
57     */

58     static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.CORE_LOGGER);
59     private static StringManager localStrings = StringManager.getManager(
60             "com.sun.enterprise.server");
61
62     /**
63      * Connector Manager is responsible for loading connector when deploy event is invoked.
64      *
65      * It is assumed that the next two params are set by the Startup Code.
66      *
67      * @param configManager encapsulates application related info
68      */

69     StandAloneConnectorModulesManager(
70         ConnectorModulesManager connectorManager, ClassLoader JavaDoc parentClassLoader) {
71         // this.configManager=configManager;
72
super(parentClassLoader, connectorManager);
73         AdminEventListenerRegistry.addModuleDeployEventListener(this);
74     }
75
76     protected AbstractLoader getLoader(String JavaDoc moduleId) {
77         return new ConnectorModuleLoader(moduleId, this.parentClassLoader,
78             (ConnectorModulesManager) this.configManager);
79     }
80
81
82     /**
83      * Loads the given stand alone connector module.
84      *
85      * @param jsr77 create jsr77 mBeans if true
86      * @param moduleName name of the stand alone connector module
87      * @return true if module was loaded successfully
88      *
89      * @throws AdminEventListenerException if an error while reading data from config
90      */

91     private boolean moduleDeployed(
92             boolean jsr77, ConfigContext config, String JavaDoc moduleName)
93         throws AdminEventListenerException {
94
95         boolean result = false;
96         boolean loadJSR77 = jsr77 || loadJSR77(moduleName, DeployableObjectType.CONN);
97
98         if (_logger.isLoggable(Level.FINEST)) {
99             _logger.log(Level.FINEST,
100                 "In connector moduleDeployed event,name=" + moduleName);
101         }
102
103         AbstractLoader modLoader = getLoader(moduleName);
104         _logger.log(Level.FINEST, "After get loader");
105
106         // create jsr77 mBean
107
if (loadJSR77) {
108             try {
109                 modLoader.createRootMBean();
110             } catch (MBeanException JavaDoc mbe) {
111                 _logger.log(Level.WARNING,
112                 "core.error_while_creating_jsr77_root_mbean",mbe);
113                 throw new AdminEventListenerException(mbe.getMessage());
114             }
115         }
116
117         if (isEnabled(config, moduleName)) {
118             if (_logger.isLoggable(Level.FINEST)) {
119                 _logger.log(Level.FINEST,
120                 "Loading enabled moduleName: " + moduleName);
121             }
122
123             // set jsr77 state STARTING
124
try {
125                 modLoader.setState(StateManageable.STARTING_STATE);
126             } catch (MBeanException JavaDoc mbe) {
127                 _logger.log(Level.WARNING,
128                     "core.error_while_setting_jsr77_state",mbe);
129             }
130
131             boolean retSts = modLoader.load(jsr77);
132
133             if (retSts) {
134                 this.id2loader.put(moduleName, modLoader);
135
136                 // set jsr77 state to RUNNING
137
try {
138                     modLoader.setState(StateManageable.RUNNING_STATE);
139                 } catch (MBeanException JavaDoc mbe) {
140                     _logger.log(Level.WARNING,
141                         "core.error_while_setting_jsr77_state",mbe);
142                 }
143
144                 result = true;
145
146                 if (_logger.isLoggable(Level.FINE)) {
147                     _logger.log(Level.FINE,
148                         "Successfully loaded moduleName: " + moduleName);
149                 }
150             } else {
151                 _logger.log(Level.WARNING,
152                     "core.error_while_loading_connector_module" + " " +
153                     moduleName);
154
155                 // delete root and leaf mBeans if it is a deploy event
156
try {
157                     if (loadJSR77) {
158                         modLoader.deleteLeafAndRootMBeans();
159                     }
160                 } catch (MBeanException JavaDoc mbe) {
161                     _logger.log(Level.WARNING,
162                         "core.error_while_deleting_jsr77_leaf_and_root_mbeans",mbe);
163                     throw new AdminEventListenerException(mbe.getMessage());
164                 }
165
166                 // set jsr77 state to FAILED
167
try {
168                     modLoader.setState(StateManageable.FAILED_STATE);
169                 } catch (MBeanException JavaDoc mbe) {
170                     _logger.log(Level.WARNING,
171                         "core.error_while_setting_jsr77_state",mbe);
172                 }
173
174                 String JavaDoc msg = localStrings.getString(
175                     "connector.error_while_loading_connector_module", moduleName);
176                 throw new AdminEventListenerException(msg);
177             }
178         } else {
179             //setting result to false if module is not enabled.
180
result = false;
181         }
182         return result;
183     }
184
185     /**
186      * Unloads the given stand alone connector module.
187      *
188      * @param jsr77 delete jsr77 mBeans if true
189      * @param moduleName name of the stand alone connector module
190      * @return true if module was unloaded successfully
191      *
192      * @throws ConfigException if an error while reading data from config
193      */

194     private boolean moduleUndeployed(boolean jsr77, String JavaDoc moduleName, boolean cascade) {
195
196         if (_logger.isLoggable(Level.FINEST)) {
197             _logger.log(Level.FINEST,
198                 "In connector moduleUndeployed event,name=" + moduleName);
199         }
200
201         // get loader
202
AbstractLoader modLoader = (ConnectorModuleLoader) this.id2loader.remove(moduleName);
203
204         if (modLoader == null) {
205             return true;
206         }
207         modLoader.setCascade(cascade);
208
209         // set jsr77 state to STOPPING
210
try {
211             modLoader.setState(StateManageable.STOPPING_STATE);
212         } catch (MBeanException JavaDoc mbe) {
213             _logger.log(Level.WARNING,
214                 "core.error_while_setting_jsr77_state",mbe);
215         }
216
217         boolean undeployed = modLoader.unload(jsr77);
218
219         if (undeployed) {
220            if (jsr77) {
221                // delete jsr77 mBean for this ejbModule
222
try {
223                    modLoader.deleteRootMBean();
224                } catch (MBeanException JavaDoc mbe) {
225                    _logger.log(Level.WARNING,
226                            "core.error_while_deleting_jsr77_root_mbean",mbe);
227                }
228            } else {
229                 // set jsr77 state to STOPPED
230
try {
231                     modLoader.setState(StateManageable.STOPPED_STATE);
232                 } catch (MBeanException JavaDoc mbe) {
233                     _logger.log(Level.WARNING,
234                         "core.error_while_setting_jsr77_state",mbe);
235                 }
236            }
237         }
238
239         return undeployed;
240     }
241
242     /**
243      * Invoked when a standalone connector module is deployed.
244      */

245     public synchronized void moduleDeployed(ModuleDeployEvent event)
246         throws AdminEventListenerException {
247
248         if (event.getModuleType().equals(event.TYPE_CONNECTOR)) {
249             DeployEventListenerHelper.getDeployEventListenerHelper().synchronize(event);
250         }
251
252         if(event.getForceDeploy() ) {
253             moduleEnabled(event);
254             return;
255         } else {
256             //As of 8.1 PE/SE/EE, RA Configs will not be deployed in the connector
257
//backend when created, but would be lazily loaded while
258
//the connector module is deployed in the backend.
259
try {
260                 ConnectorResourcesLoader connectorResourcesLoader = null;
261                 ServerContext sc = ApplicationServer.getServerContext();
262                 connectorResourcesLoader= new ConnectorResourcesLoader(sc);
263                 connectorResourcesLoader.loadRAConfigs(event.getModuleName());
264             } catch (ConfigException e) {
265                 _logger.log(Level.WARNING, "" + e.getMessage());
266                 AdminEventListenerException aele = new AdminEventListenerException();
267                 aele.initCause(e);
268                 throw aele;
269             }
270             
271             realDeployed(event);
272         }
273     }
274
275    /**
276     * Performs the deployment of a standalone connector module
277     * This is method is used by moduleDeployed and moduleEnabled
278     * to perform common operations
279     */

280    private void realDeployed(ModuleDeployEvent event) throws AdminEventListenerException {
281         boolean jsr77 = false;
282         
283
284         if (_logger.isLoggable(Level.FINEST)) {
285             _logger.log(Level.FINEST,
286                 "In StandAloneConnectorModulesManager moduleDeployed");
287             _logger.log(Level.FINEST, "ModuleType=" + event.getModuleType());
288         }
289
290         if (event.getModuleType().equals(event.TYPE_CONNECTOR)) {
291             String JavaDoc modID = event.getModuleName();
292
293             if (_logger.isLoggable(Level.FINEST)) {
294                 _logger.log(Level.FINEST, "modID=" + modID);
295             }
296
297             try {
298                 // refreshes the config context with the context from this event
299
this.configManager.refreshConfigContext(event.getConfigContext());
300
301                 // set jsr77 flag
302
// which is used to signify if the event is deploy or undeploy
303
// to create or delete jsr77 mBeans
304
String JavaDoc action = event.getAction();
305                 if ((action.equals(BaseDeployEvent.DEPLOY)) ||
306                         (action.equals(BaseDeployEvent.REDEPLOY))) {
307                     jsr77 = true;
308                 }
309
310                 if (isEnabled(event.getConfigContext(), modID) &&
311                       !moduleDeployed(jsr77, event.getConfigContext(), modID)) {
312                 // throw an exception is load fails
313
String JavaDoc msg = localStrings.getString("connector.connector_deployed_failed",
314                             modID);
315                     throw new AdminEventListenerException(msg);
316                 }
317             } catch (ConfigException ce) {
318                 throw new AdminEventListenerException(ce.getMessage());
319             }
320         }
321    }
322
323     /**
324      * Invoked when a standalone connector module is undeployed.
325      */

326     public synchronized void moduleUndeployed(ModuleDeployEvent event)
327         throws AdminEventListenerException {
328                 //If forceDeploy is true, we need to disable module instead of undeploying it
329
if(event.getForceDeploy() ) {
330                     try {
331                         moduleDisabled(event);
332                     } catch (AdminEventListenerException aele) {
333                         _logger.log(Level.FINEST, "Error while UnDeploying module: " + event.getModuleName());
334                         throw aele;
335                     }
336                 } else {
337                     realUndeployed(event);
338                 }
339     }
340
341    /**
342     * Performs the undeployment of a standalone connector module
343     * This is used by moduleUndeployed and moduleDisabled and
344     * and houses all their common operations
345     */

346    private void realUndeployed(ModuleDeployEvent event) throws AdminEventListenerException {
347         boolean jsr77 = false;
348
349         if (_logger.isLoggable(Level.FINEST)) {
350             _logger.log(Level.FINEST,
351                 "In StandAloneConnectorModulesManager moduleUndeployed");
352         }
353
354         String JavaDoc action = event.getAction();
355
356         if ((action.equals(BaseDeployEvent.UNDEPLOY)) ||
357                 (action.equals(BaseDeployEvent.REDEPLOY))) {
358             jsr77 = true;
359         }
360
361         try {
362             if (event.getModuleType().equals(event.TYPE_CONNECTOR)) {
363                 String JavaDoc modID = event.getModuleName();
364
365                 if (_logger.isLoggable(Level.FINEST)) {
366                     _logger.log(Level.FINEST, "UnDeploying module: " + modID);
367                 }
368
369                 boolean undeployed = false;
370                 try {
371                     ResourcesUtil.getInstance().setConfigContext(event.getConfigContext());
372                     ResourcesUtil.getInstance().setReinitConfigContext(false);
373                 undeployed = moduleUndeployed(jsr77, modID, event.getCascade());
374                 } finally {
375                     ResourcesUtil.getInstance().setReinitConfigContext(true);
376                 }
377                 
378                 if (!undeployed) {
379                     String JavaDoc msg = localStrings.getString("connector.connector_undeployed_failed",
380                             modID);
381                     throw new AdminEventListenerException(msg);
382                 }
383
384                 // refreshes the config context with the context from this event
385
this.configManager.refreshConfigContext(event.getConfigContext());
386             }
387         } catch (ConfigException ce) {
388             throw new AdminEventListenerException(ce.getMessage());
389         }
390     }
391
392     /**
393      * Invoked when a standalone connector module is redeployed.
394      */

395     public synchronized void moduleRedeployed(ModuleDeployEvent event)
396         throws AdminEventListenerException {
397         if (event.getModuleType().equals(event.TYPE_CONNECTOR)) {
398             String JavaDoc modID = event.getModuleName();
399
400             if (_logger.isLoggable(Level.FINEST)) {
401                 _logger.log(Level.FINEST, "ReDeploying module: " + modID);
402             }
403
404             moduleUndeployed(event);
405             moduleDeployed(event);
406         }
407     }
408
409     /**
410      * Invoked when a standalone connector module is enabled.
411      */

412     public synchronized void moduleEnabled(ModuleDeployEvent event)
413         throws AdminEventListenerException {
414         if (_logger.isLoggable(Level.FINEST)) {
415             _logger.log(Level.FINEST,
416                 "In StandAloneConnectorModulesManager moduleEnabled");
417         }
418
419         if (event.getModuleType().equals(event.TYPE_CONNECTOR)) {
420             String JavaDoc modID = event.getModuleName();
421
422             if (!isEnabled(event.getConfigContext(), event.getModuleName())) {
423                 return;
424             }
425
426             if (_logger.isLoggable(Level.FINEST)) {
427                 _logger.log(Level.FINEST, "Module : " + modID + " enabled...");
428             }
429             String JavaDoc appName = event.getModuleName();
430             ServerContext sc = ApplicationServer.getServerContext();
431             ConnectorResourcesLoader connectorResourcesLoader = null;
432             try {
433                 connectorResourcesLoader=
434                       new ConnectorResourcesLoader(sc);
435             }catch(ConfigException ce) {
436                 String JavaDoc msg = localStrings.getString(
437                       "connector.connector_enable_failed",appName);
438                 _logger.log(Level.SEVERE,
439                       "core.failed_to_get_connectorresourcesloader");
440                 throw new AdminEventListenerException(msg);
441             }
442             ResourcesUtil resutil = ResourcesUtil.getInstance();
443             resutil.setConfigContext(event.getConfigContext());
444             resutil.setReinitConfigContext(false);
445            
446             try {
447                 connectorResourcesLoader.loadRAConfigs(appName);
448                 realDeployed(event);
449                 connectorResourcesLoader.load(appName);
450             } catch(AdminEventListenerException ex) {
451                 throw ex;
452             } catch(Throwable JavaDoc th) {
453                 AdminEventListenerException aele =
454                            new AdminEventListenerException();
455                 aele.initCause(th);
456                 throw aele;
457             }finally {
458                 resutil.setReinitConfigContext(true);
459             }
460         }
461     }
462
463     /**
464      * Invoked when a standalone connector module is disabled.
465      */

466     public synchronized void moduleDisabled(ModuleDeployEvent event)
467         throws AdminEventListenerException {
468         if (_logger.isLoggable(Level.FINEST)) {
469             _logger.log(Level.FINEST,
470                 "In StandAloneConnectorModulesManager moduleDisabled");
471         }
472
473         if (event.getModuleType().equals(event.TYPE_CONNECTOR)) {
474             String JavaDoc modID = event.getModuleName();
475
476             if (_logger.isLoggable(Level.FINEST)) {
477                 _logger.log(Level.FINEST, "Module : " + modID + " disabled...");
478             }
479
480             AbstractLoader modLoader =
481                    (ConnectorModuleLoader) this.id2loader.get(modID);
482             event.setCascade(true);
483             realUndeployed(event);
484         }
485     }
486
487     
488 /**
489      * Invoked when a reference is created from a
490      * server instance (or cluster) to a particular module.
491      *
492      * @throws AdminEventListenerException when the listener is unable to
493      * process the event.
494      */

495     public void moduleReferenceAdded(ModuleDeployEvent event)
496             throws AdminEventListenerException {
497                 
498 }
499
500     /**
501      * Invoked when a reference is removed from a
502      * server instance (or cluster) to a particular module.
503      *
504      * @throws AdminEventListenerException when the listener is unable to
505      * process the event.
506      */

507     public void moduleReferenceRemoved(ModuleDeployEvent event)
508             throws AdminEventListenerException {
509                 
510     }
511     
512     
513     // ---- END OF ModuleDeployEventListener METHODS ------------------------
514
}
515
516
517 // END OF IASRI 4666602
518
Popular Tags