KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.InvalidObjectException JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import javax.naming.Name JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import javax.naming.Reference JavaDoc;
34 import javax.naming.Referenceable JavaDoc;
35 import com.mchange.v2.log.MLevel;
36 import com.mchange.v2.log.MLog;
37 import com.mchange.v2.log.MLogger;
38 import com.mchange.v2.ser.Indirector;
39 import com.mchange.v2.ser.IndirectlySerialized;
40
41 public class ReferenceIndirector implements Indirector
42 {
43     final static MLogger logger = MLog.getLogger( ReferenceIndirector.class );
44
45     Name JavaDoc name;
46     Name JavaDoc contextName;
47     Hashtable JavaDoc environmentProperties;
48
49     public Name JavaDoc getName()
50     { return name; }
51
52     public void setName( Name JavaDoc name )
53     { this.name = name; }
54
55     public Name JavaDoc getNameContextName()
56     { return contextName; }
57
58     public void setNameContextName( Name JavaDoc contextName )
59     { this.contextName = contextName; }
60
61     public Hashtable JavaDoc getEnvironmentProperties()
62     { return environmentProperties; }
63
64     public void setEnvironmentProperties( Hashtable JavaDoc environmentProperties )
65     { this.environmentProperties = environmentProperties; }
66
67     public IndirectlySerialized indirectForm( Object JavaDoc orig ) throws Exception JavaDoc
68     {
69     Reference JavaDoc ref = ((Referenceable JavaDoc) orig).getReference();
70     return new ReferenceSerialized( ref, name, contextName, environmentProperties );
71     }
72
73     private static class ReferenceSerialized implements IndirectlySerialized
74     {
75     Reference JavaDoc reference;
76     Name JavaDoc name;
77     Name JavaDoc contextName;
78     Hashtable JavaDoc env;
79
80     ReferenceSerialized( Reference JavaDoc reference,
81                  Name JavaDoc name,
82                  Name JavaDoc contextName,
83                  Hashtable JavaDoc env )
84     {
85         this.reference = reference;
86         this.name = name;
87         this.contextName = contextName;
88         this.env = env;
89     }
90
91
92     public Object JavaDoc getObject() throws ClassNotFoundException JavaDoc, IOException JavaDoc
93     {
94         try
95         {
96             Context JavaDoc initialContext;
97             if ( env == null )
98             initialContext = new InitialContext JavaDoc();
99             else
100             initialContext = new InitialContext JavaDoc( env );
101
102             Context JavaDoc nameContext = null;
103             if ( contextName != null )
104             nameContext = (Context JavaDoc) initialContext.lookup( contextName );
105
106             return ReferenceableUtils.referenceToObject( reference, name, nameContext, env );
107         }
108         catch (NamingException JavaDoc e)
109         {
110             //e.printStackTrace();
111
if ( logger.isLoggable( MLevel.WARNING ) )
112             logger.log( MLevel.WARNING, "Failed to acquire the Context necessary to lookup an Object.", e );
113             throw new InvalidObjectException JavaDoc( "Failed to acquire the Context necessary to lookup an Object: " + e.toString() );
114         }
115     }
116     }
117 }
118
Popular Tags