| 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 |
|
|
| 14 |
|
|
| 15 |
|
@author |
| 16 |
|
|
| 17 |
|
|
|
|
|
| 0% |
Uncovered Elements: 67 (67) |
Complexity: 18 |
Complexity Density: 0.51 |
|
| 18 |
|
public class NamingUtilites { |
| 19 |
|
|
| 20 |
|
private static final Logger log = Logger.getLogger(NamingUtilites.class |
| 21 |
|
.getName()); |
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
@param |
| 28 |
|
|
| 29 |
|
@param |
| 30 |
|
|
| 31 |
|
@param |
| 32 |
|
|
| 33 |
|
|
|
|
|
| 0% |
Uncovered Elements: 40 (40) |
Complexity: 9 |
Complexity Density: 0.41 |
|
| 34 |
0
|
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 |
|
|
| 83 |
|
|
| 84 |
|
@param |
| 85 |
|
@param |
| 86 |
|
@return |
| 87 |
|
@throws |
| 88 |
|
|
| 89 |
|
|
|
|
|
| 0% |
Uncovered Elements: 21 (21) |
Complexity: 7 |
Complexity Density: 0.64 |
|
| 90 |
0
|
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 |
|
|
| 118 |
|
@return |
| 119 |
|
@throws |
| 120 |
|
@throws |
| 121 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 122 |
0
|
public static final Context getDefaultInitialContext(Hashtable<?,?> environment) throws NamingException {... |
| 123 |
0
|
return new InitialContext(environment); |
| 124 |
|
} |
| 125 |
|
|
| 126 |
|
|
| 127 |
|
@return |
| 128 |
|
@throws |
| 129 |
|
@throws |
| 130 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 131 |
0
|
public static final Context getDefaultInitialContext() throws NamingException {... |
| 132 |
0
|
return new InitialContext(); |
| 133 |
|
} |
| 134 |
|
} |