KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > spice > jndikit > test > TestObjectFactory


1 /*
2  * Copyright (C) The Spice Group. All rights reserved.
3  *
4  * This software is published under the terms of the Spice
5  * Software License version 1.1, a copy of which has been included
6  * with this distribution in the LICENSE.txt file.
7  */

8 package org.codehaus.spice.jndikit.test;
9
10 import java.util.Hashtable JavaDoc;
11 import javax.naming.Context JavaDoc;
12 import javax.naming.Name JavaDoc;
13 import javax.naming.Reference JavaDoc;
14 import javax.naming.StringRefAddr JavaDoc;
15 import javax.naming.spi.ObjectFactory JavaDoc;
16
17
18 /**
19  * Implementation of {@link ObjectFactory}, for testing purposes.
20  *
21  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
22  * @version $Revision: 1.1 $ $Date: 2005/06/30 04:22:16 $
23  * @see TestDataReferenceable
24  */

25 public class TestObjectFactory implements ObjectFactory JavaDoc
26 {
27
28     /**
29      * Creates an object using the location or reference information specified.
30      *
31      * @param obj The possibly null object containing location or
32      * reference information that can be used in creating an
33      * object.
34      * @param name The name of this object relative to <code>nameCtx</code>,
35      * or null if no name is specified.
36      * @param nameCtx The context relative to which the <code>name</code>
37      * parameter is specified, or null if <code>name</code>
38      * is relative to the default initial context.
39      * @param environment The possibly null environment that is used in creating
40      * the object.
41      * @return The object created; null if an object cannot be created.
42      * @throws Exception if this object factory encountered an exception while
43      * attempting to create an object, and no other object
44      * factories are to be tried.
45      */

46     public Object JavaDoc getObjectInstance( Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx,
47                                      Hashtable JavaDoc environment ) throws Exception JavaDoc
48     {
49         Object JavaDoc result = null;
50         if( obj instanceof Reference JavaDoc )
51         {
52             Reference JavaDoc ref = ( Reference JavaDoc ) obj;
53             String JavaDoc clazz = ref.getClassName();
54             if( clazz.equals( TestDataReferenceable.class.getName() ) )
55             {
56                 String JavaDoc value = null;
57                 StringRefAddr JavaDoc str = ( StringRefAddr JavaDoc ) ref.get( "value" );
58                 if( str != null )
59                 {
60                     value = ( String JavaDoc ) str.getContent();
61                 }
62                 result = new TestDataReferenceable( value );
63             }
64             else if( clazz.equals( ExceptionReferenceable.class.getName() ) )
65             {
66                 throw new Exception JavaDoc( "Encountered " + clazz );
67             }
68         }
69         return result;
70     }
71
72 }
73
Popular Tags