001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2004 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: * Initial developer(s): Guillaume Sauthier
022: * --------------------------------------------------------------------------
023: * $Id: JAXRFactory.java 6661 2005-04-28 08:43:27Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas.jaxr.factory;
026:
027: import java.util.Hashtable;
028: import java.util.Properties;
029:
030: import javax.naming.Context;
031: import javax.naming.Name;
032: import javax.naming.RefAddr;
033: import javax.naming.Reference;
034: import javax.naming.spi.ObjectFactory;
035: import javax.xml.registry.ConnectionFactory;
036:
037: import org.objectweb.jonas.common.JNDIUtils;
038: import org.objectweb.jonas.common.Log;
039: import org.objectweb.jonas.common.PropDump;
040:
041: import org.objectweb.util.monolog.api.BasicLevel;
042: import org.objectweb.util.monolog.api.Logger;
043:
044: /**
045: * JAXR ConnectionFactory factory
046: * @author Guillaume Sauthier
047: */
048: public class JAXRFactory implements ObjectFactory {
049:
050: /**
051: * logger
052: */
053: private static Logger logger;
054:
055: /**
056: * Classname returned
057: */
058: public static final String FACTORY_TYPE = "javax.xml.registry.ConnectionFactory";
059:
060: /**
061: * Properties stored inside Reference
062: */
063: public static final String PROPS_NAME = "jaxr.properties";
064:
065: /**
066: * JAXR Factory property name
067: */
068: private static final String JAXR_FACTORY_CLASSNAME = "jaxr.factory.classname";
069:
070: /**
071: * @see javax.naming.spi.ObjectFactory#getObjectInstance(java.lang.Object, javax.naming.Name, javax.naming.Context, java.util.Hashtable)
072: */
073: public Object getObjectInstance(Object obj, Name name,
074: Context nameCtx, Hashtable environment) throws Exception {
075: //Get the logger
076: if (logger == null) {
077: logger = Log.getLogger(Log.JONAS_JAXR_PREFIX);
078: }
079:
080: //Get the reference
081: if (!(obj instanceof Reference)) {
082: return null;
083: }
084: Reference ref = (Reference) obj;
085:
086: //Get the class name
087: String clname = ref.getClassName();
088:
089: //Check the class name
090: if (!ref.getClassName().equals(FACTORY_TYPE)) {
091: logger.log(BasicLevel.ERROR,
092: "Cannot create object : required type is '"
093: + FACTORY_TYPE + "', but found type is '"
094: + clname + "'.");
095: return null;
096: }
097:
098: // Load properties
099: Properties jaxrProps = new Properties();
100: RefAddr refAddr = null;
101:
102: refAddr = ref.get(PROPS_NAME);
103: if (refAddr != null) {
104: jaxrProps = (Properties) JNDIUtils
105: .getObjectFromBytes((byte[]) refAddr.getContent());
106: if (logger.isLoggable(BasicLevel.DEBUG)) {
107: PropDump
108: .print(
109: "These are the properties used to obtain a new jaxr object",
110: jaxrProps, logger, BasicLevel.DEBUG);
111: }
112: }
113:
114: // set implementation if specified
115: String clsName = jaxrProps.getProperty(JAXR_FACTORY_CLASSNAME);
116: if (clsName != null) {
117: System.setProperty(
118: "javax.xml.registry.ConnectionFactoryClass",
119: clsName);
120: }
121: // else fallback to default implementation
122: ConnectionFactory cf = ConnectionFactory.newInstance();
123:
124: // setup properties
125: cf.setProperties(jaxrProps);
126:
127: return cf;
128: }
129:
130: }
|