001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.naming;
024:
025: import java.io.InvalidObjectException;
026: import java.io.IOException;
027: import java.util.Hashtable;
028: import javax.naming.Context;
029: import javax.naming.InitialContext;
030: import javax.naming.Name;
031: import javax.naming.NamingException;
032: import javax.naming.Reference;
033: import javax.naming.Referenceable;
034: import com.mchange.v2.log.MLevel;
035: import com.mchange.v2.log.MLog;
036: import com.mchange.v2.log.MLogger;
037: import com.mchange.v2.ser.Indirector;
038: import com.mchange.v2.ser.IndirectlySerialized;
039:
040: public class ReferenceIndirector implements Indirector {
041: final static MLogger logger = MLog
042: .getLogger(ReferenceIndirector.class);
043:
044: Name name;
045: Name contextName;
046: Hashtable environmentProperties;
047:
048: public Name getName() {
049: return name;
050: }
051:
052: public void setName(Name name) {
053: this .name = name;
054: }
055:
056: public Name getNameContextName() {
057: return contextName;
058: }
059:
060: public void setNameContextName(Name contextName) {
061: this .contextName = contextName;
062: }
063:
064: public Hashtable getEnvironmentProperties() {
065: return environmentProperties;
066: }
067:
068: public void setEnvironmentProperties(Hashtable environmentProperties) {
069: this .environmentProperties = environmentProperties;
070: }
071:
072: public IndirectlySerialized indirectForm(Object orig)
073: throws Exception {
074: Reference ref = ((Referenceable) orig).getReference();
075: return new ReferenceSerialized(ref, name, contextName,
076: environmentProperties);
077: }
078:
079: private static class ReferenceSerialized implements
080: IndirectlySerialized {
081: Reference reference;
082: Name name;
083: Name contextName;
084: Hashtable env;
085:
086: ReferenceSerialized(Reference reference, Name name,
087: Name contextName, Hashtable env) {
088: this .reference = reference;
089: this .name = name;
090: this .contextName = contextName;
091: this .env = env;
092: }
093:
094: public Object getObject() throws ClassNotFoundException,
095: IOException {
096: try {
097: Context initialContext;
098: if (env == null)
099: initialContext = new InitialContext();
100: else
101: initialContext = new InitialContext(env);
102:
103: Context nameContext = null;
104: if (contextName != null)
105: nameContext = (Context) initialContext
106: .lookup(contextName);
107:
108: return ReferenceableUtils.referenceToObject(reference,
109: name, nameContext, env);
110: } catch (NamingException e) {
111: //e.printStackTrace();
112: if (logger.isLoggable(MLevel.WARNING))
113: logger
114: .log(
115: MLevel.WARNING,
116: "Failed to acquire the Context necessary to lookup an Object.",
117: e);
118: throw new InvalidObjectException(
119: "Failed to acquire the Context necessary to lookup an Object: "
120: + e.toString());
121: }
122: }
123: }
124: }
|