View Javadoc
1 /*** 2 * Redistribution and use of this software and associated documentation 3 * ("Software"), with or without modification, are permitted provided 4 * that the following conditions are met: 5 * 6 * 1. Redistributions of source code must retain copyright 7 * statements and notices. Redistributions must also contain a 8 * copy of this document. 9 * 10 * 2. Redistributions in binary form must reproduce the 11 * above copyright notice, this list of conditions and the 12 * following disclaimer in the documentation and/or other 13 * materials provided with the distribution. 14 * 15 * 3. The name "Exolab" must not be used to endorse or promote 16 * products derived from this Software without prior written 17 * permission of Exoffice Technologies. For written permission, 18 * please contact info@exolab.org. 19 * 20 * 4. Products derived from this Software may not be called "Exolab" 21 * nor may "Exolab" appear in their names without prior written 22 * permission of Exoffice Technologies. Exolab is a registered 23 * trademark of Exoffice Technologies. 24 * 25 * 5. Due credit should be given to the Exolab Project 26 * (http://www.exolab.org/). 27 * 28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 39 * OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * Copyright 2000 (C) Exoffice Technologies Inc. All Rights Reserved. 42 */ 43 44 package org.exolab.jms.messagemgr; 45 46 import javax.naming.CompositeName; 47 import javax.naming.Context; 48 import javax.naming.InvalidNameException; 49 import javax.naming.NameAlreadyBoundException; 50 import javax.naming.NameNotFoundException; 51 import javax.naming.NamingException; 52 53 import org.exolab.jms.client.JmsDestination; 54 55 56 /*** 57 * This class provides helper methods to register destinations in JNDI 58 * 59 * @version $Revision: 1.6 $ $Date: 2003/08/07 13:33:02 $ 60 * @author <a href="mailto:tima@intalio.com">Tim Anderson</a> 61 */ 62 class ContextHelper { 63 64 /*** 65 * Binds a name to an object, overwriting any existing binding. 66 * All intermediate contexts and the target context 67 * (that named by all but the terminal atomic component of the name) 68 * are created if they don't already exist. 69 * 70 * @param context the context to rebind to 71 * @param name a destination name, 72 * possibly containing one or more '.' 73 * @param destination the destination to bind 74 * @throws NamingException 75 */ 76 public static void rebind(Context context, String name, 77 JmsDestination destination) 78 throws NamingException { 79 CompositeName composite = getCompositeName(name); 80 Context subcontext = context; 81 String component = null; 82 83 for (int i = 0; i < composite.size() - 1; i++) { 84 component = composite.get(i); 85 if (component.length() == 0) { 86 throw new InvalidNameException("'" + name + 87 "' is not a valid name"); 88 } 89 Object object = null; 90 try { 91 object = subcontext.lookup(component); 92 if (!(object instanceof Context)) { 93 String subname = ""; 94 for (int j = 0; j <= i; j++) { 95 if (j > 0) { 96 subname += "."; 97 } 98 subname += composite.get(j); 99 } 100 throw new NameAlreadyBoundException("'" + subname + 101 "' is already bound"); 102 } else { 103 subcontext = (Context) object; 104 } 105 } catch (NameNotFoundException exception) { 106 subcontext = subcontext.createSubcontext(component); 107 } 108 } 109 110 component = composite.get(composite.size() - 1); 111 if (component.length() == 0) { 112 throw new InvalidNameException("'" + name + 113 "' is not a valid name"); 114 } 115 116 try { 117 Object object = subcontext.lookup(component); 118 if (object instanceof Context) { 119 throw new NameAlreadyBoundException("'" + name + 120 "' is already bound"); 121 } 122 } catch (NameNotFoundException ignore) { 123 } catch (NamingException exception) { 124 } 125 126 subcontext.rebind(component, destination); 127 } 128 129 /*** 130 * Unbinds the named object. Removes the terminal atomic name in name 131 * from the target context - that named by all but the terminal atomic 132 * part of name. 133 * 134 * @param context the context to unbind from 135 * @param name a destination name, possibly 136 * containing one or more '.' 137 * @throws NamingException 138 */ 139 public static void unbind(Context context, String name) 140 throws NamingException { 141 try { 142 Object object = context.lookup(name); 143 if (!(object instanceof JmsDestination)) { 144 throw new NamingException("Cannot unbind name='" + name + 145 "': it does not refer to a " + 146 "Destination"); 147 } 148 context.unbind(name); 149 } catch (NameNotFoundException ignore) { 150 } 151 } 152 153 private static CompositeName getCompositeName(String name) 154 throws NamingException { 155 // Need to replace the dots in destination names with a '/' in 156 // order for it to be parsed by CompositeName 157 CompositeName composite = new CompositeName(name.replace('.', '/')); 158 if (composite.size() == 0) { 159 throw new InvalidNameException("'" + name + 160 "' is not a valid name"); 161 } 162 return composite; 163 } 164 }

This page was automatically generated by Maven