1 22 23 24 package com.mchange.v2.naming; 25 26 import java.net.*; 27 import javax.naming.*; 28 import com.mchange.v2.log.MLevel; 29 import com.mchange.v2.log.MLog; 30 import com.mchange.v2.log.MLogger; 31 import javax.naming.spi.ObjectFactory ; 32 import java.util.Hashtable ; 33 34 public final class ReferenceableUtils 35 { 36 final static MLogger logger = MLog.getLogger( ReferenceableUtils.class ); 37 38 39 final static String REFADDR_VERSION = "version"; 40 final static String REFADDR_CLASSNAME = "classname"; 41 final static String REFADDR_FACTORY = "factory"; 42 final static String REFADDR_FACTORY_CLASS_LOCATION = "factoryClassLocation"; 43 final static String REFADDR_SIZE = "size"; 44 45 final static int CURRENT_REF_VERSION = 1; 46 47 51 public static String literalNullToNull( String s ) 52 { 53 if (s == null || "null".equals( s )) 54 return null; 55 else 56 return s; 57 } 58 59 public static Object referenceToObject( Reference ref, Name name, Context nameCtx, Hashtable env) 60 throws NamingException 61 { 62 try 63 { 64 String fClassName = ref.getFactoryClassName(); 65 String fClassLocation = ref.getFactoryClassLocation(); 66 67 ClassLoader cl; 68 if ( fClassLocation == null ) 69 cl = ClassLoader.getSystemClassLoader(); 70 else 71 { 72 URL u = new URL( fClassLocation ); 73 cl = new URLClassLoader( new URL[] { u }, ClassLoader.getSystemClassLoader() ); 74 } 75 76 Class fClass = Class.forName( fClassName, true, cl ); 77 ObjectFactory of = (ObjectFactory ) fClass.newInstance(); 78 return of.getObjectInstance( ref, name, nameCtx, env ); 79 } 80 catch ( Exception e ) 81 { 82 if (Debug.DEBUG) 83 { 84 if ( logger.isLoggable( MLevel.FINE ) ) 86 logger.log( MLevel.FINE, "Could not resolve Reference to Object!", e); 87 } 88 NamingException ne = new NamingException("Could not resolve Reference to Object!"); 89 ne.setRootCause( e ); 90 throw ne; 91 } 92 } 93 94 99 public static void appendToReference(Reference appendTo, Reference orig) 100 throws NamingException 101 { 102 int len = orig.size(); 103 appendTo.add( new StringRefAddr( REFADDR_VERSION, String.valueOf( CURRENT_REF_VERSION ) ) ); 104 appendTo.add( new StringRefAddr( REFADDR_CLASSNAME, orig.getClassName() ) ); 105 appendTo.add( new StringRefAddr( REFADDR_FACTORY, orig.getFactoryClassName() ) ); 106 appendTo.add( new StringRefAddr( REFADDR_FACTORY_CLASS_LOCATION, 107 orig.getFactoryClassLocation() ) ); 108 appendTo.add( new StringRefAddr( REFADDR_SIZE, String.valueOf(len) ) ); 109 for (int i = 0; i < len; ++i) 110 appendTo.add( orig.get(i) ); 111 } 112 113 118 public static ExtractRec extractNestedReference(Reference extractFrom, int index) 119 throws NamingException 120 { 121 try 122 { 123 int version = Integer.parseInt((String ) extractFrom.get(index++).getContent()); 124 if (version == 1) 125 { 126 String className = (String ) extractFrom.get(index++).getContent(); 127 String factoryClassName = (String ) extractFrom.get(index++).getContent(); 128 String factoryClassLocation = (String ) extractFrom.get(index++).getContent(); 129 130 Reference outRef = new Reference( className, 131 factoryClassName, 132 factoryClassLocation ); 133 int size = Integer.parseInt((String ) extractFrom.get(index++).getContent()); 134 for (int i = 0; i < size; ++i) 135 outRef.add( extractFrom.get( index++ ) ); 136 return new ExtractRec( outRef, index ); 137 } 138 else 139 throw new NamingException("Bad version of nested reference!!!"); 140 } 141 catch (NumberFormatException e) 142 { 143 if (Debug.DEBUG) 144 { 145 if ( logger.isLoggable( MLevel.FINE ) ) 147 logger.log( MLevel.FINE, "Version or size nested reference was not a number!!!", e); 148 } 149 throw new NamingException("Version or size nested reference was not a number!!!"); 150 } 151 } 152 153 158 public static class ExtractRec 159 { 160 public Reference ref; 161 162 166 public int index; 167 168 private ExtractRec(Reference ref, int index) 169 { 170 this.ref = ref; 171 this.index = index; 172 } 173 } 174 175 private ReferenceableUtils() 176 {} 177 } 178 | Popular Tags |