001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: * --------------------------------------------------------------------------
021: * $Id: ResourceObjectJNDIHandler.java 5699 2004-10-29 23:33:37Z ehardesty $
022: * --------------------------------------------------------------------------
023: */package org.objectweb.jonas.resource;
024:
025: import java.util.Hashtable;
026:
027: import javax.naming.Context;
028: import javax.naming.Name;
029: import javax.naming.RefAddr;
030: import javax.naming.Reference;
031: import javax.naming.spi.ObjectFactory;
032:
033: import org.objectweb.jonas.common.JNDIUtils;
034: import org.objectweb.jonas_rar.deployment.api.ConnectorDesc;
035: import org.objectweb.jonas_rar.deployment.api.JonasConnectorDesc;
036:
037: /**
038: *
039: * @author Eric Hardesty
040: * Contributor(s):
041: *
042: */
043: public class ResourceObjectJNDIHandler implements ObjectFactory {
044:
045: /**
046: * Create an object using the information provided
047: * @param refObj the possibly null object containing reference information to create the object.
048: * @param name the name of this object relative to nameCtx, or null if no name is specified.
049: * @param nameCtx the context relative to which the name parameter is specified, or null if name
050: * is relative to the default initial context.
051: * @param env the possibly null environment that is used in creating the object.
052: * @return Object created or null if unable to create
053: * @throws Exception if an exception happened while trying to create the object
054: */
055: public Object getObjectInstance(Object refObj, Name name,
056: Context nameCtx, Hashtable env) throws Exception {
057:
058: Reference ref = (Reference) refObj;
059:
060: if (ref == null) {
061: System.out.println("No reference found");
062: return null;
063: }
064: String clname = ref.getClassName();
065: String raname = (String) ref.get(ResourceServiceImpl.JNDI_NAME)
066: .getContent();
067:
068: Object obj = null;
069: try {
070: obj = Rar.getResourceObject(raname);
071: } catch (Throwable ex) {
072: ex.printStackTrace();
073: }
074: if (obj != null) {
075: return obj;
076: }
077:
078: // Need to load the factory in the client environment
079: String rarObjectName = (String) ref.get(
080: ResourceServiceImpl.RAR_OBJNAME).getContent();
081: String factoryType = (String) ref.get(
082: ResourceServiceImpl.FACTORY_TYPE).getContent();
083: String factoryOff = (String) ref.get(
084: ResourceServiceImpl.FACTORY_OFFSET).getContent();
085: int factoryOffset = Integer.parseInt(factoryOff);
086:
087: RefAddr refAddr = null;
088: ConnectorDesc conn = null;
089: refAddr = ref.get(ResourceServiceImpl.RA_XML);
090: if (refAddr != null) {
091: conn = (ConnectorDesc) JNDIUtils
092: .getObjectFromBytes((byte[]) refAddr.getContent());
093: }
094:
095: JonasConnectorDesc jConn = null;
096: refAddr = ref.get(ResourceServiceImpl.JONAS_RA_XML);
097: if (refAddr != null) {
098: jConn = (JonasConnectorDesc) JNDIUtils
099: .getObjectFromBytes((byte[]) refAddr.getContent());
100: }
101:
102: // This will either get the other server's implemenation or a client version
103: Rar ra = new Rar();
104: obj = ra.createFactory(raname, rarObjectName, factoryOffset,
105: factoryType, conn, jConn);
106:
107: return obj;
108: }
109: }
|