001: /**
002: * EasyBeans
003: * Copyright (C) 2007 Bull S.A.S.
004: * Contact: easybeans@ow2.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: * --------------------------------------------------------------------------
022: * $Id: URLFactory.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.naming.url;
025:
026: import java.net.MalformedURLException;
027: import java.net.URL;
028: import java.util.Hashtable;
029:
030: import javax.naming.Context;
031: import javax.naming.Name;
032: import javax.naming.NamingException;
033: import javax.naming.Reference;
034: import javax.naming.spi.ObjectFactory;
035:
036: /**
037: * Allow to lookup java.net.URL object.
038: * @author Florent BENOIT
039: */
040: public class URLFactory implements ObjectFactory {
041:
042: /**
043: * The Java type for which this factory knows how to create objects.
044: */
045: protected static final String FACTORY_TYPE = "java.net.URL";
046:
047: /**
048: * Creates a java.net.URL object using the location or reference information
049: * specified.
050: * @param obj the possibly null object containing location or reference
051: * information that can be used in creating an object.
052: * @param name the name of this object relative to nameCtx, or null if no
053: * name is specified.
054: * @param nameCtx the context relative to which the name parameter is
055: * specified, or null if name is relative to the default initial
056: * context.
057: * @param environment the possibly null environment that is used in creating
058: * the object.
059: * @return a newly created java.net.URL object with the specific
060: * configuration; null if an object cannot be created.
061: * @throws NamingException if this object factory encountered an exception while
062: * attempting to create an object, and no other object factories are
063: * to be tried.
064: */
065: public Object getObjectInstance(final Object obj, final Name name,
066: final Context nameCtx, final Hashtable<?, ?> environment)
067: throws NamingException {
068:
069: // Get the reference
070: Reference ref = (Reference) obj;
071:
072: // Get the class name
073: String clname = ref.getClassName();
074:
075: // Check the class name
076: if (!ref.getClassName().equals(FACTORY_TYPE)) {
077: throw new NamingException(
078: "Cannot create object : required type is '"
079: + FACTORY_TYPE + "', but found type is '"
080: + clname + "'.");
081: }
082:
083: URL url = null;
084: String urlString = (String) ref.get("url").getContent();
085:
086: if (urlString != null) {
087: try {
088: url = new URL(urlString);
089: } catch (MalformedURLException e) {
090: NamingException ne = new NamingException(
091: "Cannot build an URL with the given string '"
092: + urlString + "'");
093: ne.initCause(e);
094: throw ne;
095: }
096: } else {
097: throw new NamingException(
098: "Can not build an object as no URL was given.");
099: }
100:
101: return url;
102: }
103:
104: }
|