KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > jmx > SunoneInterceptor


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 package com.sun.enterprise.admin.server.core.jmx;
24
25 //JDK imports
26
import java.lang.reflect.Method JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.io.ObjectOutputStream JavaDoc;
30 import java.io.ObjectInputStream JavaDoc;
31 import java.util.logging.Level JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33
34 //JMX imports
35
import javax.management.MBeanServer JavaDoc;
36 import javax.management.QueryExp JavaDoc;
37 import javax.management.ObjectInstance JavaDoc;
38 import javax.management.ObjectName JavaDoc;
39 import javax.management.Attribute JavaDoc;
40 import javax.management.AttributeList JavaDoc;
41 import javax.management.InstanceAlreadyExistsException JavaDoc;
42 import javax.management.InvalidAttributeValueException JavaDoc;
43 import javax.management.InstanceNotFoundException JavaDoc;
44 import javax.management.AttributeNotFoundException JavaDoc;
45 import javax.management.ReflectionException JavaDoc;
46 import javax.management.MBeanException JavaDoc;
47 import javax.management.IntrospectionException JavaDoc;
48 import javax.management.ListenerNotFoundException JavaDoc;
49 import javax.management.MBeanInfo JavaDoc;
50 import javax.management.MBeanAttributeInfo JavaDoc;
51 import javax.management.MBeanNotificationInfo JavaDoc;
52 import javax.management.MBeanOperationInfo JavaDoc;
53 import javax.management.MBeanConstructorInfo JavaDoc;
54 import javax.management.MBeanRegistrationException JavaDoc;
55 import javax.management.NotificationListener JavaDoc;
56 import javax.management.NotificationFilter JavaDoc;
57 import javax.management.NotCompliantMBeanException JavaDoc;
58 import javax.management.OperationsException JavaDoc;
59 import javax.management.MBeanRegistrationException JavaDoc;
60 import javax.management.NotCompliantMBeanException JavaDoc;
61
62 import com.sun.enterprise.admin.meta.MBeanRegistry;
63 import com.sun.enterprise.admin.meta.MBeanRegistryFactory;
64 //server
65
import com.sun.enterprise.server.ApplicationServer;
66
67 //Admin imports
68
import com.sun.enterprise.admin.AdminContext;
69 import com.sun.enterprise.admin.common.ObjectNames;
70 import com.sun.enterprise.admin.common.ObjectNameHelper;
71 import com.sun.enterprise.admin.common.constant.AdminConstants;
72 import com.sun.enterprise.admin.common.exception.AFRuntimeStoreException;
73 import com.sun.enterprise.admin.common.exception.AFRuntimeException;
74 import com.sun.enterprise.admin.event.AdminEventCache;
75 import com.sun.enterprise.admin.util.proxy.ProxyFactory;
76
77 //i18n import
78
import com.sun.enterprise.util.i18n.StringManager;
79
80 import javax.management.MBeanServerFactory JavaDoc;
81 import javax.management.loading.ClassLoaderRepository JavaDoc;
82
83 import com.sun.enterprise.config.serverbeans.ServerHelper;
84
85 /* for checking persistent store */
86 import com.sun.enterprise.admin.server.core.jmx.storage.PersistenceChecker;
87 import com.sun.enterprise.admin.server.core.jmx.storage.MBeanManufacturer;
88 import com.sun.enterprise.server.ondemand.entry.*;
89
90 /** A class that is a poor man's interceptor. There are following reasons that led
91  * to use this interceptor and not the standard JMX 1.2 interceptor.
92  * <LI> Tight deadlines and smoke test has to work :( </LI>
93  * <LI> We have lazy loading going on. (Again poor man's) This means that any
94  * MBean has to be registered first before it can be invoked. We did not have any
95  * good solution at the architectural level for that, nor does JMX 1.2 have it. The
96  * way we do it is to check that persistent medium and/or Java Beans derived from the dtd
97  * and initialized from domain.xml (in memory) indicate that such an element should exist,
98  * and then we register the mbean before invoking it. This solves the problem of
99  * initialization meaning the MBeans for all the elements in configuration file (domain.xml)
100  * do not have to be registered in order to invoke the operation. </LI>
101  * <LI> Still, we want a JMX 1.2 implementation of the MBeanServer as some
102  * immediate future work may need it. </LI>
103  * <LI> Ugliness to begin with gives birth to more ugliness. This class has
104  * to handle the case of config files being hand edited. :((( </LI>
105  * Hence this interceptor results. All it does is the following:
106  * <LI> Implements the javax.management.MBeanServer as sincerely as possible. </LI>
107  * <LI> Uses Lazy loading meaning that the MBeans will be registered if they
108  * do not already exist in registry of the MBeanServer. An attempt will be made
109  * to see that config bean already exists. (This is not a violation per se, but
110  * an interpretation of the MBeanServer specification).
111  * @see javax.management.MBeanServer
112  * @since 8.0
113 */

114
115 public class SunoneInterceptor implements MBeanServer JavaDoc, EntryPoint {
116     
117     public static final String JavaDoc HOT_CONFIG_METHOD_NAME = "canApplyConfigChanges";
118     public static final String JavaDoc FORCE_APPLY_METHOD_NAME = "overwriteConfigChanges";
119     public static final String JavaDoc APPLY_METHOD_NAME = "applyConfigChanges";
120     public static final String JavaDoc USE_MANUAL_METHOD_NAME = "useManualConfigChanges";
121     public static final String JavaDoc GET_HOST_AND_PORT_METHOD_NAME = "getHostAndPort";
122     
123    
124     private static final Logger JavaDoc sLogger =
125             Logger.getLogger(AdminConstants.kLoggerName);
126     private static StringManager localStrings =
127         StringManager.getManager( SunoneInterceptor.class );
128     private static AdminContext adminContext;
129
130     private MBeanServer JavaDoc realMBeanServer;
131
132     /**
133         The private constructor so that only the factory method is called.
134         Also makes sure that there is single instance of MBeanServer in the JVM.
135
136         @param defaultDomainName represents the domain of the MBeanServer.
137         @throws InitException in case the System Mbeans can't be registered. The
138         System MBeans are ServerController and GenericConfigurator.
139     */

140
141     SunoneInterceptor(String JavaDoc defaultDomainName) throws InitException {
142     }
143
144     /**
145      * Added new method. Description tBd
146      */

147     void setJmxMBeanServer(MBeanServer JavaDoc jmxMBS) throws InitException {
148         realMBeanServer = (MBeanServer JavaDoc)ProxyFactory.createProxy(
149                     MBeanServer JavaDoc.class, jmxMBS,
150                     adminContext.getMBeanServerInterceptor());
151         logMBeanServerInfo();
152         initialize();
153     }
154         
155     /**
156         The private constructor so that only the factory method is called.
157         Also makes sure that there is single instance of MBeanServer in the JVM.
158         The default domain of this MBeanServer will be "ias".
159
160         @throws InitException in case the System Mbeans can't be registered. The
161         System MBeans are ServerController and GenericConfigurator.
162     */

163
164     SunoneInterceptor() throws InitException {
165         this(ServiceName.DOMAIN);
166     }
167
168     /**
169         Factory method to generate the only instance of this class in the JVM.
170         Makes sure that the constructor of this class is called exactly once.
171         
172         @return MBeanServer instance by calling private constructor.
173         @throws InitException if the MBeanServer can not be initialized.
174         @deprecated use AppServerMBeanServerBuilder.getMBeanServerInstance();
175     */

176     
177     public static MBeanServer JavaDoc getMBeanServerInstance()
178         throws InitException {
179             return AppServerMBeanServerFactory.getMBeanServerInstance();
180     }
181     
182     /**
183      * Set admin context.
184      */

185     public static void setAdminContext(AdminContext ctx) {
186        adminContext = ctx;
187     }
188
189     /**
190         Initializes the MBeanServer. This method registers the System MBeans.
191         The System MBeans are assumed to have default constructor.
192        
193         @throws InitException if any of the System MBeans can't be initialized.
194     */

195
196     private void initialize() throws InitException {
197         try {
198             ObjectName JavaDoc controllerObjectName
199                     = ObjectNames.getControllerObjectName();
200             ObjectName JavaDoc configObjectName
201                     = ObjectNames.getGenericConfiguratorObjectName();
202             ObjectName JavaDoc[] objectNames = {
203                 controllerObjectName,
204                 configObjectName
205             };
206         String JavaDoc controllerClassName =
207             "com.sun.enterprise.admin.server.core.mbean.config.ServerController";
208         String JavaDoc configClassName =
209             "com.sun.enterprise.admin.server.core.mbean.config.GenericConfigurator";
210         String JavaDoc[] clNames = {controllerClassName, configClassName};
211             for (int i = 0 ; i < clNames.length ; i++) {
212                 createAndRegister( clNames[i], objectNames[ i ] );
213             }
214             registerDottedNameSupport();
215         }
216         catch (Exception JavaDoc e) {
217             throw new InitException(e.getMessage(), e.getCause() );
218         }
219     }
220     
221     /**
222         <LLC>
223         Load the MBeans which support dotted names.
224      */

225     
226     // screwy build dependencies for this sort of lame instantation
227
static private final String JavaDoc DottedMBeansIniterClassName =
228             "com.sun.enterprise.admin.mbeans.DottedNameMBeansIniter";
229             
230         private void
231     registerDottedNameSupport()
232         throws Exception JavaDoc
233     {
234         final Class JavaDoc initerClass = Class.forName( DottedMBeansIniterClassName );
235         
236         // invoke new DottedNamesMBeanIniter( MBeanServer m )
237
final Class JavaDoc [] signature = new Class JavaDoc [] { MBeanServer JavaDoc.class };
238         final java.lang.reflect.Constructor JavaDoc constructor = initerClass.getConstructor( signature );
239         constructor.newInstance( new Object JavaDoc [] { this } );
240         // done--it will have done its job
241
}
242     
243     private ObjectInstance JavaDoc createAndRegister( String JavaDoc className, ObjectName JavaDoc objectName )
244         throws Exception JavaDoc
245     {
246         try
247         {
248             final Class JavaDoc mbeanClass = Class.forName( className );
249             final Object JavaDoc mbeanImpl = mbeanClass.newInstance();
250             final ObjectInstance JavaDoc mbeanInstance = this.registerMBean( mbeanImpl, objectName );
251             
252             sLogger.log(Level.FINE, "core.system_mbean_init_ok", objectName.toString() );
253             return( mbeanInstance );
254         }
255         catch( Exception JavaDoc e )
256         {
257             e.printStackTrace();
258             throw e;
259         }
260     }
261     
262     /* </LLC> */
263     
264     public ObjectInstance JavaDoc registerMBean(Object JavaDoc object, ObjectName JavaDoc objectName)
265         throws InstanceAlreadyExistsException JavaDoc,
266                 MBeanRegistrationException JavaDoc,
267                 NotCompliantMBeanException JavaDoc {
268         return (realMBeanServer.registerMBean(object, objectName));
269     }
270
271     public void generateEntryContext(Object JavaDoc obj) {
272         ServerEntryHelper.generateMbeanEntryContext((ObjectName JavaDoc) obj);
273     }
274     
275     
276     public Object JavaDoc invoke(ObjectName JavaDoc objectName, String JavaDoc operationName,
277         Object JavaDoc[] params, String JavaDoc[] signature)
278         throws ReflectionException JavaDoc, InstanceNotFoundException JavaDoc, MBeanException JavaDoc {
279
280         generateEntryContext(objectName);
281       
282         if(isInstanceMBean(objectName) && isConfigCheckRequired(operationName)) {
283             checkHotConfigChanges(objectName);
284         }
285         
286          if(FORCE_APPLY_METHOD_NAME.equals(operationName)) {
287              // Manual changes to config will be overwritten, so persist the
288
// state restart is required.
289
String JavaDoc instanceName =
290                      ObjectNameHelper.getServerInstanceName(objectName);
291              AdminEventCache cache = AdminEventCache.getInstance(instanceName);
292              cache.setRestartNeeded(true);
293             //operationName = APPLY_METHOD_NAME;
294
}
295         registerWithPersistenceCheck(objectName);
296         //logMBeanInfo(objectName);
297
Object JavaDoc actualResult = realMBeanServer.invoke(objectName, operationName,
298             params, signature );
299         return ( actualResult );
300     }
301     
302     public Object JavaDoc getAttribute(ObjectName JavaDoc objectName, String JavaDoc attributeName)
303         throws InstanceNotFoundException JavaDoc, AttributeNotFoundException JavaDoc,
304                MBeanException JavaDoc, ReflectionException JavaDoc {
305         if(isInstanceMBean(objectName)) {
306             checkHotConfigChanges(objectName);
307         }
308         registerWithPersistenceCheck(objectName);
309         //logMBeanInfo(objectName);
310
Object JavaDoc value = realMBeanServer.getAttribute(objectName, attributeName);
311         return ( value );
312     }
313     
314     public void setAttribute(ObjectName JavaDoc objectName, Attribute JavaDoc attribute) throws
315         InstanceNotFoundException JavaDoc, AttributeNotFoundException JavaDoc,
316         MBeanException JavaDoc, ReflectionException JavaDoc, InvalidAttributeValueException JavaDoc {
317         if(isInstanceMBean(objectName)) {
318             checkHotConfigChanges(objectName);
319         }
320         registerWithPersistenceCheck(objectName);
321         //logMBeanInfo(objectName);
322
realMBeanServer.setAttribute(objectName, attribute);
323     }
324
325     public AttributeList JavaDoc getAttributes(ObjectName JavaDoc objectName, String JavaDoc[] attrNames)
326         throws InstanceNotFoundException JavaDoc, ReflectionException JavaDoc {//, RuntimeOperationsException
327
// if(isInstanceMBean(objectName))
328
{
329             checkHotConfigChanges(objectName);
330         }
331         
332         registerWithPersistenceCheck(objectName);
333         return ( realMBeanServer.getAttributes(objectName, attrNames) );
334     }
335
336     public AttributeList JavaDoc setAttributes (ObjectName JavaDoc objectName, AttributeList JavaDoc attributeList)
337             throws InstanceNotFoundException JavaDoc, ReflectionException JavaDoc {
338         if(isInstanceMBean(objectName)) {
339             checkHotConfigChanges(objectName);
340         }
341
342         registerWithPersistenceCheck(objectName);
343         return ( realMBeanServer.setAttributes(objectName, attributeList) );
344     }
345
346     
347     public void unregisterMBean(ObjectName JavaDoc objectName)
348         throws InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc {
349         realMBeanServer.unregisterMBean(objectName);
350     }
351     
352     public Integer JavaDoc getMBeanCount() {
353     return ( realMBeanServer.getMBeanCount() );
354     }
355
356     public Set JavaDoc queryMBeans(ObjectName JavaDoc name, QueryExp JavaDoc exp) {
357         registerConfigMBeans();
358         return ( realMBeanServer.queryMBeans(name, exp) );
359     }
360
361     public MBeanInfo JavaDoc getMBeanInfo(ObjectName JavaDoc objName) throws
362         InstanceNotFoundException JavaDoc, IntrospectionException JavaDoc, ReflectionException JavaDoc {
363         registerWithPersistenceCheck(objName);
364         return ( realMBeanServer.getMBeanInfo(objName) );
365     }
366
367     public boolean isRegistered(ObjectName JavaDoc name) {
368        /*
369         // This is the actual way to do it
370         // since there is a bug in initializing mbeans
371         // commenting this for now in the assumption that
372         // that the bug will be fixed soon.
373          
374         boolean isRegistered = realMBeanServer.isRegistered(name);
375         if(!isRegistered) {
376             try {
377                 registerWithPersistenceCheck(name);
378                 isRegistered = true;
379             } catch(Exception e) {
380                 //ignore
381             }
382         }
383         return isRegistered;
384         */

385        return realMBeanServer.isRegistered(name);
386     }
387
388     public void addNotificationListener(ObjectName JavaDoc objectName,
389         NotificationListener JavaDoc notificationListener,
390         NotificationFilter JavaDoc notificationFilter, Object JavaDoc obj)
391         throws InstanceNotFoundException JavaDoc {
392         realMBeanServer.addNotificationListener(objectName,
393             notificationListener, notificationFilter, obj);
394     }
395
396     public void addNotificationListener (ObjectName JavaDoc objectName,
397         ObjectName JavaDoc objectName1, NotificationFilter JavaDoc notificationFilter,
398         Object JavaDoc obj) throws InstanceNotFoundException JavaDoc {
399         realMBeanServer.addNotificationListener(objectName, objectName1,
400         notificationFilter, obj);
401     }
402
403     public ObjectInstance JavaDoc createMBean (String JavaDoc str, ObjectName JavaDoc objectName)
404         throws ReflectionException JavaDoc, InstanceAlreadyExistsException JavaDoc,
405         MBeanRegistrationException JavaDoc, MBeanException JavaDoc,
406         NotCompliantMBeanException JavaDoc {
407         return realMBeanServer.createMBean (str, objectName);
408     }
409
410     public ObjectInstance JavaDoc createMBean (String JavaDoc str, ObjectName JavaDoc objectName,
411         ObjectName JavaDoc objectName2) throws ReflectionException JavaDoc,
412         InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc,
413         MBeanException JavaDoc, NotCompliantMBeanException JavaDoc, InstanceNotFoundException JavaDoc {
414         return ( realMBeanServer.createMBean (str, objectName, objectName2) );
415     }
416
417     public ObjectInstance JavaDoc createMBean (String JavaDoc str, ObjectName JavaDoc objectName,
418         Object JavaDoc[] obj, String JavaDoc[] str3)
419         throws ReflectionException JavaDoc, InstanceAlreadyExistsException JavaDoc,
420         MBeanRegistrationException JavaDoc, MBeanException JavaDoc, NotCompliantMBeanException JavaDoc {
421         return realMBeanServer.createMBean (str, objectName, obj, str3);
422     }
423
424     public ObjectInstance JavaDoc createMBean (String JavaDoc str, ObjectName JavaDoc objectName,
425         ObjectName JavaDoc objectName2, Object JavaDoc[] obj, String JavaDoc[] str4)
426         throws ReflectionException JavaDoc, InstanceAlreadyExistsException JavaDoc,
427         MBeanRegistrationException JavaDoc, MBeanException JavaDoc,
428         NotCompliantMBeanException JavaDoc, InstanceNotFoundException JavaDoc {
429         return realMBeanServer.createMBean (str, objectName,
430             objectName2, obj, str4);
431     }
432
433     /* deprecated API @since 1.1 - use with caution */
434     public ObjectInputStream JavaDoc deserialize (String JavaDoc str, byte[] values)
435         throws OperationsException JavaDoc, ReflectionException JavaDoc {
436         return realMBeanServer.deserialize (str, values);
437     }
438
439     /* deprecated API @since 1.1 - use with caution */
440     public ObjectInputStream JavaDoc deserialize (ObjectName JavaDoc objectName, byte[] values)
441         throws InstanceNotFoundException JavaDoc, OperationsException JavaDoc {
442         return realMBeanServer.deserialize (objectName, values);
443     }
444
445     public ObjectInputStream JavaDoc deserialize (String JavaDoc str, ObjectName JavaDoc objectName,
446         byte[] values) throws InstanceNotFoundException JavaDoc, OperationsException JavaDoc,
447         ReflectionException JavaDoc {
448         return realMBeanServer.deserialize (str, objectName, values);
449     }
450
451     public String JavaDoc getDefaultDomain() {
452         return realMBeanServer.getDefaultDomain();
453     }
454     
455     public ObjectInstance JavaDoc getObjectInstance(ObjectName JavaDoc objectName)
456         throws InstanceNotFoundException JavaDoc {
457         return realMBeanServer.getObjectInstance(objectName);
458     }
459     
460     public Object JavaDoc instantiate(String JavaDoc str) throws ReflectionException JavaDoc,
461         MBeanException JavaDoc {
462         return realMBeanServer.instantiate(str);
463     }
464     
465     public Object JavaDoc instantiate(String JavaDoc str, ObjectName JavaDoc objectName)
466     throws ReflectionException JavaDoc, MBeanException JavaDoc, InstanceNotFoundException JavaDoc {
467         return realMBeanServer.instantiate(str, objectName);
468     }
469     
470     public Object JavaDoc instantiate(String JavaDoc str, Object JavaDoc[] obj, String JavaDoc[] str2)
471     throws ReflectionException JavaDoc, MBeanException JavaDoc {
472         return realMBeanServer.instantiate(str, obj, str2);
473     }
474     
475     public Object JavaDoc instantiate(String JavaDoc str, ObjectName JavaDoc objectName,
476     Object JavaDoc[] obj, String JavaDoc[] str3)
477     throws ReflectionException JavaDoc, MBeanException JavaDoc, InstanceNotFoundException JavaDoc {
478         return realMBeanServer.instantiate(str, objectName, obj, str3);
479     }
480
481     public boolean isInstanceOf (ObjectName JavaDoc objectName, String JavaDoc str)
482     throws InstanceNotFoundException JavaDoc {
483             return realMBeanServer.isInstanceOf(objectName, str);
484     }
485
486     public Set JavaDoc queryNames (ObjectName JavaDoc objectName, QueryExp JavaDoc queryExp) {
487         registerConfigMBeans();
488         return realMBeanServer.queryNames(objectName, queryExp);
489     }
490
491     public void removeNotificationListener (ObjectName JavaDoc objectName,
492             ObjectName JavaDoc objectName1)
493             throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc {
494             realMBeanServer.removeNotificationListener (objectName,
495                 objectName1);
496     }
497
498     public void removeNotificationListener (ObjectName JavaDoc objectName,
499             NotificationListener JavaDoc notificationListener)
500             throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc {
501             realMBeanServer.removeNotificationListener (objectName,
502                 notificationListener);
503     }
504
505     // START BACKUP_HOT ISSUE FIX APIs
506

507     private void checkHotConfigChanges(ObjectName JavaDoc mbeanName) {
508         //different case
509
try {
510             //check whether the mbeanName pertains to a server instance.
511
// String instanceName = ObjectNameHelper.getServerInstanceName(mbeanName);
512
String JavaDoc instanceName = ApplicationServer.getServerContext().getInstanceName();
513             ObjectName JavaDoc instanceObjectName = ObjectNames.getServerInstanceObjectName(instanceName);
514             Object JavaDoc canApply = this.invoke(instanceObjectName, HOT_CONFIG_METHOD_NAME,
515                 null, null);
516             //System.out.println("return value" + canApply);
517
if (canApply.equals(Boolean.FALSE)) {
518                 String JavaDoc msg = localStrings.getString(
519                        "admin.server.core.jmx.configuration_changed_apply_changes",
520                        instanceName);
521                 throw new AFRuntimeStoreException( msg );
522             }
523         } catch(AFRuntimeStoreException af) {
524             throw af;
525         } catch (Exception JavaDoc e) {
526             String JavaDoc msg = localStrings.getString( "admin.server.core.jmx.bad_server_configuration" );
527             sLogger.log(Level.INFO, msg, e);
528             throw new AFRuntimeException( msg, e );
529         }
530     }
531
532     private boolean isInstanceMBean(ObjectName JavaDoc mbeanName) {
533         if(ObjectNameHelper.getServerInstanceName(mbeanName) != null) {
534             return true;
535         } else {
536             return false;
537         }
538     }
539     
540     private boolean isConfigCheckRequired(String JavaDoc operationName) {
541         //System.out.println("Entering isConfigCheckRequired:" + operationName);
542

543         if(GET_HOST_AND_PORT_METHOD_NAME.equals(operationName)) {
544             //System.out.println("getHostAndPort: returning FALSE");
545
return false;
546         }
547         
548         if(FORCE_APPLY_METHOD_NAME.equals(operationName)) {
549                 //System.out.println("overwriteConfigChanges: returning FALSE");
550
return false;
551         }
552         
553         if(USE_MANUAL_METHOD_NAME.equals(operationName)) {
554             //System.out.println("useManualConfigChanges: returning FALSE");
555
return false;
556         }
557         
558        
559         if(HOT_CONFIG_METHOD_NAME.equals(operationName)) {
560             return false;
561         }
562         
563         //START: Optimization to prevent multiple checks for changes
564
if("getMBeanInfo".equals(operationName)) {
565             return false;
566         }
567         //END: Optimization to prevent multiple checks for changes
568

569         //System.out.println("RETURNING true");
570
return true;
571     }
572     
573     
574     /**
575      * There are many cases where we need to perform lazy instantiation before
576      * accessing the real mbean server. Currently only getClassLoader and getClassLoaderFor
577      * do this now, but FIXTHIS eventually all code that accesses and object name, throws
578      * an instanceNotFound exception and accesses the realMBeanServer should do so using
579      *this convenience method to ensure that lazy instantiation happens properly.
580      */

581     private MBeanServer JavaDoc getRealMBeanServerAndInstantiate(ObjectName JavaDoc objectName) throws InstanceNotFoundException JavaDoc
582     {
583         registerWithPersistenceCheck(objectName);
584         return realMBeanServer;
585     }
586        
587     public ClassLoader JavaDoc getClassLoader(ObjectName JavaDoc objectName)
588         throws InstanceNotFoundException JavaDoc {
589            return ( getRealMBeanServerAndInstantiate(objectName).getClassLoader(objectName) );
590     }
591     
592     public ClassLoader JavaDoc getClassLoaderFor(ObjectName JavaDoc objectName)
593         throws InstanceNotFoundException JavaDoc {
594             return ( getRealMBeanServerAndInstantiate(objectName).getClassLoaderFor(objectName) );
595             
596     }
597     
598     public ClassLoaderRepository JavaDoc getClassLoaderRepository() {
599         return ( realMBeanServer.getClassLoaderRepository() );
600     }
601     
602     public String JavaDoc[] getDomains() {
603         return ( realMBeanServer.getDomains() );
604     }
605       
606     public void removeNotificationListener(ObjectName JavaDoc objectName,
607         NotificationListener JavaDoc notificationListener, NotificationFilter JavaDoc
608         notificationFilter, Object JavaDoc obj) throws InstanceNotFoundException JavaDoc,
609         ListenerNotFoundException JavaDoc {
610             realMBeanServer.
611                 removeNotificationListener(objectName, notificationListener,
612                 notificationFilter, obj);
613     }
614     
615     public void removeNotificationListener(ObjectName JavaDoc objectName,
616         ObjectName JavaDoc objectName1, NotificationFilter JavaDoc notificationFilter,
617         Object JavaDoc obj)
618         throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc {
619             realMBeanServer.removeNotificationListener(objectName, objectName1,
620                 notificationFilter, obj);
621     }
622     
623     /** Logs the MBeanServer information. It is logged to the server's output/error
624      * log, to convey the information about MBeanServer implementation used.
625      * @throws RuntimeException if there are some problems in invoking
626      * implementation methods.
627     */

628     private void logMBeanServerInfo() {
629         try {
630             final String JavaDoc name = "JMImplementation:type=MBeanServerDelegate";
631             final ObjectName JavaDoc oName = new ObjectName JavaDoc(name);
632             
633             sLogger.log(Level.FINE, "core.mbs_info");
634             //log the implementation name
635
String JavaDoc attrName = "ImplementationName";
636             String JavaDoc result = (String JavaDoc) realMBeanServer.
637                     getAttribute(oName, attrName);
638             sLogger.log(Level.FINE, "core.mbs_implementation", result);
639             //log the implementation vendor
640
attrName = "ImplementationVendor";
641             result = (String JavaDoc) realMBeanServer.getAttribute(oName, attrName);
642             sLogger.log(Level.FINE, "core.mbs_vendor", result);
643             //log the impl version
644
attrName = "ImplementationVersion";
645             result = (String JavaDoc) realMBeanServer.getAttribute(oName, attrName);
646             sLogger.log(Level.FINE, "core.jmx_impl_version", result);
647             //log the MBeanServerId
648
attrName = "MBeanServerId";
649             result = (String JavaDoc) realMBeanServer.getAttribute(oName, attrName);
650             sLogger.log(Level.FINE, "core.mbs_id", result);
651             result = realMBeanServer.getClass().getName();
652             sLogger.log(Level.FINE, "core_mbs_classname", result);
653         }
654         catch(Exception JavaDoc e) {
655             throw new RuntimeException JavaDoc (e);
656         }
657     }
658     /**
659      * this method added to synchronize lazy loaded MBeans manufacturing and registering
660      */

661     synchronized private void manufactureAndRegisterMBean(ObjectName JavaDoc oName) throws Exception JavaDoc
662     {
663         //the "second" check inside synchronized area (it will perevent double manufacturing)
664
if (realMBeanServer.isRegistered(oName) ) {
665             //sLogger.log(Level.FINE, "core.mbean_exists", oName);
666
return; //already registered
667
}
668         //call to lazy loading here
669
final Object JavaDoc product = manufactureMBean(oName);
670         if(product==null)
671         {
672                 String JavaDoc msg = localStrings.getString(
673                        "admin.server.core.jmx.lazybean_not_found",
674                        oName.toString());
675                 throw new InstanceNotFoundException JavaDoc(msg);
676         }
677         //register MBean.
678
this.registerMBean(product, oName);
679         sLogger.log(Level.FINE, "core.create_and_register", oName);
680     }
681     
682     /** This method does the following in a sequential manner:
683      * <LI> If the MBean with given ObjectName is registered, do nothing. </LI>
684      * <LI> If the MBean does not exist, create the MBean by "manufacturing process". </LI>
685      * <LI> If the MBean is thus manufactured, then register that MBean in MBeanServer. </LI>
686      * @param oName ObjectName that corresponds to a single MBean - should not be a pattern.
687      * @since 8.0.
688      * @exception IllegalArgumentException if oName is a pattern.
689      * @exception RuntimeException with actual exception embedded in case operation fails.
690     */

691     private void registerWithPersistenceCheck(ObjectName JavaDoc oName) throws InstanceNotFoundException JavaDoc
692     {
693            
694         if (! realMBeanServer.isRegistered(oName) ) {
695             try {
696                 //Manufacture the MBean now.
697
manufactureAndRegisterMBean(oName);
698             }
699             catch (InstanceNotFoundException JavaDoc infe)
700             {
701                 throw infe;
702             }
703             catch (RuntimeException JavaDoc re)
704             {
705                 throw re;
706             }
707             catch (Exception JavaDoc e) {
708                 throw new RuntimeException JavaDoc(e);
709             }
710         }
711         //else {
712
//sLogger.log(Level.FINE, "core.mbean_exists", oName);
713
//}
714
}
715     
716     private Object JavaDoc manufactureMBean(ObjectName JavaDoc oName) throws InstanceNotFoundException JavaDoc
717     {
718         final PersistenceChecker checker = new PersistenceChecker();
719         checker.setAdminContext(adminContext);
720         final Object JavaDoc storedObject = checker.findElement(oName);
721         Object JavaDoc match = null;
722         if (storedObject != null) {
723             MBeanManufacturer producer = new MBeanManufacturer(oName, storedObject);
724             producer.setAdminContext(adminContext);
725             match = producer.createMBeanInstance();
726         }
727         else {
728             //this is severe - storage is not in sync with whatever is requested to be done.
729
sLogger.log(Level.FINEST, "core.not_in_config", oName);
730         }
731         //Actually it should be asserted that match is NOT null.
732
return ( match );
733     }
734         
735     public void shutdown() {
736         MBeanServerFactory.releaseMBeanServer(realMBeanServer);
737         sLogger.log(Level.FINE, "core.release_mbs");
738     }
739     
740     /* comment out this method later on */
741     private void logMBeanInfo(ObjectName JavaDoc oName) {
742         
743         // if we are not logging at the finest level, just return
744
if (!sLogger.isLoggable(Level.FINEST)) {
745             return;
746         }
747         
748         //This method assumes that the mbean is registered.
749
MBeanInfo JavaDoc info = null;
750     try {
751         info = realMBeanServer.getMBeanInfo(oName);
752     } catch (Exception JavaDoc e) {
753         e.printStackTrace();
754         return;
755     }
756     sLogger.log(Level.FINEST,"\nCLASSNAME: \t"+ info.getClassName());
757     sLogger.log(Level.FINEST,"\nDESCRIPTION: \t"+ info.getDescription());
758     sLogger.log(Level.FINEST,"\nATTRIBUTES");
759     MBeanAttributeInfo JavaDoc[] attrInfo = info.getAttributes();
760     if (attrInfo.length>0) {
761         for(int i=0; i<attrInfo.length; i++) {
762         sLogger.log(Level.FINEST," ** NAME: \t"+ attrInfo[i].getName());
763         sLogger.log(Level.FINEST," DESCR: \t"+ attrInfo[i].getDescription());
764         sLogger.log(Level.FINEST," TYPE: \t"+ attrInfo[i].getType() +
765              "\tREAD: "+ attrInfo[i].isReadable() +
766              "\tWRITE: "+ attrInfo[i].isWritable());
767         }
768     } else
769             sLogger.log(Level.FINEST," ** No attributes **");
770     sLogger.log(Level.FINEST,"\nCONSTRUCTORS");
771     MBeanConstructorInfo JavaDoc[] constrInfo = info.getConstructors();
772     for(int i=0; i<constrInfo.length; i++) {
773         sLogger.log(Level.FINEST," ** NAME: \t"+ constrInfo[i].getName());
774         sLogger.log(Level.FINEST," DESCR: \t"+ constrInfo[i].getDescription());
775         sLogger.log(Level.FINEST," PARAM: \t"+ constrInfo[i].getSignature().length +" parameter(s)");
776     }
777     sLogger.log(Level.FINEST,"\nOPERATIONS");
778     MBeanOperationInfo JavaDoc[] opInfo = info.getOperations();
779     if (opInfo.length>0) {
780         for(int i=0; i<opInfo.length; i++) {
781         sLogger.log(Level.FINEST," ** NAME: \t"+ opInfo[i].getName());
782         sLogger.log(Level.FINEST," DESCR: \t"+ opInfo[i].getDescription());
783         sLogger.log(Level.FINEST," PARAM: \t"+ opInfo[i].getSignature().length +" parameter(s)");
784         }
785     } else
786             sLogger.log(Level.FINEST," ** No operations ** ");
787     sLogger.log(Level.FINEST,"\nNOTIFICATIONS");
788     MBeanNotificationInfo JavaDoc[] notifInfo = info.getNotifications();
789     if (notifInfo.length>0) {
790         for(int i=0; i<notifInfo.length; i++) {
791         sLogger.log(Level.FINEST," ** NAME: \t"+ notifInfo[i].getName());
792         sLogger.log(Level.FINEST," DESCR: \t"+ notifInfo[i].getDescription());
793         }
794     } else
795              sLogger.log(Level.FINEST," ** No notifications **");
796     }
797     
798     private static boolean _alreadyCalled = false;
799     /**
800      * NOTE: I am using runtime configcontext to instantiate configmbeans
801      *
802      * FIXME: The current implementation does not load all the mbeans
803      * while creating elements. Hence, some mbeans may never be
804      * loaded. This needs to be fixed.
805      *
806      * Note that the _alreadyCalled is being used to prevent
807      * multiple calls to this method since it is expensive. Currently,
808      * it takes about 4 seconds to register all config mbeans.
809      *
810      * FIXME: Eventual plan is to move this method to AdminService where
811      * it is initialized once and there is no need to call again.
812      *
813      * This method does not throw any exception and is best-effort.
814      */

815     private void registerConfigMBeans() {
816         if(!_alreadyCalled) //first try without synchronization
817
registerConfigMBeansSynchro();
818     }
819     private synchronized void registerConfigMBeansSynchro() {
820         
821         //there is only 1 SunoneInterceptor
822
//so, synchronize on this
823
/* synchronized (this) {
824             if(_alreadyCalled)
825                 return;
826             _alreadyCalled = true;
827         }*/

828         
829         if(_alreadyCalled)
830             return;
831
832         
833         try {
834             MBeanRegistry mr = MBeanRegistryFactory.getAdminMBeanRegistry();
835             mr.instantiateAndRegisterAllConfigMBeans(
836                  adminContext.getAdminConfigContext(),
837                  ApplicationServer.getServerContext().getDefaultDomainName());
838         } catch (Throwable JavaDoc t) {
839              sLogger.log(Level.FINE, "Error in registering configMBeans", t);
840         }
841         _alreadyCalled = true;
842     }
843 }
844
Popular Tags