KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > cms > CMS


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

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 JavaDoc;
23 import org.w3c.dom.Document JavaDoc;
24 import org.w3c.dom.Element JavaDoc;
25
26 import javax.transaction.TransactionManager JavaDoc;
27 import java.io.ByteArrayInputStream JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.ByteArrayOutputStream JavaDoc;
30 import java.lang.reflect.Field JavaDoc;
31 import java.net.DatagramSocket JavaDoc;
32 import java.net.URL JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.Timer JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.LinkedList JavaDoc;
37
38 /**
39  * Integration of a slide Domain into JBoss. It tries to compensate the non sense
40  * of integration performed natively by slide.
41  *
42  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
43  * @version $Revision: 1.13 $
44  * @jmx.mbean
45  * @jboss.xmbean
46  */

47 public class CMS extends ServiceMBeanSupport
48 {
49
50    /** . */
51    private static final SecurityToken securityToken = new SecurityToken("blah");
52
53    /** The useless slide token whose goal is only to instantiate new objects. */
54    private final SecurityToken token = new SecurityToken(new Object JavaDoc());
55
56    /** There can only be only, this rule is dictated by slide. */
57    private static CMS singleton;
58
59    /** The config loaded from the XML descriptor. */
60    private Element JavaDoc config;
61
62    /** The node factory. */
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 JavaDoc("Not yet initialized");
73       }
74       return singleton;
75    }
76
77    /**
78     * @jmx.managed-attribute
79     */

80    public boolean getDoChecking()
81    {
82       return doChecking;
83    }
84
85    /**
86     * @jmx.managed-attribute
87     */

88    public void setDoChecking(boolean doChecking)
89    {
90       this.doChecking = doChecking;
91    }
92
93    /**
94     * @jmx.managed-attribute
95     */

96    public Element JavaDoc getConfig()
97    {
98       return config;
99    }
100
101    /**
102     * @jmx.managed-attribute
103     */

104    public void setConfig(Element JavaDoc config)
105    {
106       this.config = config;
107    }
108
109    public NodeFactory getNodeFactory()
110    {
111       return factory;
112    }
113
114    public void startService() throws Exception JavaDoc
115    {
116       synchronized(CMS.class)
117       {
118          if(singleton != null)
119          {
120             throw new IllegalStateException JavaDoc("CMS class has broken the singleton rule");
121          }
122
123          // Serialize the embedded configuration
124
DOMImplementation JavaDoc impl = config.getOwnerDocument().getImplementation();
125          Document JavaDoc doc = impl.createDocument("tmp", "tmp", null);
126          Element JavaDoc copy = (Element JavaDoc) doc.importNode(config, true);
127          doc.removeChild(doc.getDocumentElement());
128          doc.appendChild(copy);
129          String JavaDoc result = XML.toString(doc);
130
131          // Replace all the properties in the configuration
132
result = StringPropertyReplacer.replaceProperties(result);
133
134          // Initialize the domain with the new config
135
InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(result.getBytes("UTF-8"));
136          Domain.init(in);
137
138          //
139
NamespaceAccessToken nat = Domain.accessNamespace(securityToken, Domain.getDefaultNamespace());
140          factory = new NodeFactory(nat);
141
142          //
143
singleton = this;
144       }
145
146       if (doChecking && !doCheck())
147       {
148          //
149
createContent();
150       }
151    }
152
153    /**
154     * Can we really destroy slide ? it seems to be indestructible
155     */

156    public void destroyService() throws Exception JavaDoc
157    {
158       //
159
factory.close();
160       factory = null;
161
162       // First shoot at the static timer
163
log.debug("Closing timer");
164       NotificationTrigger trigger = NotificationTrigger.getInstance();
165       Class JavaDoc triggerClass = trigger.getClass();
166       Field JavaDoc timerField = triggerClass.getDeclaredField("timer");
167       timerField.setAccessible(true);
168       Timer JavaDoc timer = (Timer JavaDoc) timerField.get(null);
169       timer.cancel();
170
171       // Then we kill the socket
172
log.debug("Closing socket");
173       Field JavaDoc socketField = triggerClass.getDeclaredField("socket");
174       socketField.setAccessible(true);
175       DatagramSocket JavaDoc socket = (DatagramSocket JavaDoc) socketField.get(trigger);
176       socket.close();
177
178       // Now close the namespaces
179
for(Enumeration JavaDoc e = Domain.enumerateNamespaces(); e.hasMoreElements();)
180       {
181          String JavaDoc name = (String JavaDoc) e.nextElement();
182          log.debug("Closing namespace " + name);
183          Domain.closeNamespace(token, name);
184       }
185
186       // Remove singleton
187
singleton = null;
188
189       // Are we missing some hidden stuff ?
190
}
191
192    /**
193     * @jmx.managed-attribute
194     * access="read-only"
195     */

196    public int getState()
197    {
198       return super.getState();
199    }
200
201    /**
202     * @jmx.managed-attribute
203     * access="read-only"
204     */

205    public String JavaDoc getStateString()
206    {
207       return super.getStateString();
208    }
209
210    /**
211     * @jmx.managed-operation
212     */

213    public void create() throws Exception JavaDoc
214    {
215       super.create();
216    }
217
218    /**
219     * @jmx.managed-operation
220     */

221    public void start() throws Exception JavaDoc
222    {
223       super.start();
224    }
225
226    /**
227     * @jmx.managed-operation
228     */

229    public void stop()
230    {
231       super.stop();
232    }
233
234    /**
235     * @jmx.managed-operation
236     */

237    public void destroy()
238    {
239       super.destroy();
240    }
241
242    /**
243     * @jmx.managed-operation
244     */

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 JavaDoc 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 JavaDoc 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    /**
271     * @jmx.managed-operation
272     */

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       //
282
TransactionManager JavaDoc tm = nat.getTransactionManager();
283       try
284       {
285          Transactions.begin(tm);
286
287          // Get the content
288
URL JavaDoc root = Thread.currentThread().getContextClassLoader().getResource("default-content/");
289
290          // Iterate over the content
291
URLVisitor visitor = new URLVisitor()
292          {
293             // Handle the name atoms
294
LinkedList JavaDoc atoms = new LinkedList JavaDoc();
295             public void startDir(String JavaDoc name)
296             {
297                atoms.addLast(name);
298             }
299             public void endDir(String JavaDoc name)
300             {
301                atoms.removeLast();
302             }
303             public void file(String JavaDoc name, URL JavaDoc url)
304             {
305                InputStream JavaDoc in = null;
306                try
307                {
308                   // Build the URI
309
StringBuffer JavaDoc uri = new StringBuffer JavaDoc();
310                   for (Iterator JavaDoc i = atoms.iterator(); i.hasNext();)
311                   {
312                      String JavaDoc atom = (String JavaDoc)i.next();
313                      uri.append("/").append(atom);
314                   }
315                   uri.append("/").append(name);
316                   uri.delete(0, "/default/content".length());
317
318                   // Load the content
319
in = url.openStream();
320                   ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
321                   Tools.copy(in, out);
322
323                   // The file mime type
324
String JavaDoc mimeType = null;
325                   int dotIndex = name.lastIndexOf('.');
326                   if (dotIndex != -1)
327                   {
328                      String JavaDoc ext = name.substring(dotIndex + 1);
329                      mimeType = mappings.getMimeType(ext);
330                   }
331                   if (mimeType == null)
332                   {
333                      mimeType = "application/octet-stream";
334                   }
335
336                   // Create node in the repository
337
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 JavaDoc e)
345                {
346                   e.printStackTrace();
347                }
348                finally
349                {
350                   Tools.safeClose(in);
351                }
352             }
353          };
354
355          //
356
URLNavigator.visit(root, visitor);
357       }
358       catch (Exception JavaDoc 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    /**
370     * @jmx.managed-operation
371     */

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       //
380
TransactionManager JavaDoc 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