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 package org.apache.xbean.naming.reference; 018 019 import java.util.Enumeration; 020 import java.util.Hashtable; 021 import java.util.NoSuchElementException; 022 import javax.naming.Context; 023 import javax.naming.Name; 024 import javax.naming.RefAddr; 025 import javax.naming.Reference; 026 import javax.naming.NamingException; 027 import javax.naming.spi.ObjectFactory; 028 029 /** 030 * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $ 031 */ 032 public abstract class SimpleReference extends Reference { 033 private static final Enumeration<RefAddr> EMPTY_ENUMERATION = new Enumeration<RefAddr>() { 034 public boolean hasMoreElements() { 035 return false; 036 } 037 038 public RefAddr nextElement() { 039 throw new NoSuchElementException(); 040 } 041 }; 042 043 public SimpleReference() { 044 super(null); 045 } 046 047 /** 048 * Gets the actual referenced Object. 049 * @return the referenced object 050 * @throws javax.naming.NamingException on error 051 */ 052 public abstract Object getContent() throws NamingException; 053 054 /** 055 * We will atleast return an Object. Subclasses may want to provide a more specific class. 056 * @return "java.lang.Object" 057 */ 058 public String getClassName() { 059 return "java.lang.Object"; 060 } 061 062 /** 063 * If the JNDI context does not understand simple references, this method will be called 064 * to obtain the class name of a factory. This factory in turn understands the simple 065 * reference. This style is much slower because JNDI will use reflection to load and 066 * create this class. 067 * @return factory class name 068 */ 069 public final String getFactoryClassName() { 070 return SimpleObjectFactory.class.getName(); 071 } 072 073 // 074 // Disabled methods that we no longer need 075 // 076 public final String getFactoryClassLocation() { 077 return null; 078 } 079 080 public final RefAddr get(String addrType) { 081 return null; 082 } 083 084 public final RefAddr get(int posn) { 085 throw new ArrayIndexOutOfBoundsException(posn); 086 } 087 088 public final Enumeration<RefAddr> getAll() { 089 return EMPTY_ENUMERATION; 090 } 091 092 public final int size() { 093 return 0; 094 } 095 096 public final void add(RefAddr addr) { 097 throw new UnsupportedOperationException("SimpleReference has no addresses so none can be added"); 098 } 099 100 public final void add(int posn, RefAddr addr) { 101 throw new UnsupportedOperationException("SimpleReference has no addresses so none can be added"); 102 } 103 104 public final Object remove(int posn) { 105 throw new ArrayIndexOutOfBoundsException(posn); 106 } 107 108 public final void clear() { 109 } 110 111 // 112 // Reset the java.lang.Object methods back to default implementations 113 // 114 public boolean equals(Object obj) { 115 return this == obj; 116 } 117 118 public int hashCode() { 119 return System.identityHashCode(this); 120 } 121 122 public String toString() { 123 return getClass().getName() + "@" + Integer.toHexString(hashCode()); 124 } 125 126 public Object clone() { 127 throw new UnsupportedOperationException("SimpleReference can not be cloned"); 128 } 129 130 /** 131 * Simply calls getContent() on the SimpleReference 132 */ 133 public static final class SimpleObjectFactory implements ObjectFactory { 134 public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) throws Exception { 135 if (obj instanceof SimpleReference) { 136 SimpleReference reference = (SimpleReference) obj; 137 return reference.getContent(); 138 } 139 return null; 140 } 141 } 142 }