1 9 package org.jboss.portal.cms; 10 11 import org.apache.slide.authenticate.SecurityToken; 12 import org.apache.slide.common.Domain; 13 import org.apache.slide.common.NamespaceAccessToken; 14 import org.apache.slide.webdav.event.NotificationTrigger; 15 import org.jboss.portal.common.util.XML; 16 import org.jboss.portal.common.util.Tools; 17 import org.jboss.portal.common.net.URLNavigator; 18 import org.jboss.portal.common.net.URLVisitor; 19 import org.jboss.portal.common.transaction.Transactions; 20 import org.jboss.system.ServiceMBeanSupport; 21 import org.jboss.util.StringPropertyReplacer; 22 import org.w3c.dom.DOMImplementation ; 23 import org.w3c.dom.Document ; 24 import org.w3c.dom.Element ; 25 26 import javax.transaction.TransactionManager ; 27 import java.io.ByteArrayInputStream ; 28 import java.io.InputStream ; 29 import java.io.ByteArrayOutputStream ; 30 import java.lang.reflect.Field ; 31 import java.net.DatagramSocket ; 32 import java.net.URL ; 33 import java.util.Enumeration ; 34 import java.util.Timer ; 35 import java.util.Iterator ; 36 import java.util.LinkedList ; 37 38 47 public class CMS extends ServiceMBeanSupport 48 { 49 50 51 private static final SecurityToken securityToken = new SecurityToken("blah"); 52 53 54 private final SecurityToken token = new SecurityToken(new Object ()); 55 56 57 private static CMS singleton; 58 59 60 private Element config; 61 62 63 private static NodeFactory factory; 64 65 66 private boolean doChecking; 67 68 public static CMS getCMS() 69 { 70 if(singleton == null) 71 { 72 throw new IllegalStateException ("Not yet initialized"); 73 } 74 return singleton; 75 } 76 77 80 public boolean getDoChecking() 81 { 82 return doChecking; 83 } 84 85 88 public void setDoChecking(boolean doChecking) 89 { 90 this.doChecking = doChecking; 91 } 92 93 96 public Element getConfig() 97 { 98 return config; 99 } 100 101 104 public void setConfig(Element config) 105 { 106 this.config = config; 107 } 108 109 public NodeFactory getNodeFactory() 110 { 111 return factory; 112 } 113 114 public void startService() throws Exception 115 { 116 synchronized(CMS.class) 117 { 118 if(singleton != null) 119 { 120 throw new IllegalStateException ("CMS class has broken the singleton rule"); 121 } 122 123 DOMImplementation impl = config.getOwnerDocument().getImplementation(); 125 Document doc = impl.createDocument("tmp", "tmp", null); 126 Element copy = (Element ) doc.importNode(config, true); 127 doc.removeChild(doc.getDocumentElement()); 128 doc.appendChild(copy); 129 String result = XML.toString(doc); 130 131 result = StringPropertyReplacer.replaceProperties(result); 133 134 InputStream in = new ByteArrayInputStream (result.getBytes("UTF-8")); 136 Domain.init(in); 137 138 NamespaceAccessToken nat = Domain.accessNamespace(securityToken, Domain.getDefaultNamespace()); 140 factory = new NodeFactory(nat); 141 142 singleton = this; 144 } 145 146 if (doChecking && !doCheck()) 147 { 148 createContent(); 150 } 151 } 152 153 156 public void destroyService() throws Exception 157 { 158 factory.close(); 160 factory = null; 161 162 log.debug("Closing timer"); 164 NotificationTrigger trigger = NotificationTrigger.getInstance(); 165 Class triggerClass = trigger.getClass(); 166 Field timerField = triggerClass.getDeclaredField("timer"); 167 timerField.setAccessible(true); 168 Timer timer = (Timer ) timerField.get(null); 169 timer.cancel(); 170 171 log.debug("Closing socket"); 173 Field socketField = triggerClass.getDeclaredField("socket"); 174 socketField.setAccessible(true); 175 DatagramSocket socket = (DatagramSocket ) socketField.get(trigger); 176 socket.close(); 177 178 for(Enumeration e = Domain.enumerateNamespaces(); e.hasMoreElements();) 180 { 181 String name = (String ) e.nextElement(); 182 log.debug("Closing namespace " + name); 183 Domain.closeNamespace(token, name); 184 } 185 186 singleton = null; 188 189 } 191 192 196 public int getState() 197 { 198 return super.getState(); 199 } 200 201 205 public String getStateString() 206 { 207 return super.getStateString(); 208 } 209 210 213 public void create() throws Exception 214 { 215 super.create(); 216 } 217 218 221 public void start() throws Exception 222 { 223 super.start(); 224 } 225 226 229 public void stop() 230 { 231 super.stop(); 232 } 233 234 237 public void destroy() 238 { 239 super.destroy(); 240 } 241 242 245 public boolean doCheck() 246 { 247 final NodeFactory factory = singleton.getNodeFactory(); 248 SecurityToken stupidSecurityToken = new SecurityToken("blah"); 249 final NamespaceAccessToken nat = Domain.accessNamespace(stupidSecurityToken, Domain.getDefaultNamespace()); 250 251 TransactionManager tm = nat.getTransactionManager(); 252 try 253 { 254 Transactions.begin(tm); 255 Node node = factory.getNode("/files/default/index.html"); 256 return node.exists(); 257 } 258 catch (Exception e) 259 { 260 log.error("Problem when doing CMS content checking", e); 261 } 262 finally 263 { 264 Transactions.safeEnd(tm); 265 } 266 267 return true; 268 } 269 270 273 public void createContent() 274 { 275 log.info("Creating CMS content"); 276 final NodeFactory factory = singleton.getNodeFactory(); 277 SecurityToken stupidSecurityToken = new SecurityToken(""); 278 final NamespaceAccessToken nat = Domain.accessNamespace(stupidSecurityToken, Domain.getDefaultNamespace()); 279 final CMSMimeMappings mappings = new CMSMimeMappings(); 280 281 TransactionManager tm = nat.getTransactionManager(); 283 try 284 { 285 Transactions.begin(tm); 286 287 URL root = Thread.currentThread().getContextClassLoader().getResource("default-content/"); 289 290 URLVisitor visitor = new URLVisitor() 292 { 293 LinkedList atoms = new LinkedList (); 295 public void startDir(String name) 296 { 297 atoms.addLast(name); 298 } 299 public void endDir(String name) 300 { 301 atoms.removeLast(); 302 } 303 public void file(String name, URL url) 304 { 305 InputStream in = null; 306 try 307 { 308 StringBuffer uri = new StringBuffer (); 310 for (Iterator i = atoms.iterator(); i.hasNext();) 311 { 312 String atom = (String )i.next(); 313 uri.append("/").append(atom); 314 } 315 uri.append("/").append(name); 316 uri.delete(0, "/default/content".length()); 317 318 in = url.openStream(); 320 ByteArrayOutputStream out = new ByteArrayOutputStream (); 321 Tools.copy(in, out); 322 323 String mimeType = null; 325 int dotIndex = name.lastIndexOf('.'); 326 if (dotIndex != -1) 327 { 328 String ext = name.substring(dotIndex + 1); 329 mimeType = mappings.getMimeType(ext); 330 } 331 if (mimeType == null) 332 { 333 mimeType = "application/octet-stream"; 334 } 335 336 Node node = factory.getNode(uri.toString()); 338 if (!node.exists()) 339 { 340 Content content = new Content(out.toByteArray(), mimeType); 341 node.createContent(content, true); 342 } 343 } 344 catch (Exception e) 345 { 346 e.printStackTrace(); 347 } 348 finally 349 { 350 Tools.safeClose(in); 351 } 352 } 353 }; 354 355 URLNavigator.visit(root, visitor); 357 } 358 catch (Exception e) 359 { 360 log.error("Failed to create cms content, rolling back", e); 361 Transactions.setRollbackOnly(tm); 362 } 363 finally 364 { 365 Transactions.safeEnd(tm); 366 } 367 } 368 369 372 public void destroyContent() throws CMSException 373 { 374 log.info("Destroying CMS content"); 375 NodeFactory factory = singleton.getNodeFactory(); 376 SecurityToken stupidSecurityToken = new SecurityToken("blah"); 377 NamespaceAccessToken nat = Domain.accessNamespace(stupidSecurityToken, Domain.getDefaultNamespace()); 378 379 TransactionManager tm = nat.getTransactionManager(); 381 try 382 { 383 Transactions.begin(tm); 384 Node node = factory.getNode("/files"); 385 node.delete(); 386 } 387 finally 388 { 389 Transactions.safeEnd(tm); 390 } 391 } 392 393 } 394 | Popular Tags |