KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > naming > ReferenceableUtils


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

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 JavaDoc;
32 import java.util.Hashtable JavaDoc;
33
34 public final class ReferenceableUtils
35 {
36     final static MLogger logger = MLog.getLogger( ReferenceableUtils.class );
37
38     /* don't worry -- References can have duplicate RefAddrs (I think!) */
39     final static String JavaDoc REFADDR_VERSION = "version";
40     final static String JavaDoc REFADDR_CLASSNAME = "classname";
41     final static String JavaDoc REFADDR_FACTORY = "factory";
42     final static String JavaDoc REFADDR_FACTORY_CLASS_LOCATION = "factoryClassLocation";
43     final static String JavaDoc REFADDR_SIZE = "size";
44
45     final static int CURRENT_REF_VERSION = 1;
46
47     /**
48      * A null string value in a Reference sometimes goes to the literal
49      * "null". Sigh. We convert this string to a Java null.
50      */

51     public static String JavaDoc literalNullToNull( String JavaDoc s )
52     {
53     if (s == null || "null".equals( s ))
54         return null;
55     else
56         return s;
57     }
58
59     public static Object JavaDoc referenceToObject( Reference ref, Name name, Context nameCtx, Hashtable JavaDoc env)
60     throws NamingException
61     {
62     try
63         {
64         String JavaDoc fClassName = ref.getFactoryClassName();
65         String JavaDoc fClassLocation = ref.getFactoryClassLocation();
66         
67         ClassLoader JavaDoc 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 JavaDoc fClass = Class.forName( fClassName, true, cl );
77         ObjectFactory JavaDoc of = (ObjectFactory JavaDoc) fClass.newInstance();
78         return of.getObjectInstance( ref, name, nameCtx, env );
79         }
80     catch ( Exception JavaDoc e )
81         {
82         if (Debug.DEBUG)
83             {
84             //e.printStackTrace();
85
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     /**
95      * @deprecated nesting references seemed useful until I realized that
96      * references are Serializable and can be stored in a BinaryRefAddr.
97      * Oops.
98      */

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     /**
114      * @deprecated nesting references seemed useful until I realized that
115      * references are Serializable and can be stored in a BinaryRefAddr.
116      * Oops.
117      */

118     public static ExtractRec extractNestedReference(Reference extractFrom, int index)
119     throws NamingException
120     {
121     try
122         {
123         int version = Integer.parseInt((String JavaDoc) extractFrom.get(index++).getContent());
124         if (version == 1)
125             {
126             String JavaDoc className = (String JavaDoc) extractFrom.get(index++).getContent();
127             String JavaDoc factoryClassName = (String JavaDoc) extractFrom.get(index++).getContent();
128             String JavaDoc factoryClassLocation = (String JavaDoc) extractFrom.get(index++).getContent();
129
130             Reference outRef = new Reference( className,
131                               factoryClassName,
132                               factoryClassLocation );
133             int size = Integer.parseInt((String JavaDoc) 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 JavaDoc e)
142         {
143         if (Debug.DEBUG)
144             {
145             //e.printStackTrace();
146
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     /**
154      * @deprecated nesting references seemed useful until I realized that
155      * references are Serializable and can be stored in a BinaryRefAddr.
156      * Oops.
157      */

158     public static class ExtractRec
159     {
160     public Reference ref;
161
162     /**
163      * return the first RefAddr index that the function HAS NOT read to
164      * extract the reference.
165      */

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