KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > coyote > tomcat5 > MapperListener


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

27 package org.apache.coyote.tomcat5;
28
29 import java.util.Iterator JavaDoc;
30 import java.util.Set JavaDoc;
31
32 import javax.management.MBeanServer JavaDoc;
33 import javax.management.MBeanServerNotification JavaDoc;
34 // START SJSAS 6290785
35
import javax.management.MBeanAttributeInfo JavaDoc;
36 import javax.management.MBeanInfo JavaDoc;
37 // END SJSAS 6290785
38
import javax.management.Notification JavaDoc;
39 import javax.management.NotificationListener JavaDoc;
40 // START SJSAS 6313044
41
import javax.management.NotificationFilter JavaDoc;
42 // END SJSAS 6313044
43
import javax.management.ObjectInstance JavaDoc;
44 import javax.management.ObjectName JavaDoc;
45
46 import com.sun.org.apache.commons.logging.Log;
47 import com.sun.org.apache.commons.logging.LogFactory;
48
49 import com.sun.org.apache.commons.modeler.Registry;
50
51 import org.apache.tomcat.util.http.mapper.Mapper;
52
53 import org.apache.tomcat.util.res.StringManager;
54
55
56 /**
57  * Mapper listener.
58  *
59  * @author Remy Maucherat
60  * @author Costin Manolache
61  */

62 public class MapperListener
63     /* SJSAS 6313044
64     implements NotificationListener
65     */

66     // START SJSAS 6313044
67
implements NotificationListener JavaDoc, NotificationFilter JavaDoc
68     // END SJSAS 6313044
69
{
70     private static Log log = LogFactory.getLog(MapperListener.class);
71
72
73     // ----------------------------------------------------- Instance Variables
74
/**
75      * Associated mapper.
76      */

77     protected Mapper mapper = null;
78
79     /**
80      * MBean server.
81      */

82     protected MBeanServer JavaDoc mBeanServer = null;
83
84
85     /**
86      * The string manager for this package.
87      */

88     private StringManager sm =
89         StringManager.getManager(Constants.Package);
90
91     // It should be null - and fail if not set
92
private String JavaDoc domain="*";
93     private String JavaDoc engine="*";
94
95
96     // BEGIN S1AS 5000999
97
private int port;
98     private String JavaDoc defaultHost;
99     // END S1AS 5000999
100

101
102     // START SJSAS 6313044
103
private String JavaDoc myInstance;
104     // END SJSAS 6313044
105

106
107     // ----------------------------------------------------------- Constructors
108

109
110     /**
111      * Create mapper listener.
112      */

113     public MapperListener(Mapper mapper) {
114         this.mapper = mapper;
115     }
116
117
118     // --------------------------------------------------------- Public Methods
119

120     public String JavaDoc getDomain() {
121         return domain;
122     }
123
124     public void setDomain(String JavaDoc domain) {
125         this.domain = domain;
126     }
127
128     public String JavaDoc getEngine() {
129         return engine;
130     }
131
132     public void setEngine(String JavaDoc engine) {
133         this.engine = engine;
134     }
135
136     // BEGIN S1AS 5000999
137
public int getPort() {
138         return port;
139     }
140
141     public void setPort(int port) {
142         this.port = port;
143     }
144
145     public String JavaDoc getDefaultHost() {
146         return defaultHost;
147     }
148
149     public void setDefaultHost(String JavaDoc defaultHost) {
150         this.defaultHost = defaultHost;
151     }
152     // END S1AS 5000999
153

154     /**
155      * Initialize associated mapper.
156      */

157     public void init() {
158
159         // START SJSAS 6313044
160
myInstance = System.getProperty("com.sun.aas.instanceName");
161         // END SJSAS 6313044
162

163         try {
164
165             mBeanServer = Registry.getServer();
166
167             registerEngine();
168
169             // Query hosts
170
String JavaDoc onStr = domain + ":type=Host,*";
171             ObjectName JavaDoc objectName = new ObjectName JavaDoc(onStr);
172             Set JavaDoc set = mBeanServer.queryMBeans(objectName, null);
173             Iterator JavaDoc iterator = set.iterator();
174             while (iterator.hasNext()) {
175                 ObjectInstance JavaDoc oi = (ObjectInstance JavaDoc) iterator.next();
176                 registerHost(oi.getObjectName());
177             }
178
179
180             // Query contexts
181
onStr = "*:j2eeType=WebModule,*";
182             objectName = new ObjectName JavaDoc(onStr);
183             set = mBeanServer.queryMBeans(objectName, null);
184             iterator = set.iterator();
185             while (iterator.hasNext()) {
186                 ObjectInstance JavaDoc oi = (ObjectInstance JavaDoc) iterator.next();
187                 registerContext(oi.getObjectName());
188             }
189
190             // Query wrappers
191
onStr = "*:j2eeType=Servlet,*";
192             objectName = new ObjectName JavaDoc(onStr);
193             set = mBeanServer.queryMBeans(objectName, null);
194             iterator = set.iterator();
195             while (iterator.hasNext()) {
196                 ObjectInstance JavaDoc oi = (ObjectInstance JavaDoc) iterator.next();
197                 registerWrapper(oi.getObjectName());
198             }
199
200             onStr = "JMImplementation:type=MBeanServerDelegate";
201             objectName = new ObjectName JavaDoc(onStr);
202             /* SJSAS 6313044
203             mBeanServer.addNotificationListener(objectName, this, null, null);
204             */

205             // START SJSAS 6313044
206
mBeanServer.addNotificationListener(objectName, this, this, null);
207             // END SJSAS 6313044
208
} catch (Exception JavaDoc e) {
209             log.warn("Error registering contexts",e);
210         }
211
212     }
213
214
215     // START SJSAS 6313044
216
// ------------------------------------------ NotificationFilter Methods
217
/**
218      * Filters out any notifications corresponding to MBeans belonging to
219      * a different server instance than the server instance on which this
220      * MapperListener is running.
221      *
222      * @param notification The notification to be examined
223      *
224      * @return true if the notification needs to be sent to this
225      * MapperListener, false otherwise.
226      */

227     public boolean isNotificationEnabled(Notification JavaDoc notification) {
228
229         if (notification instanceof MBeanServerNotification JavaDoc) {
230             ObjectName JavaDoc objectName =
231                 ((MBeanServerNotification JavaDoc) notification).getMBeanName();
232             String JavaDoc otherInstance = objectName.getKeyProperty("J2EEServer");
233             if (myInstance != null && otherInstance != null
234                     && !otherInstance.equals(myInstance)) {
235                 return false;
236             }
237         }
238
239         return true;
240     
241     }
242     // END SJSAS 6313044
243

244
245     // ------------------------------------------- NotificationListener Methods
246

247
248     public void handleNotification(Notification JavaDoc notification,
249                                    java.lang.Object JavaDoc handback) {
250
251         if (notification instanceof MBeanServerNotification JavaDoc) {
252             ObjectName JavaDoc objectName =
253                 ((MBeanServerNotification JavaDoc) notification).getMBeanName();
254             String JavaDoc j2eeType = objectName.getKeyProperty("j2eeType");
255             String JavaDoc engineName = null;
256             if (j2eeType != null) {
257                 if ((j2eeType.equals("WebModule")) ||
258                     (j2eeType.equals("Servlet"))) {
259                     if (mBeanServer.isRegistered(objectName)) {
260                         /* SJSAS 6290785
261                         try {
262                             engineName = (String)
263                                 mBeanServer.getAttribute(objectName, "engineName");
264                         } catch (Exception e) {
265                             // Ignore
266                         }
267                         */

268                         // START SJSAS 6290785
269
MBeanInfo JavaDoc info = null;
270                         try {
271                             info = mBeanServer.getMBeanInfo(objectName);
272                         } catch (Exception JavaDoc e) {
273                             // Ignore
274
}
275                         if (info != null) {
276                             boolean hasEngineNameAttribute = false;
277                             MBeanAttributeInfo JavaDoc[] attrInfo = info.getAttributes();
278                             if (attrInfo != null) {
279                                 for (int i=0; i<attrInfo.length; i++) {
280                                     if ("engineName".equals(
281                                                     attrInfo[i].getName())) {
282                                         hasEngineNameAttribute = true;
283                                         break;
284                                     }
285                                 }
286                             }
287                             if (hasEngineNameAttribute) {
288                                 try {
289                                     engineName = (String JavaDoc)
290                                         mBeanServer.getAttribute(objectName,
291                                                                  "engineName");
292                                 } catch (Exception JavaDoc e) {
293                                     // Ignore
294
}
295                             }
296                         }
297                         // END SJSAS 6290785
298
}
299                 }
300             }
301
302             // At deployment time, engineName is always = null.
303
if ( (!"*".equals(domain)) &&
304                  ( !domain.equals(objectName.getDomain()) ) &&
305                  ( (!domain.equals(engineName) ) &&
306                    (engineName != null) ) ) {
307                 return;
308             }
309
310             log.debug( "Handle " + objectName );
311             if (notification.getType().equals
312                 (MBeanServerNotification.REGISTRATION_NOTIFICATION)) {
313                 String JavaDoc type=objectName.getKeyProperty("type");
314                 if( "Host".equals( type )) {
315                     try {
316                         registerHost(objectName);
317                     } catch (Exception JavaDoc e) {
318                         log.warn("Error registering Host " + objectName, e);
319                     }
320                 }
321     
322                 if (j2eeType != null) {
323                     if (j2eeType.equals("WebModule")) {
324                         try {
325                             registerContext(objectName);
326                         } catch (Throwable JavaDoc t) {
327                             log.warn("Error registering Context " + objectName,t);
328                         }
329                     } else if (j2eeType.equals("Servlet")) {
330                         try {
331                             registerWrapper(objectName);
332                         } catch (Throwable JavaDoc t) {
333                             log.warn("Error registering Wrapper " + objectName,t);
334                         }
335                     }
336                 }
337             } else if (notification.getType().equals
338                        (MBeanServerNotification.UNREGISTRATION_NOTIFICATION)) {
339                 String JavaDoc type=objectName.getKeyProperty("type");
340                 if( "Host".equals( type )) {
341                     try {
342                         unregisterHost(objectName);
343                     } catch (Exception JavaDoc e) {
344                         log.warn("Error unregistering Host " + objectName,e);
345                     }
346                 }
347  
348                 if (j2eeType != null) {
349                     if (j2eeType.equals("WebModule")) {
350                         try {
351                             unregisterContext(objectName);
352                         } catch (Throwable JavaDoc t) {
353                             log.warn("Error unregistering webapp " + objectName,t);
354                         }
355                     }
356                 }
357             }
358         }
359
360     }
361
362
363     // ------------------------------------------------------ Protected Methods
364

365     private void registerEngine()
366         throws Exception JavaDoc
367     {
368         ObjectName JavaDoc engineName = new ObjectName JavaDoc
369             (domain + ":type=Engine");
370         if ( ! mBeanServer.isRegistered(engineName)) return;
371         // BEGIN S1AS 5000999
372
/*
373         String defaultHost =
374             (String) mBeanServer.getAttribute(engineName, "defaultHost");
375     */

376         if (defaultHost == null) {
377             defaultHost =
378                 (String JavaDoc) mBeanServer.getAttribute(engineName, "defaultHost");
379         }
380         // END S1AS 5000999
381

382         ObjectName JavaDoc hostName = new ObjectName JavaDoc
383              (domain + ":type=Host," + "host=" + defaultHost);
384
385         if (!mBeanServer.isRegistered(hostName)) {
386
387             // Get the hosts' list
388
String JavaDoc onStr = domain + ":type=Host,*";
389             ObjectName JavaDoc objectName = new ObjectName JavaDoc(onStr);
390             Set JavaDoc set = mBeanServer.queryMBeans(objectName, null);
391             Iterator JavaDoc iterator = set.iterator();
392             String JavaDoc[] aliases;
393             boolean isRegisteredWithAlias = false;
394             
395             while (iterator.hasNext()) {
396
397                 if (isRegisteredWithAlias) break;
398             
399                 ObjectInstance JavaDoc oi = (ObjectInstance JavaDoc) iterator.next();
400                 hostName = oi.getObjectName();
401                 aliases = (String JavaDoc[])
402                     mBeanServer.invoke(hostName, "findAliases", null, null);
403
404                 for (int i=0; i < aliases.length; i++){
405                     if (aliases[i].equalsIgnoreCase(defaultHost)){
406                         isRegisteredWithAlias = true;
407                         break;
408                     }
409                 }
410             }
411             
412             if (!isRegisteredWithAlias)
413                 log.warn("Unknown default host: " + defaultHost);
414         }
415
416         // This should probablt be called later
417
if( defaultHost != null ) {
418             mapper.setDefaultHostName(defaultHost);
419         }
420     }
421
422     /**
423      * Register host.
424      */

425     private void registerHost(ObjectName JavaDoc objectName)
426         throws Exception JavaDoc {
427         String JavaDoc name=objectName.getKeyProperty("host");
428         if( name != null ) {
429             // BEGIN S1AS 5000999
430
/*
431              * Register the given Host only if one of its associated port
432              * numbers matches the port number of this MapperListener
433              */

434             int[] ports = (int[]) mBeanServer.invoke
435                     (objectName, "findPorts", null, null);
436             boolean portMatch = false;
437             if (ports != null) {
438                 for (int i=0; i<ports.length; i++) {
439                     if (ports[i] == this.port) {
440                         portMatch = true;
441                         break;
442                     }
443                 }
444             }
445             if (!portMatch) {
446                 return;
447             }
448             // END S1AS 5000999
449
String JavaDoc[] aliases = (String JavaDoc[])
450                 mBeanServer.invoke(objectName, "findAliases", null, null);
451             mapper.addHost(name, aliases, objectName);
452         }
453     }
454
455
456     /**
457      * Unregister host.
458      */

459     private void unregisterHost(ObjectName JavaDoc objectName)
460         throws Exception JavaDoc {
461         String JavaDoc name=objectName.getKeyProperty("host");
462         // BEGIN S1AS 5000999
463
if (name != null) {
464             int[] ports = (int[]) mBeanServer.invoke
465                     (objectName, "findPorts", null, null);
466             boolean portMatch = false;
467             if (ports != null) {
468                 for (int i=0; i<ports.length; i++) {
469                     if (ports[i] == this.port) {
470                         portMatch = true;
471                         break;
472                     }
473                 }
474             }
475             if (!portMatch) {
476                 return;
477             }
478         }
479         // END S1AS 5000999
480
mapper.removeHost(name);
481     }
482
483
484     /**
485      * Register context.
486      */

487     private void registerContext(ObjectName JavaDoc objectName)
488         throws Exception JavaDoc {
489
490         String JavaDoc name = objectName.getKeyProperty("name");
491         
492         // If the domain is the same with ours or the engine
493
// name attribute is the same... - then it's ours
494
String JavaDoc targetDomain=objectName.getDomain();
495         if( ! domain.equals( targetDomain )) {
496             try {
497                 targetDomain = (String JavaDoc) mBeanServer.getAttribute
498                     (objectName, "engineName");
499             } catch (Exception JavaDoc e) {
500                 // Ignore
501
}
502             if( ! domain.equals( targetDomain )) {
503                 // not ours
504
return;
505             }
506         }
507
508         String JavaDoc hostName = null;
509         String JavaDoc contextName = null;
510         if (name.startsWith("//")) {
511             name = name.substring(2);
512         }
513         int slash = name.indexOf("/");
514         if (slash != -1) {
515             hostName = name.substring(0, slash);
516             contextName = name.substring(slash);
517         } else {
518             return;
519         }
520         // Special case for the root context
521
if (contextName.equals("/")) {
522             contextName = "";
523         }
524
525         log.debug(sm.getString
526                   ("mapperListener.registerContext", contextName));
527
528         Object JavaDoc context =
529             mBeanServer.invoke(objectName, "findMappingObject", null, null);
530             //mBeanServer.getAttribute(objectName, "mappingObject");
531
javax.naming.Context JavaDoc resources = (javax.naming.Context JavaDoc)
532             mBeanServer.invoke(objectName, "findStaticResources", null, null);
533             //mBeanServer.getAttribute(objectName, "staticResources");
534
String JavaDoc[] welcomeFiles = (String JavaDoc[])
535             mBeanServer.getAttribute(objectName, "welcomeFiles");
536
537         mapper.addContext(hostName, contextName, context,
538                           welcomeFiles, resources);
539
540     }
541
542
543     /**
544      * Unregister context.
545      */

546     private void unregisterContext(ObjectName JavaDoc objectName)
547         throws Exception JavaDoc {
548
549         String JavaDoc name = objectName.getKeyProperty("name");
550
551         // If the domain is the same with ours or the engine
552
// name attribute is the same... - then it's ours
553
String JavaDoc targetDomain=objectName.getDomain();
554         if( ! domain.equals( targetDomain )) {
555             try {
556                 targetDomain = (String JavaDoc) mBeanServer.getAttribute
557                     (objectName, "engineName");
558             } catch (Exception JavaDoc e) {
559                 // Ignore
560
}
561             if( ! domain.equals( targetDomain )) {
562                 // not ours
563
return;
564             }
565         }
566
567         String JavaDoc hostName = null;
568         String JavaDoc contextName = null;
569         if (name.startsWith("//")) {
570             name = name.substring(2);
571         }
572         int slash = name.indexOf("/");
573         if (slash != -1) {
574             hostName = name.substring(0, slash);
575             contextName = name.substring(slash);
576         } else {
577             return;
578         }
579         // Special case for the root context
580
if (contextName.equals("/")) {
581             contextName = "";
582         }
583
584         log.debug(sm.getString
585                   ("mapperListener.unregisterContext", contextName));
586
587         mapper.removeContext(hostName, contextName);
588
589     }
590
591
592     /**
593      * Register wrapper.
594      */

595     private void registerWrapper(ObjectName JavaDoc objectName)
596         throws Exception JavaDoc {
597     
598         // If the domain is the same with ours or the engine
599
// name attribute is the same... - then it's ours
600
String JavaDoc targetDomain=objectName.getDomain();
601         if( ! domain.equals( targetDomain )) {
602             try {
603                 targetDomain=(String JavaDoc) mBeanServer.getAttribute(objectName, "engineName");
604             } catch (Exception JavaDoc e) {
605                 // Ignore
606
}
607             if( ! domain.equals( targetDomain )) {
608                 // not ours
609
return;
610             }
611             
612         }
613
614         String JavaDoc wrapperName = objectName.getKeyProperty("name");
615         String JavaDoc name = objectName.getKeyProperty("WebModule");
616
617         String JavaDoc hostName = null;
618         String JavaDoc contextName = null;
619         if (name.startsWith("//")) {
620             name = name.substring(2);
621         }
622         int slash = name.indexOf("/");
623         if (slash != -1) {
624             hostName = name.substring(0, slash);
625             contextName = name.substring(slash);
626         } else {
627             return;
628         }
629         // Special case for the root context
630
if (contextName.equals("/")) {
631             contextName = "";
632         }
633
634         log.debug(sm.getString
635                   ("mapperListener.registerWrapper",
636                    wrapperName, contextName));
637
638         String JavaDoc[] mappings = (String JavaDoc[])
639             mBeanServer.invoke(objectName, "findMappings", null, null);
640         Object JavaDoc wrapper =
641             mBeanServer.invoke(objectName, "findMappingObject", null, null);
642
643         for (int i = 0; i < mappings.length; i++) {
644             boolean jspWildCard = (wrapperName.equals("jsp")
645                                    && mappings[i].endsWith("/*"));
646             mapper.addWrapper(hostName, contextName, mappings[i], wrapper,
647                               jspWildCard);
648         }
649
650     }
651
652
653 }
654
Popular Tags