📄 namingcontextlistener.java
字号:
/**
* Set the specified EJBs in the naming context.
*/
public void addEjb(ContextEjb ejb) {
// Create a reference to the EJB.
Reference ref = new EjbRef
(ejb.getType(), ejb.getHome(), ejb.getRemote(), ejb.getLink());
// Adding the additional parameters, if any
addAdditionalParameters(ejb.getNamingResources(), ref, ejb.getName());
try {
createSubcontexts(envCtx, ejb.getName());
envCtx.bind(ejb.getName(), ref);
} catch (NamingException e) {
log(sm.getString("naming.bindFailed", e));
}
}
/**
* Set the specified environment entries in the naming context.
*/
public void addEnvironment(ContextEnvironment env) {
Object value = null;
// Instantiating a new instance of the correct object type, and
// initializing it.
String type = env.getType();
try {
if (type.equals("java.lang.String")) {
value = env.getValue();
} else if (type.equals("java.lang.Byte")) {
if (env.getValue() == null) {
value = new Byte((byte) 0);
} else {
value = Byte.decode(env.getValue());
}
} else if (type.equals("java.lang.Short")) {
if (env.getValue() == null) {
value = new Short((short) 0);
} else {
value = Short.decode(env.getValue());
}
} else if (type.equals("java.lang.Integer")) {
if (env.getValue() == null) {
value = new Integer(0);
} else {
value = Integer.decode(env.getValue());
}
} else if (type.equals("java.lang.Long")) {
if (env.getValue() == null) {
value = new Long(0);
} else {
value = Long.decode(env.getValue());
}
} else if (type.equals("java.lang.Boolean")) {
value = Boolean.valueOf(env.getValue());
} else if (type.equals("java.lang.Double")) {
if (env.getValue() == null) {
value = new Double(0);
} else {
value = Double.valueOf(env.getValue());
}
} else if (type.equals("java.lang.Float")) {
if (env.getValue() == null) {
value = new Float(0);
} else {
value = Float.valueOf(env.getValue());
}
} else if (type.equals("java.lang.Character")) {
if (env.getValue() == null) {
value = new Character((char) 0);
} else {
if (env.getValue().length() == 1) {
value = new Character(env.getValue().charAt(0));
} else {
throw new IllegalArgumentException();
}
}
} else {
log(sm.getString("naming.invalidEnvEntryType", env.getName()));
}
} catch (NumberFormatException e) {
log(sm.getString("naming.invalidEnvEntryValue", env.getName()));
} catch (IllegalArgumentException e) {
log(sm.getString("naming.invalidEnvEntryValue", env.getName()));
}
// Binding the object to the appropriate name
if (value != null) {
try {
if (debug >= 2)
log(" Adding environment entry " + env.getName());
createSubcontexts(envCtx, env.getName());
envCtx.bind(env.getName(), value);
} catch (NamingException e) {
log(sm.getString("naming.invalidEnvEntryValue", e));
}
}
}
/**
* Set the specified local EJBs in the naming context.
*/
public void addLocalEjb(ContextLocalEjb localEjb) {
}
/**
* Set the specified resources in the naming context.
*/
public void addResource(ContextResource resource) {
// Create a reference to the resource.
Reference ref = new ResourceRef
(resource.getType(), resource.getDescription(),
resource.getScope(), resource.getAuth());
// Adding the additional parameters, if any
addAdditionalParameters(resource.getNamingResources(), ref,
resource.getName());
try {
if (debug >= 2) {
log(" Adding resource ref " + resource.getName());
log(" " + ref);
}
createSubcontexts(envCtx, resource.getName());
envCtx.bind(resource.getName(), ref);
} catch (NamingException e) {
log(sm.getString("naming.bindFailed", e));
}
}
/**
* Set the specified resources in the naming context.
*/
public void addResourceEnvRef(String name, String type) {
// Create a reference to the resource env.
Reference ref = new ResourceEnvRef(type);
// Adding the additional parameters, if any
addAdditionalParameters(null, ref, name);
try {
if (debug >= 2)
log(" Adding resource env ref " + name);
createSubcontexts(envCtx, name);
envCtx.bind(name, ref);
} catch (NamingException e) {
log(sm.getString("naming.bindFailed", e));
}
}
/**
* Set the specified resource link in the naming context.
*/
public void addResourceLink(ContextResourceLink resourceLink) {
// Create a reference to the resource.
Reference ref = new ResourceLinkRef
(resourceLink.getType(), resourceLink.getGlobal());
// Adding the additional parameters, if any
addAdditionalParameters(resourceLink.getNamingResources(), ref,
resourceLink.getName());
javax.naming.Context ctx =
"UserTransaction".equals(resourceLink.getName())
? compCtx : envCtx;
try {
if (debug >= 2)
log(" Adding resource link " + resourceLink.getName());
createSubcontexts(envCtx, resourceLink.getName());
ctx.bind(resourceLink.getName(), ref);
} catch (NamingException e) {
log(sm.getString("naming.bindFailed", e));
}
}
/**
* Set the specified EJBs in the naming context.
*/
public void removeEjb(String name) {
try {
envCtx.unbind(name);
} catch (NamingException e) {
log(sm.getString("naming.unbindFailed", e));
}
}
/**
* Set the specified environment entries in the naming context.
*/
public void removeEnvironment(String name) {
try {
envCtx.unbind(name);
} catch (NamingException e) {
log(sm.getString("naming.unbindFailed", e));
}
}
/**
* Set the specified local EJBs in the naming context.
*/
public void removeLocalEjb(String name) {
try {
envCtx.unbind(name);
} catch (NamingException e) {
log(sm.getString("naming.unbindFailed", e));
}
}
/**
* Set the specified resources in the naming context.
*/
public void removeResource(String name) {
try {
envCtx.unbind(name);
} catch (NamingException e) {
log(sm.getString("naming.unbindFailed", e));
}
}
/**
* Set the specified resources in the naming context.
*/
public void removeResourceEnvRef(String name) {
try {
envCtx.unbind(name);
} catch (NamingException e) {
log(sm.getString("naming.unbindFailed", e));
}
}
/**
* Set the specified resources in the naming context.
*/
public void removeResourceLink(String name) {
try {
envCtx.unbind(name);
} catch (NamingException e) {
log(sm.getString("naming.unbindFailed", e));
}
}
/**
* Create all intermediate subcontexts.
*/
private void createSubcontexts(javax.naming.Context ctx, String name)
throws NamingException {
javax.naming.Context currentContext = ctx;
StringTokenizer tokenizer = new StringTokenizer(name, "/");
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
if ((!token.equals("")) && (tokenizer.hasMoreTokens())) {
try {
currentContext = currentContext.createSubcontext(token);
} catch (NamingException e) {
// Silent catch. Probably an object is already bound in
// the context.
currentContext =
(javax.naming.Context) currentContext.lookup(token);
}
}
}
}
/**
* Add additional parameters to the reference.
*/
private void addAdditionalParameters(NamingResources resources,
Reference ref, String name) {
if (resources == null) {
resources = namingResources;
}
ResourceParams resourceParameters = resources.findResourceParams(name);
if (debug >= 2)
log(" Resource parameters for " + name + " = " +
resourceParameters);
if (resourceParameters == null)
return;
Hashtable params = resourceParameters.getParameters();
Enumeration enum = params.keys();
while (enum.hasMoreElements()) {
String paramName = (String) enum.nextElement();
String paramValue = (String) params.get(paramName);
StringRefAddr refAddr = new StringRefAddr(paramName, paramValue);
ref.add(refAddr);
}
}
/**
* Log the specified message to our current Logger (if any).
*
* @param message Message to be logged
*/
protected void log(String message) {
if (!(container instanceof Container)) {
System.out.println(logName() + ": " + message);
return;
}
Logger logger = ((Container) container).getLogger();
if (logger != null)
logger.log(logName() + ": " + message);
else
System.out.println(logName() + ": " + message);
}
/**
* Log the specified message and exception to our current Logger
* (if any).
*
* @param message Message to be logged
* @param throwable Related exception
*/
protected void log(String message, Throwable throwable) {
if (!(container instanceof Container)) {
System.out.println(logName() + ": " + message + ": " + throwable);
throwable.printStackTrace(System.out);
return;
}
Logger logger = ((Container) container).getLogger();
if (logger != null)
logger.log(logName() + ": " + message, throwable);
else {
System.out.println(logName() + ": " + message + ": " + throwable);
throwable.printStackTrace(System.out);
}
}
/**
* Return the abbreviated name of this container for logging messsages
*/
protected String logName() {
String className = this.getClass().getName();
int period = className.lastIndexOf(".");
if (period >= 0)
className = className.substring(period + 1);
return (className + "[" + getName() + "]");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -