KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > naming > factory > URLFactory


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 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): Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: URLFactory.java,v 1.2 2004/05/10 12:05:27 sauthieg Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas_lib.naming.factory;
28
29
30 //import java
31
import java.net.URL JavaDoc;
32 import java.util.Hashtable JavaDoc;
33
34 //import javax
35
import javax.naming.Context JavaDoc;
36 import javax.naming.Name JavaDoc;
37 import javax.naming.Reference JavaDoc;
38 import javax.naming.spi.ObjectFactory JavaDoc;
39
40
41 /**
42  * This class provides an implementation of a mail session factory for
43  * sending mail.
44  * @author Florent Benoit
45  */

46 public class URLFactory implements ObjectFactory JavaDoc {
47
48     /**
49      * The Java type for which this factory knows how to create objects.
50      */

51     protected static final String JavaDoc FACTORY_TYPE = "java.net.URL";
52
53
54     /**
55      * Creates a java.net.URL object using the location or reference
56      * information specified.
57      * @param obj the possibly null object containing location or reference
58      * information that can be used in creating an object.
59      * @param name the name of this object relative to nameCtx, or null if no
60      * name is specified.
61      * @param nameCtx the context relative to which the name parameter is
62      * specified, or null if name is relative to the default initial context.
63      * @param environment the possibly null environment that is used in
64      * creating the object.
65      * @return a newly created java.net.URL object with the specific
66      * configuration; null if an object cannot be created.
67      * @throws Exception if this object factory encountered an exception
68      * while attempting to create an object, and no other object factories
69      * are to be tried.
70      */

71     public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx,
72                                     Hashtable JavaDoc environment) throws Exception JavaDoc {
73
74         //Get the reference
75
Reference JavaDoc ref = (Reference JavaDoc) obj;
76
77         //Get the class name
78
String JavaDoc clname = ref.getClassName();
79
80         //Check the class name
81
if (!ref.getClassName().equals(FACTORY_TYPE)) {
82             throw new Exception JavaDoc("Can not create object : required type is '" + FACTORY_TYPE + "', but found type is '" + clname + "'.");
83         }
84
85         URL JavaDoc url = null;
86         String JavaDoc urlString = (String JavaDoc) ref.get("url").getContent();
87
88         if (urlString != null) {
89             url = new URL JavaDoc(urlString);
90         } else {
91             throw new Exception JavaDoc("Can not build an object as no URL was given.");
92         }
93
94         return url;
95     }
96
97 }
98
Popular Tags