1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common Development
8 * and Distribution License("CDDL") (collectively, the "License"). You
9 * may not use this file except in compliance with the License. You can obtain
10 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
11 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
12 * language governing permissions and limitations under the License.
13 *
14 * When distributing the software, include this License Header Notice in each
15 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
16 * Sun designates this particular file as subject to the "Classpath" exception
17 * as provided by Sun in the GPL Version 2 section of the License file that
18 * accompanied this code. If applicable, add the following below the License
19 * Header, with the fields enclosed by brackets [] replaced by your own
20 * identifying information: "Portions Copyrighted [year]
21 * [name of copyright owner]"
22 *
23 * Contributor(s):
24 *
25 * If you wish your version of this file to be governed by only the CDDL or
26 * only the GPL Version 2, indicate your decision by adding "[Contributor]
27 * elects to include this software in this distribution under the [CDDL or GPL
28 * Version 2] license." If you don't indicate a single choice of license, a
29 * recipient has the option to distribute your version of this file under
30 * either the CDDL, the GPL Version 2 or to extend the choice of license to
31 * its licensees as provided above. However, if you add GPL Version 2 code
32 * and therefore, elected the GPL Version 2 license, then the option applies
33 * only if the new code is made subject to such option by the copyright
34 * holder.
35 */
36
37 /*
38 * @(#) AbstractLoader.java
39 *
40 * Copyright 2000-2001 by iPlanet/Sun Microsystems, Inc.,
41 * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
42 * All rights reserved.
43 *
44 * This software is the confidential and proprietary information
45 * of iPlanet/Sun Microsystems, Inc. ("Confidential Information").
46 * You shall not disclose such Confidential Information and shall
47 * use it only in accordance with the terms of the license
48 * agreement you entered into with iPlanet/Sun Microsystems.
49 */
50 package com.sun.enterprise.server;
51
52 import java.io.File;
53 import java.util;
54 import java.util.logging.Logger;
55 import java.util.logging.Level;
56 import javax.enterprise.deploy.shared.ModuleType;
57 import javax.naming.NameAlreadyBoundException;
58
59 import com.sun.ejb.Container;
60 import com.sun.ejb.ContainerFactory;
61
62 import com.sun.enterprise.Switch;
63 import com.sun.enterprise.deployment.Application;
64 import com.sun.enterprise.deployment.ConnectorDescriptor;
65 import com.sun.enterprise.deployment.BundleDescriptor;
66 import com.sun.enterprise.deployment.EjbDescriptor;
67 import com.sun.enterprise.deployment.PersistenceUnitDescriptor;
68 import com.sun.enterprise.deployment.backend.DeploymentUtils;
69 import com.sun.enterprise.SecurityManager;
70 import com.sun.enterprise.loader.EJBClassLoader;
71 import com.sun.enterprise.loader.EJBClassPathUtils;
72 import com.sun.enterprise.loader.InstrumentableClassLoader;
73 import com.sun.logging.LogDomains;
74 import com.sun.enterprise.security.factory.FactoryForSecurityManagerFactoryImpl;
75 import com.sun.enterprise.security.factory.SecurityManagerFactory;
76 import com.sun.enterprise.security.factory.FactoryForSecurityManagerFactory;
77
78 import com.sun.enterprise.security.SecurityUtil;
79
80 import com.sun.enterprise.config.ConfigContext;
81 import com.sun.enterprise.config.ConfigException;
82 import com.sun.enterprise.config.serverbeans.Applications;
83 import com.sun.enterprise.config.serverbeans.Domain;
84 import com.sun.enterprise.config.serverbeans.J2eeApplication;
85
86 import com.sun.enterprise.instance.BaseManager;
87
88 import com.sun.enterprise.util.io.FileUtils;
89 import com.sun.enterprise.util.Utility;
90
91 import com.sun.enterprise.connectors;
92 import com.sun.enterprise.server.ondemand.entry;
93
94 //for jsr77
95 import com.sun.enterprise.deployment.Descriptor;
96 import javax.management.MBeanException;
97 import javax.persistence.EntityManagerFactory;
98
99 import com.sun.enterprise.security.application.EJBSecurityManager;
100 import com.sun.enterprise.server.event.ApplicationEvent;
101 import com.sun.enterprise.server.event.EjbContainerEvent;
102 import com.sun.enterprise.server.event.ApplicationLoaderEventNotifier;
103
104 import com.sun.enterprise.admin.monitor.WSMonitorLifeCycleFactory;
105
106 /**
107 * Base loader to load and unload applicaton and stand alone module.
108 *
109 * @author Mahesh Kannan
110 * @author Nazrul Islam
111 * @since JDK1.4
112 */
113 abstract class AbstractLoader implements EntryPoint {
114
115 /** the registration name for this application */
116 protected String id = null;
117
118 /** parent class loader of ejb class loader */
119 ClassLoader parentClassLoader = null;
120
121 /** ejb class loader used by this application */
122 ClassLoader ejbClassLoader = null;
123
124 /** deployment descriptor object for this application */
125 Application application = null;
126
127 protected boolean robFlag = false;
128
129 /** application registry */
130 protected ApplicationRegistry registry = null;
131
132 /** encapsulates application related information */
133 protected BaseManager configManager = null;
134
135 /** logger to log loader messages */
136 static Logger _logger = LogDomains.getLogger(LogDomains.LOADER_LOGGER);
137
138 /** factory for SecurityManagerFactory */
139 private FactoryForSecurityManagerFactory ffsmf = null;
140
141 /*** This variable tells whether to load/unload pools and resources recursively. **/
142
143 protected boolean cascade=false;
144
145 /** Possible values for this:
146 * 0. LOAD_UNSET is before initialization
147 *
148 * For loading:
149 * 1. LOAD_ALL is for loading regular application
150 * 2. LOAD_RAR is for loading the rar part of the embedded rar
151 * 3. LOAD_REST is for loading the rest part of the embedded rar
152 * Embedded rar is loaded in two steps so we can create connector
153 * resources in between.
154 *
155 * For unloading:
156 * 4. UNLOAD_ALL is for unloading regular application
157 * 5. UNLOAD_RAR is for unloading the rar part of the embedded rar
158 * 6. UNLOAD_REST is for unloading the rest part of the embedded rar
159 * Embedded rar is unloaded in two steps so we can delete connector
160 * resources in between.
161 */
162 protected int loadUnloadAction;
163
164 private ApplicationLoaderEventNotifier loaderEventNotifier;
165
166 private ConfigContext dynamicConfigContext;
167
168 /**
169 * Constructor. ApplicationLoader loads an application or
170 * stand alone ejb module.
171 *
172 * @param id the registration name of this application
173 * @param parentClassLoader the parent class loader
174 * @param configMgr the config manager for this VS
175 */
176 AbstractLoader(String id, ClassLoader parentClassLoader,
177 BaseManager configMgr) {
178
179 this.id = id;
180 this.parentClassLoader = parentClassLoader;
181 this.registry = ApplicationRegistry.getInstance();
182 this.configManager = configMgr;
183
184 this.loaderEventNotifier =
185 ApplicationLoaderEventNotifier.getInstance();
186 }
187
188 /**
189 * Initializes this loader by creating the ejb class loader.
190 *
191 * @param classPaths class paths for the ejb class loader
192 */
193 protected void initializeLoader(String[] classPaths, String moduleRoot, ModuleType moduleType) {
194 this.ejbClassLoader = EJBClassPathUtils.createEJBClassLoader(
195 classPaths, moduleRoot, this.id, this.parentClassLoader, moduleType);
196 }
197
198 /**
199 * Initializes this loader to the given classloader
200 */
201 protected void initializeLoader(ClassLoader clazzloader) {
202 this.ejbClassLoader = clazzloader;
203 }
204
205 void setConfigContext(ConfigContext dynamicConfigContext) {
206 this.dynamicConfigContext = dynamicConfigContext;
207 }
208
209 ConfigContext getConfigContext() {
210 return dynamicConfigContext;
211 }
212
213 private boolean verifyModuleDirectory() {
214 String moduleDirectory;
215 try {
216 moduleDirectory = configManager.getLocation(id);
217 } catch (ConfigException ce) {
218 _logger.log(Level.WARNING, "loader.configexception", ce);
219 return false;
220 }
221 boolean result = FileUtils.safeIsDirectory(moduleDirectory);
222 if ( ! result) {
223 /*
224 *Log a warning message without a stack trace.
225 */
226 _logger.log(Level.WARNING, "loader.module_dir_error", new Object[] {id, moduleDirectory});
227 }
228 return result;
229 }
230
231 /**
232 * Loads a deployed application or stand alone module.
233 * Verifies that the module's directory exists and is valid before
234 * trying to load the module, so as to display a useful warning in the
235 * log without displaying a stack trace from later failures (such as
236 * failed attempts to load classes from the module).
237 *
238 * If jsr77 is true then corresponding jsr77 mBeans will be created
239 * during load time.
240 *
241 * @param jsr77 create jsr77 mbeans if true
242 */
243 boolean load(boolean jsr77) {
244 return (verifyModuleDirectory() && doLoad(jsr77));
245 }
246
247 /**
248 * Loads a deployed application or stand alone module.
249 * If jsr77 is true then corresponding jsr77 mBeans will be created
250 * during load time.
251 *
252 * @param jsr77 create jsr77 mbeans if true
253 */
254 abstract boolean doLoad(boolean jsr77);
255
256 /**
257 * Unloads a deployed application or stand alone module.
258 * If jsr77 is true then corresponding jsr77 mBeans will be deleted
259 * during unload time.
260 *
261 * @param jsr77 delete jsr77 mbeans if true
262 */
263 abstract boolean unload(boolean jsr77);
264
265 /**
266 * Create jsr77 root mBean
267 * The root mBean corresponds to either an application
268 * or stand-alone module depending on j2ee type
269 */
270 abstract void createRootMBean () throws MBeanException;
271
272 /**
273 * Delete jsr77 root mBean
274 * The root mBean corresponds to either an application
275 * or stand-alone module depending on j2ee type
276 */
277 abstract void deleteRootMBean () throws MBeanException;
278
279 /**
280 * Create jsr77 mBeans for components within root mBean
281 * The leaf mBeans correspond to the child objects of rootMBean.
282 * For an ejb stand-alone module it will be ejbs
283 * and for a web stand-alone module it will be servlets
284 * and for a connector stand-alone module it will be resource adapters
285 */
286 abstract void createLeafMBeans () throws MBeanException;
287
288 /**
289 * Delete jsr77 mBeans for components within root mBean
290 * The leaf mBeans correspond to the child objects of rootMBean.
291 * For an ejb stand-alone module it will be ejbs
292 * and for a web stand-alone module it will be servlets
293 * and for a connector stand-alone module it will be resource adapters
294 */
295 abstract void deleteLeafMBeans () throws MBeanException;
296
297 /**
298 * Create jsr77 mBean for the leaf object
299 * The leaf mBean correspond to the child object of rootMBean.
300 * For an ejb stand-alone module it will be ejb
301 * and for a web stand-alone module it will be servlet
302 * and for a connector stand-alone module it will be resource adapter
303 *
304 * @param descriptor descriptor object for the leaf element
305 */
306 abstract void createLeafMBean (Descriptor descriptor) throws MBeanException;
307
308 /**
309 * Delete jsr77 mBean for the leaf object
310 * The leaf mBean correspond to the child object of rootMBean.
311 * For an ejb stand-alone module it will be ejb
312 * and for a web stand-alone module it will be servlet
313 * and for a connector stand-alone module it will be resource adapter
314 */
315 abstract void deleteLeafMBean (Descriptor descriptor) throws MBeanException;
316
317 /**
318 * Delete jsr77 mBeans for the root and its' components
319 */
320 abstract void deleteLeafAndRootMBeans () throws MBeanException;
321
322 /**
323 * Set state for jsr77 root mBean
324 * The state can be set for a root mBean and it could be
325 * one of STARTING, RUNNING, STOPPING, STOPPED, or FAILED
326 *
327 * @param state state of the module or application
328 */
329 abstract void setState(int state) throws MBeanException;
330
331 /**
332 * Returns the ejb class loader used by this loader.
333 *
334 * @return the ejb class loader
335 */
336 ClassLoader getClassLoader() {
337 return this.ejbClassLoader;
338 }
339
340 /**
341 * Returns the deployment descriptor object used by this loader.
342 *
343 * @return the deployment descriptor object
344 */
345 Application getApplication() {
346 return this.application;
347 }
348
349 /**
350 * Helps garbage collector by assigning member variables to null.
351 * This is called from unload.
352 *
353 * @see #unload
354 */
355 protected void done() {
356
357 // releases resources (file handles, etc) in the class loader
358 if (this.ejbClassLoader instanceof EJBClassLoader) {
359 ((EJBClassLoader) ejbClassLoader).done();
360 }
361
362 // set connector descriptor classloaders to null
363 Set rars = this.application.getRarDescriptors();
364 for (Iterator itr = rars.iterator(); itr.hasNext();) {
365 ConnectorDescriptor cd = (ConnectorDescriptor) itr.next();
366 cd.setClassLoader(null);
367 }
368
369 this.id = null;
370 this.parentClassLoader = null;
371 this.application = null;
372 this.ejbClassLoader = null;
373 this.registry = null;
374 this.configManager = null;
375 }
376
377 /**
378 * Unloads all the ejb bean containers.
379 * If jsr77 is true then corresponding jsr77 mBeans will be deleted
380 * during unload time.
381 *
382 * @param jsr77 delete jsr77 mBeans if true
383 * @return true if unloaded successfully
384 */
385 protected boolean unloadEjbs(boolean jsr77) {
386
387 boolean result = true;
388
389 if (this.application == null) {
390 result = false;
391 } else {
392
393 // undeploy ejb module
394 Vector beanDescriptors = this.application.getEjbDescriptors();
395 Enumeration e = beanDescriptors.elements();
396
397 while ( e.hasMoreElements() ) {
398 EjbDescriptor nextDescriptor = (EjbDescriptor) e.nextElement();
399
400 notifyEjbEvent(EjbContainerEvent.BEFORE_EJB_CONTAINER_UNLOAD,
401 nextDescriptor);
402
403 // removes the unique id from the registry
404 this.registry.removeUniqueId( nextDescriptor.getUniqueId() );
405
406 try {
407 // removes the bean container from the application registry
408 Container container = (Container) this.registry.
409 removeDescriptor2Container(nextDescriptor);
410 if (container != null) {
411 container.undeploy();
412 }
413
414 } catch (Exception beanEx) {
415 result = false;
416 _logger.log(Level.WARNING,
417 "loader.unexpected_error_while_unloading_ejb_container",
418 beanEx);
419 }
420
421 notifyEjbEvent(EjbContainerEvent.AFTER_EJB_CONTAINER_UNLOAD,
422 nextDescriptor);
423
424 if (jsr77) {
425 try {
426 deleteLeafMBean(nextDescriptor);
427 } catch (MBeanException mbe) {
428 _logger.log(Level.WARNING,
429 "loader.delete_ejb_mbean_exception", mbe);
430 }
431 }
432 }
433
434 if (!application.isVirtual()) {
435 // removes the class loader from the application registry
436 this.registry.removeAppId2ClassLoader(this.id);
437 } else {
438 this.registry.removeModuleId2ClassLoader(this.id);
439 }
440
441 // removes the descriptor object from the application registry
442 this.registry.removeClassLoader2Application(this.ejbClassLoader);
443
444 }
445
446 _logger.log(Level.FINE,"[AbstractLoader] Unload EJB(s) Status: "
447 + result);
448
449 return result;
450 }
451
452 /**
453 * Creates the bean containers for all ejbs.
454 * If jsr77 is true then corresponding jsr77 mBeans will be created
455 * during load time.
456 *
457 * @return true if all modules deployed successfully
458 */
459 protected boolean loadEjbs(boolean jsr77) {
460
461 // application.getApplicationArchivist().getClassLoader();
462 final ClassLoader loader = this.ejbClassLoader;
463
464 if (this.application == null) {
465 return false;
466 }
467
468 if (_logger.isLoggable(Level.FINEST)) {
469 _logger.log(Level.FINEST,
470 "[AbstractLoader] Id: " + this.id +
471 " Setting thread context class loader to: " + loader);
472 }
473
474 /*
475 *5003242 - Save current context class loader as it is changed. Restored in finally block of next try.
476 */
477 ClassLoader savedContextClassLoader = Utility.setContextClassLoader(loader);
478
479 try {
480 if (!application.isVirtual()) {
481 // adds the ejb class loader to the repository
482 this.registry.addAppId2ClassLoader(this.id, this.ejbClassLoader);
483 } else {
484 this.registry.addModuleId2ClassLoader(this.id, this.ejbClassLoader);
485 }
486
487 // adds the deployment descriptor obj to the repository
488 this.registry.addClassLoader2Application(this.ejbClassLoader,
489 this.application);
490
491 HashSet myContainers = new HashSet();
492
493 if (_logger.isLoggable(Level.FINEST)) {
494 _logger.log(Level.FINEST,
495 "[AbstractLoader] Id: " + this.id
496 + " Unique Id: " + this.application.getUniqueId());
497 }
498
499 Vector beanDescriptors = application.getEjbDescriptors();
500
501 // create EJB containers for each EJB
502 Enumeration e = beanDescriptors.elements();
503 ContainerFactory cf = Switch.getSwitch().getContainerFactory();
504
505 while (e.hasMoreElements()) {
506 EjbDescriptor nextDescriptor = (EjbDescriptor) e.nextElement();
507
508 // detects any unique id collisions
509 if ( !this.registry.isUnique( nextDescriptor.getUniqueId() ) ) {
510
511 _logger.log(Level.SEVERE, "loader.duplicate_unique_id",
512 new Object[] {
513 String.valueOf(nextDescriptor.getUniqueId()),
514 nextDescriptor.getName()
515 });
516
517 // unload all the ejbs
518 unloadEjbs(jsr77);
519
520 // abort loading
521 return false;
522 }
523
524 try {
525 if(ffsmf == null){
526 ffsmf = FactoryForSecurityManagerFactoryImpl.getInstance();
527 }
528 SecurityManagerFactory smf = ffsmf.getSecurityManagerFactory("ejb");
529 SecurityManager sm = smf.createSecurityManager(nextDescriptor);
530 // create the bean container
531 Container container = null;
532 try {
533 notifyEjbEvent(EjbContainerEvent.BEFORE_EJB_CONTAINER_LOAD,
534 nextDescriptor);
535
536 container = cf.createContainer(nextDescriptor, loader,
537 sm, dynamicConfigContext);
538
539 notifyEjbEvent(EjbContainerEvent.AFTER_EJB_CONTAINER_LOAD,
540 nextDescriptor);
541 } catch (NameAlreadyBoundException jndiEx) {
542
543 // logs a message
544 _logger.log(Level.SEVERE, "loader.jndi_name_conflict",
545 new Object[] {
546 this.id,
547 nextDescriptor.getJndiName(),
548 nextDescriptor.getName()
549 });
550
551 // logs the actual exception
552 _logger.log(Level.SEVERE,
553 "loader.naming_exception_in_createcontainer", jndiEx);
554
555 // remove all loaded ejbs
556 unloadEjbs(jsr77);
557
558 // abort loading
559 return false;
560 }
561
562 if (_logger.isLoggable(Level.FINE)) {
563 _logger.log(Level.FINE,
564 "Created container with uinque id: "
565 + nextDescriptor.getUniqueId());
566 }
567
568 // adds the container to the repository
569 this.registry.addDescriptor2Container(nextDescriptor,container);
570
571 // adds the container to the temp local table
572 myContainers.add(container);
573
574 } catch (Exception beanEx) {
575 _logger.log(Level.SEVERE,
576 "loader.unexpected_error_while_creating_ejb_container",
577 beanEx);
578
579 // remove all loaded ejbs
580 unloadEjbs(jsr77);
581
582 return false;
583 }
584
585 if (jsr77) {
586 try {
587 createLeafMBean(nextDescriptor);
588 } catch (MBeanException mbe) {
589 _logger.log(Level.WARNING, "loader.create_ejb_mbean_exception", mbe);
590 }
591 }
592 }
593
594 try {
595 Enumeration en = beanDescriptors.elements();
596 // generate policy files for applications with ejbs not otherwise
597 while (en.hasMoreElements()) {
598 EjbDescriptor nextDescriptor = (EjbDescriptor)en.nextElement();
599 SecurityUtil.generatePolicyFile(
600 EJBSecurityManager.getContextID(nextDescriptor));
601 }
602 } catch (Exception ge) {
603 _logger.log(Level.SEVERE,
604 "code.could_not_generate/load_the_policy_files_for_system_app",
605 ge);
606
607 // remove all loaded ejbs
608 unloadEjbs(jsr77);
609
610 return false;
611 }
612
613 // notify ejb containers that application deployment succeeded.
614 Iterator iter = myContainers.iterator();
615 while ( iter.hasNext() ) {
616 Container c = (Container) iter.next();
617 c.doAfterApplicationDeploy();
618 //IASRI 4717059 BEGIN
619 /*
620 if (robFlag) {
621 if ( c instanceof EntityContainer) {
622 ( (EntityContainer) c).setROBNotifier(robNotifier);
623 }
624 }
625 */
626 //IASRI 4717059 END
627 }
628
629 // diagnostics of all the roles and acls
630 com.sun.enterprise.security.Audit.showACL(this.application);
631
632 // log that ejbs were loaded
633 if (this.application.getEjbComponentCount() > 0) {
634 _logger.log(Level.INFO, "loader.ejbs_loaded", this.id);
635 }
636
637 _logger.log(Level.FINE,"[AbstractLoader] loadEjbs completed");
638
639 return true;
640 } finally {
641 /*
642 *5003242 - Restore saved context class loader.
643 */
644 Utility.setContextClassLoader(savedContextClassLoader);
645 }
646
647 }
648
649 // ---- START OF IASRI 4666595 ---------------------------------------
650
651 /**
652 * Unloads all the rar.
653 * If jsr77 is true then corresponding jsr77 mBeans will be deleted
654 * during unload time.
655 *
656 * @param jsr77 delete jsr77 mBeans if true
657 * @return true if unloaded successfully
658 */
659 protected boolean unloadRars(boolean jsr77) {
660 boolean result = true;
661 // undeploy rar module
662 try {
663 Set rarsDescriptors = this.application.getRarDescriptors();
664 ConnectorRuntime connectorRuntime = ConnectorRuntime.getRuntime();
665
666 for (Iterator itr = rarsDescriptors.iterator(); itr.hasNext();) {
667 ConnectorDescriptor cd = (ConnectorDescriptor) itr.next();
668 String rarName = cd.getDeployName();
669 String jndiName = this.id+
670 ConnectorConstants.EMBEDDEDRAR_NAME_DELIMITER+
671 FileUtils.makeFriendlyFilenameNoExtension(rarName);
672 connectorRuntime.destroyActiveResourceAdapter(jndiName,cascade);
673 if (jsr77) {
674 deleteLeafMBean(cd);
675 }
676 }
677 } catch (Exception rarEx) {
678 result = false;
679 _logger.log(Level.WARNING,
680 "loader.application_loader_exception", rarEx);
681 }
682
683 _logger.log(Level.FINE,"[AbstractLoader] Unload RAR(s) Status: "
684 + result);
685
686 return result;
687 }
688
689
690 /**
691 * Load the rars of that application
692 * If jsr77 is true then corresponding jsr77 mBeans will be created
693 * during load time.
694 *
695 * @param jsr77 delete jsr77 mBeans if true
696 * @return true if all modules deployed successfully
697 */
698 protected boolean loadRars(boolean jsr77) {
699
700 try {
701
702 String appLocation = this.configManager.getLocation(this.id);
703 Set rars = application.getRarDescriptors();
704 ConnectorRuntime connectorRuntime = ConnectorRuntime.getRuntime();
705
706 for (Iterator itr = rars.iterator(); itr.hasNext();) {
707 ConnectorDescriptor cd = (ConnectorDescriptor) itr.next();
708 String rarName = cd.getDeployName();
709 String location = DeploymentUtils.getEmbeddedModulePath(
710 appLocation, rarName);
711 String jndiName = this.id+
712 ConnectorConstants.EMBEDDEDRAR_NAME_DELIMITER+
713 FileUtils.makeFriendlyFilenameNoExtension(rarName);
714 connectorRuntime.createActiveResourceAdapter(
715 cd,jndiName,location);
716 if (jsr77) {
717 createLeafMBean(cd);
718 }
719 }
720 // now load resources associated with these embedded RARs
721 try {
722 ConnectorResourcesLoader crl = new ConnectorResourcesLoader();
723 crl.loadEmbeddedRarResources(id,getApplication());
724 } catch (ConfigException cex) {
725 _logger.log(Level.SEVERE, "loader.connector_resource_initialization_error", cex);
726 }
727 } catch (Exception rarEx) {
728 _logger.log(Level.WARNING,
729 "loader.application_loader_exception", rarEx);
730 }
731 _logger.log(Level.FINE,"[AbstractLoader] loadRars completed");
732 return true;
733 }
734
735 protected void setCascade(boolean cascade) {
736 this.cascade=cascade;
737 }
738
739 protected boolean getCascade() {
740 return this.cascade;
741 }
742
743 protected int getLoadUnloadAction(){
744 return this.loadUnloadAction;
745 }
746
747 protected void setLoadUnloadAction(int loadUnloadAction) {
748 this.loadUnloadAction = loadUnloadAction;
749 }
750
751 // ---- END OF IASRI 4666595 ----------------------------------------
752
753 /**
754 * Loads all the Web Service Management MBeans.
755 * If jsr77 is true then corresponding jsr77 mBeans will be created
756 * during load time.
757 *
758 * @param jsr77 create jsr77 mBeans if true
759 * @return true if unloaded successfully
760 */
761 protected boolean loadWebserviceEndpoints(boolean jsr77) {
762
763 boolean result = true;
764
765 if (this.application == null) {
766 result = false;
767 } else {
768
769 Set bundleSet = new HashSet();
770 bundleSet.addAll(this.application.getEjbBundleDescriptors());
771 bundleSet.addAll(this.application.getWebBundleDescriptors());
772
773 // unload web service endpoint
774 Iterator itr = bundleSet.iterator();
775
776 while ( itr.hasNext() ) {
777 BundleDescriptor nextDescriptor = (BundleDescriptor)
778 itr.next();
779
780 try {
781 WSMonitorLifeCycleFactory.getInstance().
782 getWSMonitorLifeCycleProvider().
783 registerWebServiceEndpoints(
784 this.application.getRegistrationName(), nextDescriptor);
785 } catch (Exception e) {
786 _logger.log(Level.WARNING,
787 "loader.register_ws_endpoint_error",
788 this.application.getRegistrationName());
789 _logger.log(Level.WARNING,
790 "loader.register_ws_endpoint_exception", e);
791 result = false;
792 }
793
794 /*
795 if (jsr77) {
796 try {
797 createLeafMBeans(nextDescriptor);
798 } catch (MBeanException mbe) {
799 mbe.printStackTrace();
800 _logger.log(Level.WARNING,
801 "loader.create_ejb_mbean_exception", mbe);
802 }
803 }
804 */
805
806 _logger.log(Level.FINE,
807 "[AbstractLoader] Unload Web Service Endpoint(s) Status: " +
808 result);
809 }
810 }
811
812 return result;
813 }
814
815 /**
816 * Unloads all the Web Service Management MBeans.
817 * If jsr77 is true then corresponding jsr77 mBeans will be deleted
818 * during unload time.
819 *
820 * @param jsr77 delete jsr77 mBeans if true
821 * @return true if unloaded successfully
822 */
823 protected boolean unloadWebserviceEndpoints(boolean jsr77) {
824
825 boolean result = true;
826
827 if (this.application == null) {
828 result = false;
829 } else {
830
831 // unload web service endpoint
832 Set bundleSet = new HashSet();
833 bundleSet.addAll(this.application.getEjbBundleDescriptors());
834 bundleSet.addAll(this.application.getWebBundleDescriptors());
835 Iterator itr = bundleSet.iterator();
836
837 while ( itr.hasNext() ) {
838 BundleDescriptor nextDescriptor = (BundleDescriptor) itr.next();
839
840 try {
841 WSMonitorLifeCycleFactory.getInstance().
842 getWSMonitorLifeCycleProvider().
843 unregisterWebServiceEndpoints(
844 this.application.getRegistrationName(), nextDescriptor);
845 } catch (Exception e) {
846 _logger.log(Level.WARNING,
847 "loader.unregister_ws_endpoint_error",
848 this.application.getRegistrationName());
849 _logger.log(Level.WARNING,
850 "loader.register_ws_endpoint_exception", e);
851 result = false;
852 }
853
854 /*
855 if (jsr77) {
856 try {
857 deleteLeafMBeans(nextDescriptor);
858 } catch (MBeanException mbe) {
859 mbe.printStackTrace();
860 _logger.log(Level.WARNING,
861 "loader.delete_ejb_mbean_exception", mbe);
862 }
863 }
864 */
865
866 _logger.log(Level.FINE,
867 "[AbstractLoader] Unload Web Service Endpoint(s) Status: " +
868 result);
869 }
870 }
871
872 return result;
873 }
874
875 public void generateEntryContext(Object context) {
876 ServerEntryHelper.generateAppLoaderEntryContext((Descriptor) context);
877 }
878
879 protected void notifyAppEvent(int eventType) {
880 ApplicationEvent event = new ApplicationEvent(eventType,
881 getApplication(), getClassLoader(), dynamicConfigContext);
882 generateEntryContext(getApplication());
883 loaderEventNotifier.notifyListeners(event);
884 }
885
886 protected void notifyEjbEvent(int eventType, EjbDescriptor desc) {
887 EjbContainerEvent event = new EjbContainerEvent(eventType,
888 desc, getClassLoader());
889 generateEntryContext(desc);
890 loaderEventNotifier.notifyListeners(event);
891 }
892
893 /**
894 * Loads all the EJB 3.0 persistence entities bundled in this application.
895 */
896 protected boolean loadPersistenceUnits() {
897 try{
898 new PersistenceUnitLoaderImpl().load(new ApplicationInfoImpl());
899 return true;
900 }catch(Exception ge){
901 _logger.log(Level.WARNING, ge.getMessage(), ge);
902 return false;
903 }
904 }
905
906 /**
907 * Unloads all the EJB 3.0 persistence entities bundled in this application.
908 */
909 protected boolean unloadPersistenceUnits() {
910 try{
911 new PersistenceUnitLoaderImpl().unload(new ApplicationInfoImpl());
912 return true;
913 }catch(Exception ge){
914 _logger.log(Level.WARNING, ge.getMessage(), ge);
915 return false;
916 }
917 }
918
919 private class ApplicationInfoImpl
920 implements PersistenceUnitLoader.ApplicationInfo {
921 public Application getApplication() {
922 return application;
923 }
924
925 public InstrumentableClassLoader getClassLoader() {
926 return InstrumentableClassLoader.class.cast(ejbClassLoader);
927 }
928
929 public String getApplicationLocation() {
930 try {
931 return configManager.getLocation(id);
932 } catch (ConfigException e) {
933 throw new RuntimeException(e);
934 }
935 }
936
937 /**
938 * This method is used to find out the precise list of PUs that are
939 * referenced by the components of this application. The components
940 * include EJB and Web components, but not appclients as appclients are
941 * loaded seperately by ACC.
942 *
943 */
944 public Collection<? extends PersistenceUnitDescriptor>
945 getReferencedPUs() {
946 Collection<PersistenceUnitDescriptor> pus =
947 new HashSet<PersistenceUnitDescriptor>();
948 final boolean fineMsgLoggable = _logger.isLoggable(Level.FINE);
949 for (BundleDescriptor bundle : getEjbAndWebBundles()) {
950 if(fineMsgLoggable) {
951 _logger.fine("Finding PUs referenced by module called " + // NOI18N
952 bundle.getModuleDescriptor().getArchiveUri());
953 }
954 pus.addAll(bundle.findReferencedPUs());
955 }
956 if(fineMsgLoggable) {
957 _logger.fine("Total number of PUs referenced by this app is : " // NOI18N
958 + pus.size());
959 }
960 return pus;
961 }
962
963 /**
964 * It returns the EntityManagerFactories that needs to be closed.
965 * It searches in the Application structure to build the list of EMFs.
966 * This implementation does not search appclients.
967 */
968 public Collection<? extends EntityManagerFactory> getEntityManagerFactories() {
969 Collection<EntityManagerFactory> emfs =
970 new HashSet<EntityManagerFactory>();
971 emfs.addAll(application.getEntityManagerFactories()); // ear level PUs
972
973 for (BundleDescriptor bundle : getEjbAndWebBundles()) {
974 emfs.addAll(bundle.getEntityManagerFactories());
975 }
976 // no need to look for EMFs in appclients as they are used in ACC.
977 return emfs;
978 }
979
980 /**
981 * It returns EJBs & Wars only.
982 */
983 private Collection<BundleDescriptor> getEjbAndWebBundles() {
984 Collection<BundleDescriptor> bundles =
985 new HashSet<BundleDescriptor>();
986 bundles.addAll((Collection<BundleDescriptor>)
987 application.getEjbBundleDescriptors());
988 bundles.addAll((Collection<BundleDescriptor>)
989 application.getWebBundleDescriptors());
990 return bundles;
991 }
992 }
993 }