001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.naming;
019:
020: import org.apache.harmony.jndi.internal.nls.Messages;
021:
022: /**
023: * This is a type of <code>Reference</code> used to point to an address of
024: * type "LinkAddress" where the address given is actually the string
025: * representation of a valid <code>Name</code>.
026: *
027: * @see Reference
028: */
029: public class LinkRef extends Reference {
030:
031: /*
032: * This constant is used during deserialization to check the version which
033: * created the serialized object.
034: */
035: static final long serialVersionUID = -5386290613498931298L;
036:
037: /*
038: * The type name of the address this LinkRef points to.
039: */
040: private static final String ADDR_TYPE = "LinkAddress"; //$NON-NLS-1$
041:
042: /**
043: * Constructs a <code>LinkRef</code> instance using the supplied
044: * <code>name
045: * </code> of <code>Name</code> representation. The class name
046: * is set to the name of this <code>LinkRef</code> class. The factory
047: * class and location default to null. There is one address entry which has
048: * "LinkAddress" as the address type and the string representation of the
049: * supplied name as the address.
050: *
051: * @param name
052: * the <code>Name</code> to be used as a link which cannot be
053: * null
054: */
055: public LinkRef(Name name) {
056: this (name.toString());
057: }
058:
059: /**
060: * Constructs a <code>LinkRef</code> instance using the supplied
061: * <code>name
062: * </code> of <code>String</code> representation. The class
063: * name is set to the name of this <code>LinkRef</code> class. The factory
064: * class and location default to null. There is one address entry which has
065: * "LinkAddress" as the address type and the string representation of the
066: * supplied name as the address.
067: *
068: * @param s
069: * the name to be used as a link which cannot be null
070: */
071: public LinkRef(String s) {
072: super (LinkRef.class.getName(), new StringRefAddr(ADDR_TYPE, s));
073: }
074:
075: /**
076: * Gets the string representation of the name used as a link which cannot be
077: * null.
078: *
079: * @return the string representation of the name used as a link
080: * @throws MalformedLinkException
081: * If this is not a <code>Reference</code> with a class name
082: * which matches the name of this LinkRef class.
083: * @throws NamingException
084: * If other <code>NamingException</code> is encountered.
085: */
086: public String getLinkName() throws NamingException {
087: if (!LinkRef.class.getName().equals(this .getClassName())) {
088: // jndi.11=This is an invalid LinkRef object\!
089: throw new MalformedLinkException(Messages
090: .getString("jndi.11")); //$NON-NLS-1$
091: }
092: try {
093: RefAddr addr = get(ADDR_TYPE);
094: if (null == addr) {
095: // jndi.12=There is no address with type: {0}
096: throw new MalformedLinkException(Messages.getString(
097: "jndi.12", ADDR_TYPE)); //$NON-NLS-1$
098: }
099: return (String) addr.getContent();
100: } catch (NullPointerException e) {
101: throw new MalformedLinkException(e.getMessage());
102: }
103: }
104:
105: }
|