1 7 package org.ejtools.jndi.browser.web.action; 8 9 import java.io.IOException ; 10 import java.util.ArrayList ; 11 import java.util.Collection ; 12 import java.util.Hashtable ; 13 import java.util.Iterator ; 14 import java.util.Locale ; 15 import java.util.Vector ; 16 17 import javax.management.MBeanServer ; 18 import javax.management.MBeanServerFactory ; 19 import javax.management.ObjectName ; 20 import javax.servlet.ServletContext ; 21 import javax.servlet.ServletException ; 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 25 import org.apache.log4j.Logger; 26 import org.apache.struts.action.Action; 27 import org.apache.struts.action.ActionErrors; 28 import org.apache.struts.action.ActionForm; 29 import org.apache.struts.action.ActionForward; 30 import org.apache.struts.action.ActionMapping; 31 import org.apache.struts.util.MessageResources; 32 import org.apache.struts.util.ResponseUtils; 33 import org.ejtools.jndi.browser.web.Constants; 34 import org.ejtools.jndi.browser.web.JNDIContainer; 35 import org.jboss.ejb.Container; 36 import org.jboss.ejb.EjbModule; 37 import org.jboss.web.WebApplication; 38 39 40 48 public class RefreshAction extends Action 49 { 50 51 private static Logger logger = Logger.getLogger(RefreshAction.class); 52 53 54 55 public RefreshAction() { } 56 57 58 69 public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 70 throws IOException , ServletException 71 { 72 Locale locale = getLocale(request); 74 MessageResources messages = getResources(); 75 76 ActionErrors errors = new ActionErrors(); 78 79 logger.debug("Connecting to JNDI..."); 80 81 if (!errors.empty()) 83 { 84 saveErrors(request, errors); 85 return (new ActionForward(mapping.getInput())); 86 } 87 88 ServletContext context = this.getServlet().getServletContext(); 89 ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader(); 90 91 JNDIContainer globalTree = (JNDIContainer) context.getAttribute(Constants.GLOBAL_TREE); 95 logger.debug("globalTree=" + globalTree); 96 if (globalTree == null) 97 { 98 globalTree = new JNDIContainer(); 99 globalTree.setClassLoader(currentClassLoader); 100 globalTree.setContext(""); 101 context.setAttribute(Constants.GLOBAL_TREE, globalTree); 102 } 103 logger.debug("refresh globalTree"); 104 globalTree.refresh(); 105 106 JNDIContainer localTree = (JNDIContainer) context.getAttribute(Constants.LOCAL_TREE); 110 logger.debug("localTree=" + localTree); 111 if (localTree == null) 112 { 113 localTree = new JNDIContainer(); 114 localTree.setClassLoader(currentClassLoader); 115 localTree.setContext("java:/"); 116 context.setAttribute(Constants.LOCAL_TREE, localTree); 117 } 118 logger.debug("refresh localTree"); 119 localTree.refresh(); 120 121 ArrayList list = MBeanServerFactory.findMBeanServer(null); 125 if ((list != null) && (list.size() > 0)) 126 { 127 Iterator iterator = null; 128 MBeanServer server = (MBeanServer ) list.get(0); 129 logger.debug("server=" + server); 130 131 try 132 { 133 Vector moduleNames = new Vector (); 137 Hashtable moduleTrees = (Hashtable ) context.getAttribute(Constants.EJBMODULE_TREES); 138 if (moduleTrees == null) 139 { 140 moduleTrees = new Hashtable (); 141 } 142 Hashtable newModuleTrees = new Hashtable (); 143 144 iterator = server.queryNames(EjbModule.EJB_MODULE_QUERY_NAME, null).iterator(); 146 while (iterator.hasNext()) 147 { 148 try 149 { 150 ObjectName module = (ObjectName ) iterator.next(); 151 logger.debug("module=" + module); 152 String moduleName = (String ) module.getKeyProperty("module"); 153 String moduleUid = (String ) module.getKeyProperty("uid"); 154 if ((moduleUid != null) && (!"".equals(moduleUid))) 155 { 156 moduleName = moduleName + " (" + moduleUid + ")"; 157 } 158 moduleNames.add(ResponseUtils.filter(moduleName)); 159 logger.debug("moduleName=" + moduleName); 160 161 Vector containers = (Vector ) moduleTrees.get(moduleName); 163 if (containers == null) 164 { 165 containers = new Vector (); 166 } 167 Vector newContainers = new Vector (); 168 169 Hashtable ejbTable = new Hashtable (); 171 for (Iterator it = containers.iterator(); it.hasNext(); ) 172 { 173 JNDIContainer container = (JNDIContainer) it.next(); 174 ejbTable.put(container.getName(), container); 175 } 176 177 Collection moduleContainers = (Collection ) server.getAttribute(module, "Containers"); 178 for (Iterator iter = moduleContainers.iterator(); iter.hasNext(); ) 179 { 180 Container ejbContainer = (Container) iter.next(); 181 String ejbName = ejbContainer.getBeanMetaData().getEjbName(); 182 JNDIContainer container = (JNDIContainer) ejbTable.get(ejbName); 183 logger.debug("container=" + container); 184 if (container == null) 185 { 186 container = new JNDIContainer(); 187 container.setName(ejbName); 188 container.setClassLoader(ejbContainer.getClassLoader()); 189 container.setContext("java:/comp"); 190 } 191 logger.debug("refresh container"); 192 newContainers.add(container); 193 container.refresh(); 194 } 195 newModuleTrees.put(ResponseUtils.filter(moduleName), newContainers); 196 containers.clear(); 197 } 198 catch (Exception e) 199 { 200 logger.error("Error while refreshing getting module", e); 201 } 202 } 203 context.setAttribute(Constants.EJBMODULE_NAMES, moduleNames); 204 context.setAttribute(Constants.EJBMODULE_TREES, newModuleTrees); 205 context.setAttribute(Constants.EJBMODULE_TREE, newModuleTrees.get(moduleNames.get(0))); 206 moduleTrees.clear(); 207 } 208 catch (Exception e) 209 { 210 logger.error("Error while refreshing EJB modules", e); 211 } 212 213 try 214 { 215 Vector applications = (Vector ) context.getAttribute(Constants.WEBAPP_TREES); 219 if (applications == null) 220 { 221 applications = new Vector (); 222 } 223 Vector newApplications = new Vector (); 224 225 Hashtable appTable = new Hashtable (); 227 for (Iterator it = applications.iterator(); it.hasNext(); ) 228 { 229 JNDIContainer container = (JNDIContainer) it.next(); 230 appTable.put(container.getName(), container); 231 } 232 233 ObjectName webService = new ObjectName (Constants.WEBSERVER_OBJECTNAME); 235 iterator = (Iterator ) server.getAttribute(webService, "DeployedApplications"); 236 while (iterator.hasNext()) 237 { 238 WebApplication application = (WebApplication) iterator.next(); 239 String applicationName = application.getName(); 240 logger.debug("applicationName=" + applicationName); 241 242 JNDIContainer container = (JNDIContainer) appTable.get(applicationName); 243 logger.debug("container=" + container); 244 if (container == null) 245 { 246 container = new JNDIContainer(); 247 container.setName(application.getName()); 248 container.setClassLoader(application.getClassLoader()); 249 container.setContext("java:/comp"); 250 } 251 logger.debug("refresh container"); 252 newApplications.add(container); 253 container.refresh(); 254 } 255 context.setAttribute(Constants.WEBAPP_TREES, newApplications); 256 context.setAttribute(Constants.WEBAPP_TREE, newApplications.get(0)); 257 applications.clear(); 258 } 259 catch (Exception e) 260 { 261 logger.error("Error while refreshing Web modules", e); 262 } 263 } 264 265 return (mapping.findForward("view")); 266 } 267 } 268 269 | Popular Tags |