001: /**
002: * $Id: PASModule.java,v 1.19 2007/01/26 03:47:10 portalbld Exp $
003: * Copyright 2004 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.admin.server;
014:
015: import java.io.File;
016: import java.io.FileInputStream;
017: import java.net.InetAddress;
018: import java.util.Collections;
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.LinkedList;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.Properties;
025: import java.util.Set;
026: import java.util.TreeSet;
027: import java.util.logging.Level;
028: import java.util.logging.Logger;
029: import java.lang.reflect.Method;
030:
031: import javax.management.MBeanServer;
032: import javax.management.MBeanServerConnection;
033: import javax.management.ObjectName;
034: import javax.management.remote.JMXConnector;
035: import javax.xml.bind.JAXBContext;
036: import javax.xml.bind.JAXBException;
037: import javax.xml.bind.Unmarshaller;
038:
039: import com.iplanet.am.util.SystemProperties;
040: import com.sun.cacao.DeploymentDescriptor;
041: import com.sun.cacao.Module;
042: import com.sun.cacao.agent.auth.AccessControlDispatcher;
043: import com.sun.cacao.agent.auth.CacaoCallbackHandler;
044: import com.sun.cacao.agent.DispatchInterceptor;
045: import com.sun.cacao.agent.DomainDispatcher;
046: import com.sun.cacao.container.Container;
047: import com.sun.identity.common.Constants;
048: import com.sun.portal.admin.common.DomainAttributes;
049: import com.sun.portal.admin.common.PSConfigConstants;
050: import com.sun.portal.admin.common.context.PortalDomainContext;
051: import com.sun.portal.admin.common.context.PortalDomainContextFactory;
052: import com.sun.portal.admin.common.context.PSConfigContext;
053: import com.sun.portal.admin.common.context.PSConfigContextImpl;
054: import com.sun.portal.admin.common.jaxb.PortalResource;
055: import com.sun.portal.admin.common.util.AdminUtil;
056: import com.sun.portal.admin.server.mbeans.PSResource;
057: import com.sun.portal.util.Platform;
058: import com.sun.jdmk.interceptor.MBeanServerInterceptor;
059: import com.sun.jdmk.JdmkMBeanServer;
060:
061: /**
062: * This class implements the Portal Admin Server as a cacao module.
063: */
064: public class PASModule extends Module implements PASModuleMBean,
065: PSConfigConstants {
066:
067: /**
068: * The logger used by the Portal Admin Server.
069: */
070: private static Logger logger = PASLogger.getLogger();
071:
072: // File separator.
073: public static final String FS = Platform.fs;
074:
075: // Signature of the reloadMBeans operation.
076: public static final String[] SIGNATURE = { String.class.getName(),
077: String.class.getName(), List.class.getName() };
078:
079: // ObjectName of the Portal Admin Server cacao module.
080: public static ObjectName myObjectName = null;
081:
082: protected Unmarshaller unmarshaller = null;
083: protected static List portalResources = null;
084: protected static Map classNamesCache = new HashMap();
085: protected static Map configContexts = new HashMap();
086: protected String mechanismName;
087: protected static Map childrenCache = new HashMap();
088: private DomainDispatcher portalDomainDispatcher = null;
089: // Make Access Manager use JCE instead of JSS and enable client polling.
090: static {
091: Properties properties = new Properties();
092:
093: properties.setProperty(
094: Constants.SECURITY_SECURE_RANDOM_FACTORY_IMPL,
095: "com.iplanet.am.util.SecureRandomFactoryImpl");
096:
097: properties.setProperty(
098: Constants.SECURITY_SSL_SOCKET_FACTORY_IMPL,
099: "netscape.ldap.factory.JSSESocketFactory");
100:
101: properties.setProperty(Constants.SECURITY_ENCRYPTOR,
102: "com.iplanet.services.util.JCEEncryption");
103:
104: properties.setProperty(
105: Constants.AM_SESSION_CLIENT_POLLING_ENABLED,
106: Boolean.TRUE.toString());
107:
108: SystemProperties.initializeProperties(properties);
109: }
110:
111: /**
112: * Creates a new <code>PASModule</code> instance.
113: *
114: * @param descriptor the deployment descriptor of the PAS module.
115: */
116: public PASModule(DeploymentDescriptor descriptor) {
117: super (descriptor);
118: myObjectName = getObjectName();
119: //logger.log(Level.FINEST, "PASModule object name: " + myObjectName);
120: logger.log(Level.FINEST, "PSAD_CSPAS0201", myObjectName);
121: }
122:
123: public void reloadMBeans(String domainID, String type,
124: List parentPath) {
125: //String message = null;
126: PSConfigContext cc = (PSConfigContext) configContexts
127: .get(domainID);
128: PortalDomainContext pdc = null;
129:
130: try {
131: pdc = PortalDomainContextFactory
132: .getPortalDomainContext(domainID);
133: } catch (Exception e) {
134: //message = "Invalid portal domain ID: " + domainID;
135: //logger.log(Level.WARNING, message, e);
136: logger.log(Level.WARNING, "PSAD_CSPAS0202", domainID);
137: logger.log(Level.WARNING, "PSAD_CSPAS0000", e);
138: return;
139: }
140:
141: unregisterResourceMBeans(domainID, type, parentPath);
142: registerResourceMBeans(cc, pdc, type, parentPath);
143: }
144:
145: /**
146: * Invokes the <code>reloadMBeans</code> operation on all the
147: * running PAS instances in the portal domain with the given
148: * context to reload the MBeans of the given type under a parent
149: * with the given path.
150: *
151: * @param pdc context of the portal domain where MBeans are to
152: * be reloaded.
153: * @param type the type of the MBeans to be reloaded.
154: * @param parentPath the path of the parent of the MBeans to be reloaded.
155: */
156: public static void reloadMBeans(PortalDomainContext pdc,
157: String type, List parentPath) {
158:
159: //String message = null;
160: String domainID = pdc.getID();
161: Set hosts = null;
162:
163: try {
164: hosts = pdc.getAttributeValues(
165: AdminUtil.PORTAL_DOMAIN_MBEAN_TYPE, Collections
166: .singletonList(domainID),
167: DomainAttributes.HOST);
168: } catch (Exception e) {
169: //message = "Can't read the hosts of all running PAS "
170: // + "instances in portal domain " + domainID;
171:
172: //logger.log(Level.WARNING, message, e);
173: logger.log(Level.WARNING, "PSAD_CSPAS0203", domainID);
174: logger.log(Level.WARNING, "PSAD_CSPAS0000", e);
175: return;
176: }
177:
178: for (Iterator i = hosts.iterator(); i.hasNext();) {
179: String host = (String) i.next();
180: JMXConnector connector = null;
181:
182: try {
183: connector = AdminServerUtil.getJMXConnector(host);
184: MBeanServerConnection s = connector
185: .getMBeanServerConnection();
186: Object[] params = { domainID, type, parentPath };
187: s.invoke(myObjectName, "reloadMBeans", params,
188: SIGNATURE);
189: } catch (Exception e) {
190: //message = "Error reloading MBeans on host " + host;
191: //logger.log(Level.WARNING, message, e);
192: logger.log(Level.WARNING, "PSAD_CSPAS0204", host);
193: logger.log(Level.WARNING, "PSAD_CSPAS0000", e);
194: } finally {
195: if (connector != null) {
196: try {
197: connector.close();
198: } catch (Exception e) {
199: //message = "Can't close JMX connector to host " + host;
200: //logger.log(Level.WARNING, message, e);
201: logger.log(Level.WARNING, "PSAD_CSPAS0205",
202: host);
203: logger.log(Level.WARNING, "PSAD_CSPAS0000", e);
204: }
205: }
206: }
207: }
208: }
209:
210: /**
211: * Returns the MBean server where all the portal admin MBeans run on.
212: *
213: * @return a MBeanServer object.
214: */
215: public static MBeanServer getMBeanServer() {
216: return Container.getMbs();
217: }
218:
219: /**
220: * This method first reads all the portal domains located in the
221: * portal domains directory and registers the portal MBeans to the
222: * MBean server. It then registers an access controller to cacao
223: * to control access to those portal MBeans. Finally, it
224: * registers an authentication mechanism to cacao to authenticate
225: * PAS users using Access Manager.
226: */
227: protected void start() {
228: String message = null;
229: //plug-in the portal logging interceptor
230: try {
231: portalDomainDispatcher = new PortalDomainDispatcher(
232: getMBeanServer());
233: try {
234: //CACAO v1.x
235: DispatchInterceptor dispatcher = (DispatchInterceptor) ((JdmkMBeanServer) getMBeanServer())
236: .getMBeanServerInterceptor();
237: dispatcher.addDomainDispatcher(portalDomainDispatcher);
238: } catch (ClassCastException e) {
239: //CACAO v2.x
240: Method methodGetDispatchInterceptor = DispatchInterceptor.class
241: .getMethod(
242: "getDispatchInterceptor",
243: new Class[] { javax.management.MBeanServer.class });
244: ((DispatchInterceptor) methodGetDispatchInterceptor
245: .invoke(null, new Object[] { getMBeanServer() }))
246: .addDomainDispatcher(portalDomainDispatcher);
247: }
248: } catch (Exception e) {
249: //message="Could not plug-in portal logging interceptor!";
250: //logger.log(Level.WARNING, message, e);
251: logger.log(Level.WARNING, "PSAD_CSPAS0206", e);
252: }
253:
254: if (unmarshaller == null) {
255: try {
256: JAXBContext jaxbContext = JAXBContext
257: .newInstance("com.sun.portal.admin.common.jaxb");
258:
259: unmarshaller = jaxbContext.createUnmarshaller();
260: } catch (JAXBException e) {
261: message = "Error creating a JAXB unmarshaller.";
262: //logger.log(Level.SEVERE, message, e);
263: logger.log(Level.SEVERE, "PSAD_CSPAS0207", e);
264: throw new RuntimeException(message);
265: }
266: }
267:
268: Properties parameters = getDeploymentDescriptor()
269: .getParameters();
270: String psconfigDir = parameters.getProperty(PS_CONFIG_DIR);
271:
272: if (psconfigDir == null) {
273: message = PS_CONFIG_DIR + " is missing in module.xml file.";
274: //logger.log(Level.SEVERE, message);
275: logger.log(Level.SEVERE, "PSAD_CSPAS0208", PS_CONFIG_DIR);
276: throw new RuntimeException(message);
277: }
278:
279: File[] files = getFilesInDirectory(psconfigDir + FS + DOMAINS);
280:
281: for (int i = 0; i < files.length; i++) {
282: if (!files[i].isDirectory()) {
283: // Each portal domain is denoted by a directory.
284: continue;
285: }
286:
287: String domainID = files[i].getName();
288: //logger.log(Level.FINEST, "Domain is " + domainID);
289: logger.log(Level.FINEST, "PSAD_CSPAS0209", domainID);
290:
291: try {
292: PSConfigContext cc = new PSConfigContextImpl(domainID);
293: if (portalResources == null) {
294: loadPortalResources(cc.getPSBaseDir());
295: }
296:
297: File domainConfigFile = new File(files[i],
298: DOMAIN_CONFIG_FILE);
299:
300: // There must be a PortalDomainConfig.properties file in there.
301: if (!domainConfigFile.isFile()
302: || !domainConfigFile.canRead()) {
303: //message = "Error reading file: " + domainConfigFile;
304: //logger.log(Level.WARNING, message);
305: logger.log(Level.WARNING, "PSAD_CSPAS0210",
306: domainConfigFile);
307: continue;
308: }
309:
310: PortalDomainContextFactory.loadPortalDomainContext(
311: domainID, cc, portalResources, logger);
312: configContexts.put(domainID, cc);
313: } catch (Exception e) {
314: //logger.log(Level.WARNING, "Error loading " + domainID, e);
315: logger.log(Level.WARNING, "PSAD_CSPAS0211", domainID);
316: logger.log(Level.WARNING, "PSAD_CSPAS0000", e);
317: continue;
318: }
319: }
320:
321: // Register PAS access controller to cacao.
322: LinkedList l = new LinkedList();
323: PASAccessController pasAccessController = new PASAccessController();
324: l.add(pasAccessController);
325: AccessControlDispatcher.getPrincipalMap().put(
326: PASPrincipal.class, l);
327:
328: // Register PAS authentication mechanism to cacao.
329: PASMechanism pasMechanism = new PASMechanism();
330: mechanismName = pasMechanism.getName();
331: CacaoCallbackHandler.getMechanisms().put(mechanismName,
332: pasMechanism);
333:
334: // Add listener to receive JMX connection notifications.
335: addNotificationListener(pasMechanism);
336:
337: // Register the portal MBeans to the MBean server.
338: registerPSMBeans();
339: }
340:
341: /**
342: * Stops the Portal Admin Server cacao module.
343: *
344: */
345: protected void stop() {
346: // Unregister the portal MBeans from the MBean server.
347: unregisterPSMBeans();
348:
349: // Remove JMX connection notification listener.
350: PASMechanism pasMechanism = (PASMechanism) CacaoCallbackHandler
351: .getMechanisms().get(mechanismName);
352: removeNotificationListener(pasMechanism);
353:
354: // Unregister PAS authentication mechanism from cacao.
355: CacaoCallbackHandler.getMechanisms().remove(mechanismName);
356: mechanismName = null;
357:
358: // Unregister PAS access controller from cacao.
359: AccessControlDispatcher.getPrincipalMap().remove(
360: PASPrincipal.class);
361:
362: // Unload all contexts.
363: childrenCache.clear();
364: configContexts.clear();
365: PortalDomainContextFactory.unloadAllPortalDomainContexts();
366: classNamesCache.clear();
367: portalResources = null;
368:
369: //un-plug the portal domain dispatcher
370: try {
371: try {
372: //CACAO v1.x
373: DispatchInterceptor dispatcher = (DispatchInterceptor) ((JdmkMBeanServer) getMBeanServer())
374: .getMBeanServerInterceptor();
375: dispatcher
376: .removeDomainDispatcher(portalDomainDispatcher);
377: } catch (ClassCastException e) {
378: //CACAO v2.x
379: Method methodGetDispatchInterceptor = DispatchInterceptor.class
380: .getMethod(
381: "getDispatchInterceptor",
382: new Class[] { javax.management.MBeanServer.class });
383: ((DispatchInterceptor) methodGetDispatchInterceptor
384: .invoke(null, new Object[] { getMBeanServer() }))
385: .removeDomainDispatcher(portalDomainDispatcher);
386: }
387: } catch (Exception e) {
388: logger.log(Level.WARNING, "PSAD_CSPAS0233", e);
389: }
390: }
391:
392: /**
393: * Returns the files in the given directory. The returned list
394: * may include files and directories.
395: *
396: * @param directory the directory to list files.
397: * @return an array of File objects.
398: */
399: protected File[] getFilesInDirectory(String directory) {
400: String message = null;
401: //logger.log(Level.FINEST, "Trying to read directory " + directory);
402: logger.log(Level.FINEST, "PSAD_CSPAS0212", directory);
403: File dir = new File(directory);
404:
405: try {
406: if (!dir.exists()) {
407: message = directory + " doesn't exist.";
408: //logger.log(Level.SEVERE, message);
409: logger.log(Level.SEVERE, "PSAD_CSPAS0213", directory);
410: throw new RuntimeException(message);
411: }
412:
413: if (!dir.isDirectory()) {
414: message = directory + " isn't a directory.";
415: //logger.log(Level.SEVERE, message);
416: logger.log(Level.SEVERE, "PSAD_CSPAS0214", directory);
417: throw new RuntimeException(message);
418: }
419:
420: if (!dir.canRead()) {
421: message = "Cannot read the directory " + directory;
422: //logger.log(Level.SEVERE, message);
423: logger.log(Level.SEVERE, "PSAD_CSPAS0215", directory);
424: throw new RuntimeException(message);
425: }
426:
427: File[] files = dir.listFiles();
428:
429: if (files == null) {
430: message = directory + " isn't a directory.";
431: //logger.log(Level.SEVERE, message);
432: logger.log(Level.SEVERE, "PSAD_CSPAS0214", directory);
433: throw new RuntimeException(message);
434: }
435:
436: return files;
437: } catch (SecurityException e) {
438: //logger.log(Level.SEVERE, e.getMessage(), e);
439: logger.log(Level.SEVERE, "PSAD_CSPAS0215", directory);
440: logger.log(Level.SEVERE, "PSAD_CSPAS0000", e);
441: throw e;
442: }
443: }
444:
445: /**
446: * Loads the portal resources.
447: *
448: * @param psBaseDir the Portal Server base installation directory.
449: */
450: protected void loadPortalResources(String psBaseDir) {
451: portalResources = new LinkedList();
452: classNamesCache = new HashMap();
453: String mbeansDir = psBaseDir + FS + "admin" + FS + "mbeans";
454: File[] files = getFilesInDirectory(mbeansDir);
455:
456: for (int i = 0; i < files.length; i++) {
457: String fileName = files[i].getName();
458:
459: if (!fileName.endsWith("xml")) {
460: // Not a portal resource MBean XML file.
461: continue;
462: }
463:
464: //logger.log(Level.FINEST, "Loading resource file: " + fileName);
465: logger.log(Level.FINEST, "PSAD_CSPAS0216", fileName);
466:
467: try {
468: FileInputStream fis = new FileInputStream(files[i]);
469: PortalResource r = (PortalResource) unmarshaller
470: .unmarshal(fis);
471: String type = r.getType();
472: //logger.log(Level.FINEST, "Portal resource: " + type);
473: logger.log(Level.FINEST, "PSAD_CSPAS0217", type);
474: portalResources.add(r);
475: classNamesCache.put(type, r.getClassName());
476: } catch (Exception e) {
477: //String message = "Can't load resource file: " + fileName;
478: //logger.log(Level.WARNING, message, e);
479: logger.log(Level.WARNING, "PSAD_CSPAS0218", fileName);
480: logger.log(Level.WARNING, "PSAD_CSPAS0000", e);
481: }
482: }
483: }
484:
485: /**
486: * Loads the portal domain context with the given ID and registers
487: * all the Portal Server MBeans in it.
488: */
489: protected static void initPortalDomainContext(String domainID)
490: throws Exception {
491:
492: PSConfigContext cc = new PSConfigContextImpl(domainID);
493:
494: PortalDomainContextFactory.loadPortalDomainContext(domainID,
495: cc, portalResources, logger);
496:
497: configContexts.put(domainID, cc);
498: registerPSMBeans(domainID);
499: }
500:
501: /**
502: * Instantiates and registers all the Portal Server MBeans in the
503: * portal domain with the given ID.
504: */
505: protected static void registerPSMBeans(String domainID) {
506: //logger.log(Level.FINEST, "domainID: " + domainID);
507: logger.log(Level.FINEST, "PSAD_CSPAS0219", domainID);
508: PSConfigContext cc = (PSConfigContext) configContexts
509: .get(domainID);
510:
511: PortalDomainContext pdc = PortalDomainContextFactory
512: .getPortalDomainContext(domainID);
513:
514: for (int j = 0; j < portalResources.size(); j++) {
515: PortalResource r = (PortalResource) portalResources.get(j);
516: String type = r.getType();
517:
518: if (AdminUtil.getParentType(type) == null) {
519: // Top level PS MBean type, e.g. PortalDomain.
520: registerResourceMBeans(cc, pdc, type,
521: Collections.EMPTY_LIST);
522: }
523: }
524:
525: registerPASInstanceToDomain(pdc);
526: }
527:
528: /**
529: * Instantiates and registers all the Portal Server MBeans.
530: */
531: protected void registerPSMBeans() {
532: Set domainIDs = PortalDomainContextFactory
533: .getAllPortalDomainIDs();
534:
535: for (Iterator i = domainIDs.iterator(); i.hasNext();) {
536: registerPSMBeans((String) i.next());
537: }
538: }
539:
540: /**
541: * Unregisters all the Portal Server MBeans.
542: */
543: protected void unregisterPSMBeans() {
544: Set domainIDs = PortalDomainContextFactory
545: .getAllPortalDomainIDs();
546:
547: for (Iterator i = domainIDs.iterator(); i.hasNext();) {
548: String domainID = (String) i.next();
549: unregisterPASInstanceFromDomain(domainID);
550:
551: for (int j = 0; j < portalResources.size(); j++) {
552: PortalResource r = (PortalResource) portalResources
553: .get(j);
554: String type = r.getType();
555:
556: if (AdminUtil.getParentType(type) == null) {
557: // Top level PS MBean type, e.g. PortalDomain.
558: unregisterResourceMBeans(domainID, type,
559: Collections.EMPTY_LIST);
560: }
561: }
562: }
563: }
564:
565: /**
566: * Registers the portal MBeans with the given type under a parent
567: * MBeans with the given path in the given portal domain context.
568: *
569: * @param cc the Portal Server configuration context.
570: * @param pdc the portal domain context.
571: * @param type the type of the portal MBeans to be registered.
572: * @param parentPath the path of the parent MBean.
573: */
574: protected static void registerResourceMBeans(PSConfigContext cc,
575: PortalDomainContext pdc, String type, List parentPath) {
576:
577: String className = (String) classNamesCache.get(type);
578:
579: if (className == null) {
580: //logger.log(Level.WARNING, "No such PS MBean type: " + type);
581: logger.log(Level.WARNING, "PSAD_CSPAS0220", type);
582: return;
583: }
584:
585: Set resourceIDs = null;
586:
587: try {
588: resourceIDs = pdc.getResourceIDs(type, parentPath);
589: } catch (Exception e) {
590: //logger.log(Level.WARNING, "Can't read resource IDs for " + type, e);
591: logger.log(Level.WARNING, "PSAD_CSPAS0221", type);
592: logger.log(Level.WARNING, "PSAD_CSPAS0000", e);
593: return;
594: }
595:
596: for (Iterator i = resourceIDs.iterator(); i.hasNext();) {
597: String resourceID = (String) i.next();
598:
599: if (type.equals(AdminUtil.PORTAL_DOMAIN_MBEAN_TYPE)
600: && !resourceID.equalsIgnoreCase(pdc.getID())) {
601:
602: // Another portal domain's resources.
603: continue;
604: }
605:
606: List path = AdminUtil.getChildPath(parentPath, resourceID);
607:
608: try {
609: PSResource resource = (PSResource) Class.forName(
610: className).newInstance();
611:
612: resource.init(cc, pdc, Collections
613: .unmodifiableList(path));
614:
615: ObjectName objectName = AdminUtil
616: .getResourceMBeanObjectName(type, path);
617:
618: getMBeanServer().registerMBean(resource, objectName);
619: //logger.log(Level.FINEST, "Registered: " + objectName);
620: logger.log(Level.FINEST, "PSAD_CSPAS0222", objectName);
621: } catch (Throwable e) {
622: //logger.log(Level.WARNING, "Can't register MBean: " + type, e);
623: logger.log(Level.WARNING, "PSAD_CSPAS0223", type);
624: logger.log(Level.WARNING, "PSAD_CSPAS0000", e);
625: continue;
626: }
627:
628: Set children = getChildren(type);
629:
630: for (Iterator j = children.iterator(); j.hasNext();) {
631: registerResourceMBeans(cc, pdc, (String) j.next(), path);
632: }
633: }
634: }
635:
636: /**
637: * Unregisters the portal MBeans with the given type under a parent
638: * MBeans with the given path in the portal domain with the given ID.
639: *
640: * @param domainID ID of the portal domain.
641: * @param type the type of the portal MBeans to be unregistered.
642: * @param parentPath the path of the parent MBean.
643: */
644: protected void unregisterResourceMBeans(String domainID,
645: String type, List parentPath) {
646:
647: //String message = null;
648: ObjectName pattern = null;
649:
650: try {
651: pattern = AdminUtil.getResourcesPattern(type, parentPath);
652: } catch (Exception e) {
653: //message = "Can't get resources pattern for type: " + type;
654: //logger.log(Level.WARNING, message, e);
655: logger.log(Level.WARNING, "PSAD_CSPAS0224", type);
656: logger.log(Level.WARNING, "PSAD_CSPAS0000", e);
657: return;
658: }
659:
660: Set objectNames = getMbs().queryNames(pattern, null);
661:
662: for (Iterator i = objectNames.iterator(); i.hasNext();) {
663: ObjectName objectName = (ObjectName) i.next();
664: String resourceID = objectName
665: .getKeyProperty(AdminUtil.KEY_ID);
666:
667: if (type.equals(AdminUtil.PORTAL_DOMAIN_MBEAN_TYPE)
668: && !resourceID.equalsIgnoreCase(domainID)) {
669:
670: // Another portal domain's resources.
671: continue;
672: }
673:
674: List path = AdminUtil.getChildPath(parentPath, resourceID);
675: Set children = getChildren(type);
676:
677: for (Iterator j = children.iterator(); j.hasNext();) {
678: unregisterResourceMBeans(domainID, (String) j.next(),
679: path);
680: }
681:
682: try {
683: getMbs().unregisterMBean(objectName);
684: //logger.log(Level.FINEST, "Unregistered: " + objectName);
685: logger.log(Level.FINEST, "PSAD_CSPAS0225", objectName);
686: } catch (Exception e) {
687: //message = "Can't unregister " + objectName;
688: //logger.log(Level.WARNING, message, e);
689: logger.log(Level.WARNING, "PSAD_CSPAS0226", objectName);
690: logger.log(Level.WARNING, "PSAD_CSPAS0000", e);
691: }
692: }
693: }
694:
695: /**
696: * Returns the immediate children of the given type.
697: *
698: * @param type the type of a Portal Server resource.
699: * @return a Set of Strings.
700: */
701: protected static Set getChildren(String type) {
702: Set children = (Set) childrenCache.get(type);
703:
704: if (children == null) {
705: children = new TreeSet();
706:
707: for (int i = 0; i < portalResources.size(); i++) {
708: PortalResource r = (PortalResource) portalResources
709: .get(i);
710: String t = r.getType();
711:
712: if (type.equals(AdminUtil.getParentType(t))) {
713: children.add(t);
714: }
715: }
716:
717: childrenCache.put(type, children);
718: }
719:
720: return children;
721: }
722:
723: /**
724: * Registers this PAS instance to the portal domain with the given
725: * context by adding the name of the local host to the
726: * <code>host</code> attribute of the portal domain.
727: *
728: * @param pdc context of the portal domain to register this PAS instance.
729: */
730: protected static void registerPASInstanceToDomain(
731: PortalDomainContext pdc) {
732: String domainID = pdc.getID();
733: List path = Collections.singletonList(domainID);
734:
735: try {
736: TreeSet hosts = new TreeSet(pdc.getAttributeValues(
737: AdminUtil.PORTAL_DOMAIN_MBEAN_TYPE, path,
738: DomainAttributes.HOST));
739:
740: hosts.add(InetAddress.getLocalHost().getHostName());
741:
742: pdc.setAttributeValues(AdminUtil.PORTAL_DOMAIN_MBEAN_TYPE,
743: path, DomainAttributes.HOST, hosts);
744: } catch (Exception e) {
745: //String message = "Can't add this host to domain " + domainID;
746: //logger.log(Level.WARNING, message, e);
747: logger.log(Level.WARNING, "PSAD_CSPAS0227", domainID);
748: logger.log(Level.WARNING, "PSAD_CSPAS0000", e);
749: }
750: }
751:
752: /**
753: * Unregisters this PAS instance from the portal domain with the
754: * given ID by removing the name of the local host from the
755: * <code>host</code> attribute of the portal domain.
756: *
757: * @param domainID the ID of the portal domain to unregister
758: * this PAS instance from.
759: */
760: protected void unregisterPASInstanceFromDomain(String domainID) {
761: List path = Collections.singletonList(domainID);
762:
763: try {
764: PortalDomainContext pdc = PortalDomainContextFactory
765: .getPortalDomainContext(domainID);
766:
767: TreeSet hosts = new TreeSet(pdc.getAttributeValues(
768: AdminUtil.PORTAL_DOMAIN_MBEAN_TYPE, path,
769: DomainAttributes.HOST));
770:
771: hosts.remove(InetAddress.getLocalHost().getHostName());
772:
773: pdc.setAttributeValues(AdminUtil.PORTAL_DOMAIN_MBEAN_TYPE,
774: path, DomainAttributes.HOST, hosts);
775: } catch (Exception e) {
776: //String message = "Can't remove this host from domain " + domainID;
777: //logger.log(Level.WARNING, message, e);
778: logger.log(Level.WARNING, "PSAD_CSPAS0228", domainID);
779: logger.log(Level.WARNING, "PSAD_CSPAS0000", e);
780: }
781: }
782:
783: /**
784: * Returns the object names of the JMX connector server MBeans
785: * registered to the MBean server.
786: */
787: protected Set getJMXConnectorServers() {
788: ObjectName connectorServers = null;
789:
790: try {
791: connectorServers = new ObjectName(
792: "*:type=javax.management.remote.JMXConnectorServerMBean,*");
793: } catch (Exception e) {
794: }
795:
796: return getMbs().queryNames(connectorServers, null);
797: }
798:
799: /**
800: * Adds listener to receive JMX connection notifications.
801: */
802: protected void addNotificationListener(PASMechanism pasMechanism) {
803: //String message = null;
804: Set connectorServers = getJMXConnectorServers();
805:
806: for (Iterator i = connectorServers.iterator(); i.hasNext();) {
807: ObjectName connectorServer = (ObjectName) i.next();
808:
809: try {
810: getMbs().addNotificationListener(connectorServer,
811: pasMechanism, pasMechanism, null);
812: //message = "Added a connection notification listener to " + connectorServer;
813: //logger.log(Level.FINEST, message);
814: logger.log(Level.FINEST, "PSAD_CSPAS0229",
815: connectorServer);
816: } catch (Exception e) {
817: //message = "Can't add a connection notification listener to " + connectorServer;
818: //logger.log(Level.WARNING, message, e);
819: logger.log(Level.WARNING, "PSAD_CSPAS0230",
820: connectorServer);
821: logger.log(Level.WARNING, "PSAD_CSPAS0000", e);
822: }
823: }
824: }
825:
826: /**
827: * Removes JMX connection notification listener.
828: */
829: protected void removeNotificationListener(PASMechanism pasMechanism) {
830: //String message = null;
831: Set connectorServers = getJMXConnectorServers();
832:
833: for (Iterator i = connectorServers.iterator(); i.hasNext();) {
834: ObjectName connectorServer = (ObjectName) i.next();
835:
836: try {
837: getMbs().removeNotificationListener(connectorServer,
838: pasMechanism);
839: //message = "Removed connection notification listener from " + connectorServer;
840: //logger.log(Level.FINEST, message);
841: logger.log(Level.FINEST, "PSAD_CSPAS0231",
842: connectorServer);
843: } catch (Exception e) {
844: //message = "Can't remove connection notification listener from " + connectorServer;
845: //logger.log(Level.WARNING, message, e);
846: logger.log(Level.WARNING, "PSAD_CSPAS0232",
847: connectorServer);
848: logger.log(Level.WARNING, "PSAD_CSPAS0000", e);
849: }
850: }
851: }
852: }
|