try {
// get /users context
usersContext = (Context) jndiTemplate.lookup("/users", Context.class);
}
catch (NamingException ne) {
final PortalException portalException = new PortalException("Could not find /users context", ne);
this.logger.error(portalException.getMessage(), ne);
throw portalException;
}
// get or create /users/[userId] context
Context userIdContext = null;
Context sessionsContext = null;
Context layoutsContext = null;
try {
userIdContext = (Context) usersContext.lookup(userId);
// lookup layouts and sessions contexts
try {
layoutsContext = (Context) userIdContext.lookup("layouts");
}
catch (NamingException ne) {
this.logger.warn("The '/users/" + userId + "/layouts' Context did not exist, even though the '/users/" + userId + "' Context did. It will be created.");
layoutsContext = userIdContext.createSubcontext("layouts");
}
try {
sessionsContext = (Context) userIdContext.lookup("sessions");
}
catch (NamingException ne) {
this.logger.error("The Context '/users/" + userId + "/sessions' did not exist, even though the '/users/" + userId + "' Context did. It will be created.");
sessionsContext = userIdContext.createSubcontext("sessions");
}
}
catch (NamingException ne) {
// new user
try {
userIdContext = usersContext.createSubcontext(userId);
// create layouts and sessions context
layoutsContext = userIdContext.createSubcontext("layouts");
sessionsContext = userIdContext.createSubcontext("sessions");
if (this.logger.isDebugEnabled()) {
this.logger.debug("Created and initialized Contexts for a userId='" + userId + "'");
}
}
catch (NamingException ne2) {
final PortalException portalException = new PortalException("exception encountered while trying to create '/users/" + userId + "' and layouts/sessions Contexts", ne2);
this.logger.error(portalException.getMessage(), ne2);
throw portalException;
}
}
// bind sessions/[sessionId] context
final Context sessionIdContext;
try {
sessionIdContext = sessionsContext.createSubcontext(sessionId);
}
catch (NameAlreadyBoundException nabe) {
final PortalException portalException = new PortalException("A session context is already bound at '/users/" + userId + "/sessions/" + sessionId + "'", nabe);
this.logger.error(portalException.getMessage(), nabe);
throw portalException;
}
catch (NamingException ne) {
final PortalException portalException = new PortalException("Excpetion encountered while trying to create Context '/users/" + userId + "/sessions/" + sessionId + "'", ne);
this.logger.error(portalException.getMessage(), ne);
throw portalException;
}
// bind layoutId
try {
sessionIdContext.bind("layoutId", layoutId);
}
catch (NamingException ne) {
final PortalException portalException = new PortalException("Excpetion encountered while trying to bind '" + layoutId + "' to '/users/" + userId + "/sessions/" + sessionId + "/layoutId'", ne);
this.logger.error(portalException.getMessage(), ne);
throw portalException;
}
// make sure channel-obj context exists
try {
sessionIdContext.createSubcontext("channel-obj");
}
catch (NameAlreadyBoundException nabe) {
// ignore
}
catch (NamingException ne) {
this.logger.warn("Excpetion encountered while create Context '" + layoutId + "' to '/users/" + userId + "/sessions/" + sessionId + "/channel-obj', this will be ignored.", ne);
}
// check if the layout id binding already exists
try {
//Check if layouts/[layoutId]/ alread exists
layoutsContext.lookup(layoutId);
// assume layouts/[layoutId]/ has already been populated
// bind layouts/[layoutId]/sessions/[sessionId]
final Context layoutSessionsContext;
try {
layoutSessionsContext = (Context) userIdContext.lookup("layouts/" + layoutId + "/sessions");
}
catch (NamingException ne) {
final PortalException portalException = new PortalException("Exception occured while looking up Context '/users/" + userId + "/layouts/" + layoutId + "/sessions/' even though Context '/users/" + userId + "/layouts' already existed.", ne);
this.logger.error(portalException.getMessage(), ne);
throw portalException;
}
try {
layoutSessionsContext.createSubcontext(sessionId);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Created Context '/users/" + userId + "/layouts/" + layoutId + "/sessions/" + sessionId + "'");
}
}
catch (NamingException ne) {
final PortalException portalException = new PortalException("Exception occured while creating Context '/users/" + userId + "/layouts/" + layoutId + "/sessions/" + sessionId + "'", ne);
this.logger.error(portalException.getMessage(), ne);
throw portalException;
}
}
catch (NamingException ne) {
final Context layoutIdContext;
try {
// given layout id has not been registered yet
layoutIdContext = layoutsContext.createSubcontext(layoutId);
// bind layouts/[layoutId]/sessions/[sessionId] context
final Context layoutSessionsContext = layoutIdContext.createSubcontext("sessions");
layoutSessionsContext.createSubcontext(sessionId);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Created Context '/users/" + userId + "/layouts/" + layoutId + "'");
}
}
catch (NamingException ne2) {
final PortalException portalException = new PortalException("Exception occured while creating the '/users/" + userId + "/layouts/" + layoutId + "' Context.", ne2);
this.logger.error(portalException.getMessage(), ne2);
throw portalException;
}
try {
final Context channel_idsContext = layoutIdContext.createSubcontext("channel-ids");
// Get the list of channels in the user's layout
final NodeList channelNodes = userLayout.getElementsByTagName("channel");
// Parse through the channels and populate the JNDI
for (int channelNodeIndex = 0; channelNodeIndex < channelNodes.getLength(); channelNodeIndex++) {
// Attempt to get the fname and instance ID from the channel
final Node channelNode = channelNodes.item(channelNodeIndex);
final NamedNodeMap channelAttributes = channelNode.getAttributes();
final Node fname = channelAttributes.getNamedItem("fname");
final Node instanceId = channelAttributes.getNamedItem("ID");
if (fname != null && instanceId != null) {
//System.out.println("fname found -> " + fname);
// Create a new composite name from the fname
final CompositeName cname = new CompositeName(fname.getNodeValue());
// Get a list of the name components
final Enumeration<String> subContextNameEnum = cname.getAll();
// Get the root of the context
Context nextContext = channel_idsContext;
// Add all of the subcontexts in the fname
while (subContextNameEnum.hasMoreElements()) {
final String subContextName = subContextNameEnum.nextElement();
if (subContextNameEnum.hasMoreElements()) {
// Bind a new sub context if the current name component is not the leaf
nextContext = nextContext.createSubcontext(subContextName);
}
else {
nextContext.rebind(subContextName, instanceId.getNodeValue());
if (this.logger.isDebugEnabled()) {
this.logger.debug("Bound channel id '" + instanceId.getNodeValue() + "' to '" + nextContext.getNameInNamespace() + "/" + subContextName + "'");
}
}
}
}
}
}
catch (NamingException ne2) {
final PortalException portalException = new PortalException("Exception occured while creating or populating the '/users/" + userId + "/layouts/" + layoutId + "/channel-ids' Context.", ne2);
this.logger.error(portalException.getMessage(), ne2);
throw portalException;
}
}
this.logger.info("JNDI Context configured for sessionId='" + sessionId + "', userId='" + userId + "', and layoutId='" + layoutId + "'");