Source Code Cross Referenced for NamingContextListener.java in  » Sevlet-Container » apache-tomcat-6.0.14 » org » apache » catalina » core » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Sevlet Container » apache tomcat 6.0.14 » org.apache.catalina.core 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * Licensed to the Apache Software Foundation (ASF) under one or more
0003:         * contributor license agreements.  See the NOTICE file distributed with
0004:         * this work for additional information regarding copyright ownership.
0005:         * The ASF licenses this file to You under the Apache License, Version 2.0
0006:         * (the "License"); you may not use this file except in compliance with
0007:         * the License.  You may obtain a copy of the License at
0008:         * 
0009:         *      http://www.apache.org/licenses/LICENSE-2.0
0010:         * 
0011:         * Unless required by applicable law or agreed to in writing, software
0012:         * distributed under the License is distributed on an "AS IS" BASIS,
0013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014:         * See the License for the specific language governing permissions and
0015:         * limitations under the License.
0016:         */
0017:
0018:        package org.apache.catalina.core;
0019:
0020:        import java.beans.PropertyChangeEvent;
0021:        import java.beans.PropertyChangeListener;
0022:        import java.net.MalformedURLException;
0023:        import java.net.URL;
0024:        import java.util.HashMap;
0025:        import java.util.Hashtable;
0026:        import java.util.Iterator;
0027:        import java.util.StringTokenizer;
0028:
0029:        import javax.management.MalformedObjectNameException;
0030:        import javax.management.ObjectName;
0031:        import javax.naming.NameAlreadyBoundException;
0032:        import javax.naming.NamingException;
0033:        import javax.naming.Reference;
0034:        import javax.naming.StringRefAddr;
0035:
0036:        import org.apache.catalina.Container;
0037:        import org.apache.catalina.ContainerEvent;
0038:        import org.apache.catalina.ContainerListener;
0039:        import org.apache.catalina.Context;
0040:        import org.apache.catalina.Engine;
0041:        import org.apache.catalina.Host;
0042:        import org.apache.catalina.Lifecycle;
0043:        import org.apache.catalina.LifecycleEvent;
0044:        import org.apache.catalina.LifecycleListener;
0045:        import org.apache.catalina.Server;
0046:        import org.apache.catalina.Service;
0047:        import org.apache.catalina.deploy.ContextEjb;
0048:        import org.apache.catalina.deploy.ContextEnvironment;
0049:        import org.apache.catalina.deploy.ContextHandler;
0050:        import org.apache.catalina.deploy.ContextLocalEjb;
0051:        import org.apache.catalina.deploy.ContextResource;
0052:        import org.apache.catalina.deploy.ContextResourceEnvRef;
0053:        import org.apache.catalina.deploy.ContextResourceLink;
0054:        import org.apache.catalina.deploy.ContextService;
0055:        import org.apache.catalina.deploy.ContextTransaction;
0056:        import org.apache.catalina.deploy.NamingResources;
0057:        import org.apache.catalina.util.StringManager;
0058:        import org.apache.juli.logging.Log;
0059:        import org.apache.juli.logging.LogFactory;
0060:        import org.apache.naming.ContextAccessController;
0061:        import org.apache.naming.ContextBindings;
0062:        import org.apache.naming.EjbRef;
0063:        import org.apache.naming.HandlerRef;
0064:        import org.apache.naming.NamingContext;
0065:        import org.apache.naming.ResourceEnvRef;
0066:        import org.apache.naming.ResourceLinkRef;
0067:        import org.apache.naming.ResourceRef;
0068:        import org.apache.naming.ServiceRef;
0069:        import org.apache.naming.TransactionRef;
0070:        import org.apache.tomcat.util.modeler.Registry;
0071:
0072:        /**
0073:         * Helper class used to initialize and populate the JNDI context associated
0074:         * with each context and server.
0075:         *
0076:         * @author Remy Maucherat
0077:         * @version $Revision: 527693 $ $Date: 2007-04-11 23:51:57 +0200 (mer., 11 avr. 2007) $
0078:         */
0079:
0080:        public class NamingContextListener implements  LifecycleListener,
0081:                ContainerListener, PropertyChangeListener {
0082:
0083:            private static Log log = LogFactory
0084:                    .getLog(NamingContextListener.class);
0085:
0086:            // ----------------------------------------------------- Instance Variables
0087:
0088:            protected Log logger = log;
0089:
0090:            /**
0091:             * Name of the associated naming context.
0092:             */
0093:            protected String name = "/";
0094:
0095:            /**
0096:             * Associated container.
0097:             */
0098:            protected Object container = null;
0099:
0100:            /**
0101:             * Initialized flag.
0102:             */
0103:            protected boolean initialized = false;
0104:
0105:            /**
0106:             * Associated naming resources.
0107:             */
0108:            protected NamingResources namingResources = null;
0109:
0110:            /**
0111:             * Associated JNDI context.
0112:             */
0113:            protected NamingContext namingContext = null;
0114:
0115:            /**
0116:             * Comp context.
0117:             */
0118:            protected javax.naming.Context compCtx = null;
0119:
0120:            /**
0121:             * Env context.
0122:             */
0123:            protected javax.naming.Context envCtx = null;
0124:
0125:            /**
0126:             * Objectnames hashtable.
0127:             */
0128:            protected HashMap objectNames = new HashMap();
0129:
0130:            /**
0131:             * The string manager for this package.
0132:             */
0133:            protected static StringManager sm = StringManager
0134:                    .getManager(Constants.Package);
0135:
0136:            // ------------------------------------------------------------- Properties
0137:
0138:            /**
0139:             * Return the "name" property.
0140:             */
0141:            public String getName() {
0142:                return (this .name);
0143:            }
0144:
0145:            /**
0146:             * Set the "name" property.
0147:             *
0148:             * @param name The new name
0149:             */
0150:            public void setName(String name) {
0151:                this .name = name;
0152:            }
0153:
0154:            /**
0155:             * Return the comp context.
0156:             */
0157:            public javax.naming.Context getCompContext() {
0158:                return this .compCtx;
0159:            }
0160:
0161:            /**
0162:             * Return the env context.
0163:             */
0164:            public javax.naming.Context getEnvContext() {
0165:                return this .envCtx;
0166:            }
0167:
0168:            /**
0169:             * Return the associated naming context.
0170:             */
0171:            public NamingContext getNamingContext() {
0172:                return (this .namingContext);
0173:            }
0174:
0175:            // ---------------------------------------------- LifecycleListener Methods
0176:
0177:            /**
0178:             * Acknowledge the occurrence of the specified event.
0179:             *
0180:             * @param event LifecycleEvent that has occurred
0181:             */
0182:            public void lifecycleEvent(LifecycleEvent event) {
0183:
0184:                container = event.getLifecycle();
0185:
0186:                if (container instanceof  Context) {
0187:                    namingResources = ((Context) container)
0188:                            .getNamingResources();
0189:                    logger = log;
0190:                } else if (container instanceof  Server) {
0191:                    namingResources = ((Server) container)
0192:                            .getGlobalNamingResources();
0193:                } else {
0194:                    return;
0195:                }
0196:
0197:                if (event.getType() == Lifecycle.START_EVENT) {
0198:
0199:                    if (initialized)
0200:                        return;
0201:
0202:                    Hashtable contextEnv = new Hashtable();
0203:                    try {
0204:                        namingContext = new NamingContext(contextEnv, getName());
0205:                    } catch (NamingException e) {
0206:                        // Never happens
0207:                    }
0208:                    ContextAccessController.setSecurityToken(getName(),
0209:                            container);
0210:                    ContextBindings.bindContext(container, namingContext,
0211:                            container);
0212:                    if (log.isDebugEnabled()) {
0213:                        log.debug("Bound " + container);
0214:                    }
0215:
0216:                    // Setting the context in read/write mode
0217:                    ContextAccessController.setWritable(getName(), container);
0218:
0219:                    try {
0220:                        createNamingContext();
0221:                    } catch (NamingException e) {
0222:                        logger.error(sm.getString(
0223:                                "naming.namingContextCreationFailed", e));
0224:                    }
0225:
0226:                    // Binding the naming context to the class loader
0227:                    if (container instanceof  Context) {
0228:                        // Setting the context in read only mode
0229:                        ContextAccessController.setReadOnly(getName());
0230:                        try {
0231:                            ContextBindings.bindClassLoader(container,
0232:                                    container, ((Container) container)
0233:                                            .getLoader().getClassLoader());
0234:                        } catch (NamingException e) {
0235:                            logger.error(sm.getString("naming.bindFailed", e));
0236:                        }
0237:                    }
0238:
0239:                    if (container instanceof  Server) {
0240:                        namingResources.addPropertyChangeListener(this );
0241:                        org.apache.naming.factory.ResourceLinkFactory
0242:                                .setGlobalContext(namingContext);
0243:                        try {
0244:                            ContextBindings
0245:                                    .bindClassLoader(container, container, this 
0246:                                            .getClass().getClassLoader());
0247:                        } catch (NamingException e) {
0248:                            logger.error(sm.getString("naming.bindFailed", e));
0249:                        }
0250:                        if (container instanceof  StandardServer) {
0251:                            ((StandardServer) container)
0252:                                    .setGlobalNamingContext(namingContext);
0253:                        }
0254:                    }
0255:
0256:                    initialized = true;
0257:
0258:                } else if (event.getType() == Lifecycle.STOP_EVENT) {
0259:
0260:                    if (!initialized)
0261:                        return;
0262:
0263:                    // Setting the context in read/write mode
0264:                    ContextAccessController.setWritable(getName(), container);
0265:                    ContextBindings.unbindContext(container, container);
0266:
0267:                    if (container instanceof  Context) {
0268:                        ContextBindings.unbindClassLoader(container, container,
0269:                                ((Container) container).getLoader()
0270:                                        .getClassLoader());
0271:                    }
0272:
0273:                    if (container instanceof  Server) {
0274:                        namingResources.removePropertyChangeListener(this );
0275:                        ContextBindings.unbindClassLoader(container, container,
0276:                                this .getClass().getClassLoader());
0277:                    }
0278:
0279:                    ContextAccessController.unsetSecurityToken(getName(),
0280:                            container);
0281:
0282:                    namingContext = null;
0283:                    envCtx = null;
0284:                    compCtx = null;
0285:                    initialized = false;
0286:
0287:                }
0288:
0289:            }
0290:
0291:            // ---------------------------------------------- ContainerListener Methods
0292:
0293:            /**
0294:             * Acknowledge the occurrence of the specified event.
0295:             * Note: Will never be called when the listener is associated to a Server,
0296:             * since it is not a Container.
0297:             *
0298:             * @param event ContainerEvent that has occurred
0299:             */
0300:            public void containerEvent(ContainerEvent event) {
0301:
0302:                if (!initialized)
0303:                    return;
0304:
0305:                // Setting the context in read/write mode
0306:                ContextAccessController.setWritable(getName(), container);
0307:
0308:                String type = event.getType();
0309:
0310:                if (type.equals("addEjb")) {
0311:
0312:                    String ejbName = (String) event.getData();
0313:                    if (ejbName != null) {
0314:                        ContextEjb ejb = namingResources.findEjb(ejbName);
0315:                        addEjb(ejb);
0316:                    }
0317:
0318:                } else if (type.equals("addEnvironment")) {
0319:
0320:                    String environmentName = (String) event.getData();
0321:                    if (environmentName != null) {
0322:                        ContextEnvironment env = namingResources
0323:                                .findEnvironment(environmentName);
0324:                        addEnvironment(env);
0325:                    }
0326:
0327:                } else if (type.equals("addLocalEjb")) {
0328:
0329:                    String localEjbName = (String) event.getData();
0330:                    if (localEjbName != null) {
0331:                        ContextLocalEjb localEjb = namingResources
0332:                                .findLocalEjb(localEjbName);
0333:                        addLocalEjb(localEjb);
0334:                    }
0335:
0336:                } else if (type.equals("addResource")) {
0337:
0338:                    String resourceName = (String) event.getData();
0339:                    if (resourceName != null) {
0340:                        ContextResource resource = namingResources
0341:                                .findResource(resourceName);
0342:                        addResource(resource);
0343:                    }
0344:
0345:                } else if (type.equals("addResourceLink")) {
0346:
0347:                    String resourceLinkName = (String) event.getData();
0348:                    if (resourceLinkName != null) {
0349:                        ContextResourceLink resourceLink = namingResources
0350:                                .findResourceLink(resourceLinkName);
0351:                        addResourceLink(resourceLink);
0352:                    }
0353:
0354:                } else if (type.equals("addResourceEnvRef")) {
0355:
0356:                    String resourceEnvRefName = (String) event.getData();
0357:                    if (resourceEnvRefName != null) {
0358:                        ContextResourceEnvRef resourceEnvRef = namingResources
0359:                                .findResourceEnvRef(resourceEnvRefName);
0360:                        addResourceEnvRef(resourceEnvRef);
0361:                    }
0362:
0363:                } else if (type.equals("addService")) {
0364:
0365:                    String serviceName = (String) event.getData();
0366:                    if (serviceName != null) {
0367:                        ContextService service = namingResources
0368:                                .findService(serviceName);
0369:                        addService(service);
0370:                    }
0371:
0372:                } else if (type.equals("removeEjb")) {
0373:
0374:                    String ejbName = (String) event.getData();
0375:                    if (ejbName != null) {
0376:                        removeEjb(ejbName);
0377:                    }
0378:
0379:                } else if (type.equals("removeEnvironment")) {
0380:
0381:                    String environmentName = (String) event.getData();
0382:                    if (environmentName != null) {
0383:                        removeEnvironment(environmentName);
0384:                    }
0385:
0386:                } else if (type.equals("removeLocalEjb")) {
0387:
0388:                    String localEjbName = (String) event.getData();
0389:                    if (localEjbName != null) {
0390:                        removeLocalEjb(localEjbName);
0391:                    }
0392:
0393:                } else if (type.equals("removeResource")) {
0394:
0395:                    String resourceName = (String) event.getData();
0396:                    if (resourceName != null) {
0397:                        removeResource(resourceName);
0398:                    }
0399:
0400:                } else if (type.equals("removeResourceLink")) {
0401:
0402:                    String resourceLinkName = (String) event.getData();
0403:                    if (resourceLinkName != null) {
0404:                        removeResourceLink(resourceLinkName);
0405:                    }
0406:
0407:                } else if (type.equals("removeResourceEnvRef")) {
0408:
0409:                    String resourceEnvRefName = (String) event.getData();
0410:                    if (resourceEnvRefName != null) {
0411:                        removeResourceEnvRef(resourceEnvRefName);
0412:                    }
0413:
0414:                } else if (type.equals("removeService")) {
0415:
0416:                    String serviceName = (String) event.getData();
0417:                    if (serviceName != null) {
0418:                        removeService(serviceName);
0419:                    }
0420:
0421:                }
0422:
0423:                // Setting the context in read only mode
0424:                ContextAccessController.setReadOnly(getName());
0425:
0426:            }
0427:
0428:            // ----------------------------------------- PropertyChangeListener Methods
0429:
0430:            /**
0431:             * Process property change events.  Currently, only listens to such events
0432:             * on the <code>NamingResources</code> instance for the global naming
0433:             * resources.
0434:             *
0435:             * @param event The property change event that has occurred
0436:             */
0437:            public void propertyChange(PropertyChangeEvent event) {
0438:
0439:                if (!initialized)
0440:                    return;
0441:
0442:                Object source = event.getSource();
0443:                if (source == namingResources) {
0444:
0445:                    // Setting the context in read/write mode
0446:                    ContextAccessController.setWritable(getName(), container);
0447:
0448:                    processGlobalResourcesChange(event.getPropertyName(), event
0449:                            .getOldValue(), event.getNewValue());
0450:
0451:                    // Setting the context in read only mode
0452:                    ContextAccessController.setReadOnly(getName());
0453:
0454:                }
0455:
0456:            }
0457:
0458:            // -------------------------------------------------------- Private Methods
0459:
0460:            /**
0461:             * Process a property change on the global naming resources, by making the
0462:             * corresponding addition or removal to the associated JNDI context.
0463:             *
0464:             * @param name Property name of the change to be processed
0465:             * @param oldValue The old value (or <code>null</code> if adding)
0466:             * @param newValue The new value (or <code>null</code> if removing)
0467:             */
0468:            private void processGlobalResourcesChange(String name,
0469:                    Object oldValue, Object newValue) {
0470:
0471:                // NOTE - It seems that the Context for global JNDI resources
0472:                // is left in read-write mode, so we do not have to change it here
0473:
0474:                if (name.equals("ejb")) {
0475:                    if (oldValue != null) {
0476:                        ContextEjb ejb = (ContextEjb) oldValue;
0477:                        if (ejb.getName() != null) {
0478:                            removeEjb(ejb.getName());
0479:                        }
0480:                    }
0481:                    if (newValue != null) {
0482:                        ContextEjb ejb = (ContextEjb) newValue;
0483:                        if (ejb.getName() != null) {
0484:                            addEjb(ejb);
0485:                        }
0486:                    }
0487:                } else if (name.equals("environment")) {
0488:                    if (oldValue != null) {
0489:                        ContextEnvironment env = (ContextEnvironment) oldValue;
0490:                        if (env.getName() != null) {
0491:                            removeEnvironment(env.getName());
0492:                        }
0493:                    }
0494:                    if (newValue != null) {
0495:                        ContextEnvironment env = (ContextEnvironment) newValue;
0496:                        if (env.getName() != null) {
0497:                            addEnvironment(env);
0498:                        }
0499:                    }
0500:                } else if (name.equals("localEjb")) {
0501:                    if (oldValue != null) {
0502:                        ContextLocalEjb ejb = (ContextLocalEjb) oldValue;
0503:                        if (ejb.getName() != null) {
0504:                            removeLocalEjb(ejb.getName());
0505:                        }
0506:                    }
0507:                    if (newValue != null) {
0508:                        ContextLocalEjb ejb = (ContextLocalEjb) newValue;
0509:                        if (ejb.getName() != null) {
0510:                            addLocalEjb(ejb);
0511:                        }
0512:                    }
0513:                } else if (name.equals("resource")) {
0514:                    if (oldValue != null) {
0515:                        ContextResource resource = (ContextResource) oldValue;
0516:                        if (resource.getName() != null) {
0517:                            removeResource(resource.getName());
0518:                        }
0519:                    }
0520:                    if (newValue != null) {
0521:                        ContextResource resource = (ContextResource) newValue;
0522:                        if (resource.getName() != null) {
0523:                            addResource(resource);
0524:                        }
0525:                    }
0526:                } else if (name.equals("resourceEnvRef")) {
0527:                    if (oldValue != null) {
0528:                        ContextResourceEnvRef resourceEnvRef = (ContextResourceEnvRef) oldValue;
0529:                        if (resourceEnvRef.getName() != null) {
0530:                            removeResourceEnvRef(resourceEnvRef.getName());
0531:                        }
0532:                    }
0533:                    if (newValue != null) {
0534:                        ContextResourceEnvRef resourceEnvRef = (ContextResourceEnvRef) newValue;
0535:                        if (resourceEnvRef.getName() != null) {
0536:                            addResourceEnvRef(resourceEnvRef);
0537:                        }
0538:                    }
0539:                } else if (name.equals("resourceLink")) {
0540:                    if (oldValue != null) {
0541:                        ContextResourceLink rl = (ContextResourceLink) oldValue;
0542:                        if (rl.getName() != null) {
0543:                            removeResourceLink(rl.getName());
0544:                        }
0545:                    }
0546:                    if (newValue != null) {
0547:                        ContextResourceLink rl = (ContextResourceLink) newValue;
0548:                        if (rl.getName() != null) {
0549:                            addResourceLink(rl);
0550:                        }
0551:                    }
0552:                } else if (name.equals("service")) {
0553:                    if (oldValue != null) {
0554:                        ContextService service = (ContextService) oldValue;
0555:                        if (service.getName() != null) {
0556:                            removeService(service.getName());
0557:                        }
0558:                    }
0559:                    if (newValue != null) {
0560:                        ContextService service = (ContextService) newValue;
0561:                        if (service.getName() != null) {
0562:                            addService(service);
0563:                        }
0564:                    }
0565:                }
0566:
0567:            }
0568:
0569:            /**
0570:             * Create and initialize the JNDI naming context.
0571:             */
0572:            private void createNamingContext() throws NamingException {
0573:
0574:                // Creating the comp subcontext
0575:                if (container instanceof  Server) {
0576:                    compCtx = namingContext;
0577:                    envCtx = namingContext;
0578:                } else {
0579:                    compCtx = namingContext.createSubcontext("comp");
0580:                    envCtx = compCtx.createSubcontext("env");
0581:                }
0582:
0583:                int i;
0584:
0585:                if (log.isDebugEnabled())
0586:                    log.debug("Creating JNDI naming context");
0587:
0588:                if (namingResources == null) {
0589:                    namingResources = new NamingResources();
0590:                    namingResources.setContainer(container);
0591:                }
0592:
0593:                // Resource links
0594:                ContextResourceLink[] resourceLinks = namingResources
0595:                        .findResourceLinks();
0596:                for (i = 0; i < resourceLinks.length; i++) {
0597:                    addResourceLink(resourceLinks[i]);
0598:                }
0599:
0600:                // Resources
0601:                ContextResource[] resources = namingResources.findResources();
0602:                for (i = 0; i < resources.length; i++) {
0603:                    addResource(resources[i]);
0604:                }
0605:
0606:                // Resources Env
0607:                ContextResourceEnvRef[] resourceEnvRefs = namingResources
0608:                        .findResourceEnvRefs();
0609:                for (i = 0; i < resourceEnvRefs.length; i++) {
0610:                    addResourceEnvRef(resourceEnvRefs[i]);
0611:                }
0612:
0613:                // Environment entries
0614:                ContextEnvironment[] contextEnvironments = namingResources
0615:                        .findEnvironments();
0616:                for (i = 0; i < contextEnvironments.length; i++) {
0617:                    addEnvironment(contextEnvironments[i]);
0618:                }
0619:
0620:                // EJB references
0621:                ContextEjb[] ejbs = namingResources.findEjbs();
0622:                for (i = 0; i < ejbs.length; i++) {
0623:                    addEjb(ejbs[i]);
0624:                }
0625:
0626:                // WebServices references
0627:                ContextService[] services = namingResources.findServices();
0628:                for (i = 0; i < services.length; i++) {
0629:                    addService(services[i]);
0630:                }
0631:
0632:                // Binding a User Transaction reference
0633:                if (container instanceof  Context) {
0634:                    try {
0635:                        Reference ref = new TransactionRef();
0636:                        compCtx.bind("UserTransaction", ref);
0637:                        ContextTransaction transaction = namingResources
0638:                                .getTransaction();
0639:                        if (transaction != null) {
0640:                            Iterator params = transaction.listProperties();
0641:                            while (params.hasNext()) {
0642:                                String paramName = (String) params.next();
0643:                                String paramValue = (String) transaction
0644:                                        .getProperty(paramName);
0645:                                StringRefAddr refAddr = new StringRefAddr(
0646:                                        paramName, paramValue);
0647:                                ref.add(refAddr);
0648:                            }
0649:                        }
0650:                    } catch (NameAlreadyBoundException e) {
0651:                        // Ignore because UserTransaction was obviously 
0652:                        // added via ResourceLink
0653:                    } catch (NamingException e) {
0654:                        logger.error(sm.getString("naming.bindFailed", e));
0655:                    }
0656:                }
0657:
0658:                // Binding the resources directory context
0659:                if (container instanceof  Context) {
0660:                    try {
0661:                        compCtx.bind("Resources", ((Container) container)
0662:                                .getResources());
0663:                    } catch (NamingException e) {
0664:                        logger.error(sm.getString("naming.bindFailed", e));
0665:                    }
0666:                }
0667:
0668:            }
0669:
0670:            /**
0671:             * Create an <code>ObjectName</code> for this
0672:             * <code>ContextResource</code> object.
0673:             *
0674:             * @param resource The resource
0675:             * @return ObjectName The object name
0676:             * @exception MalformedObjectNameException if a name cannot be created
0677:             */
0678:            protected ObjectName createObjectName(ContextResource resource)
0679:                    throws MalformedObjectNameException {
0680:
0681:                String domain = null;
0682:                if (container instanceof  StandardServer) {
0683:                    domain = ((StandardServer) container).getDomain();
0684:                } else if (container instanceof  ContainerBase) {
0685:                    domain = ((ContainerBase) container).getDomain();
0686:                }
0687:                if (domain == null) {
0688:                    domain = "Catalina";
0689:                }
0690:
0691:                ObjectName name = null;
0692:                String quotedResourceName = ObjectName
0693:                        .quote(resource.getName());
0694:                if (container instanceof  Server) {
0695:                    name = new ObjectName(domain + ":type=DataSource"
0696:                            + ",class=" + resource.getType() + ",name="
0697:                            + quotedResourceName);
0698:                } else if (container instanceof  Context) {
0699:                    String path = ((Context) container).getPath();
0700:                    if (path.length() < 1)
0701:                        path = "/";
0702:                    Host host = (Host) ((Context) container).getParent();
0703:                    Engine engine = (Engine) host.getParent();
0704:                    Service service = engine.getService();
0705:                    name = new ObjectName(domain + ":type=DataSource"
0706:                            + ",path=" + path + ",host=" + host.getName()
0707:                            + ",class=" + resource.getType() + ",name="
0708:                            + quotedResourceName);
0709:                }
0710:
0711:                return (name);
0712:
0713:            }
0714:
0715:            /**
0716:             * Set the specified EJBs in the naming context.
0717:             */
0718:            public void addEjb(ContextEjb ejb) {
0719:
0720:                // Create a reference to the EJB.
0721:                Reference ref = new EjbRef(ejb.getType(), ejb.getHome(), ejb
0722:                        .getRemote(), ejb.getLink());
0723:                // Adding the additional parameters, if any
0724:                Iterator params = ejb.listProperties();
0725:                while (params.hasNext()) {
0726:                    String paramName = (String) params.next();
0727:                    String paramValue = (String) ejb.getProperty(paramName);
0728:                    StringRefAddr refAddr = new StringRefAddr(paramName,
0729:                            paramValue);
0730:                    ref.add(refAddr);
0731:                }
0732:                try {
0733:                    createSubcontexts(envCtx, ejb.getName());
0734:                    envCtx.bind(ejb.getName(), ref);
0735:                } catch (NamingException e) {
0736:                    logger.error(sm.getString("naming.bindFailed", e));
0737:                }
0738:
0739:            }
0740:
0741:            /**
0742:             * Set the specified environment entries in the naming context.
0743:             */
0744:            public void addEnvironment(ContextEnvironment env) {
0745:
0746:                Object value = null;
0747:                // Instantiating a new instance of the correct object type, and
0748:                // initializing it.
0749:                String type = env.getType();
0750:                try {
0751:                    if (type.equals("java.lang.String")) {
0752:                        value = env.getValue();
0753:                    } else if (type.equals("java.lang.Byte")) {
0754:                        if (env.getValue() == null) {
0755:                            value = new Byte((byte) 0);
0756:                        } else {
0757:                            value = Byte.decode(env.getValue());
0758:                        }
0759:                    } else if (type.equals("java.lang.Short")) {
0760:                        if (env.getValue() == null) {
0761:                            value = new Short((short) 0);
0762:                        } else {
0763:                            value = Short.decode(env.getValue());
0764:                        }
0765:                    } else if (type.equals("java.lang.Integer")) {
0766:                        if (env.getValue() == null) {
0767:                            value = new Integer(0);
0768:                        } else {
0769:                            value = Integer.decode(env.getValue());
0770:                        }
0771:                    } else if (type.equals("java.lang.Long")) {
0772:                        if (env.getValue() == null) {
0773:                            value = new Long(0);
0774:                        } else {
0775:                            value = Long.decode(env.getValue());
0776:                        }
0777:                    } else if (type.equals("java.lang.Boolean")) {
0778:                        value = Boolean.valueOf(env.getValue());
0779:                    } else if (type.equals("java.lang.Double")) {
0780:                        if (env.getValue() == null) {
0781:                            value = new Double(0);
0782:                        } else {
0783:                            value = Double.valueOf(env.getValue());
0784:                        }
0785:                    } else if (type.equals("java.lang.Float")) {
0786:                        if (env.getValue() == null) {
0787:                            value = new Float(0);
0788:                        } else {
0789:                            value = Float.valueOf(env.getValue());
0790:                        }
0791:                    } else if (type.equals("java.lang.Character")) {
0792:                        if (env.getValue() == null) {
0793:                            value = new Character((char) 0);
0794:                        } else {
0795:                            if (env.getValue().length() == 1) {
0796:                                value = new Character(env.getValue().charAt(0));
0797:                            } else {
0798:                                throw new IllegalArgumentException();
0799:                            }
0800:                        }
0801:                    } else {
0802:                        logger.error(sm.getString("naming.invalidEnvEntryType",
0803:                                env.getName()));
0804:                    }
0805:                } catch (NumberFormatException e) {
0806:                    logger.error(sm.getString("naming.invalidEnvEntryValue",
0807:                            env.getName()));
0808:                } catch (IllegalArgumentException e) {
0809:                    logger.error(sm.getString("naming.invalidEnvEntryValue",
0810:                            env.getName()));
0811:                }
0812:
0813:                // Binding the object to the appropriate name
0814:                if (value != null) {
0815:                    try {
0816:                        if (logger.isDebugEnabled())
0817:                            logger.debug("  Adding environment entry "
0818:                                    + env.getName());
0819:                        createSubcontexts(envCtx, env.getName());
0820:                        envCtx.bind(env.getName(), value);
0821:                    } catch (NamingException e) {
0822:                        logger.error(sm.getString(
0823:                                "naming.invalidEnvEntryValue", e));
0824:                    }
0825:                }
0826:
0827:            }
0828:
0829:            /**
0830:             * Set the specified local EJBs in the naming context.
0831:             */
0832:            public void addLocalEjb(ContextLocalEjb localEjb) {
0833:
0834:            }
0835:
0836:            /**
0837:             * Set the specified web service in the naming context.
0838:             */
0839:            public void addService(ContextService service) {
0840:
0841:                if (service.getWsdlfile() != null) {
0842:                    URL wsdlURL = null;
0843:
0844:                    try {
0845:                        wsdlURL = new URL(service.getWsdlfile());
0846:                    } catch (MalformedURLException e) {
0847:                        wsdlURL = null;
0848:                    }
0849:                    if (wsdlURL == null) {
0850:                        try {
0851:                            wsdlURL = ((Context) container).getServletContext()
0852:                                    .getResource(service.getWsdlfile());
0853:                        } catch (MalformedURLException e) {
0854:                            wsdlURL = null;
0855:                        }
0856:                    }
0857:                    if (wsdlURL == null) {
0858:                        try {
0859:                            wsdlURL = ((Context) container).getServletContext()
0860:                                    .getResource("/" + service.getWsdlfile());
0861:                            logger
0862:                                    .debug("  Changing service ref wsdl file for /"
0863:                                            + service.getWsdlfile());
0864:                        } catch (MalformedURLException e) {
0865:                            logger.error(sm.getString("naming.wsdlFailed", e));
0866:                        }
0867:                    }
0868:                    if (wsdlURL == null)
0869:                        service.setWsdlfile(null);
0870:                    else
0871:                        service.setWsdlfile(wsdlURL.toString());
0872:                }
0873:
0874:                if (service.getJaxrpcmappingfile() != null) {
0875:                    URL jaxrpcURL = null;
0876:
0877:                    try {
0878:                        jaxrpcURL = new URL(service.getJaxrpcmappingfile());
0879:                    } catch (MalformedURLException e) {
0880:                        jaxrpcURL = null;
0881:                    }
0882:                    if (jaxrpcURL == null) {
0883:                        try {
0884:                            jaxrpcURL = ((Context) container)
0885:                                    .getServletContext().getResource(
0886:                                            service.getJaxrpcmappingfile());
0887:                        } catch (MalformedURLException e) {
0888:                            jaxrpcURL = null;
0889:                        }
0890:                    }
0891:                    if (jaxrpcURL == null) {
0892:                        try {
0893:                            jaxrpcURL = ((Context) container)
0894:                                    .getServletContext()
0895:                                    .getResource(
0896:                                            "/"
0897:                                                    + service
0898:                                                            .getJaxrpcmappingfile());
0899:                            logger
0900:                                    .debug("  Changing service ref jaxrpc file for /"
0901:                                            + service.getJaxrpcmappingfile());
0902:                        } catch (MalformedURLException e) {
0903:                            logger.error(sm.getString("naming.wsdlFailed", e));
0904:                        }
0905:                    }
0906:                    if (jaxrpcURL == null)
0907:                        service.setJaxrpcmappingfile(null);
0908:                    else
0909:                        service.setJaxrpcmappingfile(jaxrpcURL.toString());
0910:                }
0911:
0912:                // Create a reference to the resource.
0913:                Reference ref = new ServiceRef(service.getName(), service
0914:                        .getType(), service.getServiceqname(), service
0915:                        .getWsdlfile(), service.getJaxrpcmappingfile());
0916:                // Adding the additional port-component-ref, if any
0917:                Iterator portcomponent = service.getServiceendpoints();
0918:                while (portcomponent.hasNext()) {
0919:                    String serviceendpoint = (String) portcomponent.next();
0920:                    StringRefAddr refAddr = new StringRefAddr(
0921:                            ServiceRef.SERVICEENDPOINTINTERFACE,
0922:                            serviceendpoint);
0923:                    ref.add(refAddr);
0924:                    String portlink = (String) service
0925:                            .getPortlink(serviceendpoint);
0926:                    refAddr = new StringRefAddr(ServiceRef.PORTCOMPONENTLINK,
0927:                            portlink);
0928:                    ref.add(refAddr);
0929:                }
0930:                // Adding the additional parameters, if any
0931:                Iterator handlers = service.getHandlers();
0932:                while (handlers.hasNext()) {
0933:                    String handlername = (String) handlers.next();
0934:                    ContextHandler handler = (ContextHandler) service
0935:                            .getHandler(handlername);
0936:                    HandlerRef handlerRef = new HandlerRef(handlername, handler
0937:                            .getHandlerclass());
0938:                    Iterator localParts = handler.getLocalparts();
0939:                    while (localParts.hasNext()) {
0940:                        String localPart = (String) localParts.next();
0941:                        String namespaceURI = (String) handler
0942:                                .getNamespaceuri(localPart);
0943:                        handlerRef.add(new StringRefAddr(
0944:                                HandlerRef.HANDLER_LOCALPART, localPart));
0945:                        handlerRef.add(new StringRefAddr(
0946:                                HandlerRef.HANDLER_NAMESPACE, namespaceURI));
0947:                    }
0948:                    Iterator params = handler.listProperties();
0949:                    while (params.hasNext()) {
0950:                        String paramName = (String) params.next();
0951:                        String paramValue = (String) handler
0952:                                .getProperty(paramName);
0953:                        handlerRef.add(new StringRefAddr(
0954:                                HandlerRef.HANDLER_PARAMNAME, paramName));
0955:                        handlerRef.add(new StringRefAddr(
0956:                                HandlerRef.HANDLER_PARAMVALUE, paramValue));
0957:                    }
0958:                    for (int i = 0; i < handler.getSoapRolesSize(); i++) {
0959:                        handlerRef.add(new StringRefAddr(
0960:                                HandlerRef.HANDLER_SOAPROLE, handler
0961:                                        .getSoapRole(i)));
0962:                    }
0963:                    for (int i = 0; i < handler.getPortNamesSize(); i++) {
0964:                        handlerRef.add(new StringRefAddr(
0965:                                HandlerRef.HANDLER_PORTNAME, handler
0966:                                        .getPortName(i)));
0967:                    }
0968:                    ((ServiceRef) ref).addHandler(handlerRef);
0969:                }
0970:
0971:                try {
0972:                    if (logger.isDebugEnabled()) {
0973:                        logger.debug("  Adding service ref "
0974:                                + service.getName() + "  " + ref);
0975:                    }
0976:                    createSubcontexts(envCtx, service.getName());
0977:                    envCtx.bind(service.getName(), ref);
0978:                } catch (NamingException e) {
0979:                    logger.error(sm.getString("naming.bindFailed", e));
0980:                }
0981:
0982:            }
0983:
0984:            /**
0985:             * Set the specified resources in the naming context.
0986:             */
0987:            public void addResource(ContextResource resource) {
0988:
0989:                // Create a reference to the resource.
0990:                Reference ref = new ResourceRef(resource.getType(), resource
0991:                        .getDescription(), resource.getScope(), resource
0992:                        .getAuth());
0993:                // Adding the additional parameters, if any
0994:                Iterator params = resource.listProperties();
0995:                while (params.hasNext()) {
0996:                    String paramName = (String) params.next();
0997:                    String paramValue = (String) resource
0998:                            .getProperty(paramName);
0999:                    StringRefAddr refAddr = new StringRefAddr(paramName,
1000:                            paramValue);
1001:                    ref.add(refAddr);
1002:                }
1003:                try {
1004:                    if (logger.isDebugEnabled()) {
1005:                        logger.debug("  Adding resource ref "
1006:                                + resource.getName() + "  " + ref);
1007:                    }
1008:                    createSubcontexts(envCtx, resource.getName());
1009:                    envCtx.bind(resource.getName(), ref);
1010:                } catch (NamingException e) {
1011:                    logger.error(sm.getString("naming.bindFailed", e));
1012:                }
1013:
1014:                if ("javax.sql.DataSource".equals(ref.getClassName())) {
1015:                    try {
1016:                        ObjectName on = createObjectName(resource);
1017:                        Object actualResource = envCtx.lookup(resource
1018:                                .getName());
1019:                        Registry.getRegistry(null, null).registerComponent(
1020:                                actualResource, on, null);
1021:                        objectNames.put(resource.getName(), on);
1022:                    } catch (Exception e) {
1023:                        logger.warn(sm.getString(
1024:                                "naming.jmxRegistrationFailed", e));
1025:                    }
1026:                }
1027:
1028:            }
1029:
1030:            /**
1031:             * Set the specified resources in the naming context.
1032:             */
1033:            public void addResourceEnvRef(ContextResourceEnvRef resourceEnvRef) {
1034:
1035:                // Create a reference to the resource env.
1036:                Reference ref = new ResourceEnvRef(resourceEnvRef.getType());
1037:                // Adding the additional parameters, if any
1038:                Iterator params = resourceEnvRef.listProperties();
1039:                while (params.hasNext()) {
1040:                    String paramName = (String) params.next();
1041:                    String paramValue = (String) resourceEnvRef
1042:                            .getProperty(paramName);
1043:                    StringRefAddr refAddr = new StringRefAddr(paramName,
1044:                            paramValue);
1045:                    ref.add(refAddr);
1046:                }
1047:                try {
1048:                    if (logger.isDebugEnabled())
1049:                        log.debug("  Adding resource env ref "
1050:                                + resourceEnvRef.getName());
1051:                    createSubcontexts(envCtx, resourceEnvRef.getName());
1052:                    envCtx.bind(resourceEnvRef.getName(), ref);
1053:                } catch (NamingException e) {
1054:                    logger.error(sm.getString("naming.bindFailed", e));
1055:                }
1056:
1057:            }
1058:
1059:            /**
1060:             * Set the specified resource link in the naming context.
1061:             */
1062:            public void addResourceLink(ContextResourceLink resourceLink) {
1063:
1064:                // Create a reference to the resource.
1065:                Reference ref = new ResourceLinkRef(resourceLink.getType(),
1066:                        resourceLink.getGlobal());
1067:                javax.naming.Context ctx = "UserTransaction"
1068:                        .equals(resourceLink.getName()) ? compCtx : envCtx;
1069:                try {
1070:                    if (logger.isDebugEnabled())
1071:                        log.debug("  Adding resource link "
1072:                                + resourceLink.getName());
1073:                    createSubcontexts(envCtx, resourceLink.getName());
1074:                    ctx.bind(resourceLink.getName(), ref);
1075:                } catch (NamingException e) {
1076:                    logger.error(sm.getString("naming.bindFailed", e));
1077:                }
1078:
1079:            }
1080:
1081:            /**
1082:             * Set the specified EJBs in the naming context.
1083:             */
1084:            public void removeEjb(String name) {
1085:
1086:                try {
1087:                    envCtx.unbind(name);
1088:                } catch (NamingException e) {
1089:                    logger.error(sm.getString("naming.unbindFailed", e));
1090:                }
1091:
1092:            }
1093:
1094:            /**
1095:             * Set the specified environment entries in the naming context.
1096:             */
1097:            public void removeEnvironment(String name) {
1098:
1099:                try {
1100:                    envCtx.unbind(name);
1101:                } catch (NamingException e) {
1102:                    logger.error(sm.getString("naming.unbindFailed", e));
1103:                }
1104:
1105:            }
1106:
1107:            /**
1108:             * Set the specified local EJBs in the naming context.
1109:             */
1110:            public void removeLocalEjb(String name) {
1111:
1112:                try {
1113:                    envCtx.unbind(name);
1114:                } catch (NamingException e) {
1115:                    logger.error(sm.getString("naming.unbindFailed", e));
1116:                }
1117:
1118:            }
1119:
1120:            /**
1121:             * Set the specified web services in the naming context.
1122:             */
1123:            public void removeService(String name) {
1124:
1125:                try {
1126:                    envCtx.unbind(name);
1127:                } catch (NamingException e) {
1128:                    logger.error(sm.getString("naming.unbindFailed", e));
1129:                }
1130:
1131:            }
1132:
1133:            /**
1134:             * Set the specified resources in the naming context.
1135:             */
1136:            public void removeResource(String name) {
1137:
1138:                try {
1139:                    envCtx.unbind(name);
1140:                } catch (NamingException e) {
1141:                    logger.error(sm.getString("naming.unbindFailed", e));
1142:                }
1143:
1144:                ObjectName on = (ObjectName) objectNames.get(name);
1145:                if (on != null) {
1146:                    Registry.getRegistry(null, null).unregisterComponent(on);
1147:                }
1148:
1149:            }
1150:
1151:            /**
1152:             * Set the specified resources in the naming context.
1153:             */
1154:            public void removeResourceEnvRef(String name) {
1155:
1156:                try {
1157:                    envCtx.unbind(name);
1158:                } catch (NamingException e) {
1159:                    logger.error(sm.getString("naming.unbindFailed", e));
1160:                }
1161:
1162:            }
1163:
1164:            /**
1165:             * Set the specified resources in the naming context.
1166:             */
1167:            public void removeResourceLink(String name) {
1168:
1169:                try {
1170:                    envCtx.unbind(name);
1171:                } catch (NamingException e) {
1172:                    logger.error(sm.getString("naming.unbindFailed", e));
1173:                }
1174:
1175:            }
1176:
1177:            /**
1178:             * Create all intermediate subcontexts.
1179:             */
1180:            private void createSubcontexts(javax.naming.Context ctx, String name)
1181:                    throws NamingException {
1182:                javax.naming.Context currentContext = ctx;
1183:                StringTokenizer tokenizer = new StringTokenizer(name, "/");
1184:                while (tokenizer.hasMoreTokens()) {
1185:                    String token = tokenizer.nextToken();
1186:                    if ((!token.equals("")) && (tokenizer.hasMoreTokens())) {
1187:                        try {
1188:                            currentContext = currentContext
1189:                                    .createSubcontext(token);
1190:                        } catch (NamingException e) {
1191:                            // Silent catch. Probably an object is already bound in
1192:                            // the context.
1193:                            currentContext = (javax.naming.Context) currentContext
1194:                                    .lookup(token);
1195:                        }
1196:                    }
1197:                }
1198:            }
1199:
1200:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.