KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jaxr > factory > JAXRFactory


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Guillaume Sauthier
22  * --------------------------------------------------------------------------
23  * $Id: JAXRFactory.java,v 1.2 2005/04/28 08:43:27 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26 package org.objectweb.jonas.jaxr.factory;
27
28 import java.util.Hashtable JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 import javax.naming.Context JavaDoc;
32 import javax.naming.Name JavaDoc;
33 import javax.naming.RefAddr JavaDoc;
34 import javax.naming.Reference JavaDoc;
35 import javax.naming.spi.ObjectFactory JavaDoc;
36 import javax.xml.registry.ConnectionFactory JavaDoc;
37
38 import org.objectweb.jonas.common.JNDIUtils;
39 import org.objectweb.jonas.common.Log;
40 import org.objectweb.jonas.common.PropDump;
41
42 import org.objectweb.util.monolog.api.BasicLevel;
43 import org.objectweb.util.monolog.api.Logger;
44
45
46 /**
47  * JAXR ConnectionFactory factory
48  * @author Guillaume Sauthier
49  */

50 public class JAXRFactory implements ObjectFactory JavaDoc {
51
52     /**
53      * logger
54      */

55     private static Logger logger;
56
57     /**
58      * Classname returned
59      */

60     public static final String JavaDoc FACTORY_TYPE = "javax.xml.registry.ConnectionFactory";
61
62     /**
63      * Properties stored inside Reference
64      */

65     public static final String JavaDoc PROPS_NAME = "jaxr.properties";
66
67     /**
68      * JAXR Factory property name
69      */

70     private static final String JavaDoc JAXR_FACTORY_CLASSNAME = "jaxr.factory.classname";
71
72     /**
73      * @see javax.naming.spi.ObjectFactory#getObjectInstance(java.lang.Object, javax.naming.Name, javax.naming.Context, java.util.Hashtable)
74      */

75     public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx, Hashtable JavaDoc environment) throws Exception JavaDoc {
76         //Get the logger
77
if (logger == null) {
78             logger = Log.getLogger(Log.JONAS_JAXR_PREFIX);
79         }
80
81         //Get the reference
82
if (!(obj instanceof Reference JavaDoc)) {
83             return null;
84         }
85         Reference JavaDoc ref = (Reference JavaDoc) obj;
86
87         //Get the class name
88
String JavaDoc clname = ref.getClassName();
89
90         //Check the class name
91
if (!ref.getClassName().equals(FACTORY_TYPE)) {
92             logger.log(BasicLevel.ERROR, "Cannot create object : required type is '" + FACTORY_TYPE + "', but found type is '" + clname + "'.");
93             return null;
94         }
95
96         // Load properties
97
Properties JavaDoc jaxrProps = new Properties JavaDoc();
98         RefAddr JavaDoc refAddr = null;
99
100         refAddr = ref.get(PROPS_NAME);
101         if (refAddr != null) {
102             jaxrProps = (Properties JavaDoc) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent());
103             if (logger.isLoggable(BasicLevel.DEBUG)) {
104                 PropDump.print("These are the properties used to obtain a new jaxr object", jaxrProps, logger, BasicLevel.DEBUG);
105             }
106         }
107
108         // set implementation if specified
109
String JavaDoc clsName = jaxrProps.getProperty(JAXR_FACTORY_CLASSNAME);
110         if (clsName != null) {
111             System.setProperty("javax.xml.registry.ConnectionFactoryClass", clsName);
112         }
113         // else fallback to default implementation
114
ConnectionFactory JavaDoc cf = ConnectionFactory.newInstance();
115
116         // setup properties
117
cf.setProperties(jaxrProps);
118
119         return cf;
120     }
121
122 }
123
Popular Tags