1 12 13 package org.ejtools.jmx.browser.state; 14 15 16 17 import java.beans.beancontext.BeanContext ; 18 19 import java.io.File ; 20 21 import java.net.URL ; 22 23 24 25 import javax.xml.parsers.DocumentBuilder ; 26 27 import javax.xml.parsers.DocumentBuilderFactory ; 28 29 import javax.xml.parsers.SAXParser ; 30 31 import javax.xml.parsers.SAXParserFactory ; 32 33 import javax.xml.transform.Result ; 34 35 import javax.xml.transform.Source ; 36 37 import javax.xml.transform.Transformer ; 38 39 import javax.xml.transform.TransformerFactory ; 40 41 import javax.xml.transform.dom.DOMSource ; 42 43 import javax.xml.transform.stream.StreamResult ; 44 45 46 47 import org.ejtools.graph.frame.GraphInternalFrame; 48 49 import org.ejtools.jmx.browser.Browser; 50 51 import org.ejtools.jmx.browser.frame.ServerInternalFrame; 52 53 import org.ejtools.jmx.browser.model.Domain; 54 55 import org.ejtools.jmx.browser.model.Resource; 56 57 import org.ejtools.jmx.browser.model.Server; 58 59 import org.ejtools.jmx.browser.state.rules.GraphInternalFrameRule; 60 61 import org.ejtools.jmx.browser.state.rules.ResourceGraphRule; 62 63 import org.ejtools.jmx.browser.state.rules.ResourceRule; 64 65 import org.ejtools.jmx.browser.state.rules.ServerInternalFrameRule; 66 67 import org.ejtools.jmx.browser.state.rules.ServerRule; 68 69 import org.ejtools.util.service.ProfileRule; 70 71 import org.ejtools.util.state.LoadHandler; 72 73 import org.ejtools.util.state.StoreVisitor; 74 75 import org.w3c.dom.Document ; 76 77 78 79 80 81 90 91 public class WorkbenchState 92 93 { 94 95 96 97 private BeanContext context; 98 99 100 101 private Document document; 102 103 104 105 private URL workbenchURL; 106 107 108 109 110 111 120 121 public WorkbenchState(BeanContext context) 122 123 { 124 125 this.context = context; 126 127 } 128 129 130 131 132 133 142 143 public URL getWorkbenchURL() 144 145 { 146 147 return this.workbenchURL; 148 149 } 150 151 152 153 154 155 156 157 public void load() 158 159 { 160 161 163 if ((this.workbenchURL == null) && (!this.workbenchURL.getProtocol().equals("file"))) 164 165 { 166 167 return; 168 169 } 170 171 172 173 try 174 175 { 176 177 LoadHandler handler = new LoadHandler(); 178 179 handler.getContext().put("CONTAINER", this.context); 180 181 182 183 handler.addRule("/workbench/graph-frame", new GraphInternalFrameRule()); 184 185 handler.addRule("/workbench/jmx-frame", new ServerInternalFrameRule()); 186 187 handler.addRule("/workbench/jmx-frame/profile", new ProfileRule()); 188 189 handler.addRule("/workbench/jmx-frame/profile/property", new ProfileRule.ProfilePropertyRule()); 190 191 handler.addRule("/workbench/jmx-frame/jmx-server", new ServerRule()); 192 193 handler.addRule("/workbench/jmx-frame/jmx-server/jmx-resource", new ResourceRule()); 194 195 handler.addRule("/workbench/jmx-frame/jmx-server/jmx-resource/jmx-graph", new ResourceGraphRule()); 196 197 198 199 SAXParserFactory factory = SAXParserFactory.newInstance(); 200 201 SAXParser parser = factory.newSAXParser(); 202 203 File file = new File (this.workbenchURL.getFile()); 204 205 parser.parse(file, handler); 206 207 } 208 209 catch (Exception e) 210 211 { 212 213 e.printStackTrace(); 214 215 } 216 217 } 218 219 220 221 222 223 232 233 public void setWorkbenchURL(URL workbenchURL) 234 235 { 236 237 this.workbenchURL = workbenchURL; 238 239 } 240 241 242 243 244 245 246 247 public void store() 248 249 { 250 251 253 if ((this.workbenchURL == null) && (!this.workbenchURL.getProtocol().equals("file"))) 254 255 { 256 257 return; 258 259 } 260 261 262 263 try 264 265 { 266 267 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 268 269 DocumentBuilder builder = dbFactory.newDocumentBuilder(); 270 271 this.document = builder.newDocument(); 272 273 274 275 StoreVisitor visitor = new WorkbenchStoreVisitor(this.document); 276 277 visitor.registerForPersistence(Browser.class); 278 279 visitor.registerForPersistence(ServerInternalFrame.class); 280 281 visitor.registerForPersistence(GraphInternalFrame.class); 282 283 visitor.registerForPersistence(Server.class); 284 285 visitor.registerForPersistence(Domain.class); 286 287 visitor.registerForPersistence(Resource.class); 288 289 visitor.persist(this.context); 290 291 292 293 TransformerFactory tFactory = TransformerFactory.newInstance(); 294 295 Transformer transformer = tFactory.newTransformer(); 296 297 transformer.setOutputProperty("indent", "true"); 298 299 300 301 Source source = new DOMSource (this.document); 302 303 File file = new File (this.workbenchURL.getFile()); 304 305 Result result = new StreamResult (file); 306 307 transformer.transform(source, result); 308 309 } 310 311 catch (Exception e) 312 313 { 314 315 e.printStackTrace(); 316 317 } 318 319 } 320 321 } 322 323 | Popular Tags |