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 EMPTY_ENUMERATION = new Enumeration() { 034 public boolean hasMoreElements() { 035 return false; 036 } 037 038 public Object nextElement() { 039 return 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 */ 051 public abstract Object getContent() throws NamingException; 052 053 /** 054 * We will atleast return an Object. Subclasses may want to provide a more specific class. 055 * @return "java.lang.Object" 056 */ 057 public String getClassName() { 058 return "java.lang.Object"; 059 } 060 061 /** 062 * If the JNDI context does not understand simple references, this method will be called 063 * to obtain the class name of a factory. This factory in turn understands the simple 064 * reference. This style is much slower because JNDI will use reflection to load and 065 * create this class. 066 * @return 067 */ 068 public final String getFactoryClassName() { 069 return SimpleObjectFactory.class.getName(); 070 } 071 072 // 073 // Disabled methods that we no longer need 074 // 075 public final String getFactoryClassLocation() { 076 return null; 077 } 078 079 public final RefAddr get(String addrType) { 080 return null; 081 } 082 083 public final RefAddr get(int posn) { 084 throw new ArrayIndexOutOfBoundsException(posn); 085 } 086 087 public final Enumeration getAll() { 088 return EMPTY_ENUMERATION; 089 } 090 091 public final int size() { 092 return 0; 093 } 094 095 public final void add(RefAddr addr) { 096 throw new UnsupportedOperationException("SimpleReference has no addresses so none can be added"); 097 } 098 099 public final void add(int posn, RefAddr addr) { 100 throw new UnsupportedOperationException("SimpleReference has no addresses so none can be added"); 101 } 102 103 public final Object remove(int posn) { 104 throw new ArrayIndexOutOfBoundsException(posn); 105 } 106 107 public final void clear() { 108 } 109 110 // 111 // Reset the java.lang.Object methods back to default implementations 112 // 113 public boolean equals(Object obj) { 114 return this == obj; 115 } 116 117 public int hashCode() { 118 return System.identityHashCode(this); 119 } 120 121 public String toString() { 122 return getClass().getName() + "@" + Integer.toHexString(hashCode()); 123 } 124 125 public Object clone() { 126 throw new UnsupportedOperationException("SimpleReference can not be cloned"); 127 } 128 129 /** 130 * Simply calls getContent() on the SimpleReference 131 */ 132 public static final class SimpleObjectFactory implements ObjectFactory { 133 public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) throws Exception { 134 if (obj instanceof SimpleReference) { 135 SimpleReference reference = (SimpleReference) obj; 136 return reference.getContent(); 137 } 138 return null; 139 } 140 } 141 }