Clover Coverage Report - perfectjpattern(Aggregated)
Coverage timestamp: Sat Feb 28 2009 14:35:07 CET
../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
12   138   8   3
2   69   0.67   4
4     2  
1    
21.7% of code in this file is excluded from these metrics.
 
  ServiceLocator       Line # 42 12 21.7% 8 0 100% 1.0
 
  (2)
 
1    //----------------------------------------------------------------------
2    //
3    // PerfectJPattern: "Design patterns are good but components are better!"
4    // ServiceLocator.java Copyright (c) 2009 Giovanni Azua Garcia
5    // bravegag@hotmail.com
6    //
7    // This program is free software; you can redistribute it and/or
8    // modify it under the terms of the GNU General Public License
9    // as published by the Free Software Foundation; either version 3
10    // of the License, or (at your option) any later version.
11    //
12    // This program is distributed in the hope that it will be useful,
13    // but WITHOUT ANY WARRANTY; without even the implied warranty of
14    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15    // GNU General Public License for more details.
16    //
17    // You should have received a copy of the GNU General Public License
18    // along with this program; if not, see <http://www.gnu.org/licenses/>.
19    //
20    //----------------------------------------------------------------------
21    package org.perfectjpattern.jee.business.servicelocator;
22   
23    import java.util.*;
24   
25    import javax.naming.*;
26   
27    import org.perfectjpattern.core.api.creational.singleton.*;
28    import org.perfectjpattern.jee.api.business.servicelocator.*;
29   
30    /**
31    * Generic componentized implementation of {@link IServiceLocator}. Provides
32    * concrete {@link IServiceLocator} implementations with reusable JNDI service
33    * lookup and Services caching.
34    * <br/><br/>
35    * Note that all initial properties are loaded from the <code>jndi.properties
36    * </code> file
37    *
38    * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
39    * @version $Revision: 1.0 $Date: Feb 7, 2009 2:35:20 PM $
40    */
41    public final
 
42    class ServiceLocator
43    implements IServiceLocator, ISingleton
44    {
45    //------------------------------------------------------------------------
46    // public
47    //------------------------------------------------------------------------
48    /**
49    * Returns Singleton instance of {@link IServiceLocator}
50    *
51    * @return Singleton instance of {@link IServiceLocator}
52    */
 
53  2 toggle public static IServiceLocator
54    getInstance()
55    {
56  2 return INSTANCE;
57    }
58   
59    //------------------------------------------------------------------------
60    /**
61    * {@inheritDoc}
62    */
 
63  5 toggle @SuppressWarnings("unchecked")
64    public <S> S
65    locate(String aName)
66    throws IllegalArgumentException
67    {
68  5 S myService = null;
69   
70  5 if (theServiceCache.containsKey(aName))
71    {
72  1 myService = (S) theServiceCache.get(aName);
73    }
74    else
75    {
76  4 try
77    {
78  4 myService = (S) theContext.lookup(aName);
79   
80    assert myService != null : "'myService' must not be null";
81   
82    // save in cache
83  3 theServiceCache.put(aName, myService);
84    }
85    catch (NamingException anException)
86    {
87    // propagate the exception
88    throw new ServiceLocatorException(anException);
89    }
90    }
91   
92  4 return myService;
93    }
94   
95    //------------------------------------------------------------------------
96    /**
97    * {@inheritDoc}
98    */
 
99  2 toggle public void
100    shutdown()
101    throws ServiceLocatorException
102    {
103  2 try
104    {
105  2 theContext.close();
106    }
107    catch (NamingException anException)
108    {
109    // propagate the exception
110    throw new ServiceLocatorException(anException);
111    }
112    }
113   
114    //------------------------------------------------------------------------
115    // private
116    //------------------------------------------------------------------------
 
117  2 toggle private
118    ServiceLocator()
119    {
120  2 try
121    {
122  2 theContext = new InitialContext();
123    }
124    catch (NamingException anException)
125    {
126    // propagate the exception
127    throw new ServiceLocatorException(anException);
128    }
129    }
130   
131    //------------------------------------------------------------------------
132    // members
133    //------------------------------------------------------------------------
134    private static final IServiceLocator INSTANCE = new ServiceLocator();
135    private final Context theContext;
136    private final Map<String, Object> theServiceCache =
137    new HashMap<String, Object>();
138    }