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