KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > impl > lookup > FortressServiceSelector


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.fortress.impl.lookup;
19
20 import org.apache.avalon.fortress.Container;
21 import org.apache.avalon.fortress.impl.handler.ComponentHandler;
22 import org.apache.avalon.framework.service.ServiceException;
23 import org.apache.avalon.framework.service.ServiceSelector;
24
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 /**
30  * This is the Default ServiceSelector for the Container. It provides
31  * a very simple abstraction, and makes it easy for the Container to manage
32  * the references.
33  *
34  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
35  * @version CVS $Revision: 1.17 $ $Date: 2004/02/28 15:16:25 $
36  */

37 public class FortressServiceSelector
38     implements ServiceSelector
39 {
40     private final String JavaDoc m_key;
41     private final Container m_container;
42     private final Map JavaDoc m_used;
43
44     /**
45      * Creation of new service selector.
46      * @param container the impl
47      * @param key a key
48      */

49     public FortressServiceSelector( final Container container,
50                                     final String JavaDoc key )
51     {
52         if ( null == container )
53         {
54             throw new NullPointerException JavaDoc( "impl" );
55         }
56         if ( null == key )
57         {
58             throw new NullPointerException JavaDoc( "key" );
59         }
60
61         m_key = key;
62         m_container = container;
63         m_used = Collections.synchronizedMap(new HashMap JavaDoc());
64     }
65
66     public Object JavaDoc select( final Object JavaDoc hint )
67         throws ServiceException
68     {
69         try
70         {
71             final ComponentHandler handler = getHandler( hint );
72             final Object JavaDoc component = handler.get();
73             m_used.put( new ComponentKey( component ), handler );
74             return component;
75         }
76         catch ( final ServiceException ce )
77         {
78             throw ce; // rethrow
79
}
80         catch ( final Exception JavaDoc e )
81         {
82             final String JavaDoc name = m_key + "/" + hint.toString();
83             final String JavaDoc message = "Could not return a reference to the Component";
84             throw new ServiceException( name, message, e );
85         }
86     }
87
88     public boolean isSelectable( final Object JavaDoc hint )
89     {
90         return m_container.has( m_key, hint );
91     }
92
93     public void release( final Object JavaDoc component )
94     {
95         final ComponentHandler handler =
96             (ComponentHandler) m_used.remove( new ComponentKey( component ) );
97         if ( null != handler )
98         {
99             handler.put( component );
100         }
101     }
102
103     private ComponentHandler getHandler( final Object JavaDoc hint )
104         throws ServiceException
105     {
106         if ( null == hint )
107         {
108             final String JavaDoc message = "hint cannot be null";
109             throw new IllegalArgumentException JavaDoc( message );
110         }
111
112         final ComponentHandler handler =
113             (ComponentHandler) m_container.get( m_key, hint );
114         if ( null == handler )
115         {
116             final String JavaDoc message =
117                 "The hint does not exist in the ComponentSelector";
118             throw new ServiceException( m_key + "/" + hint.toString(),
119                 message );
120         }
121         return handler;
122     }
123     
124     public String JavaDoc getKey()
125     {
126         return m_key;
127     }
128 }
129
Popular Tags