KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > spice > jndikit > AbstractNamespace


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;
9
10 import java.util.Hashtable JavaDoc;
11 import javax.naming.Context JavaDoc;
12 import javax.naming.Name JavaDoc;
13 import javax.naming.NamingException JavaDoc;
14 import javax.naming.spi.ObjectFactory JavaDoc;
15 import javax.naming.spi.StateFactory JavaDoc;
16
17 /**
18  * This is the class to extend that provides
19  * basic facilities for Namespace management.
20  *
21  * @author Peter Donald
22  * @version $Revision: 1.1 $
23  */

24 public abstract class AbstractNamespace
25     implements Namespace
26 {
27     protected ObjectFactory JavaDoc[] m_objectFactorySet;
28     protected StateFactory JavaDoc[] m_stateFactorySet;
29
30     public Object JavaDoc getStateToBind( final Object JavaDoc object,
31                                   final Name JavaDoc name,
32                                   final Context JavaDoc parent,
33                                   final Hashtable JavaDoc environment )
34         throws NamingException JavaDoc
35     {
36         //for thread safety so that member variable can be updated
37
//at any time
38
final StateFactory JavaDoc[] stateFactorySet = m_stateFactorySet;
39
40         for( int i = 0; i < stateFactorySet.length; i++ )
41         {
42             final Object JavaDoc result =
43                 stateFactorySet[ i ].getStateToBind( object, name, parent, environment );
44
45             if( null != result )
46             {
47                 return result;
48             }
49         }
50
51         return object;
52     }
53
54     public Object JavaDoc getObjectInstance( final Object JavaDoc object,
55                                      final Name JavaDoc name,
56                                      final Context JavaDoc parent,
57                                      final Hashtable JavaDoc environment )
58         throws Exception JavaDoc
59     {
60         //for thread safety so that member variable can be updated
61
//at any time
62
final ObjectFactory JavaDoc[] objectFactorySet = m_objectFactorySet;
63
64         for( int i = 0; i < objectFactorySet.length; i++ )
65         {
66             final Object JavaDoc result =
67                 objectFactorySet[ i ].getObjectInstance( object, name, parent, environment );
68
69             if( null != result )
70             {
71                 return result;
72             }
73         }
74
75         return object;
76     }
77
78     /**
79      * Utility method for subclasses to add factorys.
80      *
81      * @param stateFactory the StateFactory to add
82      */

83     protected synchronized void addStateFactory( final StateFactory JavaDoc stateFactory )
84     {
85         //create new array of factory objects
86
final StateFactory JavaDoc[] stateFactorySet =
87             new StateFactory JavaDoc[ m_stateFactorySet.length + 1 ];
88
89         //copy old factory objects to new array
90
System.arraycopy( m_stateFactorySet, 0, stateFactorySet, 0, m_stateFactorySet.length );
91
92         //add in new factory at end
93
stateFactorySet[ m_stateFactorySet.length ] = stateFactory;
94
95         //update factory set
96
m_stateFactorySet = stateFactorySet;
97     }
98
99     /**
100      * Utility method for subclasses to add factorys.
101      *
102      * @param objectFactory the ObjectFactory to add
103      */

104     protected synchronized void addObjectFactory( final ObjectFactory JavaDoc objectFactory )
105     {
106         //create new array of factory objects
107
final ObjectFactory JavaDoc[] objectFactorySet =
108             new ObjectFactory JavaDoc[ m_objectFactorySet.length + 1 ];
109
110         //copy old factory objects to new array
111
System.arraycopy( m_objectFactorySet, 0, objectFactorySet, 0, m_objectFactorySet.length );
112
113         //add in new factory at end
114
objectFactorySet[ m_objectFactorySet.length ] = objectFactory;
115
116         //update factory set
117
m_objectFactorySet = objectFactorySet;
118     }
119 }
120
Popular Tags