Clover Coverage Report - itracker
Coverage timestamp: Tue May 1 2012 16:42:12 CEST
35   134   18   8.75
28   77   0.51   4
4     4.5  
1    
 
 
  NamingUtilites       Line # 18 35 18 0% 0.0
 
No Tests
 
1    package org.itracker.services.util;
2   
3    import java.util.Hashtable;
4   
5    import javax.naming.Context;
6    import javax.naming.InitialContext;
7    import javax.naming.NameNotFoundException;
8    import javax.naming.NamingException;
9   
10    import org.apache.log4j.Logger;
11   
12    /**
13    * Utilities class for naming
14    *
15    * @author ranks@rosa.com
16    *
17    */
 
18    public class NamingUtilites {
19   
20    private static final Logger log = Logger.getLogger(NamingUtilites.class
21    .getName());
22   
23   
24    /**
25    * Read a String value of any type of object
26    *
27    * @param ctx -
28    * Context for lookup
29    * @param lookupName -
30    * lookup name
31    * @param defaultValue -
32    * default value
33    */
 
34  0 toggle public static final String getStringValue(Context ctx, String lookupName,
35    String defaultValue) {
36  0 String value = null;
37  0 Object val;
38  0 if (log.isDebugEnabled()) {
39  0 log.debug("getStringValue: look up " + lookupName + " in context "
40    + ctx + ", default: " + defaultValue);
41   
42    }
43   
44   
45   
46  0 if (null == ctx) {
47  0 log.debug("getStringValue: creating new InitialContext");
48  0 try {
49  0 ctx = new InitialContext();
50  0 if (log.isDebugEnabled()) {
51  0 log.debug("created new InitialContext: " + ctx);
52    }
53    } catch (NamingException e) {
54  0 log.warn("getStringValue: failed to create InitialContext", e);
55  0 if (log.isDebugEnabled())
56  0 log.debug("getStringValue: context was null, exception from new initial context caught", e);
57    }
58    }
59   
60   
61  0 if (null != ctx) {
62   
63   
64  0 val = lookup(ctx, lookupName);
65  0 if (val instanceof String) {
66  0 value = (String) val;
67    } else {
68  0 value = (null == val) ? null : String.valueOf(val);
69    }
70   
71    }
72  0 value = (null == value) ? defaultValue : value;
73  0 if (log.isDebugEnabled()) {
74  0 log.debug("getStringValue: returning '" + value + "' for " + lookupName);
75   
76    }
77   
78  0 return value;
79    }
80   
81    /**
82    * savely get object from naming context
83    *
84    * @param ctx
85    * @param lookupName
86    * @return Object - value
87    * @throws IllegalArgumentException -
88    * if any argument is null, or the lookup name was empty
89    */
 
90  0 toggle public static final Object lookup(Context ctx, String lookupName) {
91  0 if (null == ctx) {
92  0 throw new IllegalArgumentException("context must not be null");
93    }
94  0 if (null == lookupName || lookupName.trim().length() == 0) {
95  0 throw new IllegalArgumentException(
96    "lookup name must not be empty, got: "
97  0 + ((null == lookupName) ? "<null>" : "'"
98    + lookupName + "'"));
99    }
100  0 try {
101  0 return ctx.lookup(lookupName);
102    } catch (NamingException e) {
103  0 if (e instanceof NameNotFoundException) {
104  0 if (log.isDebugEnabled()){
105  0 log.debug("lookup: failed to lookup " + lookupName
106    + ": name not found");
107    }
108    } else {
109  0 log.warn("lookup: failed to lookup " + lookupName
110    + " in context " + ctx, e);
111    }
112  0 return null;
113    }
114    }
115   
116    /**
117    * get a default initial context
118    * @return initial context.
119    * @throws NamingException
120    * @throws IllegalStateException - if initial context was null
121    */
 
122  0 toggle public static final Context getDefaultInitialContext(Hashtable<?,?> environment) throws NamingException {
123  0 return new InitialContext(environment);
124    }
125    /**
126    * get a default initial context
127    * @return initial context.
128    * @throws NamingException
129    * @throws IllegalStateException - if initial context was null
130    */
 
131  0 toggle public static final Context getDefaultInitialContext() throws NamingException {
132  0 return new InitialContext();
133    }
134    }